From e07553c3627b80f20cdc81a35030bf0540924db8 Mon Sep 17 00:00:00 2001 From: jesopo Date: Tue, 30 Oct 2018 14:58:48 +0000 Subject: Add type/return hints throughout src/ and, in doing so, fix some cyclical references. --- src/utils/parse.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/utils/parse.py (limited to 'src/utils/parse.py') diff --git a/src/utils/parse.py b/src/utils/parse.py new file mode 100644 index 00000000..03a585c2 --- /dev/null +++ b/src/utils/parse.py @@ -0,0 +1,57 @@ +import io, typing + +COMMENT_TYPES = ["#", "//"] +def hashflags(filename: str) -> typing.List[typing.Tuple[str, str]]: + hashflags = {} + with io.open(filename, mode="r", encoding="utf8") as f: + for line in f: + line = line.strip("\n") + found = False + for comment_type in COMMENT_TYPES: + if line.startswith(comment_type): + line = line.replace(comment_type, "", 1).lstrip() + found = True + break + + if not found: + break + elif line.startswith("--"): + hashflag, sep, value = line[2:].partition(" ") + hashflags[hashflag] = value if sep else None + return list(hashflags.items()) + +class Docstring(object): + def __init__(self, description: str, items: dict, var_items: dict): + self.description = description + self.items = items + self.var_items = var_items + +def docstring(s: str) -> Docstring: + description = "" + last_item = None + items = {} + var_items = {} + if s: + for line in s.split("\n"): + line = line.strip() + + if line: + if line[0] == ":": + key, _, value = line[1:].partition(": ") + last_item = key + + if key in var_items: + var_items[key].append(value) + elif key in items: + var_items[key] = [items.pop(key), value] + else: + items[key] = value + else: + if last_item: + items[last_item] += " %s" % line + else: + if description: + description += " " + description += line + return Docstring(description, items, var_items) + -- cgit v1.3.1-10-gc9f91