Skip to content

Commit

Permalink
fix(action_manager): hop
Browse files Browse the repository at this point in the history
Signed-off-by: TsXor <[email protected]>
  • Loading branch information
TsXor committed Sep 8, 2022
1 parent 3a9cd2b commit 3267d20
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 64 deletions.
21 changes: 4 additions & 17 deletions photoshop/api/action_manager/_main_types/action_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ def load(cls, adict: dict, namespace: dict): # pass globals() for namespace
continue
# py37 compat
try:
exec(
"""val = v if (dtype := parsetype(v)) == "others" else namespace[dtype].load(v)"""
)
exec("""val = v if (dtype := parsetype(v)) == "others" else namespace[dtype].load(v)""")
except SyntaxError:
val = v if parsetype(v) == "others" else namespace[parsetype(v)].load(v)
new.uput(k, val)
Expand Down Expand Up @@ -61,26 +59,15 @@ def dump(self) -> dict:
ddict = {"_classID": self.classID}
# py37 compat
try:
exec(
"""ext = {key: (value.dump() if hasattr(value := self.uget(key), "dump") else value) for key in self}"""
)
exec(r'ext = {key: (value.dump() if hasattr(value := self.uget(key), "dump") else value) for key in self}')
except SyntaxError:
ext = {
key: (
self.uget(key).dump()
if hasattr(self.uget(key), "dump")
else self.uget(key)
)
for key in self
}
ext = {key: (self.uget(key).dump() if hasattr(self.uget(key), "dump") else self.uget(key)) for key in self}
ddict.update(ext)
return ddict

def _unpacker(self) -> tuple:
value = self
if self.classID is None:
raise RuntimeError(
"Do not use old methods and new methods mixedly."
)
raise RuntimeError("Do not use old methods and new methods mixedly.")
clsid = str2id(self.classID)
return (clsid, value)
14 changes: 5 additions & 9 deletions photoshop/api/action_manager/_main_types/action_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ def load(cls, alist: list, namespace: dict): # pass globals() for namespace
for v in alist:
# py37 compat
try:
exec(
"""val = v if (dtype := parsetype(v)) == "others" else namespace[dtype].load(v)"""
)
exec("""val = v if (dtype := parsetype(v)) == "others" else namespace[dtype].load(v)""")
except SyntaxError:
val = v if parsetype(v) == "others" else namespace[parsetype(v)].load(v)
new.uput(val)
Expand All @@ -48,9 +46,9 @@ def uput(self, val: Any):
# py37 compat
try:
exec(
'''assert True if (dtype := self.dtype) is None ''' +
'''else dtype == typestr, ''' +
'''"ActionList can only hold things of the same type"'''
"""assert ("""
+ """True if (dtype := self.dtype) is None else dtype == typestr, """
+ '''), "ActionList can only hold things of the same type"'''
)
except SyntaxError:
assert (
Expand All @@ -68,7 +66,5 @@ def __iter__(self) -> ActionList_Iterator:
def dump(self) -> list:
"""Convert an ActionList to a python object."""
# This is a list comprehension.
dlist = [
(elem.dump() if hasattr(elem, "dump") else elem) for elem in self
]
dlist = [(elem.dump() if hasattr(elem, "dump") else elem) for elem in self]
return dlist
51 changes: 13 additions & 38 deletions photoshop/api/action_manager/js_converter/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,15 @@ def unhead(string):
"ActionList": lambda x: parselist(x),
}
str2refgetpacker = {
"default": lambda x: ReferenceKey(
unhead(x["DesiredClass"]), unhead(x["Value"])
),
"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"])
), # noqa
),
"Identifier": lambda x: ReferenceKey(
unhead(x["DesiredClass"]), Identifier + int(x["Value"])
),
"Index": lambda x: ReferenceKey(
unhead(x["DesiredClass"]), Index + int(x["Value"])
),
"Offset": lambda x: ReferenceKey(
unhead(x["DesiredClass"]), Offset + int(x["Value"])
),
"Property": lambda x: ReferenceKey(
unhead(x["DesiredClass"]), toid(x["Value"])
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"])),
"Offset": lambda x: ReferenceKey(unhead(x["DesiredClass"]), Offset + int(x["Value"])),
"Property": lambda x: ReferenceKey(unhead(x["DesiredClass"]), toid(x["Value"])),
}


Expand All @@ -79,19 +67,14 @@ def parsedict(tdict):
tdict["_classID"] = None
else:
tdict["_classID"] = unhead(tdict["_classID"])
pdict = {
unhead(k): (str2getpacker[v["type"]](v) if type(v) == dict else v)
for k, v in tdict.items()
}
pdict = {unhead(k): (str2getpacker[v["type"]](v) if type(v) == dict else v) for k, v in tdict.items()}
del pdict["type"]
return pdict


def parselist(tdict):
d2l = [tdict[str(i)] for i in range(tdict["len"])]
plist = [
(str2getpacker[e["type"]](e) if type(e) == dict else e) for e in d2l
]
plist = [(str2getpacker[e["type"]](e) if type(e) == dict else e) for e in d2l]
return plist


Expand All @@ -101,17 +84,13 @@ def parseref(tdict):
# py37 compat
try:
exec(
"""ext = [(str2refgetpacker[val["type"]](e) """ +
"""if type(val := e["Value"]) == dict """ +
"""else str2refgetpacker["default"](e)) for e in d2l]"""
"""ext = [(str2refgetpacker[val["type"]](e) """
+ """if type(val := e["Value"]) == dict """
+ """else str2refgetpacker["default"](e)) for e in d2l]"""
)
except SyntaxError:
ext = [
(
str2refgetpacker[e["Value"]["type"]](e)
if type(e["Value"]) == dict
else str2refgetpacker["default"](e)
)
(str2refgetpacker[e["Value"]["type"]](e) if type(e["Value"]) == dict else str2refgetpacker["default"](e))
for e in d2l
]
plist.extend(ext)
Expand All @@ -120,11 +99,7 @@ def parseref(tdict):

def json2obj(jsont):
obj_init = json.loads(jsont)
obj_desc = (
parsedict(obj_init["ActionDescriptor"])
if "ActionDescriptor" in obj_init
else None
)
obj_desc = parsedict(obj_init["ActionDescriptor"]) if "ActionDescriptor" in obj_init else None
obj_operation = unhead(obj_init["Operation"])
obj_option = obj_init["Option"]
return (obj_operation, obj_desc, obj_option)
Expand Down

0 comments on commit 3267d20

Please sign in to comment.