mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-01-15 09:53:32 +01:00
Add type annotations from monkeytype + jw-devops/test
Add type annotations as generated by monkeytype and jw-devops/test, plus some hand editing to satisfy both monkeytype and mypy. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
952bf4a4e1
commit
77d43aebad
7 changed files with 51 additions and 42 deletions
|
|
@ -1,4 +1,6 @@
|
|||
from __future__ import annotations
|
||||
from collections import OrderedDict
|
||||
from typing import Optional, Union
|
||||
from jwutils.log import *
|
||||
|
||||
def quote(s):
|
||||
|
|
@ -10,7 +12,7 @@ def quote(s):
|
|||
return "'" + s + "'"
|
||||
return '"' + s + '"'
|
||||
|
||||
def is_quoted(s):
|
||||
def is_quoted(s: str) -> bool:
|
||||
if isinstance(s, StringTree):
|
||||
return False
|
||||
s = s.strip()
|
||||
|
|
@ -21,7 +23,7 @@ def is_quoted(s):
|
|||
return True
|
||||
return False
|
||||
|
||||
def cleanup_string(s):
|
||||
def cleanup_string(s: str) -> str:
|
||||
if isinstance(s, StringTree):
|
||||
return s
|
||||
s = s.strip()
|
||||
|
|
@ -31,9 +33,9 @@ def cleanup_string(s):
|
|||
|
||||
class StringTree: # export
|
||||
|
||||
def __init__(self, path, content):
|
||||
def __init__(self, path: str, content: str) -> None:
|
||||
slog(DEBUG, "ctor, path =", path, "content =", content)
|
||||
self.children = OrderedDict()
|
||||
self.children: OrderedDict[str, StringTree] = OrderedDict()
|
||||
self.content = None
|
||||
self.__set(path, content)
|
||||
assert(hasattr(self, "content"))
|
||||
|
|
@ -106,10 +108,10 @@ class StringTree: # export
|
|||
self.children[nibble].children[gc.content] = gc
|
||||
return self.children[nibble]
|
||||
|
||||
def __str__(self):
|
||||
def __str__(self) -> str:
|
||||
return 'st:"{}"'.format(self.content)
|
||||
|
||||
def __getitem__(self, path):
|
||||
def __getitem__(self, path: str) -> str:
|
||||
r = self.get(path)
|
||||
if r is None:
|
||||
raise KeyError(path)
|
||||
|
|
@ -139,11 +141,11 @@ class StringTree: # export
|
|||
raise Exception("Tried to set empty content")
|
||||
self.content = content
|
||||
|
||||
def add(self, path, content = None, split = True):
|
||||
def add(self, path: str, content: Optional[Union[str, StringTree]] = None, split: bool = True) -> StringTree:
|
||||
slog(DEBUG, "adding >{}< at >{}< to >{}<".format(content, path, self.content))
|
||||
return self.__set(path, content, split)
|
||||
|
||||
def get(self, path_):
|
||||
def get(self, path_: str) -> Optional[StringTree]:
|
||||
slog(DEBUG, 'looking for "{}" in "{}"'.format(path_, self.content))
|
||||
assert not isinstance(path_, int)
|
||||
path = cleanup_string(path_)
|
||||
|
|
@ -168,10 +170,10 @@ class StringTree: # export
|
|||
relpath = '.'.join(components[1:])
|
||||
return self.children[name].get(relpath)
|
||||
|
||||
def value(self):
|
||||
def value(self) -> str:
|
||||
if len(self.children) == 0:
|
||||
return None
|
||||
return self.children[next(reversed(self.children))].content
|
||||
raise Exception('tried to get value from leave "{}"'.format(self.content))
|
||||
return self.children[next(reversed(self.children))].content # type: ignore
|
||||
|
||||
def child_list(self, depth_first=True):
|
||||
if depth_first == False:
|
||||
|
|
@ -181,7 +183,7 @@ class StringTree: # export
|
|||
r.append(c)
|
||||
r.extend(c.to_list())
|
||||
|
||||
def dump(self, prio, *args, **kwargs):
|
||||
def dump(self, prio: int, *args, **kwargs) -> None:
|
||||
caller = kwargs['caller'] if 'caller' in kwargs.keys() else get_caller_pos(1)
|
||||
msg = ''
|
||||
if args is not None:
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
from jwutils.stree.StringTree import *
|
||||
from jwutils.log import *
|
||||
|
||||
def _cleanup_line(line):
|
||||
def _cleanup_line(line: str) -> str:
|
||||
line = line.strip()
|
||||
r = ''
|
||||
in_quote = None
|
||||
|
|
@ -20,7 +20,7 @@ def _cleanup_line(line):
|
|||
return r[1:-1]
|
||||
return r
|
||||
|
||||
def parse(s, allow_full_lines=True, root_content='root'): # export
|
||||
def parse(s: str, allow_full_lines: bool=True, root_content: str='root') -> StringTree: # export
|
||||
slog(DEBUG, "parsing", s)
|
||||
root = StringTree('', content=root_content)
|
||||
sec = ''
|
||||
|
|
@ -66,7 +66,7 @@ def parse(s, allow_full_lines=True, root_content='root'): # export
|
|||
root.add(sec + '.' + cleanup_string(lhs), cleanup_string(rhs), split=split)
|
||||
return root
|
||||
|
||||
def read(path, root_content='root'): # export
|
||||
def read(path: str, root_content: str='root') -> StringTree: # export
|
||||
with open(path, 'r') as infile:
|
||||
s = infile.read()
|
||||
return parse(s, root_content=root_content)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue