Skip to content

Commit

Permalink
Merge branch 'rename-concat-padded'
Browse files Browse the repository at this point in the history
Conflicts:
	CHANGELOG.md
  • Loading branch information
kba committed Jun 18, 2018
2 parents adf1d9e + 409cf1d commit 18ed488
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Fixed:

* CLI: validate of ocrd-tool.json didn't parse JSON, #115

Changed:

* `mets_file_id` in utils is now `concat_padded` and more flexible

## [0.3.0] - 2018-06-18

Added
Expand Down
15 changes: 12 additions & 3 deletions ocrd/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import subprocess
import logging
import re
import sys

logging.basicConfig(level=logging.DEBUG)
# logging.getLogger('ocrd.resolver').setLevel(logging.INFO)
Expand Down Expand Up @@ -81,12 +82,20 @@ def xmllint_format(xml):
output, _ = proc.communicate(xml)
return output

# TODO better name
def mets_file_id(grp, n):
def is_string(val):
return isinstance(val, (str, unicode)) if sys.version_info < (3, 0) else isinstance(val, str)

def concat_padded(base, *args):
"""
Concatenate string and zero-padded 4 digit number
"""
return "%s_%04i" % (grp, n + 1)
ret = base
for n in args:
if is_string(n):
ret = "%s_%s" % (ret, n)
else:
ret = "%s_%04i" % (ret, n + 1)
return ret

def safe_filename(url):
ret = re.sub('[^A-Za-z0-9]+', '.', url)
Expand Down
10 changes: 10 additions & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from test.base import TestCase, main
from ocrd.utils import (
points_from_xywh,
concat_padded,
points_from_x0y0x1y1,
xywh_from_points,
polygon_from_points
Expand Down Expand Up @@ -33,5 +34,14 @@ def test_polygon_from_points(self):
polygon_from_points('100,100 200,100 200,200 100,200'),
[[100, 100], [200, 100], [200, 200], [100, 200]])

def test_concat_padded(self):
self.assertEqual(concat_padded('x', 0), 'x_0001')
self.assertEqual(concat_padded('x', 0, 1, 2), 'x_0001_0002_0003')
self.assertEqual(concat_padded('x', 0, '1', 2), 'x_0001_1_0003')

def test_is_string(self):
self.assertTrue('x')
self.assertTrue(u'x')

if __name__ == '__main__':
main()

0 comments on commit 18ed488

Please sign in to comment.