linetypes.py

Wed, 31 Jan 2018 14:34:23 +0200

author
Santeri Piippo
date
Wed, 31 Jan 2018 14:34:23 +0200
changeset 32
75f44d3063da
parent 7
0ab0d61ccee8
child 35
865cd526e8b6
permissions
-rw-r--r--

Reworked web front, problems are now sorted by category as well as line number

def ldraw_str(value):
    ' Like str() except removes unneeded ".0"-suffixes '
    rep = str(value)
    if isinstance(value, float):
        if rep.endswith('.0'):
            rep = rep[:-2]
        if rep == '-0':
            rep = '0'
    return rep

class EmptyLine:
    def __repr__(self):
        return 'linetypes.EmptyLine()'
    def textual_representation(self):
        return ''
    def typename(self):
        return 'empty line'

class Comment:
    def __init__(self, text, style = 'old'):
        if style == 'old' and text.startswith('//'):
            self.text = text[2:].strip()
            self.style = 'new'
        else:
            self.text = text
            self.style = style
    def __repr__(self):
        return str.format('linetypes.Comment({text!r}, {style!r})',
            text = self.text,
            style = self.style,
        )
    def textual_representation(self):
        if self.style == 'old':
            return '0 ' + self.text
        else:
            return '0 // ' + self.text
    def typename(self):
        return 'comment'

class SubfileReference:
    def __init__(self, *, colour, subfile_path, anchor, matrix):
        self.colour, self.subfile_path, = colour, subfile_path
        self.anchor, self.matrix = anchor, matrix
    def __repr__(self):
        return str.format('linetypes.SubfileReference(' \
            'colour = {colour!r}, ' \
            'subfile_path = {subfile_path!r}, ' \
            'anchor = {anchor!r}, ' \
            'matrix = {matrix!r})', **self.__dict__)
    def textual_representation(self):
        args = [
            1,
            self.colour,
            *self.anchor.coordinates,
            *self.matrix.values,
            self.subfile_path,
        ]
        return ' '.join(ldraw_str(arg) for arg in args)
    def typename(self):
        return 'subfile reference'

class BasePolygon:
    def __init__(self, *, colour, geometry):
        self.colour, self.geometry = colour, geometry
    def __repr__(self):
        return str.format('linetypes.{typename}(' \
            'colour = {colour!r}, ' \
            'geometry = {geometry!r})',
            typename = type(self).__name__,
            colour = self.colour,
            geometry = self.geometry,
        )
    def base_textual_representation(self):
        args = [self.colour]
        for vertex in self.geometry.vertices:
            args += vertex.coordinates
        return ' '.join(ldraw_str(arg) for arg in args)

class LineSegment(BasePolygon):
    def textual_representation(self):
        return '2 ' + self.base_textual_representation()
    def typename(self):
        return 'line segment'

class Triangle(BasePolygon):
    def textual_representation(self):
        return '3 ' + self.base_textual_representation()
    def typename(self):
        return 'triangle'

class Quadrilateral(BasePolygon):
    def textual_representation(self):
        return '4 ' + self.base_textual_representation()
    def typename(self):
        return 'quadrilateral'

class Contour(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(' \
            'colour = {colour!r}, ' \
            'geometry = {geometry!r}, ' \
            'control_points = {control_points!r})', **self.__dict__)
    def textual_representation(self):
        result = '5 ' + self.base_textual_representation()
        for control_point in self.control_points:
            strings = (ldraw_str(value) for value in control_point.coordinates)
            result += ' '
            result += ' '.join(strings)
        return result
    def typename(self):
        return 'contour line segment'

mercurial