Skip to content

Commit

Permalink
Merge pull request #426 from NaturalHistoryMuseum/feature/425-warnings
Browse files Browse the repository at this point in the history
[#425] Warnings
  • Loading branch information
quicklizard99 authored Jan 9, 2017
2 parents bb5f7cc + 8fb1f4e commit 4053be0
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 55 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ This is an overview of major changes. Refer to the git repository for a full log

Version 0.1.35
-------------
- #425 Warnings
- #420 zbar decoder on Mac OS X
- #418 Latest gouda and pylibdmtx
- #416 User testing / Python 3 - error message when opening document on 32-bit Windows
Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ for module in cv2 numpy libdmtx scipy sklearn zbar; do
done

echo Tests
nosetests --with-coverage --cover-html --cover-inclusive --cover-erase --cover-tests --cover-package=inselect inselect
PYTHONWARNINGS=module nosetests --with-coverage --cover-html --cover-inclusive --cover-erase --cover-tests --cover-package=inselect inselect

echo Wheel build
./setup.py bdist_wheel --universal
Expand Down
73 changes: 38 additions & 35 deletions inselect/lib/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,43 +220,46 @@ def load(cls, path):
path = Path(path)

# Sniff the first few bytes - file must look like a json document
if not re.match(b'^{[ (\n)|(\r\n)]*"', path.open('rb').read(20)):
with path.open('rb') as infile:
if not re.match(b'^{[ (\n)|(\r\n)]*"', infile.read(20)):
raise InselectError('Not an inselect document')

with path.open(encoding='utf8') as infile:
doc = json.load(infile)

v = doc.get('inselect version')

if not v:
raise InselectError('Not an inselect document')
elif v not in cls.FILE_VERSIONS:
raise InselectError('Unsupported version [{0}]'.format(v))
else:
doc = json.load(path.open(encoding='utf8'))
v = doc.get('inselect version')

if not v:
raise InselectError('Not an inselect document')
elif v not in cls.FILE_VERSIONS:
raise InselectError('Unsupported version [{0}]'.format(v))
else:
if 1 == v:
# Version 1 contained just three illustrative fields -
# convert these to Darwin Core fields
for item in doc['items']:
fields = item['fields']
if fields.get('Taxonomic group'):
fields['scientificName'] = fields.pop('Taxonomic group')
if fields.get('Location'):
fields['otherCatalogNumbers'] = fields.pop('Location')
if fields.get('Specimen number'):
fields['catalogNumber'] = fields.pop('Specimen number')
item['fields'] = fields

scanned = path.with_suffix(doc['scanned extension'])

properties = doc.get('properties', {})

# Parse datetimes
for dt in {'Saved on', 'Created on'}.intersection(properties.keys()):
properties[dt] = cls._parse_datetime(properties[dt])

msg = 'Loaded [{0}] items from [{1}]'
debug_print(msg.format(len(doc['items']), path))

return cls(scanned_path=scanned, items=doc['items'],
properties=properties)
if 1 == v:
# Version 1 contained just three illustrative fields -
# convert these to Darwin Core fields
for item in doc['items']:
fields = item['fields']
if fields.get('Taxonomic group'):
fields['scientificName'] = fields.pop('Taxonomic group')
if fields.get('Location'):
fields['otherCatalogNumbers'] = fields.pop('Location')
if fields.get('Specimen number'):
fields['catalogNumber'] = fields.pop('Specimen number')
item['fields'] = fields

scanned = path.with_suffix(doc['scanned extension'])

properties = doc.get('properties', {})

# Parse datetimes
for dt in {'Saved on', 'Created on'}.intersection(properties.keys()):
properties[dt] = cls._parse_datetime(properties[dt])

msg = 'Loaded [{0}] items from [{1}]'
debug_print(msg.format(len(doc['items']), path))

return cls(scanned_path=scanned, items=doc['items'],
properties=properties)

def save(self):
"Saves to self.document_path"
Expand Down
8 changes: 6 additions & 2 deletions inselect/lib/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,12 @@ def parse_in_choices(choices, value):
else:
return value

# Populate dict {name: parse function}.
# Populate dict {name: parse function}. See comment alongside the first
# declaration of the PARSERS global, towards the top of this file.
PARSERS = inspect.getmembers(sys.modules[__name__], inspect.isfunction)
PARSERS = filter(lambda v: re.match(r'^parse_.+$', v[0]), PARSERS)
PARSERS = filter(lambda v: ['value'] == inspect.getargspec(v[1]).args, PARSERS)
PARSERS = filter(
lambda v: ['value'] == list(inspect.signature(v[1]).parameters.keys()),
PARSERS
)
PARSERS = dict(PARSERS)
11 changes: 6 additions & 5 deletions inselect/tests/gui/test_export_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def _test_csv(self):
csv = doc.document_path.with_suffix('.csv')

# Check CSV contents
with csv.open('rb') as f:
res = unicodecsv.DictReader(f, encoding='utf-8')
with csv.open('rb') as infile:
res = unicodecsv.DictReader(infile, encoding='utf-8')
for index, item, row in zip(count(), doc.items, res):
expected = item['fields']
expected.update({
Expand Down Expand Up @@ -73,7 +73,7 @@ def test_export_overwite_existing_csv(self, mock_information, mock_question):
TESTDATA / 'shapes.png') as tempdir:

# Create a CSV file to force the GUI to prompt for over-write
(tempdir / 'shapes.csv').open('w')
(tempdir / 'shapes.csv').touch()

# Load document and export CSV file
w.open_document(path=tempdir / 'shapes.inselect')
Expand Down Expand Up @@ -102,14 +102,15 @@ def test_export_do_not_overwite_existing_csv(self, mock_information, mock_questi
TESTDATA / 'shapes.png') as tempdir:

# Create a CSV file to force the GUI to prompt for over-write
(tempdir / 'shapes.csv').open('w')
(tempdir / 'shapes.csv').touch()

# Load document and export CSV file
w.open_document(tempdir / 'shapes.inselect')
w.export_csv(user_template=DWC)

# File should not have been altered
self.assertEqual('', (tempdir / 'shapes.csv').open().read())
with (tempdir / 'shapes.csv').open() as infile:
self.assertEqual('', infile.read())

# User should not have been told about the export
self.assertFalse(mock_information.called)
Expand Down
4 changes: 2 additions & 2 deletions inselect/tests/gui/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_reveal_path_windows(self, mock_subprocess):
"User reveals a path"
with temp_directory_with_files() as tempdir:
path = tempdir / 'xyz'
path.open('w')
path.touch()
reveal_path(path)
expected = "explorer.exe /select,{0}".format(path.resolve())
mock_subprocess.assert_called_once_with(expected)
Expand All @@ -42,7 +42,7 @@ def test_reveal_path_os_x(self, mock_subprocess):
"User reveals a path"
with temp_directory_with_files() as tempdir:
path = tempdir / 'xyz'
path.open('w')
path.touch()
reveal_path(path)
expected = [
'/usr/bin/osascript',
Expand Down
11 changes: 6 additions & 5 deletions inselect/tests/lib/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,22 @@ def test_load_images(self):
source = TESTDATA / 'shapes.inselect'
with temp_directory_with_files(TESTDATA / 'shapes.inselect') as tempdir:
doc_temp = tempdir / 'shapes.inselect'
doc_temp.open('w').write(source.open().read())
with doc_temp.open('w') as outfile, source.open() as infile:
outfile.write(infile.read())

# Document load with neither scanned image file nor thumbnail
self.assertRaises(InselectError, InselectDocument.load, doc_temp)

# Document load with thumbnail but no scanned image file
thumbnail_temp = tempdir / 'shapes_thumbnail.jpg'
thumbnail_temp.open('w') # File only needs to exist
thumbnail_temp.touch() # File only needs to exist
doc = InselectDocument.load(doc_temp)
self.assertFalse(doc.scanned.available)
self.assertTrue(doc.thumbnail.available)

# Document load with both scanned and thumbnail files
scanned_temp = tempdir / 'shapes.png'
scanned_temp.open('w') # File only needs to exist
scanned_temp.touch() # File only needs to exist
actual = InselectDocument.load(doc_temp)
self.assertEqual(InselectDocument.load(source).items, actual.items)
self.assertTrue(actual.scanned.available)
Expand Down Expand Up @@ -278,13 +279,13 @@ def test_thumbnail_path_of_scanned(self):
def test_path_is_thumbnail_file(self):
with temp_directory_with_files() as tempdir:
thumbnail = tempdir / 'xx_thumbnail.jpg'
thumbnail.open('w') # File only needs to exist
thumbnail.touch() # File only needs to exist

# Thumbnail file exists but there is no corresponding .inselect doc
self.assertFalse(InselectDocument.path_is_thumbnail_file(thumbnail))

doc = tempdir / 'xx.inselect'
doc.open('w') # File only needs to exist
doc.touch() # File only needs to exist

# Thumbnail file and corresponding .inselect file both exist
self.assertTrue(InselectDocument.path_is_thumbnail_file(thumbnail))
Expand Down
4 changes: 2 additions & 2 deletions inselect/tests/lib/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ def test_rmtree_readonly(self):
d = tempfile.mkdtemp()
path = Path(d)
try:
(path / 'a file').open('w')
(path / 'a file').touch()
(path / 'a directory').mkdir()
(path / 'a directory' / 'another file').open('w')
(path / 'a directory' / 'another file').touch()
make_readonly(path)
self.assertTrue(path.is_dir())
finally:
Expand Down
6 changes: 3 additions & 3 deletions inselect/tests/scripts/test_ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def test_images_suffixes_re(self):
self.assertRegex('x.TIFF', re)
self.assertRegex('x.Tiff', re)

self.assertNotRegexpMatches('x.jpgx', re)
self.assertNotRegexpMatches('x.jpg ', re)
self.assertNotRegexpMatches('x.txt', re)
self.assertNotRegex('x.jpgx', re)
self.assertNotRegex('x.jpg ', re)
self.assertNotRegex('x.txt', re)


class TestIngest(unittest.TestCase):
Expand Down

0 comments on commit 4053be0

Please sign in to comment.