photoshop-python-api icon indicating copy to clipboard operation
photoshop-python-api copied to clipboard

refactor(ActionDescriptor,ActionList,ActionReference): improve type communication

Open TsXor opened this issue 3 years ago • 10 comments

There is a problem in the current AM part of this api: When you use "get" functions, what you get is AM thing, but not an AM thing as well. It is an AM thing: you can use those "put" and "get" functions documented in Adobe's js scripting reference It is not an AM thing: It is just a COM bind object, using 'getType' will return number. So I made this modification to unify.

Also, it seems that the functions written for the AM classes are not nessesary because it seems that COM bindings have already done that.

TsXor avatar Aug 21 '22 13:08 TsXor

@TsXor Thank you for your contribution

You can use the following command line to format codes.

setting up the dev environment

pip install poetry
poetry install

format code

poetry run black photoshop
poetry run isort photoshop

commit code

git add photoshop/api/action_descriptor.py
git add photoshop/api/action_list.py
git add photoshop/api/action_list.py
git add photoshop/api/action_reference.py

https://github.com/commitizen-tools/commitizen

cz commit 

loonghao avatar Aug 22 '22 16:08 loonghao

@TsXor Thank you for your contribution

You can use the following command line to format codes.

setting up the dev environment

pip install poetry
poetry install

format code

poetry run black photoshop
poetry run isort photoshop

commit code

git add photoshop/api/action_descriptor.py
git add photoshop/api/action_list.py
git add photoshop/api/action_list.py
git add photoshop/api/action_reference.py

https://github.com/commitizen-tools/commitizen

cz commit 

Thanks, in fact I just test the code locally and manually sync them to github in browser. I'll try these. OK, now it's ready.

TsXor avatar Aug 22 '22 16:08 TsXor

I thought I should patch app.executeAction() and app.executeActionGet(), but it just works fine! Maybe that's the magic of duck typing.

TsXor avatar Aug 26 '22 16:08 TsXor

@loonghao Is there an api to find out which version of photoshop is running?

TsXor avatar Aug 28 '22 06:08 TsXor

@TsXor

Three things need to be addressed before merging this MR

  • Change the name of the files:

    photoshop/api/_AMtypebinder.py >> photoshop/api/_action_manager_typebinder.py
    
    photoshop/api/_AMunify.py >> photoshop/api/_action_manager_unify.py
    

    reference: https://google.github.io/styleguide/pyguide.html#316-naming

  • We need to add some examples for the current new functions

    like these: https://github.com/loonghao/photoshop-python-api/tree/main/examples

  • this commit tag should be feat because you added some new functions image

Thanks for taking the time to contribute the code.

loonghao avatar Aug 29 '22 14:08 loonghao

* We need to add some examples for the current new functions
  like these:
  https://github.com/loonghao/photoshop-python-api/tree/main/examples

* this commit tag should be `feat` because you added some new functions
  ![image](https://user-images.githubusercontent.com/13111745/187229820-24a66b80-a45c-4698-a1a9-ab9f44d48c21.png)

Thanks for taking the time to contribute the code.

I think these 2 things are not needed, becauses it does not change anything when using this api. It just makes sure ActionDescriptor always have ActionDescriptor as its type (the same for ActionList, ActionReference). Files for typebinder and unify have a _ before their name to show that they are "internal" -- no one should use them unless they know what they are doing.

These 2 pictures may show why to make typebinder and unify: typebinder (search engine told me that it should be circular import instead of circuit importing) (at first, I redirect all getattribute, and expicitly define a list that should not be redirected, after some searching on getattribute and getattr, the idea of unify occurred to me)

TsXor avatar Aug 29 '22 15:08 TsXor

@loonghao I think you should know about the implementation of unify well, because _core.py uses the same thing:

    def __getattribute__(self, item):
        try:
            return super().__getattribute__(item)
        except AttributeError:
            return getattr(self.app, item)

Well, that equals to:

    def __getattr__(self, item):
        try:
            return getattr(self.app, item)
        except AttributeError:
            raise AttributeError

So I have now found out that unify is not needed. _core is ~~almost~~ exactly the same thing as unify. Just ~~monkey patching the app attribute~~ set the parent parameter is enough.

TsXor avatar Aug 31 '22 14:08 TsXor

So I also found that lots of the code in this repo is to fix the drawback of _core, exactly the same as unify. We can't exactly find which attribute is overwritten in subclass and which attribute is inherited from object class. I think this repo would greatly shrink if we fix this problem. Or... Well... Those functions exist for type hinting? venn

TsXor avatar Aug 31 '22 14:08 TsXor

@TsXor COM itself is property dynamic binding, but some functions are inconsistent with the description in the JS documentation, so we made some patches, Then the re-implementation of these functions is not only for patching but also for the convenience of code completion and improving ease of use.

loonghao avatar Sep 01 '22 02:09 loonghao

也就是说其实在Session里用了AM代码的例子全是扯淡🤣

Session的ActionDescriptor, ActionList, ActionReference全是一个对象,这就是为什么用了Session的例子里这仨类都不实例化。 那为啥能正常工作呢?因为正如我一个issue中所描述,AM对象有个特性,子对象的更改都不会同步给父对象,一个AM对象嵌到其他AM对象里就像是copy了再存一样,这也就意味着你一直在Session里用一个对象还真有可能正常工作。好家伙,连上了,全连上了!🤣负负得正了属于是

但是也有几率翻车:你所有AM对象都用的一个,就像用同一个烧瓶装完试剂A,然后直接往里倒试剂B,迟早会有那么几次发生化学反应。况且这样导致了在Session模式和非Session模式中行为不一致,因此我强烈建议改了它。

本来这段我也想用英语说的,但是我是真的没绷住🤣🤣🤣

突然想起来在python里有这样一个特性:

class MyClass:
    def __init__(self, mylist=[]):
        self.mylist = mylist

这样写的话,每个没给mylist参数的实例的mylist都会是同一个列表 好家伙,搁这学的🤣🤣🤣(这是开玩笑)

TsXor avatar Sep 08 '22 16:09 TsXor