'Dynamic modules can\'t be imported/loaded from a zipfile.
So this routine will extract the appropriate
library for the target OS and copy it to the \'alfcrypto\' subdirectory of
calibre\'s configuration directory. That \'alfcrypto\' directory is then
inserted into the syspath (as the very first entry) in the run function
so the CDLL stuff will work in the alfcrypto.py script.
The extraction only happens once per version of the plugin
Also perform upgrade of preferences once per version'

def initialize(self):

    try:
        self.pluginsdir = os.path.join(config_dir, u'plugins')
        if (not os.path.exists(self.pluginsdir)):
            os.mkdir(self.pluginsdir)
        self.maindir = os.path.join(self.pluginsdir, u'DeDRM')
        if (not os.path.exists(self.maindir)):
            os.mkdir(self.maindir)
        self.helpdir = os.path.join(self.maindir, u'help')
        if (not os.path.exists(self.helpdir)):
            os.mkdir(self.helpdir)
        self.alfdir = os.path.join(self.maindir, u'libraryfiles')
        if (not os.path.exists(self.alfdir)):
            os.mkdir(self.alfdir)
        self.verdir = os.path.join(self.maindir, PLUGIN_VERSION)
        if (not os.path.exists(self.verdir)):
            if iswindows:
                names = [u'alfcrypto.dll', u'alfcrypto64.dll']
            elif isosx:
                names = [u'libalfcrypto.dylib']
            else:
                names = [u'libalfcrypto32.so', u'libalfcrypto64.so', u'kindlekey.py', u'adobekey.py', u'subasyncio.py']
            lib_dict = self.load_resources(names)
            print u"{0}    v{1}:    Copying    needed    library    files    from    plugin's    zip".format(PLUGIN_NAME, PLUGIN_VERSION)
            for (entry, data) in lib_dict.items():
                file_path = os.path.join(self.alfdir, entry)
                try:
                    os.remove(file_path)
                except:
                    pass
                try:
                    open(file_path, 'wb').write(data)
                except:
                    print u'{0}    v{1}:    Exception    when    copying    needed    library    files'.format(PLUGIN_NAME, PLUGIN_VERSION)
                    traceback.print_exc()
                    pass
            from calibre_plugins.dedrm.prefs import convertprefs
            convertprefs()
            os.mkdir(self.verdir)
    except Exception as e:
        traceback.print_exc()
        raise
