Commit 03404315 authored by eckhart's avatar eckhart

- typos corrected

parent 1439eeed
...@@ -112,6 +112,7 @@ def flatten_xml(xml: str) -> str: ...@@ -112,6 +112,7 @@ def flatten_xml(xml: str) -> str:
# works only with regex # works only with regex
# return re.sub(r'\s+(?=<\w)', '', re.sub(r'(?<=</\w+>)\s+', '', xml)) # return re.sub(r'\s+(?=<\w)', '', re.sub(r'(?<=</\w+>)\s+', '', xml))
assert RX_IS_XML.match(xml) assert RX_IS_XML.match(xml)
def tag_only(m): def tag_only(m):
return m.groupdict()['closing_tag'] return m.groupdict()['closing_tag']
return re.sub(r'\s+(?=<[\w:])', '', re.sub(r'(?P<closing_tag></:?\w+>)\s+', tag_only, xml)) return re.sub(r'\s+(?=<[\w:])', '', re.sub(r'(?P<closing_tag></:?\w+>)\s+', tag_only, xml))
...@@ -131,7 +132,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil ...@@ -131,7 +132,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
TODO: Add some documentation and doc-tests here... TODO: Add some documentation and doc-tests here...
Attributes: Attributes and Properties:
tag_name (str): The name of the node, which is either its tag_name (str): The name of the node, which is either its
parser's name or, if that is empty, the parser's class name parser's name or, if that is empty, the parser's class name
...@@ -146,13 +147,6 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil ...@@ -146,13 +147,6 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
difference to ``str(node)`` is that ``node.content`` does difference to ``str(node)`` is that ``node.content`` does
not add the error messages to the returned string. not add the error messages to the returned string.
parser (Parser): The parser which generated this node.
WARNING: In case you use mock syntax trees for testing or
parser replacement during the AST-transformation: DO NOT
rely on this being a real parser object in any phase after
parsing (i.e. AST-transformation and compiling), for
example by calling ``isinstance(node.parer, ...)``.
len (int): The full length of the node's string result if the len (int): The full length of the node's string result if the
node is a leaf node or, otherwise, the concatenated string node is a leaf node or, otherwise, the concatenated string
result's of its descendants. The figure always represents result's of its descendants. The figure always represents
...@@ -196,6 +190,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil ...@@ -196,6 +190,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
self.result = result self.result = result
self.tag_name = tag_name # type: str self.tag_name = tag_name # type: str
def __deepcopy__(self, memo): def __deepcopy__(self, memo):
if self.children: if self.children:
duplicate = self.__class__(self.tag_name, copy.deepcopy(self.children), False) duplicate = self.__class__(self.tag_name, copy.deepcopy(self.children), False)
...@@ -207,6 +202,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil ...@@ -207,6 +202,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
# duplicate._xml_attr = copy.deepcopy(self._xml_attr) # this is not cython compatible # duplicate._xml_attr = copy.deepcopy(self._xml_attr) # this is not cython compatible
return duplicate return duplicate
def __str__(self): def __str__(self):
if isinstance(self, RootNode): if isinstance(self, RootNode):
root = cast(RootNode, self) root = cast(RootNode, self)
...@@ -214,11 +210,11 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil ...@@ -214,11 +210,11 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
if errors: if errors:
e_pos = errors[0].pos e_pos = errors[0].pos
content = self.content content = self.content
return content[:e_pos] + \ return content[:e_pos] + ' <<< Error on "%s" | %s >>> ' % \
' <<< Error on "%s" | %s >>> ' % \ (content[e_pos - self.pos:], '; '.join(e.message for e in errors))
(content[e_pos - self.pos:], '; '.join(e.message for e in errors))
return self.content return self.content
def __repr__(self): def __repr__(self):
# mpargs = {'name': self.parser.name, 'ptype': self.parser.ptype} # mpargs = {'name': self.parser.name, 'ptype': self.parser.ptype}
# name, ptype = (self._tag_name.split(':') + [''])[:2] # name, ptype = (self._tag_name.split(':') + [''])[:2]
...@@ -341,10 +337,10 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil ...@@ -341,10 +337,10 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
@result.setter @result.setter
def result(self, result: ResultType): def result(self, result: ResultType):
# # made obsolete by static type checking with mypy # # made obsolete by static type checking with mypy
assert ((isinstance(result, tuple) and all(isinstance(child, Node) for child in result)) # assert ((isinstance(result, tuple) and all(isinstance(child, Node) for child in result))
or isinstance(result, Node) # or isinstance(result, Node)
or isinstance(result, str) # or isinstance(result, str)
or isinstance(result, StringView)), "%s (%s)" % (str(result), str(type(result))) # or isinstance(result, StringView)), "%s (%s)" % (str(result), str(type(result)))
# Possible optimization: Do not allow single nodes as argument: # Possible optimization: Do not allow single nodes as argument:
# assert not isinstance(result, Node) # assert not isinstance(result, Node)
# self._content = None # self._content = None
...@@ -525,7 +521,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil ...@@ -525,7 +521,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
'(X (c "d"))' '(X (c "d"))'
Args: Args:
tag_name(set): A tag name or set of tag names that is being tag_names(set): A tag name or set of tag names that is being
searched for searched for
include_root (bool): If False, only descendant nodes will be include_root (bool): If False, only descendant nodes will be
checked for a match. checked for a match.
...@@ -630,7 +626,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil ...@@ -630,7 +626,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
flatten_threshold: int = 92) -> str: flatten_threshold: int = 92) -> str:
""" """
Returns content as S-expression, i.e. in lisp-like form. If this Returns content as S-expression, i.e. in lisp-like form. If this
method is callad on a RootNode-object, method is called on a RootNode-object,
Args: Args:
src: The source text or `None`. In case the source text is src: The source text or `None`. In case the source text is
...@@ -733,7 +729,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil ...@@ -733,7 +729,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
"""Returns the closing string for the representation of `node`.""" """Returns the closing string for the representation of `node`."""
if node.tag_name in omit_tags or node.tag_name in empty_tags: if node.tag_name in omit_tags or node.tag_name in empty_tags:
return '' return ''
return ('\n</') + node.tag_name + '>' return '\n</' + node.tag_name + '>'
def sanitizer(content: str) -> str: def sanitizer(content: str) -> str:
"""Substitute "&", "<", ">" in XML-content by the respective entities.""" """Substitute "&", "<", ">" in XML-content by the respective entities."""
...@@ -756,7 +752,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil ...@@ -756,7 +752,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
def to_json_obj(self) -> Dict: def to_json_obj(self) -> Dict:
"""Seralize a node or tree as json-object""" """Serialize a node or tree as json-object"""
data = [self.tag_name, data = [self.tag_name,
[child.to_json_obj() for child in self.children] [child.to_json_obj() for child in self.children]
if self.children else str(self._result)] if self.children else str(self._result)]
...@@ -793,7 +789,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil ...@@ -793,7 +789,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
separators=(', ', ': ') if indent is not None else (',', ':')) separators=(', ', ': ') if indent is not None else (',', ':'))
def serialize(node: Node, how: str='default') -> str: def serialize(node: Node, how: str = 'default') -> str:
""" """
Serializes the tree starting with `node` either as S-expression, XML, JSON, Serializes the tree starting with `node` either as S-expression, XML, JSON,
or in compact form. Possible values for `how` are 'S-expression', or in compact form. Possible values for `how` are 'S-expression',
...@@ -990,7 +986,7 @@ class RootNode(Node): ...@@ -990,7 +986,7 @@ class RootNode(Node):
""" """
Adds an error to this tree, locating it at a specific node. Adds an error to this tree, locating it at a specific node.
Parameters: Parameters:
pos(int): The position of the error in the source text node(Node): The node where the error occurred
message(str): A string with the error message.abs message(str): A string with the error message.abs
code(int): An error code to identify the kind of error code(int): An error code to identify the kind of error
""" """
...@@ -1182,7 +1178,7 @@ def parse_xml(xml: Union[str, StringView], ignore_pos: bool=False) -> Node: ...@@ -1182,7 +1178,7 @@ def parse_xml(xml: Union[str, StringView], ignore_pos: bool=False) -> Node:
d = match.groupdict() d = match.groupdict()
attributes[d['attr']] = d['value'] attributes[d['attr']] = d['value']
restart = s.index(match.end()) restart = s.index(match.end())
return (s[restart:], attributes) return s[restart:], attributes
def parse_opening_tag(s: StringView) -> Tuple[StringView, str, OrderedDict, bool]: def parse_opening_tag(s: StringView) -> Tuple[StringView, str, OrderedDict, bool]:
""" """
...@@ -1288,6 +1284,7 @@ def parse_tree(xml_sxpr_json: str) -> Optional[Node]: ...@@ -1288,6 +1284,7 @@ def parse_tree(xml_sxpr_json: str) -> Optional[Node]:
snippet = m.group(1) if m else '' snippet = m.group(1) if m else ''
raise ValueError('Snippet seems to be neither S-expression nor XML: ' + snippet + ' ...') raise ValueError('Snippet seems to be neither S-expression nor XML: ' + snippet + ' ...')
# if __name__ == "__main__": # if __name__ == "__main__":
# st = parse_sxpr("(alpha (beta (gamma i\nj\nk) (delta y)) (epsilon z))") # st = parse_sxpr("(alpha (beta (gamma i\nj\nk) (delta y)) (epsilon z))")
# print(st.as_sxpr()) # print(st.as_sxpr())
......
...@@ -2,5 +2,8 @@ ...@@ -2,5 +2,8 @@
license_files = LICENSE.txt license_files = LICENSE.txt
[bdist_wheel] [bdist_wheel]
universal=0 universal = 0
[pep8]
max-line-length = 100
ignore = E303
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment