Skip to content

Commit

Permalink
Added the option to group options within a drop-down selection.
Browse files Browse the repository at this point in the history
Added the option to hide selectors that only ever have one element.
Webpage imrpovements tagged in issue #6
  • Loading branch information
Malcolm Brooks authored and Malcolm Brooks committed Dec 6, 2017
1 parent 0edb3cb commit 37446e0
Show file tree
Hide file tree
Showing 28 changed files with 534 additions and 200 deletions.
2 changes: 1 addition & 1 deletion ImageMetaTag/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
'''

# see release_process for details on incrementing the version
__version__ = '0.6.8'
__version__ = '0.6.9'
__documentation__ = 'http://scitools-incubator.github.io/image-meta-tag/build/html/'

# list fo file formats which are valid for saving metadata to:
Expand Down
19 changes: 18 additions & 1 deletion ImageMetaTag/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,28 @@ def open_or_create_db_file(db_file, img_info, restart_db=False, timeout=DEFAULT_

def create_table_for_img_info(dbcr, img_info):
'Creates a database table, in a database cursor, to store for the input img_info'

create_command = 'CREATE TABLE {}(fname TEXT PRIMARY KEY,'.format(SQLITE_IMG_INFO_TABLE)
for key in img_info.keys():
create_command += ' "{}" TEXT,'.format(info_key_to_db_name(key))
create_command = create_command[0:-1] + ')'
dbcr.execute(create_command)
# Can make a rare race condition if multiple processes try to create the file at the same time;
# If that happens, the error is:
# sqlite3.OperationalError: table img_info already exists for file .......
try:
dbcr.execute(create_command)
except sqlite3.OperationalError as OpErr:
if 'table {} already exists'.format(SQLITE_IMG_INFO_TABLE) in OpErr.message:
# another process has just created the table, so sleep(1)
# This is only when a db file is created so isn't called often (and the race condition is rare!)
time.sleep(1)
else:
# everything else needs to be reported and raised immediately:
raise sqlite3.OperationalError(OpErr.message)
except sqlite3.Error as SqErr:
# everything else needs to be reported and raised immediately:
raise sqlite3.Error(SqErr.message)


def open_db_file(db_file, timeout=DEFAULT_DB_TIMEOUT):
'''
Expand Down
11 changes: 5 additions & 6 deletions ImageMetaTag/img_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,19 +846,18 @@ def simple_dict_filter(simple_dict, tests, raise_key_mismatch=False):
# the input tests can also contain a tuple that define how multiple
# images can be grouped together:
has_complex_test = False
if not tests is None:
if tests is not None:
for i_test, test in enumerate(tests.keys()):
if tests[test] is None:
# None here means no filter is applied:
pass
else:
if not test in simple_dict.keys():
msg = 'Specified filter test "%s" not a property of the input dict "%s"' \
%(test, simple_dict)
if test not in simple_dict.keys():
msg = 'Specified filter test "{}" not a property of the input dict "{}"'
if raise_key_mismatch:
raise ValueError(msg)
raise ValueError(msg.format(test, simple_dict))
else:
print msg
print msg.format(test, simple_dict)
return (False, False, False)
if isinstance(tests[test], list):
# simple test, does it meet the normal criteria:
Expand Down
101 changes: 75 additions & 26 deletions ImageMetaTag/javascript/imt_dropdown.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ImageMetaTag dropdown menu scripting - vn0.6.8
// ImageMetaTag dropdown menu scripting - vn0.6.9
// ImageMetaTag is a python package built around a wrapper for savefig in
// matplotlib, which adds metadata tags to supported image file formats.
// See https://github.com/SciTools-incubator/image-meta-tag for details.
Expand Down Expand Up @@ -276,35 +276,84 @@ function update_selectors(options_at_depth, selected_at_depth, start_depth) {
function update_selector(depth, options, selected) {
// updates a selector at a particular depth, with a set of options
// and the selected value as the current selection:
target_div = key_to_selector[depth];

//console.log("updating sel", depth);
//console.log(" at div", target_div);
//console.log(" with options", options);
//console.log(" and selected val", selected);

// set up the text to define the selector:
sel_text = "<select id='select_"+ depth
sel_text += "' onChange='OnSelected("+ depth +")'>\n"
for (var i_opt=0, len=options.length; i_opt < len; i_opt++){
// loop over the options, and write out a line for each one:
for (var j_opt=0, len_j=key_lists[depth].length; j_opt < len_j; j_opt++){
// first, get the index in key_lists[depth] to which i_opt refers
// as not every option is used in every selection:
if (key_lists[depth][j_opt] == options[i_opt]){break};
}
if (options[i_opt] == selected){
sel_text += " <option value=" + j_opt + " selected=selected>"+options[i_opt]+"</option>\n"
} else {
sel_text += " <option value=" + j_opt + ">"+options[i_opt]+"</option>\n"
}
}
// finish it off:
sel_text += "</select>"

// now set the sel div:
var _ = document.getElementById("sel"+depth)
_.innerHTML = sel_text;
//console.log(sel_text)
if (show_singleton_selectors == 1 || key_lists[depth].length > 1){
target_div = key_to_selector[depth];

// set up the text to define the selector:
sel_text = "<select id='select_"+ depth;
sel_text += "' onChange='OnSelected("+ depth +")'>\n";
// find which optgroup is for the current depth:
var optgroup_depth = optgroups[depth];
// the number of optgroups, minus one to account for 'imt_optgroup_order'
n_optgroups = Object.keys(optgroup_depth).length - 1;
if ( n_optgroups > 0 ){
// now loop over the actual optgroups:
for (var i_optgrp=0, n_optgroups; i_optgrp<n_optgroups; i_optgrp++){
// get the name of this optgroup:
var optgrp_name = optgroup_depth['imt_optgroup_order'][i_optgrp];
// so we can get the actual optgroup information:
var optgroup = optgroup_depth[optgrp_name];
// start the optgroup:
sel_text += " <optgroup label='" + optgrp_name + "'>";
// now within the optgroup, add the options:
for (var i_opt=0, n_opt=optgroup.length; i_opt< n_opt; i_opt++){
// loop over the options, and write out a line for each one:
for (var j_opt=0, len_j=key_lists[depth].length; j_opt < len_j; j_opt++){
// first, get the index in key_lists[depth] to which i_opt refers
// as not every option is used in every selection:
if (key_lists[depth][j_opt] == optgroup[i_opt]){break};
};
if (optgroup[i_opt] == selected){
sel_text += " <option value=" + j_opt + " selected=selected>"+optgroup[i_opt]+"</option>\n";
} else {
sel_text += " <option value=" + j_opt + ">"+optgroup[i_opt]+"</option>\n";
};
};
// close the optgroup:
sel_text += " </optgroup>";
};
// now add any residuals:
var resids = optgroup_redisual[depth];
for (var i_opt=0, len=resids.length; i_opt < len; i_opt++){
// loop over the options, and write out a line for each one:
for (var j_opt=0, len_j=key_lists[depth].length; j_opt < len_j; j_opt++){
// first, get the index in key_lists[depth] to which i_opt refers
// as not every option is used in every selection:
if (key_lists[depth][j_opt] == resids[i_opt]){break};
}
if (resids[i_opt] == selected){
sel_text += " <option value=" + j_opt + " selected=selected>"+resids[i_opt]+"</option>\n";
} else {
sel_text += " <option value=" + j_opt + ">"+resids[i_opt]+"</option>\n";
};
};
} else {
for (var i_opt=0, len=options.length; i_opt < len; i_opt++){
// loop over the options, and write out a line for each one:
for (var j_opt=0, len_j=key_lists[depth].length; j_opt < len_j; j_opt++){
// first, get the index in key_lists[depth] to which i_opt refers
// as not every option is used in every selection:
if (key_lists[depth][j_opt] == options[i_opt]){break};
};
if (options[i_opt] == selected){
sel_text += " <option value=" + j_opt + " selected=selected>"+options[i_opt]+"</option>\n";
} else {
sel_text += " <option value=" + j_opt + ">"+options[i_opt]+"</option>\n";
};
};
};
// finish off the selector:
sel_text += "</select>"
// now set the sel div:
var _ = document.getElementById("sel"+depth)
_.innerHTML = sel_text;
//console.log(sel_text)
}; // closes the test on whether this selector is to be displayed
}

function OnSelected(depth){
Expand Down
Loading

0 comments on commit 37446e0

Please sign in to comment.