cleanup

Fri, 24 May 2019 14:18:31 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 24 May 2019 14:18:31 +0200
changeset 35
865cd526e8b6
parent 34
7ed2e831acd4
child 36
2753aad79678

cleanup

linetypes.py file | annotate | diff | comparison | revisions
parse.py file | annotate | diff | comparison | revisions
--- a/linetypes.py	Fri May 24 14:18:28 2019 +0200
+++ b/linetypes.py	Fri May 24 14:18:31 2019 +0200
@@ -94,13 +94,13 @@
     def typename(self):
         return 'quadrilateral'
 
-class Contour(LineSegment):
+class ConditionalLine(LineSegment):
     def __init__(self, *, colour, geometry, control_points):
         super().__init__(colour = colour, geometry = geometry)
         self.control_points = control_points
         assert(len(self.control_points) == 2)
     def __repr__(self):
-        return str.format('linetypes.Contour(' \
+        return str.format('linetypes.ConditionalLine(' \
             'colour = {colour!r}, ' \
             'geometry = {geometry!r}, ' \
             'control_points = {control_points!r})', **self.__dict__)
--- a/parse.py	Fri May 24 14:18:28 2019 +0200
+++ b/parse.py	Fri May 24 14:18:31 2019 +0200
@@ -15,7 +15,7 @@
     elif line == '0':
         return linetypes.Comment('')
     elif line.startswith('0 '):
-        return linetypes.Comment(line[2:].strip())
+        return linetypes.Comment(line[2:])
     elif line.startswith('1 '):
         return parse_ldraw_subfile_reference(line)
     elif line.startswith('2 '):
@@ -25,7 +25,7 @@
     elif line.startswith('4 '):
         return parse_ldraw_quadrilateral(line)
     elif line.startswith('5 '):
-        return parse_ldraw_contour(line)
+        return parse_ldraw_conditional_line(line)
     else:
         raise BadLdrawLine('unknown line type')
 
@@ -55,11 +55,11 @@
     )
 
 def generic_parse_polygon(line, *, type_code, vertex_count):
-    pattern = r'^' \
-        + str(type_code) \
-        + '\s+([^ ]+)' \
-        + r'\s+([^ ]+)' * (vertex_count * 3) \
-        + r'\s*$'
+    pattern = r'^' # matches the start of line
+    pattern += str(type_code) # matches the type code
+    pattern += '\s+([^ ]+)' # matches the colour
+    pattern += r'\s+([^ ]+)' * (vertex_count * 3) # matches the vertices
+    pattern += r'\s*$' # matches any trailing space
     match = re.search(pattern, line)
     if not match:
         raise BadLdrawLine(str.format('cannot parse type-{} line', type_code))
@@ -100,9 +100,9 @@
         geometry = Polygon(parse_result['vertices']),
     )
 
-def parse_ldraw_contour(line):
+def parse_ldraw_conditional_line(line):
     parse_result = generic_parse_polygon(line, type_code = 5, vertex_count = 4)
-    return linetypes.Contour(
+    return linetypes.ConditionalLine(
         colour = parse_result['colour'],
         geometry = LineSegment(*parse_result['vertices'][0:2]),
         control_points = parse_result['vertices'][2:],

mercurial