Sublime Forum

Splitting commands between multiple files

#1

Hi,

I was developing my plugin MSBuildSelector in a single file till it cames big enough to annoy me.

But now that I am splitting it with one file per class, I does not work any more.
Basically I have one MsbuildSeletor class inheriting TextCommand and providing the common tools. This class is inherited by MsbuildSelectorFileCommand and MsbuildSelectorProjectCommand. When I try to invoke the build command, I got:

Traceback (most recent call last):
  File "C:\Program Files\Sublime Text 3\sublime_plugin.py", line 553, in run_
    return self.run()
  File "C:\Users\xxx\AppData\Roaming\Sublime Text 3\Packages\MSBuildSelector\MSBuildSelectorFileCommand.py", line 82, in run
    params)
TypeError: 'module' object is not callable

I tried renaming the files, wrongly assuming there was a conflict between file names and class names, but it did not changed anything.

The code is pushed to this branch.

Thanks for your attention.

0 Likes

#2

maybe
from . import BuildInfo as BuildInfo

0 Likes

#3

No, I’ve tried and pushed it (by the way there was a big typo so I do not even know how it did manage to load anything), no changes.

The typo I’ve just fixed makes me thing that the files are not even evaluated correctly.

0 Likes

#4

I think it’s:
from .BuildInfo import BuildInfo
Or change the call to:
build_info = BuildInfo.BuildInfo(name_for_file,

0 Likes

#5

You’re right it works ! I add to remove the prefixes and also, important point, to restart SublimeText. I do not know why but it seemed to keep previous class definition.
So to sum up, when you have a class defined in a file you want to use:

from .filename import classname

class OtherClass(classname):
    pass

And important point for us, Windowers: THE FILENAME IS CASE SENSITIVE

Many thanks

0 Likes

#6

You dont need to restart, unless you using threads or similar. What you need to do is to save the main file, so does reload the main package.

0 Likes

#7

What define the main file ? I have 4 files .py, why would one be “mainer” than the others ?

0 Likes

#8

One file is including other files, in order to pick the new changes of external files you should save the top most file, so for example, to reload this command:

{
“caption”: “MSBuildSelector: Build project”,
“command”: “msbuild_selector_project”
},

you should save the file that has the msbuild_selector_project command defined.

0 Likes

#9

In a package you can have more than one file that define command, so there’s not always a main file.
In addition, ST reload the files when they are modified, but don’t clean the class/object (it couldn’t).
So depending of your code, you can have (and often you have) some issues if you don’t restart ST (like Events that are NOT removed when reloading, leading to events triggered for old code or multiple trigger of the same event).

0 Likes

#10

Yes but I doubt this is the case, with main file I was referring to the main file that defines the command he is running.

0 Likes

#11

This is the new absolute import syntax from Python.
I advise you not to use the same name for the source file (usually lowercase) and the class name(usually CamelCase), Python is not Java.
In addition, I think importing the module and use the complete notation is better, but it’s my personal opinion:

from . import mymodule
class OtherClass(mymodule.myclass):
    pass
0 Likes

#12

Thanks, will update the file names.

0 Likes