Skip to content

Commit f648c6b

Browse files
committed
fix(action_manager): do monkey patch to support py37
It seems that hound still think a line should only have 80 chars though you set it to 120. Signed-off-by: TsXor <[email protected]>
1 parent 4461d91 commit f648c6b

25 files changed

+166
-56
lines changed

examples/convert_smartobject_to_layer.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Convert Smart object to artLayer."""
22

3-
# Import builtin modules
43
# Import built-in modules
54
from textwrap import dedent
65

examples/emboss_action.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"_classID": "contentLayer",
2727
"type": {
2828
"_classID": "solidColorLayer",
29-
"color": {"_classID": "RGBColor", "red": index, "grain": index, "blue": index},
29+
"color": {"_classID": "RGBColor", "red": index, "grain": index, "blue": index}, # noqa
3030
},
3131
},
3232
}

examples/import_image_as_layer.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010

1111
with Session(action="new_document") as ps:
12-
import_dict = {"_classID": None, "null": pathlib.Path("your/image/path")} # replace it with your own path here
12+
# replace it with your own path here
13+
import_dict = {"_classID": None, "null": pathlib.Path("your/image/path")}
1314
import_desc = ps.ActionDescriptor.load(import_dict)
14-
ps.app.executeAction(am.str2id("Plc "), import_desc) # `Plc` need one space in here.
15+
ps.app.executeAction(am.str2id("Plc "), import_desc)
16+
# length of charID should always be 4, if not, pad with spaces

examples/replace_images.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Replace the image of the current active layer with a new image."""
22

3-
# Import builtin modules
43
# Import built-in modules
54
import pathlib
65

examples/session_smart_sharpen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
def SmartSharpen(inAmount, inRadius, inNoise):
2323
ss_dict = {
2424
"_classID": None,
25-
"presetKindType": am.Enumerated(type="presetKindType", value="presetKindCustom"),
25+
"presetKindType": am.Enumerated(type="presetKindType", value="presetKindCustom"), # noqa
2626
"amount": am.UnitDouble(unit="radius", double=inAmount),
2727
"radius": am.UnitDouble(unit="pixelsUnit", double=inRadius),
2828
"noiseReduction": am.UnitDouble(unit="percentUnit", double=inNoise),

examples/smart_sharpen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
def SmartSharpen(inAmount, inRadius, inNoise):
2929
ss_dict = {
3030
"_classID": None,
31-
"presetKindType": am.Enumerated(type="presetKindType", value="presetKindCustom"),
31+
"presetKindType": am.Enumerated(type="presetKindType", value="presetKindCustom"), # noqa
3232
"amount": am.UnitDouble(unit="radius", double=inAmount),
3333
"radius": am.UnitDouble(unit="pixelsUnit", double=inRadius),
3434
"noiseReduction": am.UnitDouble(unit="percentUnit", double=inNoise),

photoshop/api/action_manager/__init__.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
from .desc_value_types import *
2-
from .jprint import *
1+
from .desc_value_types import Enumerated
2+
from .desc_value_types import TypeID
3+
from .desc_value_types import UnitDouble
4+
from .jprint import jformat
5+
from .jprint import jprint
36
from .js_converter import dump as dumpjs
4-
from .ref_form_types import *
5-
from .utils import *
7+
from .ref_form_types import Identifier
8+
from .ref_form_types import Index
9+
from .ref_form_types import Offset
10+
from .ref_form_types import ReferenceKey
11+
from .utils import id2str
12+
from .utils import str2id
613

714

815
__all__ = [ # noqa: F405

photoshop/api/action_manager/_main_types/_type_mapper.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
Handles almost all type mappings. (Some else are in ReferenceKey.)
33
This module is INTERNAL. You should not import functions from it."""
44

5-
from ..desc_value_types import *
6-
from ..ref_form_types import *
7-
from ..utils import *
5+
from ..desc_value_types import Enumerated
6+
from ..desc_value_types import TypeID
7+
from ..desc_value_types import UnitDouble
8+
from ..utils import id2str
89

910

1011
__all__ = ["unpack", "pack", "parsetype"]
@@ -34,7 +35,7 @@ def unpack(val):
3435
typestr = pytype2str[vtype]
3536
try:
3637
args = val._unpacker()
37-
except:
38+
except BaseException:
3839
args = (val,)
3940
return (typestr, args)
4041

photoshop/api/action_manager/_main_types/action_descriptor.py

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
# Import built-in modules
22
from abc import ABC
33
from abc import abstractclassmethod
4+
import sys
45
from typing import Any
56

6-
from ..utils import *
7-
from ._type_mapper import *
7+
from ..utils import str2id
8+
from ._type_mapper import pack
9+
from ._type_mapper import parsetype
10+
from ._type_mapper import unpack
811
from .action_descriptor_iterator import ActionDescriptor_Iterator
912

1013

11-
class ActionDescriptor:
14+
class ActionDescriptor(ABC):
1215
"""A vessel for my extra utils.
1316
You should not use, and cannot initialize it
1417
because it is an abstract class."""
@@ -20,7 +23,7 @@ def load(cls, adict: dict, namespace: dict): # pass globals() for namespace
2023
for k, v in adict.items():
2124
if k == "_classID":
2225
continue
23-
v = v if (dtype := parsetype(v)) == "others" else namespace[dtype].load(v)
26+
v = v if (dtype := parsetype(v)) == "others" else namespace[dtype].load(v) # noqa
2427
new.uput(k, v)
2528
return new
2629

@@ -51,7 +54,9 @@ def dump(self) -> dict:
5154
"""Convert an ActionDescriptor to a python object."""
5255
# This is a dict comprehension.
5356
ddict = {"_classID": self.classID}
54-
ddict.update({key: (value.dump() if hasattr(value := self.uget(key), "dump") else value) for key in self})
57+
ddict.update(
58+
{key: (value.dump() if hasattr(value := self.uget(key), "dump") else value) for key in self} # noqa
59+
)
5560
return ddict
5661

5762
def _unpacker(self) -> tuple:
@@ -60,3 +65,32 @@ def _unpacker(self) -> tuple:
6065
raise RuntimeError("Do not use old methods and new methods mixedly.")
6166
clsid = str2id(self.classID)
6267
return (clsid, value)
68+
69+
70+
# Monkey patching for py37
71+
72+
73+
if sys.version.split("(")[0].split(".")[1] <= 7:
74+
75+
@abstractclassmethod
76+
def load(cls, adict: dict, namespace: dict): # pass globals() for namespace
77+
clsid = adict["_classID"] if "_classID" in adict else None
78+
new = cls(classID=clsid)
79+
for k, v in adict.items():
80+
if k == "_classID":
81+
continue
82+
v = v if parsetype(v) == "others" else namespace[parsetype(v)].load(v)
83+
new.uput(k, v)
84+
return new
85+
86+
def dump(self) -> dict:
87+
"""Convert an ActionDescriptor to a python object."""
88+
# This is a dict comprehension.
89+
ddict = {"_classID": self.classID}
90+
ddict.update(
91+
{key: (self.uget(key).dump() if hasattr(self.uget(key), "dump") else self.uget(key)) for key in self}
92+
)
93+
return ddict
94+
95+
ActionDescriptor.load = load
96+
ActionDescriptor.dump = dump

photoshop/api/action_manager/_main_types/action_descriptor_iterator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
from ..utils import *
1+
from ..utils import id2str
22

33

44
class ActionDescriptor_Iterator:
55
"""An iterator. You don't need to initialize it manually."""
66

7-
def __init__(self, psobj: "ActionDescriptor"):
7+
def __init__(self, psobj):
88
self.curobj = psobj
99
self.n = -1
1010

1111
def __next__(self) -> str:
1212
self.n += 1
1313
try:
1414
keyid = self.curobj.getKey(self.n)
15-
except:
15+
except BaseException:
1616
raise StopIteration
1717
keystr = id2str(keyid)
1818
return keystr

0 commit comments

Comments
 (0)