Skip to content

Commit

Permalink
update docs, validate parameters explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Wexler" committed Dec 10, 2015
1 parent f4c3a51 commit a1c5376
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 29 deletions.
25 changes: 25 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,31 @@ to the conversions implemented in `rios.conversion`_

See the `home page source`_ for more details.

Installation
============

rios.converter is a rex.web application which needs no database.

::

$ virtualenv --system-site-packages rios.converter
$ cd rios.converter
$ . bin/activate
$ hg clone ssh://[email protected]/prometheus/rios.converter
$ pip install -e rios.converter

Create a simple **rex.yaml**. For example::

$ cat >rex.yaml <<!
project: rios.converter
uwsgi:
uwsgi-socket: localhost:5839
!

Start the server::

$ rex start

.. _rios.conversion: https://bitbucket.org/prometheus/rios.conversion/overview
.. _home page source: https://bitbucket.org/prometheus/rios.converter/src/tip/static/templates/home.rst

57 changes: 34 additions & 23 deletions src/rios/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ class ConvertToRios(Command):
access = 'anybody'
parameters = [
Parameter('system', StrVal('(qualtrics)|(redcap)'), ),
Parameter('format', StrVal('(yaml)|(json)'), default='yaml'),
Parameter('instrument_title', StrVal('.+')),
Parameter('instrument_id', StrVal('[a-z][a-z0-9_]*')),
Parameter('format', StrVal('(yaml)|(json)')),
Parameter('instrument_title', StrVal('.*')),
Parameter('instrument_id', StrVal('([a-z][a-z0-9_]*)?')),
Parameter('instrument_version', StrVal('(\d+\.\d+)?')),
Parameter('localization', StrVal('.*'), default='en'),
Parameter('outname', StrVal('.+'), default='rios'),
Parameter('localization', StrVal('.*')),
Parameter('outname', StrVal('.*')),
Parameter('infile', AnyVal()),
]

Expand All @@ -87,12 +87,25 @@ def render(self, req,
tempfile.tempdir = self.settings.temp_dir
temp_dir = tempfile.mkdtemp()
outfile_prefix = os.path.join(temp_dir, outname)
errors = []
if not hasattr(infile, 'file'):
errors.append('Input file is required.')
if not outname:
errors.append('Output filename prefix is required.')
if not instrument_version:
instrument_version = '1.0'
args = [
'--id', instrument_id,
'--infile', '-', # stdin
'--outfile-prefix', outfile_prefix,
'--instrument-version', instrument_version,
'--title', instrument_title, ]
'--instrument-version', instrument_version, ]
if system == 'redcap':
if not instrument_id:
errors.append('Instrument ID is required.')
if not instrument_title:
errors.append('Instrument Title is required.')
args.extend([
'--id', instrument_id,
'--title', instrument_title, ])
if localization:
args.extend(['--localization', localization])
if format:
Expand All @@ -101,6 +114,12 @@ def render(self, req,
format = 'yaml'
error_filename = outfile_prefix + '.stderr'

if errors:
shutil.rmtree(temp_dir)
return Response(json={
"status": 400,
"errors": errors
})
with open(error_filename, 'wb') as stderr:
sys.stdin = infile.file
# I can't explain why I am
Expand Down Expand Up @@ -150,12 +169,12 @@ class ConvertFromRios(Command):
access = 'anybody'
parameters = [
Parameter('system', StrVal('(qualtrics)|(redcap)'), ),
Parameter('format', StrVal('(yaml)|(json)'), default='yaml'),
Parameter('localization', StrVal('.*'), default='en'),
Parameter('format', StrVal('(yaml)|(json)')),
Parameter('localization', StrVal('.*')),
Parameter('instrument_file', AnyVal()),
Parameter('form_file', AnyVal()),
Parameter('calculationset_file', AnyVal()),
Parameter('outname', StrVal('.+')), ]
Parameter('outname', StrVal('.*')), ]

converter_class = {
'qualtrics': QualtricsFromRios,
Expand All @@ -182,6 +201,10 @@ def render(self, req,
self.temp_dir = tempfile.mkdtemp()
outfile = os.path.join(self.temp_dir, outname)
errors = []
if not outname:
errors.append('Output filename prefix is required.')
if not format:
format = 'yaml'
args = [
'--verbose',
'--format', format,
Expand Down Expand Up @@ -303,15 +326,3 @@ def render(self, req):
response.body,
writer_name='html')
return Response(html_output)


class HelloCmd(Command):

path = '/hello'
access = 'anybody'
parameters = [
Parameter('name', StrVal('[A-Za-z]+'), default='World'),
]

def render(self, req, name):
return Response("Hello, %s!" % name, content_type='text/plain')
3 changes: 1 addition & 2 deletions static/settings.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
access: {}
#temp_dir: ./temp
temp_dir: /home/pwexler/1127/rios.converter/temp
temp_dir: rios.converter/temp
9 changes: 9 additions & 0 deletions static/templates/from_rios.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ <h2>Convert from RIOS</h2>
<INPUT type="text" name="localization"><BR>
</P>
<P>
The converter determines the input format from each input file extension:
<b>.yaml</b> or <b>.yml</b> for YAML, and
<b>.json</b> for JSON.
Otherwise it uses the following:<br>
<LABEL for="format">Default input format: </LABEL>
<INPUT type="radio" name="format" value="json"> JSON
<INPUT type="radio" checked name="format" value="yaml"> YAML<BR>
</P>
<P>
<INPUT type="submit" value="Send"> <INPUT type="reset">
</P>
</FORM>
Expand Down
12 changes: 9 additions & 3 deletions static/templates/to_rios.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,19 @@ <h2>Convert to RIOS</h2>
<INPUT type="radio" checked name="format" value="yaml"> YAML<BR>
</P>
<P>
<LABEL for="instrument_version">
Output Instrument version (default is <b>1.0</b>): </LABEL>
<INPUT type="text" name="instrument_version"><BR>
</P>
<P>
<hr>
Additional fields required for converting from REDCap:
</P>
<P>
<LABEL for="instrument_title">Output Instrument title: </LABEL>
<INPUT type="text" name="instrument_title"><BR>
<LABEL for="instrument_id">Output Instrument ID: </LABEL>
<INPUT type="text" name="instrument_id"><BR>
<LABEL for="instrument_version">
Output Instrument version: </LABEL>
<INPUT type="text" name="instrument_version"><BR>
</P>
<P>
<INPUT type="submit" value="Send"> <INPUT type="reset">
Expand Down
2 changes: 1 addition & 1 deletion temp/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ This file exists so hg will include **temp** in the repository.
**temp** is the directory the server uses for temporary files.
It is in the repository so that the server does not have to create it dynamically.

- ``static/settings.yaml`` sets temp_dir to **./temp**
- ``static/settings.yaml`` sets temp_dir to **rios.conversion/temp**
- ``.hgignore`` is configured to ignore files in **temp**

0 comments on commit a1c5376

Please sign in to comment.