Home Download Buy Blog Forum Support

[SOLVED] Obtain a plugin's directory at runtime

[SOLVED] Obtain a plugin's directory at runtime

Postby phillip.koebbe on Fri Jan 20, 2012 8:34 pm

Is it possible to obtain the plugin's directory at runtime? Suppose I write a plugin that has supporting files in the same directory how do I access them dynamically, that is, without hardcoding the expected path in the plugin?

Example:

I expect DetectSyntax to live in Packages/DetectSyntax, but what if someone puts it in Packages/Detect Syntax (notice the space)?
Last edited by phillip.koebbe on Sun Jan 22, 2012 8:40 pm, edited 1 time in total.
-- Phillip

Code: Select all
while self.living? do
    mistake = Mistake.new
    self.learn_from!(mistake) unless mistake.fatal?
end
phillip.koebbe
 
Posts: 208
Joined: Tue Dec 20, 2011 4:58 pm

Re: Obtain a plugin's directory at runtime

Postby jps on Sat Jan 21, 2012 10:43 pm

The current directory will be set to the plugin's directory when it's loaded, so you can capture os.getcwdu() then (it must be captured when the module is loaded, it will be set to other values later).

It's not ideal to rely on this, but I believe it's the only option at the moment, apart from requiring a specific package name.
jps
Site Admin
 
Posts: 3062
Joined: Wed Mar 19, 2008 12:33 pm

Re: Obtain a plugin's directory at runtime

Postby phillip.koebbe on Sun Jan 22, 2012 3:32 am

When you speak of loading, are you meaning __init__? I tried

Code: Select all
   def __init__(self):
      self.plugin_dir = os.getcwdu()


and then printed it in my routine and it was /. Trying to use / when loading a file later generated an error because it was looking for the file at the filesystem's root. So I'm a bit confused.

Any chance you will consider adding something that let's the plugin discover where it is?
-- Phillip

Code: Select all
while self.living? do
    mistake = Mistake.new
    self.learn_from!(mistake) unless mistake.fatal?
end
phillip.koebbe
 
Posts: 208
Joined: Tue Dec 20, 2011 4:58 pm

Re: Obtain a plugin's directory at runtime

Postby phillip.koebbe on Sun Jan 22, 2012 8:39 pm

Nevermind. I got it sorted out. I put the call to os.getcwd() right after the import statements before the class definition and it worked.

Thanks for the support, Jon.
-- Phillip

Code: Select all
while self.living? do
    mistake = Mistake.new
    self.learn_from!(mistake) unless mistake.fatal?
end
phillip.koebbe
 
Posts: 208
Joined: Tue Dec 20, 2011 4:58 pm


Return to Plugin Development

Who is online

Users browsing this forum: Majestic-12 [Bot] and 2 guests