AndersMalmgren / FreePIE

Programmable Input Emulator
644 stars 144 forks source link

Getting the path of the currently running script #194

Open jmriego opened 4 years ago

jmriego commented 4 years ago

Hi! I have been a long time user and FreePIE and find it a really good program!

I have a script that is more complicated than usual and would like to separate it into a different library. At the moment, I'm doing this to be able to import libraries located in the FreePIE folder inside my home directory: sys.path.append(os.path.expandvars(r'%USERPROFILE%\FreePIE'))

I would like instead to be able to replace the hardcoded directory to read from the same directory the currently executing script is located. I tried to use the __file__ python variable but it seems to not have what I expected. Is there any way to do this? Thanks!

AndersMalmgren commented 4 years ago

Hi the script is read into memory and executed so it does not have any connection the file when executed.

I think the default path is the lib path or similar. I haven't touch the python bits in years I'm afraid.

jmriego commented 4 years ago

thanks for your answer! I checked the path and it seems like it's only reading from the pylib folder do you think it would be possible to add the current file name script as a variable somewhere? If it wouldn't be a lot of work

AndersMalmgren commented 4 years ago

Problem is its the UI that loads the file and sends it off to the backend for exection. Needs some refactor for it to work. I might have time eventually, but if you want it done now you can branch freepie ;)

AndersMalmgren commented 4 years ago

If someone want a crack at this here is the place were the paths are beign loaded into the python egine

https://github.com/AndersMalmgren/FreePIE/blob/2595a11f960d9caddb507d1e18b5787b0b033642/FreePIE.Core/ScriptEngine/Python/PythonScriptEngine.cs#L151

jmriego commented 4 years ago

thanks @AndersMalmgren ! that seems simple enough. I'll try and see if I can do this one

cjroehrig commented 4 years ago

I'm using a fairly complex script across several modules and I solved this by creating a windows symlink in the FreePIE/pylib directory that points to the root of my own modules dir under my Documents. Something like: mklink /d C:\Program Files (x86)\FreePIE\pylib\cjr C:\Users\cjr\Documents\CJRControllers\Lib\pylib Then you can import modules by: import cjr.mymodule

You'll also have trouble with file and line number errors which I work around by testing first under regular Windows Python to catch any syntax errors. (I check if sys.implementation.name != 'ironpython' and import a sim_plugin module that provides stubs for the FreePIE plugins I use).

You'll also notice that none of the special FreePIE globals are available in modules. I wrap them in a class object I call G which I pass to all the modules and save in their class instances (e.g. self.G.mouse.deltaX).

Also any "indexer" functions (filters) won't work inside a module. Easiest thing is to just not use them. They are easily implemented as dedicated variables/functions in your module's class and will be a bit faster too since they won't require a textual hash lookup, and it will also work with regular python.

AndersMalmgren commented 4 years ago

Maybe we should add a way to add extra paths using command line or config

jmriego commented 4 years ago

thanks @cjroehrig ! That's a really good idea! I never thought of linking the directory itself, just files before

I have just sent a pull request related to this but it just adds the directory of the currently running file

cjroehrig commented 4 years ago

Maybe we should add a way to add extra paths using command line or config

That would be nice, but I think it should also include an update to error handling so that the syntax/runtime errors can report the file and line number. I had a go at a patch a couple years ago but I don't think I ever got it working. The filename doesn't seem to be readily accessible in the IronPython Exception which looks to be designed around streams. Needs more digging...