Subfolder for Python modules

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
simonvanderveldt
Posts: 62
Joined: Tue Mar 14, 2017 2:11 pm

Re: Subfolder for Python modules

Post by simonvanderveldt »

sgrogan wrote:
simonvanderveldt wrote:If they are to be normal Python models then there shouldn't be dashes/hyphens in their name, see https://stackoverflow.com/questions/761 ... mport-them
@simonvanderveldt, can you post an outline of what the transition would look like at the moment, similar to what looo did previously? Including add-on WB/Modules and macros.
That's a good idea!

What's been done so far:
- Add non-GUI initialization of normal python modules to FreeCADInit
- Add GUI initialization of normal python modules to FreeCADGuiInit.py
- Add new Python package to contain the modules, should be a namespace package so it's easy to extend
- Add example/prototype migration of FreeCAD module to regular Python module, where the module has dependencies on non-migrated FreeCAD modules (Arch)
- Add example/prototype migration of FreeCAD module, where the module has dependencies on a migrated FreeCAD module (Start + Web)

The first two steps are backwards compatible, it's been working fine for me, so I guess I can create a PR to add that to master?
The latter two steps aren't 100% done because the custom FreeCAD filecopy action on make for the .py files in the CMakeListsstill needs to be added. Apart from that both should be fine. I'd like some feedback that it also works on Windows and macOS though.
Also their package and modules names still have caps in them, which ideally shouldn't be the case.

TODO:
- The web module adds some commands that can be added to toolbars which contain imports that are incorrect, these need to be fixed. Might be the same for the other modules
- Migrate all the packages, these can be done one by one, no need to do them all at once
- If we want we can also change the current FreeCAD custom init every FreeCAD Modules must have to the use of the regular __init__.py as mentioned here. Would be nice to know why a custom init module was used in the first place though :)
- Update the docs
- Add a deprecation warning/remove the old module code

P.S. I've just rebased the code on current master
[edit] The code lives here https://github.com/simonvanderveldt/Fre ... ce-modules
Last edited by simonvanderveldt on Mon Mar 27, 2017 6:26 pm, edited 4 times in total.
User avatar
sgrogan
Veteran
Posts: 6499
Joined: Wed Oct 22, 2014 5:02 pm

Re: Subfolder for Python modules

Post by sgrogan »

simonvanderveldt wrote:I'd like some feedback that it also works on Windows
I will test your bench and post a build.
https://github.com/simonvanderveldt/fre ... es-example??

Thanks for the explanation, I almost understand.
"fight the good fight"
simonvanderveldt
Posts: 62
Joined: Tue Mar 14, 2017 2:11 pm

Re: Subfolder for Python modules

Post by simonvanderveldt »

sgrogan wrote:
simonvanderveldt wrote:I'd like some feedback that it also works on Windows
I will test your bench and post a build.
https://github.com/simonvanderveldt/fre ... es-example??

Thanks for the explanation, I almost understand.
No that's a different repo. The code lives here https://github.com/simonvanderveldt/Fre ... ce-modules
Thanks for doing a build and testing!
simonvanderveldt
Posts: 62
Joined: Tue Mar 14, 2017 2:11 pm

Re: Subfolder for Python modules

Post by simonvanderveldt »

Alright, just added a commit to add the namespace packaging, it works as expected :)

Tested with the start workbench, where I removed FreeCAD's Web module.
The output shows the error on startup + I printed the value of freecad.modules.__path__ to stdout as well:

Code: Select all

$ ./bin/FreeCAD --log-file freecad.log                   
FreeCAD 0.17, Libs: 0.17R10642 +4 (Git)
© Juergen Riegel, Werner Mayer, Yorik van Havre 2001-2017
  #####                 ####  ###   ####  
  #                    #      # #   #   # 
  #     ##  #### ####  #     #   #  #   # 
  ####  # # #  # #  #  #     #####  #   # 
  #     #   #### ####  #    #     # #   # 
  #     #   #    #     #    #     # #   #  ##  ##  ##
  #     #   #### ####   ### #     # ####   ##  ##  ##

No module named Web
Traceback (most recent call last):
  File "/home/simon/src/simonvanderveldt/freecad-build/build/Mod/freecad/modules/Start/init_gui.py", line 44, in Initialize
    from . import StartGui

Arch workbench activated

** (FreeCAD:19275): WARNING **: invalid source position for vertical gradient

** (FreeCAD:19275): WARNING **: invalid source position for vertical gradient
['/home/simon/src/simonvanderveldt/freecad-build/build/Mod/freecad/modules']
Now add an external location to PYTHONPATH which contains the Web module:

Code: Select all

$ tree -L 4 modules
modules
└── freecad
    └── modules
        ├── __init__.py
        └── Web
            ├── Gui
            ├── init_gui.py
            ├── __init__.py
            ├── WebGui.so
            ├── Webscripts
            └── Web.so
And it works :)

Code: Select all

$ PYTHONPATH=modules ./bin/FreeCAD --log-file freecad.log
FreeCAD 0.17, Libs: 0.17R10642 +4 (Git)
© Juergen Riegel, Werner Mayer, Yorik van Havre 2001-2017
  #####                 ####  ###   ####  
  #                    #      # #   #   # 
  #     ##  #### ####  #     #   #  #   # 
  ####  # # #  # #  #  #     #####  #   # 
  #     #   #### ####  #    #     # #   # 
  #     #   #    #     #    #     # #   #  ##  ##  ##
  #     #   #### ####   ### #     # ####   ##  ##  ##


** (FreeCAD:16609): WARNING **: invalid source position for vertical gradient

** (FreeCAD:16609): WARNING **: invalid source position for vertical gradient

['/home/simon/src/simonvanderveldt/freecad-build/build/Mod/freecad/modules', '/home/simon/src/simonvanderveldt/freecad-build/build/modules/freecad/modules']
triplus
Veteran
Posts: 9471
Joined: Mon Dec 12, 2011 4:45 pm

Re: Subfolder for Python modules

Post by triplus »

simonvanderveldt wrote:It seems like FreeCAD modules aren't required to have both an init and a GUI init module.
Right now it' pretty crude, but simple, if an Init.py or InitGui.py exists in a modules then they are being executed by the relevant (non-)GUI FreeCAD init script.
I think that even though changing this would be nice it'd propose to do so at a later point and keep these initial changes small. I'd expect it to be a lot more work to change the initialisation, mainly because there are currently 2 separate init steps (non-GUI and GUI) and ideally you'd just want to import the module, let the module check during import if it's being imported in a GUI session and execute the relevant code.
There needs to be a simple way to distinguish between when running FreeCAD in GUI or in command line mode. Currently we have that. As AFAIK currently if running FreeCAD in command line mode InitGui.py isn't interpreted. Therefore it's easy to add UX piece of functionality to FreeCAD from Python and to make it start only when FreeCAD is run in GUI mode.
simonvanderveldt
Posts: 62
Joined: Tue Mar 14, 2017 2:11 pm

Re: Subfolder for Python modules

Post by simonvanderveldt »

triplus wrote:There needs to be a simple way to distinguish between when running FreeCAD in GUI or in command line mode. Currently we have that. As AFAIK currently if running FreeCAD in command line mode InitGui.py isn't interpreted. Therefore it's easy to add UX piece of functionality to FreeCAD from Python and to make it start only when FreeCAD is run in GUI mode.
Yeah, that's correct. But there are always different ways to solve a problem :)
Anyway, I think it makes sense to leave the convention of two init modules (one for non-GUI and one for GUI) in place for now, don't want to introduce too many changes :)
User avatar
looo
Veteran
Posts: 3941
Joined: Mon Nov 11, 2013 5:29 pm

Re: Subfolder for Python modules

Post by looo »

I have cherry picked your two commits regarding the namespace-support. There was also one line from the arch-commit necessary to make FreeCAD start: https://github.com/simonvanderveldt/Fre ... sts.txt#L8

Now I added a directory ..../lib/python3.5/site-packages/freecad/modules/test with these files: __init__.py, init.py, and init_gui.py. But non of the files are executed during start up.

I think I have still not understood this approach.
simonvanderveldt
Posts: 62
Joined: Tue Mar 14, 2017 2:11 pm

Re: Subfolder for Python modules

Post by simonvanderveldt »

looo wrote:I have cherry picked your two commits regarding the namespace-support. There was also one line from the arch-commit necessary to make FreeCAD start: https://github.com/simonvanderveldt/Fre ... sts.txt#L8

Now I added a directory ..../lib/python3.5/site-packages/freecad/modules/test with these files: __init__.py, init.py, and init_gui.py. But non of the files are executed during start up.

I think I have still not understood this approach.
If I understand your setup correctly, it's Python 3 and you want to try out extending FreeCAD by adding some packages to your system wide site-packages directory. Is that correct?
User avatar
looo
Veteran
Posts: 3941
Joined: Mon Nov 11, 2013 5:29 pm

Re: Subfolder for Python modules

Post by looo »

Exactly. Is there anything different with py3?
simonvanderveldt
Posts: 62
Joined: Tue Mar 14, 2017 2:11 pm

Re: Subfolder for Python modules

Post by simonvanderveldt »

looo wrote:Exactly. Is there anything different with py3?
I'd expect not really, but since I haven't tried can't guarantee it. Is FreeCAD master already compatible with Python3? Then I can start trying it.

Did you pick these two commits? Or the older ones:
- https://github.com/simonvanderveldt/Fre ... 777c61f9d7
- https://github.com/simonvanderveldt/Fre ... a807728c43

And do you have any code in the different init files?

Can you post the output you get when running the following in FreeCAD GUI's Python console:

Code: Select all

freecad.modules.__path__
Post Reply