Skip to content

Commit

Permalink
Refactored parsing of length arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Pelle Nilsson committed Jul 21, 2018
1 parent ed41147 commit ff6fa6b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 25 deletions.
50 changes: 25 additions & 25 deletions countersheet.py
Expand Up @@ -1038,6 +1038,18 @@ def add_outlinemarks(self, layer, x1, y1, x2, y2):
layer.append(self.create_registrationline(
x2, y1, x2, y2))


def from_len_arg(self, argvalue, name):
if argvalue is None or len(argvalue) == 0:
return 0.0
value = self.unittouu(argvalue)
if value < 0:
sys.exit("Negative %s marks makes no sense." % name)

self.logwrite("%s: %f\n"
% (name, value))
return value

def calculateScale(self, svg):
"""Calculates scale of the document (user-units size) and
saves as self.xscale and self.yscale.
Expand Down Expand Up @@ -1076,7 +1088,7 @@ def getDocumentViewBoxValue(self, svg, n, fallback):
except:
return float(svg.get(fallback)) # let it crash if this fails

# Because getDocumentWiddth in inkex fails because it makes assumptions about
# Because getDocumentWidth in inkex fails because it makes assumptions about
# user-units. Trusting the viewBox instead for now.
def getViewBoxWidth(self, svg):
return self.getDocumentViewBoxValue(svg, 2, "width")
Expand Down Expand Up @@ -1106,6 +1118,10 @@ def effect(self):

self.logwrite("one-sided sheets: %r\n" % self.oneside)

self.logwrite("getDocumentWidth: %s\n" % self.getDocumentWidth())
self.logwrite("getDocumentHeight: %s\n" % self.getDocumentHeight())
self.logwrite("getDocumentUnit: %s\n" % self.getDocumentUnit())

self.fullregistrationmarks = (self.options.fullregistrationmarks
== "true")
self.logwrite("full registration marks: %r\n"
Expand All @@ -1124,30 +1140,14 @@ def effect(self):
if self.bleed:
self.bleedmaker = BleedMaker(svg, self.defs)

try:
self.registrationmarkslen = self.unittouu(
self.options.registrationmarkslen)
except:
sys.exit("Failed to parse registration marks length %s. "
"Must be a non-negative number optionally followed by "
"a unit supported by SVG (eg mm, in, pt)."
% self.options.registrationmarkslen)
if self.registrationmarkslen < 0:
sys.exit("Negative length of registration marks makes no sense.")
self.logwrite("registration marks length %f\n"
% self.registrationmarkslen)

self.outlinemarks = (len(self.options.outlinedist) > 0)
if self.outlinemarks:
try:
self.outlinedist = self.unittouu(
self.options.outlinedist)
except:
sys.exit("Failed to parse outline distance %s. "
"Must be a number optionally followed by "
"a unit supported by SVG (eg mm, in, pt)."
% self.options.outlinedist)
self.logwrite("outline marks distance: %f\n" % self.outlinedist)
self.registrationmarkslen = self.from_len_arg(
self.options.registrationmarkslen,
"registration marks length")
self.outlinedist = self.from_len_arg(
self.options.outlinedist,
"outline distance")

self.outlinemarks = (self.outlinedist > 0)

self.calculateScale(svg)

Expand Down
1 change: 1 addition & 0 deletions unittests/alltests.py
Expand Up @@ -25,6 +25,7 @@ def make_suite(test):
tests = (countersheetstest.CountersheetsTest,
countersheetstest.SingleCounterTest,
countersheetstest.LayerTranslationTest,
countersheetstest.ParseLengthTest,
countersheetstest.DocumentTopLeftCoordinateConverterTest,
countertest.SingleCounterTest,
csvcounterdefinitionparsertest.CSVCounterDefinitionParserTest,
Expand Down
40 changes: 40 additions & 0 deletions unittests/countersheetstest.py
Expand Up @@ -12,6 +12,10 @@ def dummy_logwrite(msg):
def stdout_logwrite(msg):
print "LOG >> " + msg,

class DummyLog(object):
def write(self, msg):
pass

class CountersheetsTest(unittest.TestCase):
def setUp(self):
pass
Expand Down Expand Up @@ -225,5 +229,41 @@ def test_valid_replace_name_not_percent(self):
def test_valid_replace_name_not_amp(self):
self.assertFalse(countersheet.is_valid_name_to_replace("A&"))

class PartialCountersheetEffect(countersheet.CountersheetEffect):
def __init__(self):
self.log = DummyLog()

def getDocumentWidth(self):
return "210mm"

def getDocumentHeight(self):
return "297mm"

def getDocumentUnit(self):
return "mm"

class ParseLengthTest(unittest.TestCase):
def setUp(self):
self.cs = PartialCountersheetEffect()

def check_parse(self, s, expected):
self.assertAlmostEqual(self.cs.from_len_arg(s, "test"),
expected, 2)

def test_1mm(self):
self.check_parse("1mm", 1.0)

def test_1in(self):
self.check_parse("1in", 25.4)

def test_none(self):
self.check_parse(None, 0.0)

def test_empty(self):
self.check_parse("", 0.0)

def test_0(self):
self.check_parse("0", 0.0)

if __name__ == '__main__':
unittest.main()

0 comments on commit ff6fa6b

Please sign in to comment.