diff --git a/photoshop/api/action_manager/_main_types/action_descriptor.py b/photoshop/api/action_manager/_main_types/action_descriptor.py index 3208afad..f27d5098 100644 --- a/photoshop/api/action_manager/_main_types/action_descriptor.py +++ b/photoshop/api/action_manager/_main_types/action_descriptor.py @@ -22,7 +22,7 @@ def load(cls, adict: dict, namespace: dict): # pass globals() for namespace for k, v in adict.items(): if k == "_classID": continue - v = v if (dtype := parsetype(v)) == "others" else namespace[dtype].load(v) + v = v if (dtype := parsetype(v)) == "others" else namespace[dtype].load(v) # noqa new.uput(k, v) return new @@ -53,7 +53,7 @@ def dump(self) -> dict: """Convert an ActionDescriptor to a python object.""" # This is a dict comprehension. ddict = {"_classID": self.classID} - ddict.update({key: (value.dump() if hasattr(value := self.uget(key), "dump") else value) for key in self}) + ddict.update({key: (value.dump() if hasattr(value := self.uget(key), "dump") else value) for key in self}) # noqa return ddict def _unpacker(self) -> tuple: @@ -62,3 +62,28 @@ def _unpacker(self) -> tuple: raise RuntimeError("Do not use old methods and new methods mixedly.") clsid = str2id(self.classID) return (clsid, value) + + +# Monkey patching for py37 +import sys + +if sys.version.split('(')[0].split('.')[1] <= 7: + @abstractclassmethod + def load(cls, adict: dict, namespace: dict): # pass globals() for namespace + clsid = adict["_classID"] if "_classID" in adict else None + new = cls(classID=clsid) + for k, v in adict.items(): + if k == "_classID": + continue + v = v if parsetype(v) == "others" else namespace[parsetype(v)].load(v) + new.uput(k, v) + return new + + def dump(self) -> dict: + """Convert an ActionDescriptor to a python object.""" + # This is a dict comprehension. + ddict = {"_classID": self.classID} + ddict.update({key: (self.uget(key).dump() if hasattr(self.uget(key), "dump") else self.uget(key)) for key in self}) + return ddict + ActionDescriptor.load = load + ActionDescriptor.dump = dump \ No newline at end of file diff --git a/photoshop/api/action_manager/_main_types/action_list.py b/photoshop/api/action_manager/_main_types/action_list.py index 601cef73..aa22fa3b 100644 --- a/photoshop/api/action_manager/_main_types/action_list.py +++ b/photoshop/api/action_manager/_main_types/action_list.py @@ -56,3 +56,29 @@ def dump(self) -> list: # This is a list comprehension. dlist = [(elem.dump() if hasattr(elem, "dump") else elem) for elem in self] return dlist + + +# Monkey patching for py37 +import sys + +if sys.version.split('(')[0].split('.')[1] <= 7: + @abstractclassmethod + def load(cls, alist: list, namespace: dict): # pass globals() for namespace + new = cls() + for v in alist: + v = v if (dtype := parsetype(v)) == "others" else namespace[dtype].load(v) + new.uput(v) + return new + + def uput(self, val: Any): + """Put an element into an ActionList, no matter its type.""" + typestr, args = unpack(val) + # ActionList type checking + assert ( + True if (dtype := self.dtype) is None else dtype == typestr + ), "ActionList can only hold things of the same type" + put_func = getattr(self, "put" + typestr) + put_func(*args) + + ActionList.load = load + ActionList.uput = uput diff --git a/photoshop/api/action_manager/_main_types/action_reference.py b/photoshop/api/action_manager/_main_types/action_reference.py index d35bb5ba..d551ba1d 100644 --- a/photoshop/api/action_manager/_main_types/action_reference.py +++ b/photoshop/api/action_manager/_main_types/action_reference.py @@ -25,7 +25,7 @@ def load(cls, alist: list): def uget(self, index: int) -> ReferenceKey: """Get a key in an ActionReference as ReferenceKey, no matter its type.""" target = self - for i in range(index + 1): + for _i in range(index + 1): try: target = target.getContainer() except BaseException: diff --git a/photoshop/api/action_manager/js_converter/convert.py b/photoshop/api/action_manager/js_converter/convert.py index 6f84fa82..e96df83f 100644 --- a/photoshop/api/action_manager/js_converter/convert.py +++ b/photoshop/api/action_manager/js_converter/convert.py @@ -52,7 +52,7 @@ def unhead(string): str2refgetpacker = { "default": lambda x: ReferenceKey(unhead(x["DesiredClass"]), unhead(x["Value"])), "Enumerated": lambda x: ReferenceKey( - unhead(x["DesiredClass"]), Enumerated(unhead(x["Value"]["enumtype"]), unhead(x["Value"]["enumval"])) + unhead(x["DesiredClass"]), Enumerated(unhead(x["Value"]["enumtype"]), unhead(x["Value"]["enumval"])) # noqa ), "Identifier": lambda x: ReferenceKey(unhead(x["DesiredClass"]), Identifier + int(x["Value"])), "Index": lambda x: ReferenceKey(unhead(x["DesiredClass"]), Index + int(x["Value"])), @@ -82,12 +82,26 @@ def parseref(tdict): plist = ["!ref"] plist.extend( [ - (str2refgetpacker[val["type"]](e) if type(val := e["Value"]) == dict else str2refgetpacker["default"](e)) + (str2refgetpacker[val["type"]](e) if type(val := e["Value"]) == dict else str2refgetpacker["default"](e)) # noqa for e in d2l ] ) return plist +# Monkey patching for py37 +import sys + +if sys.version.split('(')[0].split('.')[1] <= 7: + def parseref(tdict): + d2l = [tdict[str(i)] for i in range(tdict["len"])] + plist = ["!ref"] + plist.extend( + [ + (str2refgetpacker[e["Value"]["type"]](e) if type(e["Value"]) == dict else str2refgetpacker["default"](e)) + for e in d2l + ] + ) + return plist def json2obj(jsont): obj_init = json.loads(jsont) diff --git a/photoshop/api/action_manager/ref_form_types/referencekey.py b/photoshop/api/action_manager/ref_form_types/referencekey.py index 20e5333d..21ed10d9 100644 --- a/photoshop/api/action_manager/ref_form_types/referencekey.py +++ b/photoshop/api/action_manager/ref_form_types/referencekey.py @@ -39,7 +39,7 @@ def _packer(cls, obj): if ftype == "Class": v = None elif ftype == "Enumerated": - v = Enumerated(id2str(obj.getEnumeratedType()), id2str(obj.getEnumeratedValue())) + v = Enumerated(id2str(obj.getEnumeratedType()), id2str(obj.getEnumeratedValue())) # noqa elif ftype == "Property": v = TypeID(id2str(obj.getProperty())) elif ftype == "Name":