|
| 1 | +--- |
| 2 | +authors: |
| 3 | +- CyrilFerlicot |
| 4 | +title: "Python importer" |
| 5 | +subtitle: "How to import a python projet as a Moose model" |
| 6 | +--- |
| 7 | + |
| 8 | +It is possible to import python projets using `MoosePy`. |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +To install `MoosePy` on your Pharo image, execute the following script: |
| 13 | + |
| 14 | +```smalltalk |
| 15 | +Metacello new |
| 16 | + githubUser: 'moosetechnology' project: 'MoosePy' commitish: 'master' path: 'src'; |
| 17 | + baseline: 'MoosePy'; |
| 18 | + load |
| 19 | +``` |
| 20 | + |
| 21 | +To add `MoosePy` to your baseline: |
| 22 | + |
| 23 | +```smalltalk |
| 24 | +spec |
| 25 | + baseline: 'MoosePy' |
| 26 | + with: [ spec repository: 'github://moosetechnology/MoosePy:master/src' ] |
| 27 | +``` |
| 28 | + |
| 29 | +## Usage |
| 30 | + |
| 31 | +### Import a project programatically |
| 32 | + |
| 33 | +You can launch the import of a project like this: |
| 34 | + |
| 35 | +```smalltalk |
| 36 | + FamixPythonImporter import: '/Users/cyril/testPython/reader' asFileReference |
| 37 | +``` |
| 38 | + |
| 39 | +It will return a Famix python model generated from the `.py` files in this folder and if there are errors during the parsing, it'll open an inspector with the list of errors. |
| 40 | +It is also possible to provide a single `.py` file to the importer. |
| 41 | + |
| 42 | +#### Resolution of absolute imports |
| 43 | + |
| 44 | +In order to determine the origin of the absolute imports, we are using the first folder that contains all .py to parse of the project. But sometimes it is not that easy. In that case it is possible to configure manually the folder to consider the root of the project. |
| 45 | + |
| 46 | +Let's take this example: |
| 47 | + |
| 48 | +``` |
| 49 | + /project |
| 50 | + /src |
| 51 | + /package |
| 52 | + __initi__.py |
| 53 | + module.py |
| 54 | + /resources |
| 55 | + /example |
| 56 | + example.py |
| 57 | +``` |
| 58 | + |
| 59 | +If no root package path is specify, `project` will be considered the root directory and will be the start point for the absolute import resolution. But it's highly possible that the real working directory here is /src. |
| 60 | + |
| 61 | +It is possible to do: |
| 62 | + |
| 63 | +```smalltalk |
| 64 | + FamixPythonImporter import: 'path/to/project' asFileReference rootPackagePath: 'src' |
| 65 | +``` |
| 66 | + |
| 67 | +This will start the import from the src folder and not project. The file `example.py` will be ignored. |
| 68 | + |
| 69 | +#### Ignore files |
| 70 | + |
| 71 | +It as also possible to indicate that we want to ignore files or folder. In order to do that, the variable `#filesToIgnoreBlock` should be used. It should be a valuable (like a block) taking file references as parameter and returning true if we should ignore those files/folders. |
| 72 | +By default I'm excluding folders named `venv` because it is common to have a python interpreter of this name in projects if we develop them on the computer and files `setup.py`. |
| 73 | + |
| 74 | +For the example above we could do: |
| 75 | + |
| 76 | +```smalltalk |
| 77 | + FamixPythonImporter new |
| 78 | + filesToIgnoreBlock: [ :file | #('venv' 'setup.py' 'resources') includes: file basename ] ]; |
| 79 | + import: 'path/to/project' |
| 80 | +``` |
| 81 | + |
| 82 | +### Import from the graphical interface |
| 83 | + |
| 84 | +It is also possible to import the project using the `Models browser` of Moose. |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +This will open a window with the different options explained in the previous section |
| 90 | + |
| 91 | + |
0 commit comments