Skip to content

Commit

Permalink
Merge pull request #88 from SciTools-incubator/merge_db_can_replace
Browse files Browse the repository at this point in the history
Allow the db entries to be replaced
  • Loading branch information
Malcolm Brooks authored Dec 11, 2017
2 parents 0b178ff + bd95b01 commit 6f26271
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 49 deletions.
14 changes: 9 additions & 5 deletions ImageMetaTag/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def db_name_to_info_key(in_str):
return str(in_str).replace('__', ' ')

def write_img_to_dbfile(db_file, img_filename, img_info, add_strict=False,
attempt_replace=False,
timeout=DEFAULT_DB_TIMEOUT):
'''
Writes image metadata to a database.
Expand All @@ -48,6 +49,7 @@ def write_img_to_dbfile(db_file, img_filename, img_info, add_strict=False,
Options:
* add_strict - passed into :func:`ImageMetaTag.db.write_img_to_open_db`
* attempt_replace - passed into :func:`ImageMetaTag.db.write_img_to_open_db`
* timeout - default timeout to try and write to the database.
This is commonly used in :func:`ImageMetaTag.savefig`
Expand All @@ -61,7 +63,8 @@ def write_img_to_dbfile(db_file, img_filename, img_info, add_strict=False,
# open the database:
dbcn, dbcr = open_or_create_db_file(db_file, img_info, timeout=timeout)
# now write:
write_img_to_open_db(dbcr, img_filename, img_info, add_strict=add_strict)
write_img_to_open_db(dbcr, img_filename, img_info,
add_strict=add_strict, attempt_replace=attempt_replace)
# now commit that databasde entry and close:
dbcn.commit()
dbcn.close()
Expand Down Expand Up @@ -140,7 +143,7 @@ def read(db_file, required_tags=None, tag_strings=None,
read_img_info_from_dbfile = read

def merge_db_files(main_db_file, add_db_file, delete_add_db=False,
delete_added_entries=False,
delete_added_entries=False, attempt_replace=False,
db_timeout=DEFAULT_DB_TIMEOUT, db_attempts=DEFAULT_DB_ATTEMPTS):
'''
Merges two ImageMetaTag database files, with the contents of add_db_file added
Expand Down Expand Up @@ -169,7 +172,8 @@ def merge_db_files(main_db_file, add_db_file, delete_add_db=False,
dbcn, dbcr = open_db_file(main_db_file, timeout=db_timeout)
# and add in the new contents:
for add_file, add_info in add_tags.iteritems():
write_img_to_open_db(dbcr, add_file, add_info)
write_img_to_open_db(dbcr, add_file, add_info,
attempt_replace=attempt_replace)
dbcn.commit()
# if we got here, then we're good!
wrote_db = True
Expand Down Expand Up @@ -332,9 +336,9 @@ def write_img_to_open_db(dbcr, filename, img_info, add_strict=False, attempt_rep
except sqlite3.IntegrityError:
if attempt_replace:
# try an INSERT OR REPLACE
add_command.replace('INSERT ', 'INSERT OR REPLACE ')
add_repl_command = add_command.replace('INSERT ', 'INSERT OR REPLACE ')
# if this fails, want it to report it's error message as is, so no 'try':
dbcr.execute(add_command, add_list)
dbcr.execute(add_repl_command, add_list)
else:
# this file is already in the database (as the primary key, so do nothing...)
pass
Expand Down
8 changes: 6 additions & 2 deletions ImageMetaTag/savefig.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def savefig(filename, img_format=None, img_converter=0, do_trim=False, trim_bord
do_thumb=False, img_tags=None, keep_open=False, dpi=None,
logo_file=None, logo_width=40, logo_padding=0, logo_pos=0,
db_file=None, db_timeout=DEFAULT_DB_TIMEOUT, db_attempts=DEFAULT_DB_ATTEMPTS,
db_full_paths=False,
db_replace=False, db_full_paths=False,
verbose=False, ):
'''
A wrapper around matplotlib.pyplot.savefig, to include file size optimisation and
Expand All @@ -54,6 +54,9 @@ def savefig(filename, img_format=None, img_converter=0, do_trim=False, trim_bord
db_full_paths is True.
* db_timeout - change the database timeout (in seconds).
* db_attempts - change the number of attempts to write to the database.
* db_replace - if True, an image's metadata will be replaced in the database if it \
already exists. This can be slow, and the metadata is usually the same so \
the default is db_replace=False.
* dpi - change the image resolution passed into matplotlib.savefig.
* keep_open - by default, this savefig wrapper closes the figure after use, except if \
keep_open is True.
Expand Down Expand Up @@ -151,7 +154,8 @@ def savefig(filename, img_format=None, img_converter=0, do_trim=False, trim_bord
n_tries = 1
while not wrote_db and n_tries <= db_attempts:
try:
db.write_img_to_dbfile(db_file, db_filename, img_tags, timeout=db_timeout)
db.write_img_to_dbfile(db_file, db_filename, img_tags, timeout=db_timeout,
attempt_replace=db_replace)
wrote_db = True
except sqlite3.OperationalError as OpErr:
if 'database is locked' in OpErr.message:
Expand Down
3 changes: 2 additions & 1 deletion ImageMetaTag/webpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ def write_full_page(img_dict, filepath, title, page_filename=None, tab_s_name=No
* compression - default False. If True, then the json data object will be compressed \
using zlib string compression. When read into the browser, we will use \
pako to inflate it (https://github.com/nodeca/pako)
* css - CSS file used to style webpage
* css - Optional CSS file used to style webpage. By default a small amount of css is \
written out in the page header.
Returns a list of files that the the created webpage is dependent upon
'''
Expand Down
Binary file modified docs/build/doctrees/db.doctree
Binary file not shown.
Binary file modified docs/build/doctrees/environment.pickle
Binary file not shown.
Binary file modified docs/build/doctrees/savefig.doctree
Binary file not shown.
Binary file modified docs/build/doctrees/webpage.doctree
Binary file not shown.
Loading

0 comments on commit 6f26271

Please sign in to comment.