fix bytes extraction + give created objects also a name when None is provided (same as links)
This commit is contained in:
parent
07edcc0a8e
commit
ced3edbd08
3 changed files with 14 additions and 8 deletions
16
api/od.py
16
api/od.py
|
|
@ -10,7 +10,8 @@ from uuid import UUID
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from util.timer import Timer
|
from util.timer import Timer
|
||||||
|
|
||||||
NEXT_ID = 0
|
NEXT_LINK_ID = 0
|
||||||
|
NEXT_OBJ_ID = 0
|
||||||
|
|
||||||
# Models map names to elements
|
# Models map names to elements
|
||||||
# This builds the inverse mapping, so we can quickly lookup the name of an element
|
# This builds the inverse mapping, so we can quickly lookup the name of an element
|
||||||
|
|
@ -247,7 +248,7 @@ class ODAPI:
|
||||||
raise Exception("Unimplemented type "+value)
|
raise Exception("Unimplemented type "+value)
|
||||||
|
|
||||||
def create_link(self, link_name: Optional[str], assoc_name: str, src: UUID, tgt: UUID):
|
def create_link(self, link_name: Optional[str], assoc_name: str, src: UUID, tgt: UUID):
|
||||||
global NEXT_ID
|
global NEXT_LINK_ID
|
||||||
types = self.bottom.read_outgoing_elements(self.mm, assoc_name)
|
types = self.bottom.read_outgoing_elements(self.mm, assoc_name)
|
||||||
if len(types) == 0:
|
if len(types) == 0:
|
||||||
raise Exception(f"No such association: '{assoc_name}'")
|
raise Exception(f"No such association: '{assoc_name}'")
|
||||||
|
|
@ -255,13 +256,18 @@ class ODAPI:
|
||||||
raise Exception(f"More than one association exists with name '{assoc_name}' - this means the MM is invalid.")
|
raise Exception(f"More than one association exists with name '{assoc_name}' - this means the MM is invalid.")
|
||||||
typ = types[0]
|
typ = types[0]
|
||||||
if link_name == None:
|
if link_name == None:
|
||||||
link_name = f"__{assoc_name}{NEXT_ID}"
|
link_name = f"__{assoc_name}{NEXT_LINK_ID}"
|
||||||
NEXT_ID += 1
|
NEXT_LINK_ID += 1
|
||||||
link_id = self.od._create_link(link_name, typ, src, tgt)
|
link_id = self.od._create_link(link_name, typ, src, tgt)
|
||||||
self.__recompute_mappings()
|
self.__recompute_mappings()
|
||||||
|
|
||||||
return link_id
|
return link_id
|
||||||
|
|
||||||
def create_object(self, object_name: Optional[str], class_name: str):
|
def create_object(self, object_name: Optional[str], class_name: str):
|
||||||
|
global NEXT_OBJ_ID
|
||||||
|
if object_name == None:
|
||||||
|
object_name = f"__{class_name}{NEXT_OBJ_ID}"
|
||||||
|
NEXT_OBJ_ID += 1
|
||||||
obj = self.od.create_object(object_name, class_name)
|
obj = self.od.create_object(object_name, class_name)
|
||||||
self.__recompute_mappings()
|
self.__recompute_mappings()
|
||||||
return obj
|
return obj
|
||||||
|
|
@ -286,7 +292,7 @@ def bind_api_readonly(odapi):
|
||||||
'get_type_name': odapi.get_type_name,
|
'get_type_name': odapi.get_type_name,
|
||||||
'get_outgoing': odapi.get_outgoing,
|
'get_outgoing': odapi.get_outgoing,
|
||||||
'get_incoming': odapi.get_incoming,
|
'get_incoming': odapi.get_incoming,
|
||||||
'has_slot': odapi.has_slot,
|
'has_slot': odapi.has_slot
|
||||||
}
|
}
|
||||||
return funcs
|
return funcs
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -93,8 +93,8 @@ def parse_od(state,
|
||||||
return (_Code(str(token[1:-1])), token.line) # strip the ``
|
return (_Code(str(token[1:-1])), token.line) # strip the ``
|
||||||
|
|
||||||
def BYTES(self, token):
|
def BYTES(self, token):
|
||||||
# return (bytes(token[2:-1], "utf-8"), token.line) # Strip b"" or b''
|
# Strip b"" or b'', and make \\ back to \ (happens when reading the file as a string)
|
||||||
return (bytes(token[2:-1], "utf-8"), token.line) # Strip b"" or b''
|
return (token[2:-1].encode().decode('unicode_escape').encode('raw_unicode_escape'), token.line) # Strip b"" or b''
|
||||||
|
|
||||||
def INDENTED_CODE(self, token):
|
def INDENTED_CODE(self, token):
|
||||||
skip = 4 # strip the ``` and the following newline character
|
skip = 4 # strip the ``` and the following newline character
|
||||||
|
|
|
||||||
|
|
@ -148,7 +148,7 @@ class OD:
|
||||||
actioncode_t.create(value)
|
actioncode_t.create(value)
|
||||||
return self.create_model_ref(name, "ActionCode", actioncode_node)
|
return self.create_model_ref(name, "ActionCode", actioncode_node)
|
||||||
|
|
||||||
def create_bytes_value(self, name: str, value: str):
|
def create_bytes_value(self, name: str, value: bytes):
|
||||||
from services.primitives.bytes_type import Bytes
|
from services.primitives.bytes_type import Bytes
|
||||||
bytes_node = self.bottom.create_node()
|
bytes_node = self.bottom.create_node()
|
||||||
bytes_t = Bytes(bytes_node, self.bottom.state)
|
bytes_t = Bytes(bytes_node, self.bottom.state)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue