-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
style(action_manager): processed with black and isort
Signed-off-by: TsXor <[email protected]>
- Loading branch information
Showing
26 changed files
with
660 additions
and
586 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,21 @@ | ||
from .ref_form_types import * | ||
from .desc_value_types import * | ||
from .utils import * | ||
from .js_converter import dump as dumpjs | ||
from .jprint import * | ||
from .js_converter import dump as dumpjs | ||
from .ref_form_types import * | ||
from .utils import * | ||
|
||
|
||
__all__ = [ # noqa: F405 | ||
'str2id', | ||
'id2str', | ||
'Enumerated', | ||
'TypeID', | ||
'UnitDouble', | ||
'Identifier', | ||
'Index', | ||
'Offset', | ||
'ReferenceKey', | ||
'dumpjs', | ||
'jprint', | ||
'jformat', | ||
] | ||
"str2id", | ||
"id2str", | ||
"Enumerated", | ||
"TypeID", | ||
"UnitDouble", | ||
"Identifier", | ||
"Index", | ||
"Offset", | ||
"ReferenceKey", | ||
"dumpjs", | ||
"jprint", | ||
"jformat", | ||
] |
102 changes: 53 additions & 49 deletions
102
photoshop/api/action_manager/_main_types/_type_mapper.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,72 @@ | ||
'''Maybe the core of this submodule. | ||
"""Maybe the core of this submodule. | ||
Handles almost all type mappings. (Some else are in ReferenceKey.) | ||
This module is INTERNAL. You should not import functions from it.''' | ||
This module is INTERNAL. You should not import functions from it.""" | ||
|
||
from ..desc_value_types import * | ||
from ..ref_form_types import * | ||
from ..utils import * | ||
|
||
__all__ = ['unpack', 'pack', 'parsetype'] | ||
|
||
__all__ = ["unpack", "pack", "parsetype"] | ||
|
||
pytype2str = { | ||
bool:'Boolean', | ||
int:'Integer', | ||
float:'Double', | ||
str:'String', | ||
Enumerated:'Enumerated', | ||
UnitDouble:'UnitDouble', | ||
TypeID:'Class', | ||
'ActionDescriptor':'Object', | ||
'ActionList':'List', | ||
'ActionReference':'Reference', | ||
bool: "Boolean", | ||
int: "Integer", | ||
float: "Double", | ||
str: "String", | ||
Enumerated: "Enumerated", | ||
UnitDouble: "UnitDouble", | ||
TypeID: "Class", | ||
"ActionDescriptor": "Object", | ||
"ActionList": "List", | ||
"ActionReference": "Reference", | ||
} | ||
|
||
str2pytype = { | ||
'Enumerated':Enumerated, | ||
'UnitDouble':UnitDouble, | ||
'Class':TypeID, | ||
"Enumerated": Enumerated, | ||
"UnitDouble": UnitDouble, | ||
"Class": TypeID, | ||
} | ||
|
||
|
||
def unpack(val): | ||
vtype = val.typename if hasattr(val, 'typename') else type(val) | ||
typestr = pytype2str[vtype] | ||
try: | ||
args = val._unpacker() | ||
except: | ||
args = (val,) | ||
return (typestr, args) | ||
vtype = val.typename if hasattr(val, "typename") else type(val) | ||
typestr = pytype2str[vtype] | ||
try: | ||
args = val._unpacker() | ||
except: | ||
args = (val,) | ||
return (typestr, args) | ||
|
||
|
||
def pack(obj, index): # "index" means id of key string or list index. | ||
valtype = obj.getType(index) | ||
typestr = str(valtype)[14:-4] | ||
if typestr == 'Data': | ||
# No plan to support RawType because it seldom runs successfully | ||
# and is seldom used in regular scripting. | ||
return None | ||
if typestr in str2pytype: | ||
pytype = str2pytype[typestr] | ||
val = pytype._packer(obj, index) | ||
elif typestr == 'Object': | ||
val = obj.getObjectValue(index) | ||
val.classID = id2str(obj.getObjectType(index)) | ||
else: | ||
get_func = getattr(obj, 'get'+typestr) | ||
val = get_func(index) | ||
return val | ||
valtype = obj.getType(index) | ||
typestr = str(valtype)[14:-4] | ||
if typestr == "Data": | ||
# No plan to support RawType because it seldom runs successfully | ||
# and is seldom used in regular scripting. | ||
return None | ||
if typestr in str2pytype: | ||
pytype = str2pytype[typestr] | ||
val = pytype._packer(obj, index) | ||
elif typestr == "Object": | ||
val = obj.getObjectValue(index) | ||
val.classID = id2str(obj.getObjectType(index)) | ||
else: | ||
get_func = getattr(obj, "get" + typestr) | ||
val = get_func(index) | ||
return val | ||
|
||
|
||
def parsetype(obj): | ||
if type(obj) == dict: | ||
dtype = 'ActionDescriptor' | ||
elif type(obj) == list: | ||
first = obj[0] if obj else None | ||
if first == '!ref': | ||
dtype = 'ActionReference' | ||
if type(obj) == dict: | ||
dtype = "ActionDescriptor" | ||
elif type(obj) == list: | ||
first = obj[0] if obj else None | ||
if first == "!ref": | ||
dtype = "ActionReference" | ||
else: | ||
dtype = "ActionList" | ||
else: | ||
dtype = 'ActionList' | ||
else: | ||
dtype = 'others' | ||
return dtype | ||
dtype = "others" | ||
return dtype |
122 changes: 58 additions & 64 deletions
122
photoshop/api/action_manager/_main_types/action_descriptor.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,62 @@ | ||
from ._type_mapper import * | ||
from ..utils import * | ||
from .action_descriptor_iterator import ActionDescriptor_Iterator | ||
# Import built-in modules | ||
from abc import ABC | ||
from abc import abstractclassmethod | ||
from typing import Any | ||
from abc import ABC, abstractclassmethod | ||
|
||
class ActionDescriptor: | ||
'''A vessel for my extra utils. | ||
You should not use, and cannot initialize it | ||
because it is an abstract class.''' | ||
|
||
@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 (dtype := parsetype(v)) == 'others' \ | ||
else namespace[dtype].load(v) | ||
new.uput(k,v) | ||
return new | ||
|
||
def uget(self, key: str) -> Any: | ||
'''Get a value of a key in an ActionDescriptor, no matter its type.''' | ||
keyid = str2id(key) | ||
val = pack(self, keyid) | ||
return val | ||
|
||
def uput(self, key: str, val: Any): | ||
'''Put a value of a key into an ActionDescriptor, no matter its type.''' | ||
keyid = str2id(key) | ||
typestr, args = unpack(val) | ||
put_func = getattr(self, 'put'+typestr) | ||
put_func(keyid, *args) | ||
|
||
def __len__(self): | ||
return self.count | ||
|
||
def __iter__(self) -> ActionDescriptor_Iterator: | ||
return ActionDescriptor_Iterator(self) | ||
|
||
def __contains__(self, key): | ||
keys = [key for key in self] | ||
return key in keys | ||
from ..utils import * | ||
from ._type_mapper import * | ||
from .action_descriptor_iterator import ActionDescriptor_Iterator | ||
|
||
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 | ||
}) | ||
return ddict | ||
|
||
def _unpacker(self) -> tuple: | ||
value = self | ||
if self.classID is None: | ||
raise RuntimeError('Do not use old methods and new methods mixedly.') | ||
clsid = str2id(self.classID) | ||
return (clsid, value) | ||
class ActionDescriptor: | ||
"""A vessel for my extra utils. | ||
You should not use, and cannot initialize it | ||
because it is an abstract class.""" | ||
|
||
@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 (dtype := parsetype(v)) == "others" else namespace[dtype].load(v) | ||
new.uput(k, v) | ||
return new | ||
|
||
def uget(self, key: str) -> Any: | ||
"""Get a value of a key in an ActionDescriptor, no matter its type.""" | ||
keyid = str2id(key) | ||
val = pack(self, keyid) | ||
return val | ||
|
||
def uput(self, key: str, val: Any): | ||
"""Put a value of a key into an ActionDescriptor, no matter its type.""" | ||
keyid = str2id(key) | ||
typestr, args = unpack(val) | ||
put_func = getattr(self, "put" + typestr) | ||
put_func(keyid, *args) | ||
|
||
def __len__(self): | ||
return self.count | ||
|
||
def __iter__(self) -> ActionDescriptor_Iterator: | ||
return ActionDescriptor_Iterator(self) | ||
|
||
def __contains__(self, key): | ||
keys = [key for key in self] | ||
return key in keys | ||
|
||
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}) | ||
return ddict | ||
|
||
def _unpacker(self) -> tuple: | ||
value = self | ||
if self.classID is None: | ||
raise RuntimeError("Do not use old methods and new methods mixedly.") | ||
clsid = str2id(self.classID) | ||
return (clsid, value) |
29 changes: 15 additions & 14 deletions
29
photoshop/api/action_manager/_main_types/action_descriptor_iterator.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,21 @@ | ||
from ..utils import * | ||
|
||
|
||
class ActionDescriptor_Iterator: | ||
'''An iterator. You don't need to initialize it manually.''' | ||
"""An iterator. You don't need to initialize it manually.""" | ||
|
||
def __init__(self, psobj: 'ActionDescriptor'): | ||
self.curobj = psobj | ||
self.n = -1 | ||
def __init__(self, psobj: "ActionDescriptor"): | ||
self.curobj = psobj | ||
self.n = -1 | ||
|
||
def __next__(self) -> str: | ||
self.n += 1 | ||
try: | ||
keyid = self.curobj.getKey(self.n) | ||
except: | ||
raise StopIteration | ||
keystr = id2str(keyid) | ||
return keystr | ||
def __next__(self) -> str: | ||
self.n += 1 | ||
try: | ||
keyid = self.curobj.getKey(self.n) | ||
except: | ||
raise StopIteration | ||
keystr = id2str(keyid) | ||
return keystr | ||
|
||
def __repr__(self): | ||
return '<ActionDescriptor_Iterator at index:%d>'%self.n | ||
def __repr__(self): | ||
return "<ActionDescriptor_Iterator at index:%d>" % self.n |
Oops, something went wrong.