Sublime Forum

Configuring a build system for python3 (run files as modules)

#1

Hello, I’m struggling with a regex and I need it to be dynamic , but I can’t get it in one shot,. I read [this] (http://docs.sublimetext.info/en/sublime-text-3/extensibility/snippets.html#substitutions) and here my “python3.sublime-build” file is it possible nested substitutions?.

{
“cmd”: ["/usr/bin/python3", “-u”, “-m”, “${file///}”]
, “working_dir”: “/home/bar/spam/regex/TFG/”
, “selector”: “source.python”
, “file_regex”: “file “(…*?)”, line ([0-9]+)”
}

now it does nothing, but I tried it all.

Basically I have, this string “/foo/bar/spam/regex/TFG/pacman/search/file.py” and I need this from TFG/ onward “pacman.search.file” so when I run the current file using that build (ctrl+b) actually it’ll be running something like “python3 -u -m pacman.search.file” instead of “python3 -u -m /foo/bar/spam/regex/TFG/pacman/search/file.py” keeping in mind the work directory is set in “.sublime-build”.

I really need help, regards

0 Likes

#2

I have a similar wish, to be able to add to the line number returned from the regex, but unfortunately this is currently not possible.

0 Likes

#3

 
Try writing a separate build script instead of executing python directly.   Doing so would allow you to manipulate the passed arguments as necessary.

I posted a thread that demonstrates my personal build system for Windows.  Maybe you can get some context from checking it out & use it as a reference to write one of your own.

0 Likes

#4

Unfortunately your task would require building a path relative to a certain directory, the project folder, which can not be done dynamically with just build systems. You’d need to either hard-code paths in the build system (as you seem to be doing already anyway) or write a Python plugin.

Going down the first option, which is more simple, you’d do something along the lines of this (untested):

"cmd": ["/usr/bin/python3", "-u", "-m", "${file/\\/home\\/bar\\/spam\\/regex\\/TFG\\/|(\\/)/(?1:.:)/}"]
0 Likes

#5

Thank you guys. I opted for the fico’s solution, I wrote a simple python script which is called from the build system and it’s there where the actual file that I want to run from ST is executed by using the subprocess module. Probably is not the best sol but it’s not gonna be production code :stuck_out_tongue:

1 Like

#6

2 Likes