-
Notifications
You must be signed in to change notification settings - Fork 249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix parsing of csv template files #219
Changes from 1 commit
71bd53d
d70bc37
6ed9686
fa62a8d
f1d7802
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,26 +74,37 @@ def load_elements(self, elements): | |
self.elements = elements | ||
self.keys = [v["name"].lower() for v in self.elements] | ||
|
||
def _parse_colorcode(self, s): | ||
""" Allow hex and oct values for colors """ | ||
s = s.strip() | ||
if not s: | ||
raise ValueError('Foreground and Background must be numeric') | ||
if s[:2] in ['0x', '0X']: | ||
return int(s, 16) | ||
elif s[0] == '0': | ||
return int(s, 8) | ||
return int(s) | ||
|
||
def parse_csv(self, infile, delimiter=",", decimal_sep=".", encoding=None): | ||
"""Parse template format csv file and create elements dict""" | ||
keys = ( | ||
"name", | ||
"type", | ||
"x1", | ||
"y1", | ||
"x2", | ||
"y2", | ||
"font", | ||
"size", | ||
"bold", | ||
"italic", | ||
"underline", | ||
"foreground", | ||
"background", | ||
"align", | ||
"text", | ||
"priority", | ||
"multiline", | ||
handlers = ( | ||
("name", str.strip), | ||
("type", str.strip), | ||
("x1", float), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i like this approach. "in the name of deterministic behavior" why strip the fields? but this is a good place to put the actual conversions, and less lines of code than what i was considering when i put the util function in. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Everything got stripped in the original version, so I just left it that way in order not to change the behaviour. The alternative would be to just use |
||
("y1", float), | ||
("x2", float), | ||
("y2", float), | ||
("font", str.strip), | ||
("size", float), | ||
("bold", int), | ||
("italic", int), | ||
("underline", int), | ||
("foreground", self._parse_colorcode), | ||
("background", self._parse_colorcode), | ||
("align", str.strip), | ||
("text", str.strip), | ||
("priority", int), | ||
("multiline", int), | ||
) | ||
self.elements = [] | ||
self.pg_no = 0 | ||
|
@@ -105,9 +116,7 @@ def parse_csv(self, infile, delimiter=",", decimal_sep=".", encoding=None): | |
for i, v in enumerate(row): | ||
if not v.startswith("'") and decimal_sep != ".": | ||
v = v.replace(decimal_sep, ".") | ||
stripped_value = v.strip() | ||
typed_value = try_to_type(stripped_value) | ||
kargs[keys[i]] = typed_value | ||
kargs[handlers[i][0]] = handlers[i][1](v) | ||
self.elements.append(kargs) | ||
self.keys = [v["name"].lower() for v in self.elements] | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
line0;L;20.0;12.0;190.0;12.0;times;0.5;0;0;0;0;16777215;C;;0 | ||
line1;L;20.0;36.0;190.0;36.0;times;0.5;0;0;0;0;16777215;C;;0 | ||
name0;T;21;14;104;25;times;16.0;0;0;0;0;16777215;L;name;2 | ||
title0;T;21;26;104;30;times;10.0;0;0;0;0;16777215;L;title;2 | ||
line0;L;20.0;12.0;190.0;12.0;times;0.5;0;0;0;0;16777215;C;;0;0 | ||
line1;L;20.0;36.0;190.0;36.0;times;0.5;0;0;0;0;16777215;C;;0;0 | ||
name0;T;21.0;14.0;104.0;25.0;times;16.0;0;0;0;0;16777215;L;name;2;0 | ||
title0;T;21.0;26.0;104.0;30.0;times;10.0;0;0;0;0;16777215;L;title;2;0 | ||
multiline;T;21.0;50.0;28.0;54.0;times;10.5;0;0;0;0;0xffff00;L;multi line;0;1 | ||
numeric_text;T;21.0;80.0;100.0;84.0;times;10.5;0;0;0;0;16777215;R;007;0;0 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ def test_template_nominal_hardcoded(tmp_path): | |
"align": "I", | ||
"text": "logo", | ||
"priority": 2, | ||
"multiline": 0, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I only changed the tests in this area. Previously multiline was not tested at all, so I added it there. |
||
}, | ||
{ | ||
"name": "company_name", | ||
|
@@ -45,6 +46,26 @@ def test_template_nominal_hardcoded(tmp_path): | |
"align": "I", | ||
"text": "", | ||
"priority": 2, | ||
"multiline": 0, | ||
}, | ||
{ | ||
"name": "multline_text", | ||
"type": "T", | ||
"x1": 20, | ||
"y1": 100, | ||
"x2": 40, | ||
"y2": 105, | ||
"font": "helvetica", | ||
"size": 12, | ||
"bold": 0, | ||
"italic": 0, | ||
"underline": 0, | ||
"foreground": 0, | ||
"background": 0x88ff00, | ||
"align": "I", | ||
"text": "Lorem ipsum dolor sit amet, consectetur adipisici elit", | ||
"priority": 2, | ||
"multiline": 1, | ||
}, | ||
{ | ||
"name": "box", | ||
|
@@ -62,6 +83,7 @@ def test_template_nominal_hardcoded(tmp_path): | |
"align": "I", | ||
"text": None, | ||
"priority": 0, | ||
"multiline": 0, | ||
}, | ||
{ | ||
"name": "box_x", | ||
|
@@ -79,6 +101,7 @@ def test_template_nominal_hardcoded(tmp_path): | |
"align": "I", | ||
"text": None, | ||
"priority": 2, | ||
"multiline": 0, | ||
}, | ||
{ | ||
"name": "line1", | ||
|
@@ -96,6 +119,7 @@ def test_template_nominal_hardcoded(tmp_path): | |
"align": "I", | ||
"text": None, | ||
"priority": 3, | ||
"multiline": 0, | ||
}, | ||
{ | ||
"name": "barcode", | ||
|
@@ -113,6 +137,7 @@ def test_template_nominal_hardcoded(tmp_path): | |
"align": "I", | ||
"text": "200000000001000159053338016581200810081", | ||
"priority": 3, | ||
"multiline": 0, | ||
}, | ||
] | ||
tmpl = Template(format="A4", elements=elements, title="Sample Invoice") | ||
|
@@ -124,7 +149,8 @@ def test_template_nominal_hardcoded(tmp_path): | |
|
||
|
||
def test_template_nominal_csv(tmp_path): | ||
"""Taken from docs/Templates.md""" | ||
"""Same data as in docs/Templates.md | ||
The numeric_text tests for a regression.""" | ||
tmpl = Template(format="A4", title="Sample Invoice") | ||
tmpl.parse_csv(HERE / "mycsvfile.csv", delimiter=";") | ||
tmpl.add_page() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe consider putting a different error message as s is "not empty" here, not "not numeric". (unless i am missing something?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I phrased it in this way to inform the user about what's supposed to go there. "Not empty" doesn't give that information.
The documentation could also be more specific about this, but fleshing that out is a project for another time...