Skip to content

Commit

Permalink
Generate relative iri when base is not specified.
Browse files Browse the repository at this point in the history
Fixes issue #2 (version 0.3.2).  Also adds a default context to shexj file (0.3.1)
  • Loading branch information
hsolbrig committed Jan 18, 2018
1 parent 7eec912 commit 2c7f39d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
5 changes: 2 additions & 3 deletions parsers/python/pyshexc/parser_impl/generate_shexj.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))),
Expand Down
2 changes: 1 addition & 1 deletion parsers/python/pyshexc/parser_impl/parser_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)* '>'
Expand Down
12 changes: 8 additions & 4 deletions parsers/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down
44 changes: 44 additions & 0 deletions parsers/python/tests/test_issue_2.py
Original file line number Diff line number Diff line change
@@ -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 = '<S1> {<p1> [<o1>]}'
shex = parse(shex_str)
self.assertTrue(isinstance(shex, ShExJ.Schema))


if __name__ == '__main__':
unittest.main()

0 comments on commit 2c7f39d

Please sign in to comment.