Stricter locking (lock contained ndarrays and turn lists into tuples)

This commit is contained in:
Jan Petykiewicz 2020-05-11 19:29:00 -07:00
parent 79990e3806
commit c79eee2a8c
11 changed files with 88 additions and 9 deletions

View File

@ -80,7 +80,7 @@ class Label:
offset: vector2 = (0.0, 0.0), offset: vector2 = (0.0, 0.0),
layer: layer_t = 0, layer: layer_t = 0,
locked: bool = False): locked: bool = False):
self.unlock() object.__setattr__(self, 'locked', False)
self.identifier = () self.identifier = ()
self.string = string self.string = string
self.offset = numpy.array(offset, dtype=float, copy=True) self.offset = numpy.array(offset, dtype=float, copy=True)
@ -156,6 +156,7 @@ class Label:
Return: Return:
self self
""" """
self.offset.flags.writeable = False
object.__setattr__(self, 'locked', True) object.__setattr__(self, 'locked', True)
return self return self
@ -167,4 +168,5 @@ class Label:
self self
""" """
object.__setattr__(self, 'locked', False) object.__setattr__(self, 'locked', False)
self.offset.flags.writeable = True
return self return self

View File

@ -71,7 +71,7 @@ class Pattern:
name: An identifier for the Pattern name: An identifier for the Pattern
locked: Whether to lock the pattern after construction locked: Whether to lock the pattern after construction
""" """
self.unlock() object.__setattr__(self, 'locked', False)
if isinstance(shapes, list): if isinstance(shapes, list):
self.shapes = shapes self.shapes = shapes
else: else:
@ -715,6 +715,9 @@ class Pattern:
Returns: Returns:
self self
""" """
self.shapes = tuple(self.shapes)
self.labels = tuple(self.labels)
self.subpatterns = tuple(self.subpatterns)
object.__setattr__(self, 'locked', True) object.__setattr__(self, 'locked', True)
return self return self
@ -726,6 +729,9 @@ class Pattern:
self self
""" """
object.__setattr__(self, 'locked', False) object.__setattr__(self, 'locked', False)
self.shapes = list(self.shapes)
self.labels = list(self.labels)
self.subpatterns = list(self.subpatterns)
return self return self
def deeplock(self) -> 'Pattern': def deeplock(self) -> 'Pattern':

View File

@ -121,7 +121,8 @@ class GridRepetition:
if b_count < 1: if b_count < 1:
raise PatternError('Repetition has too-small b_count: ' raise PatternError('Repetition has too-small b_count: '
'{}'.format(b_count)) '{}'.format(b_count))
self.unlock()
object.__setattr__(self, 'locked', False)
self.a_vector = a_vector self.a_vector = a_vector
self.b_vector = b_vector self.b_vector = b_vector
self.a_count = a_count self.a_count = a_count
@ -481,6 +482,11 @@ class GridRepetition:
Returns: Returns:
self self
""" """
self.offset.flags.writeable = False
self.a_vector.flags.writeable = False
self.mirrored.flags.writeable = False
if self.b_vector is not None:
self.b_vector.flags.writeable = False
object.__setattr__(self, 'locked', True) object.__setattr__(self, 'locked', True)
return self return self
@ -491,6 +497,11 @@ class GridRepetition:
Returns: Returns:
self self
""" """
self.offset.flags.writeable = True
self.a_vector.flags.writeable = True
self.mirrored.flags.writeable = True
if self.b_vector is not None:
self.b_vector.flags.writeable = True
object.__setattr__(self, 'locked', False) object.__setattr__(self, 'locked', False)
return self return self

View File

@ -158,7 +158,7 @@ class Arc(Shape):
layer: layer_t = 0, layer: layer_t = 0,
dose: float = 1.0, dose: float = 1.0,
locked: bool = False): locked: bool = False):
self.unlock() object.__setattr__(self, 'locked', False)
self.identifier = () self.identifier = ()
self.radii = radii self.radii = radii
self.angles = angles self.angles = angles
@ -386,3 +386,15 @@ class Arc(Shape):
a.append((a0, a1)) a.append((a0, a1))
return numpy.array(a) return numpy.array(a)
def lock(self) -> 'Arc':
self.radii.flags.writeable = False
self.angles.flags.writeable = False
Shape.lock(self)
return self
def unlock(self) -> 'Arc':
Shape.unlock(self)
self.radii.flags.writeable = True
self.angles.flags.writeable = True
return self

View File

@ -46,7 +46,7 @@ class Circle(Shape):
layer: layer_t = 0, layer: layer_t = 0,
dose: float = 1.0, dose: float = 1.0,
locked: bool = False): locked: bool = False):
self.unlock() object.__setattr__(self, 'locked', False)
self.identifier = () self.identifier = ()
self.offset = numpy.array(offset, dtype=float) self.offset = numpy.array(offset, dtype=float)
self.layer = layer self.layer = layer

View File

@ -93,7 +93,7 @@ class Ellipse(Shape):
layer: layer_t = 0, layer: layer_t = 0,
dose: float = 1.0, dose: float = 1.0,
locked: bool = False): locked: bool = False):
self.unlock() object.__setattr__(self, 'locked', False)
self.identifier = () self.identifier = ()
self.radii = radii self.radii = radii
self.offset = offset self.offset = offset
@ -180,3 +180,12 @@ class Ellipse(Shape):
(self.offset, scale/norm_value, angle, False, self.dose), \ (self.offset, scale/norm_value, angle, False, self.dose), \
lambda: Ellipse(radii=radii*norm_value, layer=self.layer) lambda: Ellipse(radii=radii*norm_value, layer=self.layer)
def lock(self) -> 'Ellipse':
self.radii.flags.writeable = False
Shape.lock(self)
return self
def unlock(self) -> 'Ellipse':
Shape.unlock(self)
self.radii.flags.writeable = True
return self

View File

@ -149,7 +149,7 @@ class Path(Shape):
dose: float = 1.0, dose: float = 1.0,
locked: bool = False, locked: bool = False,
): ):
self.unlock() object.__setattr__(self, 'locked', False)
self._cap_extensions = None # Since .cap setter might access it self._cap_extensions = None # Since .cap setter might access it
self.identifier = () self.identifier = ()
@ -394,3 +394,16 @@ class Path(Shape):
extensions = numpy.zeros(2) extensions = numpy.zeros(2)
return extensions return extensions
def lock(self) -> 'Path':
self.vertices.flags.writeable = False
if self.cap_extensions is not None:
self.cap_extensions.flags.writeable = False
Shape.lock(self)
return self
def unlock(self) -> 'Path':
Shape.unlock(self)
self.vertices.flags.writeable = True
if self.cap_extensions is not None:
self.cap_extensions.flags.writeable = True
return self

View File

@ -76,7 +76,7 @@ class Polygon(Shape):
dose: float = 1.0, dose: float = 1.0,
locked: bool = False, locked: bool = False,
): ):
self.unlock() object.__setattr__(self, 'locked', False)
self.identifier = () self.identifier = ()
self.layer = layer self.layer = layer
self.dose = dose self.dose = dose
@ -329,3 +329,13 @@ class Polygon(Shape):
''' '''
self.vertices = remove_colinear_vertices(self.vertices, closed_path=True) self.vertices = remove_colinear_vertices(self.vertices, closed_path=True)
return self return self
def lock(self) -> 'Polygon':
self.vertices.flags.writeable = False
Shape.lock(self)
return self
def unlock(self) -> 'Polygon':
Shape.unlock(self)
self.vertices.flags.writeable = True
return self

View File

@ -443,6 +443,7 @@ class Shape(metaclass=ABCMeta):
Returns: Returns:
self self
""" """
self.offset.flags.writeable = False
object.__setattr__(self, 'locked', True) object.__setattr__(self, 'locked', True)
return self return self
@ -454,4 +455,5 @@ class Shape(metaclass=ABCMeta):
self self
""" """
object.__setattr__(self, 'locked', False) object.__setattr__(self, 'locked', False)
self.offset.flags.writeable = True
return self return self

View File

@ -77,7 +77,7 @@ class Text(Shape):
dose: float = 1.0, dose: float = 1.0,
locked: bool = False, locked: bool = False,
): ):
self.unlock() object.__setattr__(self, 'locked', False)
self.identifier = () self.identifier = ()
self.offset = offset self.offset = offset
self.layer = layer self.layer = layer
@ -243,3 +243,13 @@ def get_char_as_polygons(font_path: str,
polygons = path.to_polygons() polygons = path.to_polygons()
return polygons, advance return polygons, advance
def lock(self) -> 'Text':
self.mirrored.flags.writeable = False
Shape.lock(self)
return self
def unlock(self) -> 'Text':
Shape.unlock(self)
self.mirrored.flags.writeable = True
return self

View File

@ -296,6 +296,8 @@ class SubPattern:
Returns: Returns:
self self
""" """
self.offset.flags.writeable = False
self.mirrored.flags.writeable = False
object.__setattr__(self, 'locked', True) object.__setattr__(self, 'locked', True)
return self return self
@ -306,6 +308,8 @@ class SubPattern:
Returns: Returns:
self self
""" """
self.offset.flags.writeable = True
self.mirrored.flags.writeable = True
object.__setattr__(self, 'locked', False) object.__setattr__(self, 'locked', False)
return self return self