diff --git a/parsers/python/pyshexc/parser_impl/generate_shexj.py b/parsers/python/pyshexc/parser_impl/generate_shexj.py index 5b833a2..077b369 100644 --- a/parsers/python/pyshexc/parser_impl/generate_shexj.py +++ b/parsers/python/pyshexc/parser_impl/generate_shexj.py @@ -95,12 +95,11 @@ def do_parse(infilename: str, jsonfilename: Optional[str], rdffilename: Optional """ shexj = parse(FileStream(infilename, encoding="utf-8")) if shexj is not None: + shexj['@context'] = context if context else "http://www.w3.org/ns/shex.jsonld" if jsonfilename: with open(jsonfilename, 'w') as outfile: outfile.write(shexj._as_json_dumps()) if rdffilename: - if context: - shexj['@context'] = context g = Graph().parse(data=shexj._as_json, format="json-ld") g.serialize(open(rdffilename, "wb"), format=rdffmt) return True @@ -159,7 +158,7 @@ def genargs() -> ArgumentParser: parser.add_argument("-nr", "--nordf", help="Do not produce rdf output", action="store_true") parser.add_argument("-j", "--jsonfile", help="Output ShExJ file (Default: {infile}.json)") parser.add_argument("-r", "--rdffile", help="Output ShExR file (Default: {infile}.{fmt suffix})") - parser.add_argument("--context", help="Alternative @context for json to RDF") + parser.add_argument("--context", help="Alternative @context") parser.add_argument("-f", "--format", choices=list(set(x.name for x in rdflib_plugins(None, rdflib_Serializer) if '/' not in str(x.name))), diff --git a/parsers/python/pyshexc/parser_impl/parser_context.py b/parsers/python/pyshexc/parser_impl/parser_context.py index 18c9104..fc4afe0 100644 --- a/parsers/python/pyshexc/parser_impl/parser_context.py +++ b/parsers/python/pyshexc/parser_impl/parser_context.py @@ -65,7 +65,7 @@ def _lookup_prefix(self, prefix: PREFIXstr) -> str: def iriref_to_str(self, ref: ShExDocParser.IRIREF) -> str: """ IRIREF: '<' (~[\u0000-\u0020=<>\"{}|^`\\] | UCHAR)* '>' """ rval = ref.getText()[1:-1].encode('utf-8').decode('unicode-escape') - return rval if ':' in rval else self.base.val + rval + return rval if ':' in rval or not self.base else self.base.val + rval def iriref_to_shexj_iriref(self, ref: ShExDocParser.IRIREF) -> ShExJ.IRIREF: """ IRIREF: '<' (~[\u0000-\u0020=<>\"{}|^`\\] | UCHAR)* '>' diff --git a/parsers/python/setup.py b/parsers/python/setup.py index b7324f2..3032ce7 100644 --- a/parsers/python/setup.py +++ b/parsers/python/setup.py @@ -6,14 +6,18 @@ # typing library was introduced as a core module in version 3.5.0 # NOTE: the antlr4-python3-runtime must be the same version that was used to create the parser library -with open('requirements.txt') as reqs: - requires = [l for l in reqs.readline()] +requires = ['antlr4-python3-runtime>=4.7', + 'jsonasobj>=1.1.2', + 'ShExJSG>=0.1.1', + 'requests>=2.18', + 'rdflib>=4.2.2', + 'rdflib-jsonld>=0.4.0'] if sys.version_info < (3, 5): requires.append("typing") setup( name='PyShExC', - version='0.3.0', + version='0.3.2', packages=['pyshexc.parser', 'pyshexc.parser_impl'], url="http://github.com/shexSpec/grammar/parsers/python", license='Apache 2.0', @@ -22,7 +26,7 @@ description='"PyShExC - a Python ShExC parser', long_description='PyShExC - a ShExC to PyJSG, ShExJ and ShExR parser', install_requires=requires, - test_requires=['dict_compare'], + tests_require=['yadict-compare>=1.1.2'], scripts=['scripts/shexc_to_shexj'], classifiers=[ 'Development Status :: 3 - Alpha', diff --git a/parsers/python/tests/test_issue_2.py b/parsers/python/tests/test_issue_2.py new file mode 100644 index 0000000..844f479 --- /dev/null +++ b/parsers/python/tests/test_issue_2.py @@ -0,0 +1,44 @@ +# Copyright (c) 2018, Mayo Clinic +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# Redistributions of source code must retain the above copyright notice, this +# list of conditions and the following disclaimer. +# +# Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# +# Neither the name of the Mayo Clinic nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, +# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED +# OF THE POSSIBILITY OF SUCH DAMAGE. + +import unittest + +from ShExJSG import ShExJ + +from pyshexc.parser_impl.generate_shexj import parse + + +class TestIssue2(unittest.TestCase): + def test_something(self): + shex_str = ' { []}' + shex = parse(shex_str) + self.assertTrue(isinstance(shex, ShExJ.Schema)) + + +if __name__ == '__main__': + unittest.main()