Jelajahi Sumber

fix bytes extraction + give created objects also a name when None is provided (same as links)

Inte Vleminckx 5 bulan lalu
induk
melakukan
ced3edbd08
3 mengubah file dengan 14 tambahan dan 8 penghapusan
  1. 11 5
      api/od.py
  2. 2 2
      concrete_syntax/textual_od/parser.py
  3. 1 1
      services/od.py

+ 11 - 5
api/od.py

@@ -10,7 +10,8 @@ from uuid import UUID
 from typing import Optional
 from util.timer import Timer
 
-NEXT_ID = 0
+NEXT_LINK_ID = 0
+NEXT_OBJ_ID = 0
 
 # Models map names to elements
 # 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)
 
     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) 
         if len(types) == 0:
             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.")
         typ = types[0]
         if link_name == None:
-            link_name = f"__{assoc_name}{NEXT_ID}"
-            NEXT_ID += 1
+            link_name = f"__{assoc_name}{NEXT_LINK_ID}"
+            NEXT_LINK_ID += 1
         link_id = self.od._create_link(link_name, typ, src, tgt)
         self.__recompute_mappings()
+
         return link_id
 
     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)
         self.__recompute_mappings()
         return obj
@@ -286,7 +292,7 @@ def bind_api_readonly(odapi):
         'get_type_name': odapi.get_type_name,
         'get_outgoing': odapi.get_outgoing,
         'get_incoming': odapi.get_incoming,
-        'has_slot': odapi.has_slot,
+        'has_slot': odapi.has_slot
     }
     return funcs
 

+ 2 - 2
concrete_syntax/textual_od/parser.py

@@ -93,8 +93,8 @@ def parse_od(state,
             return (_Code(str(token[1:-1])), token.line) # strip the ``
 
         def BYTES(self, token):
-            # return (bytes(token[2:-1], "utf-8"), token.line)  # Strip b"" or b''
-            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 (token[2:-1].encode().decode('unicode_escape').encode('raw_unicode_escape'), token.line)  # Strip b"" or b''
 
         def INDENTED_CODE(self, token):
             skip = 4 # strip the ``` and the following newline character

+ 1 - 1
services/od.py

@@ -148,7 +148,7 @@ class OD:
         actioncode_t.create(value)
         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
         bytes_node = self.bottom.create_node()
         bytes_t = Bytes(bytes_node, self.bottom.state)