Skip to content

Commit 10670e6

Browse files
committed
some improvements to oslc query parsing
1 parent 33a6659 commit 10670e6

File tree

9 files changed

+69
-485
lines changed

9 files changed

+69
-485
lines changed

elmclient/_ccm.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@
2323

2424
logger = logging.getLogger(__name__)
2525

26+
# generate a compact stacktrace of function-line-file because it's often
27+
# helpful to know how a function was called
28+
import inspect
29+
def callers():
30+
caller_list = []
31+
# 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
32+
frame = inspect.currentframe().f_back.f_back
33+
while frame.f_back:
34+
caller_list.append(
35+
'{2}:{1}:{0}()'.format(frame.f_code.co_name, frame.f_lineno, frame.f_code.co_filename.split("\\")[-1]))
36+
frame = frame.f_back
37+
callers = ' <= '.join(caller_list)
38+
return callers
39+
2640
#################################################################################################
2741

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

5064
pbar.close()
5165

52-
return
5366
self.typesystem_loaded = True
5467
return None
5568

@@ -237,6 +250,9 @@ def is_type_uri(self, uri):
237250
return True
238251
return False
239252

253+
def resolve_uri_to_name(self, uri, trytouseasid=False):
254+
return self.__super__.resolve_uri_to_name(self, uri, trytouseasid=True)
255+
240256
#################################################################################################
241257

242258
@utils.mixinomatic

elmclient/_gcm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ def _hook_beforequery(querydetails):
3838
if 'orderBy' in querydetails:
3939
del querydetails['orderBy']
4040
# make sure dcterms and oslc not in prefix
41-
if 'dcterms=' in querydetails.get('oslc.prefix',"") or 'oslc' in querydetails.get('oslc.prefix',"") or 'rdf' in querydetails.get('oslc.prefix',""):
41+
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',""):
4242
oldprefix = querydetails['oslc.prefix']
4343
prefixes = oldprefix.split(",")
44-
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=")]
44+
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=")]
4545
querydetails['oslc.prefix'] = ",".join(newprefixes)
4646
newprefix = querydetails['oslc.prefix']
4747
return querydetails

elmclient/_project.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ def resolve_enum_name_to_uri(self, name, propertyuri=None, exception_if_not_foun
295295
return result
296296

297297
# for OSLC query, given a type URI, return its name
298-
def resolve_uri_to_name(self, uri):
298+
def resolve_uri_to_name(self, uri, trytouseasid=False):
299299
logger.debug( f"rutn {uri}" )
300300
if not uri:
301301
result = None
@@ -312,7 +312,7 @@ def resolve_uri_to_name(self, uri):
312312
logger.debug( f"NOT Changed {uri} to {uri1}" )
313313
# use the transformed URI
314314
uri = uri1
315-
if not uri.startswith( "http://" ) and not uri.startswith( "https://" ):
315+
if not trytouseasid and not uri.startswith( "http://" ) and not uri.startswith( "https://" ):
316316
# not a URI so return it unmodified
317317
return uri
318318
if uri.startswith( self.reluri() ) and not self.is_known_uri(uri):
@@ -331,6 +331,14 @@ def resolve_uri_to_name(self, uri):
331331
logger.debug( f"iku2" )
332332
logger.info( f"{result=}" )
333333
# self.register_name(result,uri)
334+
elif trytouseasid:
335+
# assume it's an id
336+
result = self.enums.get(uri)
337+
if not result:
338+
split = uri.rsplit("/",1)[-1]
339+
340+
result = self.enums.get(uri.rsplit("/",1)[-1])
341+
burp
334342
else:
335343
result = self.get_uri_name(uri)
336344
if result is None:

elmclient/_queryparser.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
| uriofuser
7979
| uriofmodule
8080
| uriofconfig
81+
| dottedname
8182
8283
valueidentifier : ( ( URI_REF_ESC | NAME | "'" SPACYNAME "'" ) ":" )? NAME
8384
| "'" SPACYNAME "'"

elmclient/_typesystem.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ def normalise_uri( self, uri, exception_if_name=False ):
145145
result = rdfxml.tag_to_uri( uri )
146146
logger.info( f"tag_to_uri {uri=} {result=}" )
147147
else:
148-
raise Exception( f"Expecting a uri but this doesn't look like a URI {uri}" )
148+
if exception_if_name:
149+
raise Exception( f"Expecting a uri but this doesn't look like a URI {uri}" )
150+
print( f"Warning: Expecting a uri but this doesn't look like a URI {uri} - assuming it's a name" )
151+
result = uri
149152
return result
150153

151154
def is_known_shape_uri(self,shape_uri ):
@@ -262,6 +265,9 @@ def register_enum( self, enum_name, enum_uri, property_uri, *, id=None ):
262265
enum_uri = self.normalise_uri( enum_uri )
263266
property_uri = self.normalise_uri( property_uri )
264267
self.enums[enum_uri] = {'name': enum_name, 'id':id, 'property': property_uri}
268+
if id:
269+
self.enums[id] = {'name': enum_name, 'id':id, 'property': property_uri}
270+
265271
self.properties[property_uri]['enums'].append(enum_uri)
266272
self.loaded = True
267273

@@ -287,6 +293,7 @@ def get_enum_id( self, enum_name, property_uri ):
287293
for enum_uri in self.properties[property_uri]['enums']:
288294
if self.enums[enum_uri]['name']==enum_name:
289295
result = self.enums[enum_uri]['id'] or enum_uri
296+
result = enum_uri
290297
break
291298
logger.info( f"get_enum_id {enum_name=} {property_uri=} {result=}" )
292299
return result

0 commit comments

Comments
 (0)