Skip to content

Commit

Permalink
style(action_manager): processed with black and isort
Browse files Browse the repository at this point in the history
Signed-off-by: TsXor <[email protected]>
  • Loading branch information
TsXor committed Sep 4, 2022
1 parent b37f3ff commit b9a8a0b
Show file tree
Hide file tree
Showing 26 changed files with 660 additions and 586 deletions.
24 changes: 15 additions & 9 deletions photoshop/api/_actionmanager_type_binder.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,25 @@

# Import local modules
from photoshop.api.action_descriptor import ActionDescriptor as AD_proto
from photoshop.api.action_manager._main_types.action_descriptor import ActionDescriptor as AD_utils_proto
from photoshop.api.action_list import ActionList as AL_proto
from photoshop.api.action_manager._main_types.action_list import ActionList as AL_utils_proto
from photoshop.api.action_manager._main_types.action_descriptor import (
ActionDescriptor as AD_utils_proto,
)
from photoshop.api.action_manager._main_types.action_list import (
ActionList as AL_utils_proto,
)
from photoshop.api.action_manager._main_types.action_reference import (
ActionReference as AR_utils_proto,
)
from photoshop.api.action_reference import ActionReference as AR_proto
from photoshop.api.action_manager._main_types.action_reference import ActionReference as AR_utils_proto
from photoshop.api.enumerations import DescValueType
from photoshop.api.enumerations import ReferenceFormType


class ActionDescriptor(AD_proto, AD_utils_proto):
@classmethod
def load(cls, adict: dict) -> 'ActionDescriptor':
'''Convert a python object to an ActionDescriptor'''
def load(cls, adict: dict) -> "ActionDescriptor":
"""Convert a python object to an ActionDescriptor"""
return super().load(adict, globals())

def __init__(self, parent=None, classID=None):
Expand All @@ -45,8 +51,8 @@ def getReference(self, key: int) -> "ActionReference":

class ActionList(AL_proto, AL_utils_proto):
@classmethod
def load(cls, alist: list) -> 'ActionList':
'''Convert a python object to an ActionList'''
def load(cls, alist: list) -> "ActionList":
"""Convert a python object to an ActionList"""
return super().load(alist, globals())

def getType(self, index: int) -> DescValueType:
Expand All @@ -68,8 +74,8 @@ def getReference(self, index: int) -> "ActionReference":

class ActionReference(AR_proto, AR_utils_proto):
@classmethod
def load(cls, adict: dict) -> 'ActionReference':
'''Convert a python object to an ActionReference'''
def load(cls, adict: dict) -> "ActionReference":
"""Convert a python object to an ActionReference"""
return super().load(adict)

def getForm(self) -> ReferenceFormType:
Expand Down
33 changes: 17 additions & 16 deletions photoshop/api/action_manager/__init__.py
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 photoshop/api/action_manager/_main_types/_type_mapper.py
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 photoshop/api/action_manager/_main_types/action_descriptor.py
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)
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
Loading

0 comments on commit b9a8a0b

Please sign in to comment.