If you are using GitLab outside of the ILL, then you will need to use HTTPS and not SSH for clone/push/pull operations. SSH will work normally if you are using the VPN.

Commit ebd65a03 authored by eric pellegrini's avatar eric pellegrini

started to repopulate MDANSE

parent c774502c
import abc
class _Meta(type):
'''
Metaclass that allows to use the __getitem__ method at a class level for the class that has been built.
The class that uses this metaclass must define a class attribute named _registry that will be used
by the __getitem__ method.
'''
def __getitem__(self, item):
"""
Returns a given item stored in the class registry
"""
return self._registry[item]
class ClassRegistry(abc.ABCMeta):
'''
Metaclass that registers the subclasses of bases classes.
The internal registry is defined as a nested dictionary whose keys
are the |type| class attribute of the base classes and values another dictionary
whose keys are the |type| class attribute of the subclasses and values are the corresponding
class instances.
Hence any base or child class that does not define |type| class attribute will not be registered.
'''
__metaclass__ = _Meta
__interfaces = []
_registry = {}
def __init__(self, name, bases, namespace):
'''
Constructor of a class metaclassed by ClassFactory
:param name: the name of the class to be built by this metaclass
:param bases: the base classes of the class to be built by this metaclass
:param namespace: the attributes and methods of the class to be built by this metaclass
'''
super(ClassRegistry, self).__init__(name, bases, namespace)
# Get the typ of the class
typ = getattr(self, 'type', None)
if typ is None:
return
metaClass = namespace.get("__metaclass__", None)
if metaClass is ClassRegistry:
ClassRegistry.__interfaces.append(self)
ClassRegistry._registry[typ] = {}
else:
for interface in ClassRegistry.__interfaces:
if issubclass(self, interface):
ClassRegistry._registry[interface.type][typ] = self
break
@classmethod
def info(cls, interface):
'''
Returns informations about the subclasses of a given base class stored in the registry.
:param cls: the ClassRegsitry instance
:param interface: the name of base class of whom information about its subclasses is requested
'''
if not cls._registry.has_key(interface):
return "The interface " + interface + " is not registered"
import inspect
import os
# Dictionnay whose keys are the package names and values and list of (job name, job path) stored in the corresponding package.
packages = {}
# Loop over the registry items.
for k, v in cls._registry[interface].items():
# Get the module corresponding to the job class.
mod = inspect.getmodule(v)
# The package hosting the module.
modPackage = mod.__package__
# The module file.
modFilename = mod.__file__
# If no package could be found, guess a name using the directory name of the module file.
if modPackage is None:
modPackage = os.path.split(os.path.dirname(modFilename))[1]
# Update the packages dictionary.
if packages.has_key(modPackage):
packages[modPackage].append([k, v.__name__])
else:
packages[modPackage] = [[k, v.__name__]]
contents = []
# Print the contents of the packages dictionary.
contents.append("="*130)
contents.append("%-50s %-40s %-s" % ("Package", "Name", "Class"))
contents.append("="*130)
for k, v in sorted(packages.items()):
for vv in sorted(v):
contents.append("%-50s %-40s %-s" % (k, vv[0], vv[1]))
contents.append('-' * 130)
contents = "\n".join(contents)
return contents
"""
This class implements the MDANSE error handler.
It inherits from the Exception class. It can be used to filter the exceptions
raised within the MDANSE framework
"""
class Error(Exception):
def __init__(self, msg=None):
self._msg = msg
def __str__(self):
return repr(self._msg)
import logging
import sys
class Logger(object):
levels = {"debug" : logging.DEBUG,
"info" : logging.INFO,
"warning" : logging.WARNING,
"error" : logging.ERROR,
"fatal" : logging.CRITICAL,
"critical" : logging.FATAL
}
def __call__(self, message, level="info", loggers=None):
lvl = Logger.levels.get(level, None)
# If the logging level is unkwnown, skip that log
if lvl is None:
return
if loggers is None:
loggers = logging.Logger.manager.loggerDict.keys()
else:
loggers = [n for n in loggers if logging.Logger.manager.loggerDict.has_key(n)]
for n in loggers:
logging.getLogger(n).log(lvl, message)
def start(self, logger=None):
from nMOLDYN import _nmoldyn_excepthook
sys.excepthook = _nmoldyn_excepthook
if loggers is None:
loggers = logging.Logger.manager.loggerDict.keys()
else:
loggers = [n for n in loggers if logging.Logger.manager.loggerDict.has_key(n)]
for n in loggers:
logging.getLogger(n).disabled = False
def stop(self, loggers=None):
sys.excepthook = _sys_excepthook
if loggers is None:
loggers = logging.Logger.manager.loggerDict.keys()
else:
loggers = [n for n in loggers if logging.Logger.manager.loggerDict.has_key(n)]
for n in loggers:
logging.getLogger(n).disabled = True
def set_level(self, level, loggers=None):
lvl = Logger.levels.get(level, None)
if lvl is None:
return
if loggers is None:
loggers = logging.Logger.manager.loggerDict.keys()
else:
loggers = [n for n in loggers if logging.Logger.manager.loggerDict.has_key(n)]
for loggerName in loggers:
logging.getLogger(loggerName).setLevel(lvl)
def add_logger(self, name, handler, level="error"):
if logging.Logger.manager.loggerDict.has_key(name):
return
logging.getLogger(name).addHandler(handler)
self.set_level(level, loggers=[name])
This diff is collapsed.
'''
MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
------------------------------------------------------------------------------------------
Copyright (C)
2015- Eric C. Pellegrini Institut Laue-Langevin
BP 156
6, rue Jules Horowitz
38042 Grenoble Cedex 9
France
pellegrini[at]ill.fr
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Created on Mar 23, 2015
@author: pellegrini
'''
class Singleton(type):
__instances = {}
def __call__(self, *args, **kwargs):
if self not in self.__instances:
self.__instances[self] = super(Singleton, self).__call__(*args, **kwargs)
return self.__instances[self]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment