Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making flags dynamic #15

Merged
merged 3 commits into from
Jan 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ install:
- python setup.py install
script:
- python setup.py test
- python prezi2to3.py tests/input_data/manifest-basic.json /dev/null
- python prezi2to3.py tests/input_data/manifest-basic.json --output /dev/null
after_success:
- coverage run --source=iiif_prezi_upgrader setup.py test
- coveralls
32 changes: 25 additions & 7 deletions prezi2to3.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,46 @@
# encoding: utf-8
"""IIIF Convertor Service"""

import argparse
import json
import os
import sys

from iiif_prezi_upgrader import Upgrader
from iiif_prezi_upgrader.prezi_upgrader import FLAGS

if __name__ == "__main__":
if len(sys.argv) != 2 and len(sys.argv) != 3:
print ('Usage:\n\tpython prezi2to3.py [file_path or url to manifest] [optional output file name]')
sys.exit(0)
#if len(sys.argv) != 2 and len(sys.argv) != 3:
# print ('Usage:\n\tpython prezi2to3.py [file_path or url to manifest] [optional output file name]')
# sys.exit(0)

manifest = sys.argv[1]
parser = argparse.ArgumentParser(description=__doc__.strip(),
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('manifest', metavar='manifest', type=str, help='file_path or url to manifest')
parser.add_argument('--output', metavar='filename', type=str, help='output file name')
for key in FLAGS:
name=key
description=FLAGS[key]['description']
default=FLAGS[key]['default']
type=type(default)
if FLAGS[key]['default'] is not str:
type=bool
parser.add_argument('--%s' % name, default=default, type=type, help=description)


args = parser.parse_args()

manifest = args.manifest
# default flags currently but if flags are assessible then could convert them to use --ext_ok=true using ArgumentParser
upgrader = Upgrader(flags={"ext_ok": False, "deref_links": False}) # create an upgrader
upgrader = Upgrader(flags=vars(args)) # create an upgrader
if 'http' in manifest: # should catch https as well
v3 = upgrader.process_uri(manifest)
else:
v3 = upgrader.process_cached(manifest)

if len(sys.argv) == 3:
if args.output:
# output to filename
with open(sys.argv[2], 'w') as outfile:
with open(args.output, 'w') as outfile:
json.dump(v3, outfile, indent=2)
else:
print(json.dumps(v3, indent=2))
48 changes: 20 additions & 28 deletions index.html → templates/index.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

.tooltip .tooltiptext {
visibility: hidden;
width: 150%;
width: 500px;
color: black;
background: #eaf6fc;
text-align: center;
Expand All @@ -50,7 +50,7 @@
position: absolute;
z-index: 1;
top: -5px;
left: 105%;
left: 125%;
}

.tooltip:hover .tooltiptext {
Expand Down Expand Up @@ -102,36 +102,28 @@
<b>Options</b>:
<div style="border: 1px solid black;margin-left: 10px; margin-bottom: 20px; margin-top:10px; padding: 10px">
<ul>
<% for key in flags:
if isinstance(flags[key]['default'], bool):
type='checkbox'
if flags[key]['default']:
checked = 'checked'
else:
checked = ''
end
value = ''
else:
type = 'input'
value = 'value=%s' % flags[key]['default']
checked = ''
end
%>
<li>
<div class="tooltip">
Move description to Metadata section &nbsp;&nbsp;<input name="desc_2_md" type="checkbox" checked /><br/>
<span class="tooltiptext">The `description` property is not a summary, and hence should be put in as a `metadata` pair. The label generated will be "Description". The default is `True`.</span>
</div>
</li>
<li>
<div class="tooltip">
Move related to Metadata section &nbsp;&nbsp;<input name="related_2_md" type="checkbox" /><br/>
<span class="tooltiptext">The related property is not a homepage, and hence should be put in as a metadata pair. The label generated will be "Related". The default is False (and hence the property will simply be renamed as homepage)</span>
</div>
</li>
<li>
<div class="tooltip">
Copy extensions &nbsp;&nbsp;<input name="ext_ok" type="checkbox" /><br/>
<span class="tooltiptext">Should extensions be copied through to the new version. The default is False.</span>
</div>
</li>
<li>
<div class="tooltip">
Default language (Values are from <a href="http://tools.ietf.org/html/rfc5646">RFC5646</a>)&nbsp;&nbsp;<input name="default_lang" type="input" value="@none" /><br/>
<span class="tooltiptext">The default language to use for language maps. The default is "@none". </span>
</div>
</li>
<li>
<div class="tooltip">
Dereference links without format &nbsp;&nbsp;<input name="deref_links" type="checkbox" checked /><br/>
<span class="tooltiptext">Should links without a format property be dereferenced and the HTTP response inspected for the media type? The default is True.</span>
{{ flags[key]['prop'].replace('_',' ').title() }} &nbsp;&nbsp;<input name="{{ key }}" type="{{ type }}" {{ checked }} {{ value }} /><br/>
<span class="tooltiptext">{{ flags[key]['description'] }}</span>
</div>
</li>
% end
</ul>
</div>

Expand Down
11 changes: 5 additions & 6 deletions twoToThreeUpgraderService.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
from urllib2 import urlopen, HTTPError
from urlparse import urlparse

from bottle import Bottle, abort, request, response, run
from bottle import Bottle, abort, request, response, run, template

#egg_cache = "/path/to/web/egg_cache"
#os.environ['PYTHON_EGG_CACHE'] = egg_cache

from iiif_prezi_upgrader import Upgrader
from iiif_prezi_upgrader.prezi_upgrader import FLAGS

class Service(object):

Expand Down Expand Up @@ -78,7 +79,7 @@ def do_GET_upgrade(self):
return self.return_json({'okay': 0, 'error': 'Invalid JSON for supplied url.', 'url': url, 'json_error': str(error)})

# And look for flags
fs = ['desc_2_md', 'related_2_md', 'ext_ok', 'default_lang', 'deref_links'] # not all flags supported...
fs = FLAGS
flags = {}
for f in fs:
if request.query.get(f, None):
Expand All @@ -92,10 +93,8 @@ def do_GET_upgrade(self):
return self.do_upgrade(data, flags)

def index_route(self):
fh = open(os.path.join(os.path.dirname(__file__),'index.html')) # file() not supported on python 3
data = fh.read()
fh.close()
return data
output = template('templates/index.tpl', flags=FLAGS)
return output

def dispatch_views(self):
self.app.route("/", "GET", self.index_route)
Expand Down