Skip to content

Commit

Permalink
fix(action_manager): do monkey patch to support py37
Browse files Browse the repository at this point in the history
Signed-off-by: TsXor <[email protected]>
  • Loading branch information
TsXor committed Sep 7, 2022
1 parent 9263ef9 commit 50c6ff3
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 6 deletions.
29 changes: 27 additions & 2 deletions photoshop/api/action_manager/_main_types/action_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -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
26 changes: 26 additions & 0 deletions photoshop/api/action_manager/_main_types/action_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 16 additions & 2 deletions photoshop/api/action_manager/js_converter/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])),
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down

0 comments on commit 50c6ff3

Please sign in to comment.