Skip to content

Commit 94b0b18

Browse files
committed
Added new pagesizes module, based on reportlab, with some tests
1 parent 058f414 commit 94b0b18

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

domdf_python_tools/pagesizes.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,14 @@ def _sequence_convert(seq, to):
314314

315315

316316
def parse_measurement(measurement):
317-
match = re.findall(r"\d*\.?\d+", measurement)
317+
match = re.findall(r"(\d*\.?\d+) *([A-Za-z]*)", measurement)[0]
318+
print(match)
319+
print(len(match))
318320
if len(match) < 2:
319321
raise ValueError("Unable to parse measurement")
320322
else:
321-
val, unit, *_ = match
323+
val = float(match[0])
324+
unit = match[1]
322325
if unit == "mm":
323326
return val*mm
324327
elif unit == "cm":
@@ -328,6 +331,8 @@ def parse_measurement(measurement):
328331
return val
329332
elif unit == "inch":
330333
return val*inch
334+
elif unit == "in":
335+
return val*inch
331336
elif unit == "pc":
332337
return val*pc
333338
elif unit == "dd":

tests/test_pagesizes.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
test_pagesizes
4+
~~~~~~~~~~~~~~~
5+
6+
Test functions in pagesizes.py
7+
8+
"""
9+
10+
import types
11+
import pathlib
12+
import decimal
13+
14+
import pytest
15+
16+
from domdf_python_tools.utils import str2tuple, tuple2str, chunks, list2str, list2string, bdict, pyversion
17+
from domdf_python_tools.pagesizes import *
18+
19+
20+
def test_orientation():
21+
assert is_portrait(A4)
22+
assert is_portrait(portrait(A4))
23+
assert is_portrait(portrait(landscape(A4)))
24+
assert is_landscape(landscape(A4))
25+
assert is_landscape((10, 5))
26+
assert landscape((10, 5)) == (10, 5)
27+
assert portrait((5, 10)) == (5, 10)
28+
29+
30+
def test_parse_measurement():
31+
assert parse_measurement("12mm") == 12*mm
32+
assert parse_measurement("12 mm") == 12*mm
33+
assert parse_measurement("12.34 mm") == 12.34*mm
34+
assert parse_measurement("5inch") == 5*inch
35+
assert parse_measurement("5in") == 5*inch

0 commit comments

Comments
 (0)