Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is possible to know the path of the file of a subclass in python?

I have a plugin system. The plugins subclass from a common ancestor... ad look like this:

-- SDK
--- basePlugin.py
-- PLUGINS
--- PluginA
---- Plugin.py
---- Config.ini
--- PluginB
---- Plugin.py
---- Config.ini

I need to read the info of Config.ini in basePlugin.py __init__. CUrrently in each plugin I do:

class PluginA(BaseSync):
  __init__(self, path):
    super(PluginA,self).__init__(self, __file__)

But wonder if is possible to know in the parent class in which file is located the sub-class...

like image 858
mamcx Avatar asked Oct 23 '10 21:10

mamcx


1 Answers

Assuming BaseSync is a new-style class, the parent class BaseSync could find the file that defines PluginA this way:

import sys
class BaseSync(object):
    def __init__(self):
        path=sys.modules[self.__module__].__file__

(so you don't have to pass the path explicitly).

like image 188
unutbu Avatar answered Oct 14 '22 07:10

unutbu