I have quite a few custom endpoints in my web.conf file.
This leads to many Python scripts in my appserver/controllers directory and many helper scripts for functions used by several endpoints.
I would like to tidy up this directory by putting some of the scripts in local packages or subdirectories.
As an example, here's my file structure:
appserver --->controllers ------->my_script.py ------->mylib ---------->mymod.py
#mymod.py def scream(): return 'YEAH!'
#my_script.py import splunk.appserver.mrsparkle.controllers as controllers from splunk.appserver.mrsparkle.lib.decorators import expose_page import mylib.mymod as mm class Controller(controllers.BaseController): @expose_page(must_login=False, methods=['GET']) def scream(self, **kwargs) : return mm.scream()
Now, if I add a empty
__init__.py file to
mylib behaves like a package, and if I test the script locally with splunk's python interpreter in CLI:
splunk cmd python my_script.py , I am able to import to import my module and use the scream() function from my_script.py.
However, the import doesn't work one I try to run the script through the web service:
http://localhost:8000/en-US/custom/my_app/my_script/scream throws a 404 Error.
What's the Splunk-approved way of organizing code in my
controllers directory ?
I have a controller in one of my apps. The only real difference I can see is that you did not call the class the same as the filename. Change it, and let me know.
This should present it self and work. Additionally, you may want to try putting this in
Hi, thank you for your answer.
My controllers already work fine with :
my_script.py as a file name in
class random_name(controllers.BaseController): in
In this Splunk Wiki page, someone shows an example where the class name doesn't match the file name. https://wiki.splunk.com/Community:40GUIDevelopment
What I am trying to achieve is moving some code from
controllers to subdirectories.
did you ever manager to get it to run? I need a ready made class and i can not load it inside my controller 😕
I still haven't found a solution to my problem which was: keeping the code inside the app structure.
If you don't have this requirement, have you tried putting your ready made class inside a module and inside the following folder?
actually i managed... will post it as a answer but it is hackish, you can exted the python path... 😕
You need to extend the python path 😞
import sys sys.path.append(os.path.join(os.environ['SPLUNK_HOME'],'etc','apps','YourAppNameHere','appserver','controllers')) #build local path and add it to the python path so we can load modules, hack!