Skip to content

Commit

Permalink
Pass files-to-ignore to unit test generator. Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
p2 committed Jan 20, 2015
1 parent 3301935 commit 7ca6bec
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 138 deletions.
2 changes: 1 addition & 1 deletion Python/mappings.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
'Reference',
}

# Which class names are native to the lannguage
# Which class names are native to the language (or can be treated this way)
natives = ['bool', 'int', 'float', 'str', 'dict']

# Which classes are to be expected from JSON decoding
Expand Down
4 changes: 2 additions & 2 deletions fhirspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
r'geneticpedigreefamilyhistory',
r'valueset-shareable-definition',
r'cda-.+\.profile\.json',
r'xds-document\w+\.profile\.json',
r'^xds-',
r'-uslab-',
r'-daf-',
r'-sdc-',
Expand Down Expand Up @@ -161,7 +161,7 @@ def star_expand_types(self):

def parse_unit_tests(self):
controller = fhirunittest.FHIRUnitTestController(self, self.settings)
controller.find_and_parse_tests(self.directory)
controller.find_and_parse_tests(self.directory, skip=skip_because_unsupported)
self.unit_tests = controller.collections


Expand Down
17 changes: 13 additions & 4 deletions fhirunittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-

import io
import re
import sys
import glob
import json
Expand All @@ -21,8 +22,8 @@ def __init__(self, spec, settings):
self.files = None
self.collections = None

def find_and_parse_tests(self, directory):
self.files = FHIRResourceFile.find_all(directory)
def find_and_parse_tests(self, directory, skip=None):
self.files = FHIRResourceFile.find_all(directory, skip=skip)

# create tests
tests = []
Expand Down Expand Up @@ -189,13 +190,21 @@ class FHIRResourceFile(object):
""" A FHIR example resource file.
"""
@classmethod
def find_all(cls, directory):
def find_all(cls, directory, skip=None):
""" Finds all example JSON files in the given directory.
"""
assert os.path.isdir(directory)
all_tests = []
for utest in glob.glob(os.path.join(directory, '*-example*.json')):
all_tests.append(cls(filepath=utest))
use = True
if skip is not None:
basename = os.path.basename(utest)
for pattern in skip:
if re.search(pattern, basename) is not None:
use = False
break
if use:
all_tests.append(cls(filepath=utest))

return all_tests

Expand Down
131 changes: 0 additions & 131 deletions generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,11 @@
import datetime
import logging

from jinja2 import Environment, PackageLoader
from jinja2.filters import environmentfilter

import fhirspec

_cache = 'downloads'


# deprecated, going away when FHIRSpec is done
jinjaenv = Environment(loader=PackageLoader('generate', '.'))


def download(url, path):
""" Download the given URL to the given path.
"""
Expand All @@ -46,130 +39,6 @@ def expand(path, target):
z.extractall(target)


# deprecated, going away when FHIRSpec is done
def parse_DEPRECATED(path):
""" Parse all JSON profile definitions found in the given expanded
directory, create classes for all found profiles, collect all search params
and generate the search param extension.
"""
assert(os.path.exists(path))

# get FHIR version
version = None
with io.open(os.path.join(path, 'version.info'), 'r', encoding='utf-8') as handle:
text = handle.read()
for line in text.split("\n"):
if '=' in line:
(n, v) = line.split('=', 2)
if 'FhirVersion' == n:
version = v

assert(version is not None)
print("-> This is FHIR version {}".format(version))
now = datetime.date.today()
info = {
'version': version.strip() if version else 'X',
'date': now.isoformat(),
'year': now.year
}

# parse profiles
## IMPLEMENTED in FHIRSpec()

# process element factory
## IMPLEMENTED in FHIRSpec()

# process search parameters
process_search(search_params, in_profiles, info)

# detect and process unit tests
## IMPLEMENTED in FHIRSpec()/FHIRUnitTest()


# deprecated, going away when FHIRSpec is done
def process_search(params, in_profiles, info):
""" Processes and renders the FHIR search params extension.
"""
if not write_searchparams:
print("oo> Skipping search parameters")
return

extensions = []
dupes = set()
for param in sorted(params):
(name, orig, typ) = param.split('|')
finalname = reservedmap.get(name, name)
for d in extensions:
if finalname == d['name']:
dupes.add(finalname)

extensions.append({'name': finalname, 'original': orig, 'type': typ})

data = {
'info': info,
'extensions': extensions,
'in_profiles': in_profiles,
'dupes': dupes,
}
render(data, tpl_searchparams_source, tpl_searchparams_target)


# deprecated, going away when FHIRSpec is done
def render(data, template, filepath):
""" Render the given class data using the given Jinja2 template, writing
the output into the file at `filepath`.
"""
assert(os.path.exists(template))
template = jinjaenv.get_template(template)

if not filepath:
raise Exception("No target filepath provided")
dirpath = os.path.dirname(filepath)
if not os.path.isdir(dirpath):
os.makedirs(dirpath)

with io.open(filepath, 'w', encoding='utf-8') as handle:
logging.info('Writing {}'.format(filepath))
rendered = template.render(data)
handle.write(rendered)
# handle.write(rendered.encode('utf-8'))


# deprecated, going away when FHIRSpec is done

# There is a bug in Jinja's wordwrap (inherited from `textwrap`) in that it
# ignores existing linebreaks when applying the wrap:
# https://github.com/mitsuhiko/jinja2/issues/175
# Here's the workaround:
@environmentfilter
def do_wordwrap(environment, s, width=79, break_long_words=True, wrapstring=None):
"""
Return a copy of the string passed to the filter wrapped after
``79`` characters. You can override this default using the first
parameter. If you set the second parameter to `false` Jinja will not
split words apart if they are longer than `width`.
"""
import textwrap
if not wrapstring:
wrapstring = environment.newline_sequence

accumulator = []
# Workaround: pre-split the string
for component in re.split(r"\r?\n", s):
# textwrap will eat empty strings for breakfirst. Therefore we route them around it.
if len(component) is 0:
accumulator.append(component)
continue
accumulator.extend(
textwrap.wrap(component, width=width, expand_tabs=False,
replace_whitespace=False,
break_long_words=break_long_words)
)
return wrapstring.join(accumulator)

jinjaenv.filters['wordwrap'] = do_wordwrap


if '__main__' == __name__:
import settings as _settings
from logger import logger
Expand Down

0 comments on commit 7ca6bec

Please sign in to comment.