summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Pokorný <jpokorny@redhat.com>2013-01-21 20:22:25 +0100
committerJan Pokorný <jpokorny@redhat.com>2013-01-21 20:23:36 +0100
commitb08398d0cbb2d49e79b4f7583b59baa5b4f9e230 (patch)
treeab7995d3c7dd27e19c3d76f0c8cf56c3fd3ab943
parente7714aacfb0246b66c75bfee4f2428aca26acd64 (diff)
downloadrnc2rng-b08398d0cbb2d49e79b4f7583b59baa5b4f9e230.tar.gz
rnc2rng-b08398d0cbb2d49e79b4f7583b59baa5b4f9e230.tar.xz
rnc2rng-b08398d0cbb2d49e79b4f7583b59baa5b4f9e230.zip
Get closer to PEP8 (suppress some pep8 and pyflake msgs)
Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
-rwxr-xr-xrnctree.py161
1 files changed, 85 insertions, 76 deletions
diff --git a/rnctree.py b/rnctree.py
index 23c825e..fb78d05 100755
--- a/rnctree.py
+++ b/rnctree.py
@@ -5,6 +5,7 @@ from __future__ import generators
import sys
from rnc_tokenize import token_list
+
class ParseError(SyntaxError): pass
for t in """
@@ -13,14 +14,14 @@ for t in """
DEFAULT_NS NS DATATYPES DATATAG PATTERN START DEFINE
""".split(): globals()[t] = t
-PAIRS = {'BEG_BODY':('END_BODY',BODY),
- 'BEG_PAREN':('END_PAREN',GROUP),
- 'BEG_ANNO':('END_ANNO',ANNOTATION)}
+PAIRS = {'BEG_BODY': ('END_BODY', BODY),
+ 'BEG_PAREN': ('END_PAREN', GROUP),
+ 'BEG_ANNO': ('END_ANNO', ANNOTATION)}
-TAGS = { ONE: 'group',
- SOME: 'oneOrMore',
- MAYBE: 'optional',
- ANY: 'zeroOrMore'}
+TAGS = {ONE: 'group',
+ SOME: 'oneOrMore',
+ MAYBE: 'optional',
+ ANY: 'zeroOrMore'}
DEFAULT_NAMESPACE = None
DATATYPE_LIB = [0, '"http://www.w3.org/2001/XMLSchema-datatypes"']
@@ -28,12 +29,14 @@ OTHER_NAMESPACE = {}
CONTEXT_FREE = 0
try: enumerate
-except: enumerate = lambda seq: zip(range(len(seq)),seq)
+except: enumerate = lambda seq: zip(range(len(seq)), seq)
nodetypes = lambda nl: tuple(map(lambda n: n.type, nl))
toNodes = lambda toks: map(lambda t: Node(t.type, t.value), toks)
+
class Node(object):
- __slots__ = ('type','value','name','quant')
+ __slots__ = ('type', 'value', 'name', 'quant')
+
def __iter__(self): yield self
__len__ = lambda self: 1
@@ -44,14 +47,14 @@ class Node(object):
self.quant = quant
def format(self, indent=0):
- out = [' '*indent+repr(self)]
+ out = [' ' * indent + repr(self)]
write = out.append
if isinstance(self.value, str):
- if self.type==COMMENT:
- write(' '*(1+indent)+self.value)
+ if self.type == COMMENT:
+ write(' ' * (1 + indent) + self.value)
else:
for node in self.value:
- write(node.format(indent+1))
+ write(node.format(indent + 1))
return '\n'.join(out)
def prettyprint(self):
@@ -70,7 +73,6 @@ class Node(object):
else:
return self.add_ns(self.xmlnode())
-
def xmlnode(self, indent=0):
out = []
write = out.append
@@ -79,66 +81,66 @@ class Node(object):
for x in self.value:
if not isinstance(x, Node):
- raise TypeError, "Unhappy Node.value: "+repr(x)
+ raise TypeError("Unhappy Node.value: " + repr(x))
elif x.type == START:
startelem = '<start><ref name="%s"/></start>' % x.value
- write(' '*indent+startelem)
+ write(' ' * indent + startelem)
elif x.type == DEFINE:
- write(' '*indent+'<define name="%s">' % x.name)
- write(x.xmlnode(indent+1))
- write(' '*indent+'</define>')
+ write(' ' * indent + '<define name="%s">' % x.name)
+ write(x.xmlnode(indent + 1))
+ write(' ' * indent + '</define>')
elif x.type == NAME:
- write(' '*indent+ '<ref name="%s"/>' % x.value)
+ write(' ' * indent + '<ref name="%s"/>' % x.value)
elif x.type == COMMENT:
- write(' '*indent+'<!-- %s -->' % x.value)
+ write(' ' * indent + '<!-- %s -->' % x.value)
elif x.type == LITERAL:
- write(' '*indent+'<value>%s</value>' % x.value)
+ write(' ' * indent + '<value>%s</value>' % x.value)
elif x.type == ANNOTATION:
- write(' '*indent+\
- '<a:documentation>%s</a:documentation>' % x.value)
+ write(' ' * indent
+ + '<a:documentation>%s</a:documentation>' % x.value)
elif x.type == INTERLEAVE:
- write(' '*indent+'<interleave>')
- write(x.xmlnode(indent+1))
- write(' '*indent+'</interleave>')
+ write(' ' * indent + '<interleave>')
+ write(x.xmlnode(indent + 1))
+ write(' ' * indent + '</interleave>')
elif x.type == SEQ:
- write(x.xmlnode(indent+1))
+ write(x.xmlnode(indent + 1))
elif x.type == CHOICE:
- write(' '*indent+'<choice>')
- write(x.xmlnode(indent+1))
- write(' '*indent+'</choice>')
+ write(' ' * indent + '<choice>')
+ write(x.xmlnode(indent + 1))
+ write(' ' * indent + '</choice>')
elif x.type == GROUP:
write(x.xmlnode(indent))
elif x.type == TEXT:
- write(' '*indent+'<text/>')
+ write(' ' * indent + '<text/>')
elif x.type == EMPTY:
- write(' '*indent+'<empty/>')
+ write(' ' * indent + '<empty/>')
elif x.type == DATATAG:
DATATYPE_LIB[0] = 1 # Use datatypes
if x.name is None: # no paramaters
- write(' '*indent+'<data type="%s"/>' % x.value)
+ write(' ' * indent + '<data type="%s"/>' % x.value)
else:
- write(' '*indent+'<data type="%s">' % x.name)
+ write(' ' * indent + '<data type="%s">' % x.name)
p = '<param name="pattern">%s</param>' % x.value
- write(' '*(indent+1)+p)
- write(' '*indent+'</data>')
+ write(' ' * (indent + 1) + p)
+ write(' ' * indent + '</data>')
elif x.type == ELEM:
if x.quant == ONE:
- write(' '*indent+'<element name="%s">' % x.name)
- write(x.xmlnode(indent+1))
- write(' '*indent+'</element>')
+ write(' ' * indent + '<element name="%s">' % x.name)
+ write(x.xmlnode(indent + 1))
+ write(' ' * indent + '</element>')
else:
- write(' '*indent+'<%s>' % TAGS[x.quant])
- write(' '*(indent+1)+'<element name="%s">' % x.name)
- write(x.xmlnode(indent+2))
- write(' '*(indent+1)+'</element>')
- write(' '*indent+'</%s>' % TAGS[x.quant])
+ write(' ' * indent + '<%s>' % TAGS[x.quant])
+ write(' ' * (indent + 1) + '<element name="%s">' % x.name)
+ write(x.xmlnode(indent + 2))
+ write(' ' * (indent + 1) + '</element>')
+ write(' ' * indent + '</%s>' % TAGS[x.quant])
elif x.type == ATTR:
if x.value[0].type == TEXT:
- write(' '*indent+'<attribute name="%s"/>' % x.name)
+ write(' ' * indent + '<attribute name="%s"/>' % x.name)
elif x.value[0].type == EMPTY:
- write(' '*indent+'<attribute name="%s">' % x.name)
- write(' '*(indent+1)+'<empty/>')
- write(' '*indent+'</attribute>')
+ write(' ' * indent + '<attribute name="%s">' % x.name)
+ write(' ' * (indent + 1) + '<empty/>')
+ write(' ' * indent + '</attribute>')
return '\n'.join(out)
@@ -149,10 +151,10 @@ class Node(object):
def add_ns(self, xml):
"Add namespace attributes to top level element"
lines = xml.split('\n')
- self.nest_annotations(lines) # annots not allowed before root elem
+ self.nest_annotations(lines) # annots not allowed before root elem
for i, line in enumerate(lines):
ltpos = line.find('<')
- if ltpos >= 0 and line[ltpos+1] not in ('!','?'):
+ if ltpos >= 0 and line[ltpos + 1] not in ('!', '?'):
# We've got an element tag, not PI or comment
new = line[:line.find('>')]
new += ' xmlns="http://relaxng.org/ns/structure/1.0"'
@@ -176,21 +178,23 @@ class Node(object):
del lines[i]
else:
ltpos = line.find('<')
- if ltpos >= 0 and line[ltpos+1] not in ('!','?'):
+ if ltpos >= 0 and line[ltpos + 1] not in ('!', '?'):
break
for line in top_annotations:
- lines.insert(i, ' '+line)
+ lines.insert(i, ' ' + line)
+
def findmatch(beg, nodes, offset):
level = 1
end = PAIRS[beg][0]
- for i,t in enumerate(nodes[offset:]):
+ for i, t in enumerate(nodes[offset:]):
if t.type == beg: level += 1
elif t.type == end: level -= 1
if level == 0:
- return i+offset
- raise EOFError, ("No closing token encountered for %s @ %d"
- % (beg,offset))
+ return i + offset
+ raise EOFError("No closing token encountered for %s @ %d"
+ % (beg, offset))
+
def match_pairs(nodes):
newnodes = []
@@ -200,12 +204,12 @@ def match_pairs(nodes):
node = nodes[i]
if node.type in PAIRS.keys():
# Look for enclosing brackets
- match = findmatch(node.type, nodes, i+1)
+ match = findmatch(node.type, nodes, i + 1)
matchtype = PAIRS[node.type][1]
- node = Node(type=matchtype, value=nodes[i+1:match])
+ node = Node(type=matchtype, value=nodes[i + 1:match])
node.value = match_pairs(node.value)
newnodes.append(node)
- i = match+1
+ i = match + 1
else:
newnodes.append(node)
i += 1
@@ -216,20 +220,21 @@ def match_pairs(nodes):
nodes[:] = newnodes
return nodes
+
def type_bodies(nodes):
newnodes = []
i = 0
while 1:
if i >= len(nodes): break
- if nodetypes(nodes[i:i+3]) == (ELEM, NAME, BODY) or \
- nodetypes(nodes[i:i+3]) == (ATTR, NAME, BODY):
- name, body = nodes[i+1].value, nodes[i+2]
+ if nodetypes(nodes[i:i + 3]) == (ELEM, NAME, BODY) or \
+ nodetypes(nodes[i:i + 3]) == (ATTR, NAME, BODY):
+ name, body = nodes[i + 1].value, nodes[i + 2]
value, quant = type_bodies(body.value), body.quant
node = Node(nodes[i].type, value, name, quant)
newnodes.append(node)
i += 3
- elif nodetypes(nodes[i:i+2]) == (DATATAG, PATTERN):
- node = Node(DATATAG, nodes[i+1].value, nodes[i].value)
+ elif nodetypes(nodes[i:i + 2]) == (DATATAG, PATTERN):
+ node = Node(DATATAG, nodes[i + 1].value, nodes[i].value)
newnodes.append(node)
i += 2
elif nodes[i] == DEFINE:
@@ -243,6 +248,7 @@ def type_bodies(nodes):
nodes[:] = newnodes
return nodes
+
def nest_defines(nodes):
"Attach groups to named patterns"
newnodes = []
@@ -253,8 +259,8 @@ def nest_defines(nodes):
newnodes.append(node)
if node.type == DEFINE:
group = []
- while (i+1) < len(nodes) and nodes[i+1].type <> DEFINE:
- group.append(nodes[i+1])
+ while (i + 1) < len(nodes) and nodes[i + 1].type != DEFINE:
+ group.append(nodes[i + 1])
i += 1
node.name = node.value
node.value = Node(GROUP, group)
@@ -262,27 +268,29 @@ def nest_defines(nodes):
nodes[:] = newnodes
return nodes
+
def intersperse(nodes):
"Look for interleaved, choice, or sequential nodes in groups/bodies"
for node in nodes:
if node.type in (ELEM, ATTR, GROUP, LITERAL):
val = node.value
- ntypes = [n.type for n in val if not isinstance(val,str)]
- inters = [t for t in ntypes if t in (INTERLEAVE,CHOICE,SEQ)]
- inters = dict(zip(inters,[0]*len(inters)))
+ ntypes = [n.type for n in val if not isinstance(val, str)]
+ inters = [t for t in ntypes if t in (INTERLEAVE, CHOICE, SEQ)]
+ inters = dict(zip(inters, [0] * len(inters)))
if len(inters) > 1:
- raise ParseError, "Ambiguity in sequencing: %s" % node
+ raise ParseError("Ambiguity in sequencing: %s" % node)
if len(inters) > 0:
intertype = inters.keys()[0]
items = []
for pat in node.value:
- if pat.type <> intertype:
+ if pat.type != intertype:
items.append(pat)
node.value = Node(intertype, items)
- if not isinstance(node.value, str): # No recurse to terminal str
+ if not isinstance(node.value, str): # No recurse to terminal str
intersperse(node.value)
return nodes
+
def scan_NS(nodes):
"Look for any namespace configuration lines"
global DEFAULT_NAMESPACE, OTHER_NAMESPACE, CONTEXT_FREE
@@ -292,7 +300,7 @@ def scan_NS(nodes):
elif node.type == NS:
ns, url = map(str.strip, node.value.split('='))
OTHER_NAMESPACE[ns] = url
- elif node.type == ANNOTATION and not OTHER_NAMESPACE.has_key('a'):
+ elif node.type == ANNOTATION and 'a' not in OTHER_NAMESPACE:
OTHER_NAMESPACE['a'] =\
'"http://relaxng.org/ns/compatibility/annotations/1.0"'
elif node.type == DATATYPES:
@@ -300,6 +308,7 @@ def scan_NS(nodes):
elif node.type == START:
CONTEXT_FREE = 1
+
def make_nodetree(tokens):
nodes = toNodes(tokens)
match_pairs(nodes)
@@ -310,6 +319,6 @@ def make_nodetree(tokens):
root = Node(ROOT, nodes)
return root
-if __name__=='__main__':
- make_nodetree(token_list(sys.stdin.read())).prettyprint()
+if __name__ == '__main__':
+ make_nodetree(token_list(sys.stdin.read())).prettyprint()