Skip to content

Commit

Permalink
some improvements to oslc query parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
barny committed Apr 19, 2024
1 parent 33a6659 commit 10670e6
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 485 deletions.
18 changes: 17 additions & 1 deletion elmclient/_ccm.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@

logger = logging.getLogger(__name__)

# generate a compact stacktrace of function-line-file because it's often
# helpful to know how a function was called
import inspect
def callers():
caller_list = []
# get the stacktrace and do a couple of f_back-s to remove the call to this function and to the _log_request()/_log_response() function
frame = inspect.currentframe().f_back.f_back
while frame.f_back:
caller_list.append(
'{2}:{1}:{0}()'.format(frame.f_code.co_name, frame.f_lineno, frame.f_code.co_filename.split("\\")[-1]))
frame = frame.f_back
callers = ' <= '.join(caller_list)
return callers

#################################################################################################

class _CCMProject(_project._Project):
Expand All @@ -49,7 +63,6 @@ def _load_types(self,force=False):

pbar.close()

return
self.typesystem_loaded = True
return None

Expand Down Expand Up @@ -237,6 +250,9 @@ def is_type_uri(self, uri):
return True
return False

def resolve_uri_to_name(self, uri, trytouseasid=False):
return self.__super__.resolve_uri_to_name(self, uri, trytouseasid=True)

#################################################################################################

@utils.mixinomatic
Expand Down
4 changes: 2 additions & 2 deletions elmclient/_gcm.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ def _hook_beforequery(querydetails):
if 'orderBy' in querydetails:
del querydetails['orderBy']
# make sure dcterms and oslc not in prefix
if 'dcterms=' in querydetails.get('oslc.prefix',"") or 'oslc' in querydetails.get('oslc.prefix',"") or 'rdf' in querydetails.get('oslc.prefix',""):
if 'dcterms=' in querydetails.get('oslc.prefix',"") or 'oslc' in querydetails.get('oslc.prefix',"") or 'rdf' in querydetails.get('oslc.prefix',"") or 'rdfs' in querydetails.get('oslc.prefix',""):
oldprefix = querydetails['oslc.prefix']
prefixes = oldprefix.split(",")
newprefixes = [p for p in prefixes if not p.startswith("dcterms=") and not p.startswith("oslc=") and not p.startswith("rdf=") and not p.startswith("oslc_config=")]
newprefixes = [p for p in prefixes if not p.startswith("dcterms=") and not p.startswith("oslc=") and not p.startswith("rdf=") and not p.startswith("oslc_config=") and not p.startswith("rdfs=")]
querydetails['oslc.prefix'] = ",".join(newprefixes)
newprefix = querydetails['oslc.prefix']
return querydetails
Expand Down
12 changes: 10 additions & 2 deletions elmclient/_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def resolve_enum_name_to_uri(self, name, propertyuri=None, exception_if_not_foun
return result

# for OSLC query, given a type URI, return its name
def resolve_uri_to_name(self, uri):
def resolve_uri_to_name(self, uri, trytouseasid=False):
logger.debug( f"rutn {uri}" )
if not uri:
result = None
Expand All @@ -312,7 +312,7 @@ def resolve_uri_to_name(self, uri):
logger.debug( f"NOT Changed {uri} to {uri1}" )
# use the transformed URI
uri = uri1
if not uri.startswith( "http://" ) and not uri.startswith( "https://" ):
if not trytouseasid and not uri.startswith( "http://" ) and not uri.startswith( "https://" ):
# not a URI so return it unmodified
return uri
if uri.startswith( self.reluri() ) and not self.is_known_uri(uri):
Expand All @@ -331,6 +331,14 @@ def resolve_uri_to_name(self, uri):
logger.debug( f"iku2" )
logger.info( f"{result=}" )
# self.register_name(result,uri)
elif trytouseasid:
# assume it's an id
result = self.enums.get(uri)
if not result:
split = uri.rsplit("/",1)[-1]

result = self.enums.get(uri.rsplit("/",1)[-1])
burp
else:
result = self.get_uri_name(uri)
if result is None:
Expand Down
1 change: 1 addition & 0 deletions elmclient/_queryparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
| uriofuser
| uriofmodule
| uriofconfig
| dottedname
valueidentifier : ( ( URI_REF_ESC | NAME | "'" SPACYNAME "'" ) ":" )? NAME
| "'" SPACYNAME "'"
Expand Down
9 changes: 8 additions & 1 deletion elmclient/_typesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ def normalise_uri( self, uri, exception_if_name=False ):
result = rdfxml.tag_to_uri( uri )
logger.info( f"tag_to_uri {uri=} {result=}" )
else:
raise Exception( f"Expecting a uri but this doesn't look like a URI {uri}" )
if exception_if_name:
raise Exception( f"Expecting a uri but this doesn't look like a URI {uri}" )
print( f"Warning: Expecting a uri but this doesn't look like a URI {uri} - assuming it's a name" )
result = uri
return result

def is_known_shape_uri(self,shape_uri ):
Expand Down Expand Up @@ -262,6 +265,9 @@ def register_enum( self, enum_name, enum_uri, property_uri, *, id=None ):
enum_uri = self.normalise_uri( enum_uri )
property_uri = self.normalise_uri( property_uri )
self.enums[enum_uri] = {'name': enum_name, 'id':id, 'property': property_uri}
if id:
self.enums[id] = {'name': enum_name, 'id':id, 'property': property_uri}

self.properties[property_uri]['enums'].append(enum_uri)
self.loaded = True

Expand All @@ -287,6 +293,7 @@ def get_enum_id( self, enum_name, property_uri ):
for enum_uri in self.properties[property_uri]['enums']:
if self.enums[enum_uri]['name']==enum_name:
result = self.enums[enum_uri]['id'] or enum_uri
result = enum_uri
break
logger.info( f"get_enum_id {enum_name=} {property_uri=} {result=}" )
return result
Expand Down
Loading

0 comments on commit 10670e6

Please sign in to comment.