Skip to content

Commit

Permalink
move code to utils.copy_picture_with_thumbnail, and reuse it. #225.
Browse files Browse the repository at this point in the history
  • Loading branch information
mfrasca committed Jan 13, 2018
1 parent 4947ce6 commit a155f7a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 29 deletions.
24 changes: 2 additions & 22 deletions bauble/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2043,30 +2043,10 @@ def on_activate_browse_button(self, widget, data=None):
filename = fileChooserDialog.get_filename()
if filename:
## rememberl chosen location for next time
PictureBox.last_folder, basename = os.path.split(filename)
import shutil
## copy file to picture_root_dir (if not yet there).
if not filename.startswith(
prefs.prefs[prefs.picture_root_pref]):
shutil.copy(
filename, prefs.prefs[prefs.picture_root_pref])
## make thumbnail in thumbs subdirectory
from PIL import Image
PictureBox.last_folder, basename = os.path.split(unicode(filename))
logger.debug('new current folder is: %s' % self.last_folder)
full_dest_path = os.path.join(
prefs.prefs[prefs.picture_root_pref], 'thumbs', basename)
try:
im = Image.open(filename)
im.thumbnail((400, 400))
logger.debug('copying %s to %s' % (filename, full_dest_path))
im.save(full_dest_path)
except IOError, e:
logger.warning("can't make thumbnail")
except Exception, e:
logger.warning("unexpected exception making thumbnail: "
"(%s)%s" % (type(e), e))
## get dirname and basename from selected file, memorize
## dirname
utils.copy_picture_with_thumbnail(self.last_folder, basename)
## make sure the category is <picture>
self.set_model_attr('category', u'<picture>')
## store basename in note field and fire callbacks.
Expand Down
20 changes: 13 additions & 7 deletions bauble/plugins/garden/picture_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,12 @@ def do_import(self): # step 2
for row in self.review_rows:
if not row[use_me_col]:
continue
# create or retrieve genus and species
# get unicode strings from row
epgn, epsp = unicode(row[binomial_col] + ' sp').split(' ')[:2]
filename = unicode(row[filename_col])
code_accession, code_plant = unicode(row[accno_col] + '.1').split('.')[:2]

# create or retrieve genus and species
genus = self.session.query(Genus).filter_by(epithet=epgn).one()
try:
species = self.session.query(Species).filter_by(genus=genus, epithet=epsp).one()
Expand All @@ -235,8 +239,7 @@ def do_import(self): # step 2
species = Species(genus=genus, epithet=epsp)
self.session.add(species)
logger.info('created species %s %s' % (epgn, epsp))
# create or retrieve accession (needs species)
code_accession, code_plant = unicode(row[accno_col] + '.1').split('.')[:2]
# create or retrieve accession (needs species)
try:
accession = self.session.query(Accession).filter_by(code=code_accession).one()
except NoResultFound, e:
Expand All @@ -245,7 +248,8 @@ def do_import(self): # step 2
accession = Accession(species=species, code=code_accession, quantity_recvd=1)
self.session.add(accession)
logger.info('created accession %s for species %s %s' % (code_accession, epgn, epsp))
# create or retrieve plant (needs: accession, location)

# create or retrieve plant (needs: accession, location)
try:
plant = self.session.query(Plant).filter_by(accession=accession, code='1').one()
except NoResultFound, e:
Expand All @@ -254,9 +258,11 @@ def do_import(self): # step 2
plant = Plant(accession=accession, quantity=1, location=location, code=code_plant)
self.session.add(plant)
logger.info('created plant %s.%s' % (code_accession, code_plant))
# copy picture file - possibly renaming it
# add picture note
filename = unicode(row[filename_col])

# copy picture file - possibly renaming it
utils.copy_picture_with_thumbnail(self.model.filepath, filename)

# add picture note
try:
note = self.session.query(PlantNote).filter_by(plant=plant, note=filename, category=u'<picture>').one()
except NoResultFound, e:
Expand Down
29 changes: 29 additions & 0 deletions bauble/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,35 @@ def get(self, key, getter, on_hit=lambda x: None):
self.storage[key] = time.time(), value
return value

def copy_picture_with_thumbnail(path, basename=None):
"""copy file from path to picture_root, and make thumbnail, preserving name
"""
import os.path
if basename is None:
filename = path
path, basename = os.path.split(filename)
else:
filename = os.path.join(path, basename)
from bauble import prefs
if not filename.startswith(prefs.prefs[prefs.picture_root_pref]):
import shutil
shutil.copy(filename, prefs.prefs[prefs.picture_root_pref])
## make thumbnail in thumbs subdirectory
from PIL import Image
full_dest_path = os.path.join(prefs.prefs[prefs.picture_root_pref],
'thumbs', basename)
try:
im = Image.open(filename)
im.thumbnail((400, 400))
logger.debug('copying %s to %s' % (filename, full_dest_path))
im.save(full_dest_path)
except IOError, e:
logger.warning("can't make thumbnail")
except Exception, e:
logger.warning("unexpected exception making thumbnail: "
"(%s)%s" % (type(e), e))


class ImageLoader(threading.Thread):
cache = Cache(12) # class-global cached results
Expand Down

0 comments on commit a155f7a

Please sign in to comment.