Quote:
Originally Posted by Doitsu
@CalibUser: Since the Sigil plugin root directory and the user_dictionary directory are sibling directories it's relatively easy to get the user_dictionary directory location.
For example, you could use the following code to get the dictionary folder:
Code:
import os, inspect
def run(bk):
# get plugin directory path
plugin_path = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
print(plugin_path)
# get rid of the last two directories
tmp_path = plugin_path.split(os.path.sep)[:7]
print(tmp_path)
# add the dictionary path
tmp_path.extend(['user_dictionaries', 'WordDictionary.txt'])
# convert list back to file path
dictionary_path = os.path.sep.join(tmp_path)
print(dictionary_path)
The above code will produce the following output:
Windows:
Code:
C:\Users\Doitsu\AppData\Local\sigil-ebook\sigil\plugins\test
['C:', 'Users', 'Doitsu', 'AppData', 'Local', 'sigil-ebook', 'sigil']
C:\Users\Doitsu\AppData\Local\sigil-ebook\sigil\user_dictionaries\WordDictionary.txt
Linux (DiapDealer's build):
Code:
/home/doitsu/.local/share/sigil-ebook/sigil/plugins/test
['', 'home', 'doitsu', '.local', 'share', 'sigil-ebook', 'sigil']
/home/doitsu/.local/share/sigil-ebook/sigil/user_dictionaries/WordDictionary.txt
I've attached a test plugin that you can play with.
|
Thanks for mentioning the 'inspect' module method of finding the current script directory. Saves me the trouble.
![Smilie](https://www2.mobileread.com/i/smilies/smile.gif)
Though there may be shorter ways to get the current directory of the script that's being run, it's the only one that's guaranteed to work even when a script was invoked as a module.
I would, however suggest something other than the relatively fragile method of converting a path to a list of strings and then using the [:7] slice to strip off the last two directories. If the depth of that path ever increases, it won't point to the sigil preferences directory anymore. To be clear: it's the [:7] slice I find fragile, not the list of strings conversion and eventual re-joining.
I would suggest using [:-2] if you're going to split the path into a list of strings that later get rejoined. Or just use os.path.dirname twice without converting to a list of strings and rejoining.
It's
all a bit fragile I guess (even mine), considering that the plugin directory could conceivably change in relation to the Sigil preferences directory.
Code:
import os, inspect
# get plugin directory path
plugin_path = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
print(plugin_path)
# get rid of the last two directories
tmp_path = os.path.dirname(os.path.dirname(plugin_path))
print(tmp_path)
# add the dictionary path
dictionary_path = os.path.join(tmp_path, 'user_dictionaries', 'WordDictionary.txt')
print(dictionary_path)
This will all be simplified when accessing hunspell/dictionaries is incorporated into the plugin launcher framework, but Doitsu's above suggestion could work in the meantime (cross-platform) and wouldn't break even when the new version is released.
You could also determine the path of the current plugin script in the run method of a plugin by using:
Code:
def run(bk):
ppath = bk._w.plugin_dir
It's not really recommended to access those wrapper script properties/methods directly--as they could change at any time. Though in this particular instance ... I don't foresee the plugin_dir property of the wrapper script ever disappearing or having its name changed.