__init__.py (pymol-v1.8.6.0.tar.bz2) | : | __init__.py (pymol-v2.1.0.tar.bz2) | ||
---|---|---|---|---|
skipping to change at line 15 | skipping to change at line 15 | |||
License: BSD-2-Clause | License: BSD-2-Clause | |||
''' | ''' | |||
from __future__ import print_function | from __future__ import print_function | |||
import os | import os | |||
import sys | import sys | |||
import pymol | import pymol | |||
from pymol import cmd | from pymol import cmd | |||
from pymol import colorprinting | ||||
from .legacysupport import * | from .legacysupport import * | |||
# variables | # variables | |||
PYMOLPLUGINSRC = os.path.expanduser('~/.pymolpluginsrc.py') | PYMOLPLUGINSRC = os.path.expanduser('~/.pymolpluginsrc.py') | |||
preferences = { | preferences = { | |||
'verbose': False, | 'verbose': False, | |||
'instantsave': True, | 'instantsave': True, | |||
} | } | |||
autoload = {} | autoload = {} | |||
plugins = {} | plugins = {} | |||
HAVE_QT = False | ||||
# exception types | ||||
class QtNotAvailableError(Exception): | ||||
pass | ||||
# plugins from PYMOL_DATA | ||||
startup.__path__.append(cmd.exp_path('$PYMOL_DATA/startup')) | ||||
N_NON_USER_PATHS = len(startup.__path__) | ||||
# API functions | # API functions | |||
def is_verbose(debug=0): | def is_verbose(debug=0): | |||
verbose = pref_get('verbose') | verbose = pref_get('verbose') | |||
if debug and verbose < 0: | if debug and verbose < 0: | |||
return True | return True | |||
return verbose and pymol.invocation.options.show_splash | return verbose and pymol.invocation.options.show_splash | |||
def get_startup_path(useronly=False): | def get_startup_path(useronly=False): | |||
if useronly: | if useronly: | |||
# assume last item is always from installation directory | # assume last item is always from installation directory | |||
return startup.__path__[:-1] | return startup.__path__[:-N_NON_USER_PATHS] | |||
return startup.__path__ | return startup.__path__ | |||
def set_startup_path(p, autosave=True): | def set_startup_path(p, autosave=True): | |||
if isinstance(p, list): | if isinstance(p, list): | |||
startup.__path__[:-1] = p | startup.__path__[:-N_NON_USER_PATHS] = p | |||
if autosave: | if autosave: | |||
set_pref_changed() | set_pref_changed() | |||
else: | else: | |||
print(' Error: set_startup_path failed') | print(' Error: set_startup_path failed') | |||
def pref_set(k, v): | def pref_set(k, v): | |||
preferences[k] = v | preferences[k] = v | |||
set_pref_changed() | set_pref_changed() | |||
def pref_get(k, d=None): | def pref_get(k, d=None): | |||
skipping to change at line 88 | skipping to change at line 101 | |||
f.close() | f.close() | |||
if not int(quiet): | if not int(quiet): | |||
print(' Plugin settings saved!') | print(' Plugin settings saved!') | |||
def set_pref_changed(): | def set_pref_changed(): | |||
if pref_get('instantsave', True): | if pref_get('instantsave', True): | |||
verbose = pref_get('verbose', False) | verbose = pref_get('verbose', False) | |||
pref_save(quiet=not verbose) | pref_save(quiet=not verbose) | |||
def addmenuitemqt(label, command=None, menuName='PluginQt'): | ||||
''' | ||||
Adds plugin menu item to main 'Plugin' menu. | ||||
Intended for plugins which open a PyQt window. | ||||
''' | ||||
if not HAVE_QT: | ||||
raise QtNotAvailableError() | ||||
addmenuitem(label, command, menuName) | ||||
def addmenuitem(label, command=None, menuName='Plugin'): | def addmenuitem(label, command=None, menuName='Plugin'): | |||
''' | ''' | |||
Generic replacement for MegaWidgets menu item adding | Generic replacement for MegaWidgets menu item adding | |||
''' | ''' | |||
labels1 = [menuName] + label.split('|') | labels1 = [menuName] + label.split('|') | |||
labels2 = ['|'.join(labels1[0:i]) for i in range(1, len(labels1))] | labels2 = ['|'.join(labels1[0:i]) for i in range(1, len(labels1))] | |||
pmgapp = get_pmgapp() | pmgapp = get_pmgapp() | |||
if pmgapp is not None: | if pmgapp is not None: | |||
for i in range(1, len(labels2)): | for i in range(1, len(labels2)): | |||
try: | try: | |||
skipping to change at line 176 | skipping to change at line 199 | |||
@property | @property | |||
def is_temporary(self): | def is_temporary(self): | |||
return self.mod_name is None | return self.mod_name is None | |||
def get_metadata(self): | def get_metadata(self): | |||
''' | ''' | |||
Parse plugin file for metadata (hash-commented block at beginning of fil e). | Parse plugin file for metadata (hash-commented block at beginning of fil e). | |||
''' | ''' | |||
metadata = dict() | metadata = dict() | |||
f = open(self.filename) | f = open(self.filename, 'rU') | |||
for line in f: | for line in f: | |||
if line.strip() == '': | if line.strip() == '': | |||
continue | continue | |||
if not line.startswith('#'): | if not line.startswith('#'): | |||
break | break | |||
if ':' in line: | if ':' in line: | |||
key, value = line[1:].split(':', 1) | key, value = line[1:].split(':', 1) | |||
metadata[key.strip()] = value.strip() | metadata[key.strip()] = value.strip() | |||
f.close() | f.close() | |||
self.get_metadata = lambda: metadata | self.get_metadata = lambda: metadata | |||
skipping to change at line 216 | skipping to change at line 239 | |||
def get_docstring(self): | def get_docstring(self): | |||
''' | ''' | |||
Get docstring either from loaded module, or try to parse first python | Get docstring either from loaded module, or try to parse first python | |||
statement from file, without executing any code. | statement from file, without executing any code. | |||
''' | ''' | |||
if self.loaded: | if self.loaded: | |||
return self.module.__doc__ | return self.module.__doc__ | |||
try: | try: | |||
c = compile(''.join(open(self.filename)), 'x', 'single') | c = compile(''.join(open(self.filename)), 'x', 'exec', dont_inherit= True) | |||
s = c.co_consts[0] | s = c.co_consts[0] | |||
if cmd.is_string(s): | if cmd.is_string(s): | |||
return s | return s | |||
except SyntaxError as e: | ||||
if sys.version_info[0] > 2: | ||||
return 'WARNING: Plugin not Python 3.x compatible: ' + str(e) | ||||
except: | except: | |||
pass | pass | |||
def load(self, pmgapp=None, force=0): | def load(self, pmgapp=None, force=0): | |||
''' | ''' | |||
Load and initialize plugin. | Load and initialize plugin. | |||
If pmgapp == -1, do not initialize. | If pmgapp == -1, do not initialize. | |||
''' | ''' | |||
import time | import time | |||
skipping to change at line 249 | skipping to change at line 275 | |||
try: | try: | |||
# overload cmd.extend to register commands | # overload cmd.extend to register commands | |||
extend_orig = cmd.extend | extend_orig = cmd.extend | |||
def extend_overload(a, b=None): | def extend_overload(a, b=None): | |||
self.commands.append(a if b else a.__name__) | self.commands.append(a if b else a.__name__) | |||
return extend_orig(a, b) | return extend_orig(a, b) | |||
cmd.extend = extend_overload | cmd.extend = extend_overload | |||
# do not use self.loaded here | # do not use self.loaded here | |||
if force and self.module is not None: | if force and self.module is not None: | |||
if sys.version_info[0] > 2: | ||||
from importlib import reload | ||||
else: | ||||
from __builtin__ import reload | ||||
reload(self.module) | reload(self.module) | |||
else: | else: | |||
__import__(self.mod_name, level=0) | __import__(self.mod_name, level=0) | |||
if pmgapp != -1: | if pmgapp != -1: | |||
self.legacyinit(pmgapp) | self.legacyinit(pmgapp) | |||
cmd.extend = extend_orig | cmd.extend = extend_orig | |||
self.loadtime = time.time() - starttime | self.loadtime = time.time() - starttime | |||
if verbose and pymol.invocation.options.show_splash: | if verbose and pymol.invocation.options.show_splash: | |||
print(' Plugin "%s" loaded in %.2f seconds' % (self.name, self.l oadtime)) | print(' Plugin "%s" loaded in %.2f seconds' % (self.name, self.l oadtime)) | |||
except QtNotAvailableError: | ||||
colorprinting.warning("Plugin '%s' only available with PyQt GUI." % | ||||
(self.name,)) | ||||
except: | except: | |||
e = sys.exc_info()[1] | ||||
if verbose: | if verbose: | |||
import traceback | colorprinting.print_exc([__file__]) | |||
traceback.print_exc() | elif 'libX11' in str(e) and sys.platform == 'darwin': | |||
colorprinting.error('Please install XQuartz (https://www.xquartz | ||||
.org/)') | ||||
else: | else: | |||
print(sys.exc_info()[1]) | colorprinting.error(e) | |||
print("Unable to initialize plugin '%s' (%s)." % (self.name, self.mo | colorprinting.warning("Unable to initialize plugin '%s' (%s)." % (se | |||
d_name)) | lf.name, self.mod_name)) | |||
return False | return False | |||
return True | return True | |||
def legacyinit(self, pmgapp): | def legacyinit(self, pmgapp): | |||
''' | ''' | |||
Call the __init__ or __init_plugin__ function which takes the PMGApp | Call the __init__ or __init_plugin__ function which takes the PMGApp | |||
instance as argument (usually adds menu items). | instance as argument (usually adds menu items). | |||
This must be called after loading and after launching of the external | This must be called after loading and after launching of the external | |||
End of changes. 13 change blocks. | ||||
9 lines changed or deleted | 45 lines changed or added |