Changeset 257


Ignore:
Timestamp:
10.08.2010 15:20:51 (9 years ago)
Author:
marcus
Message:

Singleton implementation changes to meta class usage. Maybe log is broken now.

Location:
SHX/trunk/src/SeismicHandler
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • SHX/trunk/src/SeismicHandler/basics/tools.py

    r255 r257  
    44Collection of globally used helper functions. 
    55""" 
    6  
    76# property wrapper 
    87def Property(func): 
    98    return property(**func()) 
     9 
     10class Singleton(type): 
     11    """ 
     12    Meta-class for singletons. 
     13    """ 
     14 
     15    def __init__(cls, name, bases, dic): 
     16        super(Singleton, cls).__init__(name, bases, dic) 
     17        cls.instance = None 
     18 
     19    def __call__(cls, *args, **kwargs): 
     20        if cls.instance is None: 
     21            cls.instance = super(Singleton, cls).__call__(*args, **kwargs) 
     22 
     23        return cls.instance 
  • SHX/trunk/src/SeismicHandler/core/__init__.py

    r256 r257  
    2121 
    2222from SeismicHandler.basics import codes 
    23 from SeismicHandler.modules.traces import Traces 
     23from SeismicHandler.modules.traces import Traces as sh_traces 
    2424from SeismicHandler.modules.history import History as history 
    2525from SeismicHandler.modules.log import Logging 
     
    3333 
    3434# trace access 
    35 t = Traces() 
    36 Traces = t.visible 
    37 Hidden = t.hidden 
     35Traces = sh_traces().visible 
     36Hidden = sh_traces().hidden 
    3837 
    3938def read(*args, **kwargs): 
     
    4140    Wrapper for obspy read. 
    4241 
    43     Escpecially 
     42    Especially pay respect to certain forms of miniseed indizes (sds, db, sfd). 
    4443    """ 
    4544    return obspy_read(*args, **kwargs) 
    46  
  • SHX/trunk/src/SeismicHandler/modules/history.py

    r255 r257  
    2222"""history function for SHX""" 
    2323 
     24from SeismicHandler.basics.tools import Singleton 
    2425from time import time 
    2526 
    2627class History(object): 
     28    __metaclass__ = Singleton 
    2729    history = [] 
    2830     
    29     def __new__(type, *args, **kwargs): 
    30         """ensure single instance""" 
    31          
    32         if not '_the_instance' in type.__dict__: 
    33             type._the_instance = object.__new__(type) 
    34              
    35         return type._the_instance 
    36  
    3731    def __init__(self, cmd, args, kwargs): 
    3832        """save history entry""" 
  • SHX/trunk/src/SeismicHandler/modules/log.py

    r255 r257  
    2222"""global logging instance for SHX""" 
    2323 
     24from SeismicHandler.basics.tools import Singleton 
    2425import logging 
    2526import os 
    2627import atexit 
    2728 
    28 class Logging(logging.Logger): 
     29class Logging(object): 
     30    __metaclass__ = Singleton 
    2931    logging = None 
    30  
    31     def __new__(cls, *args, **kwargs): 
    32         """ensure single instance""" 
    33  
    34         if not '_the_instance' in cls.__dict__: 
    35             cls._the_instance = object.__new__(cls) 
    36  
    37         return cls._the_instance 
    3832 
    3933    def __init__(self): 
    4034        """configure logging once""" 
    4135 
    42         logging.Logger.__init__(self, "shx") 
     36#        logging.Logger.__init__(self, "shx") 
    4337 
    4438        if not self.logging: 
     
    6054            self.backupdest = os.path.join(dir, backupfile) 
    6155 
    62             # add more levels 
    63             # works only for root logger, not for children from getLogger() 
    64             newlvl = { 
    65                    "DEBUG2": logging.DEBUG + 1, 
    66                    } 
    67  
    68             for lvl in newlvl: 
    69                 logging.addLevelName(lvl, newlvl[lvl]) 
    70 #                setattr(self, lvl.lower(), lambda *args: self.log(newlvl[lvl], *args)) 
    71  
    7256            try: 
    7357                logging.basicConfig( 
     
    7963                ) 
    8064            except IOError: 
    81                 self.warning("Cannot log to disc, path %s not writable! " 
     65                logging.warning("Cannot log to disc, path %s not writable! " 
    8266                             "Logging everything to console." % dir) 
    8367 
     
    10791            atexit.register(self.shutdown) 
    10892 
    109     def debug2(self, args, kwargs): 
    110         if self.manager.disable >= logging.DEBUG+1: 
    111             return 
    112  
    11393    def shutdown(self): 
    11494        """Move our logfile to backup destination for later checking. 
  • SHX/trunk/src/SeismicHandler/modules/runtime.py

    r256 r257  
    2323 
    2424import sys 
    25 from SeismicHandler.basics.tools import Property 
     25from SeismicHandler.basics.tools import Property, Singleton 
    2626from obspy.core.util import AttribDict 
    2727 
    2828class runtime(object): 
     29    """ 
     30    >>> Settings = runtime() 
     31 
     32    >>> print >> Settings.echo_channel, Settings.echo_channel 
     33    >>> Settings.echo_channel = "TEST" 
     34    >>> print >> Settings.echo_channel, Settings.echo_channel 
     35    >>> Settings.echo_channel = "" 
     36    >>> print >> Settings.echo_channel, Settings.echo_channel #doctest: +ELLIPSIS 
     37    <doctest... 
     38     
     39    Now a file named TEST should contain 
     40    >>> open("TEST").read()[:34] 
     41    "<open file 'TEST', mode 'ab' at 0x" 
     42 
     43    This is only for clean-up: 
     44    >>> import os 
     45    >>> os.unlink("TEST") 
     46    """ 
     47    __metaclass__ = Singleton 
     48 
    2949    # see echo_channel() 
    3050    __echo_channel = sys.stdout 
     
    5676    Globals = {} 
    5777 
    58     def __new__(cls): 
    59         """ 
    60         Ensure that there's only one instance of the settings class. This 
    61         enables us to set global configuration options and carry global 
    62         variables. 
    63  
    64         Technically this is a singleton. 
    65         """ 
    66  
    67         if not '__instance' in cls.__dict__: 
    68             cls.__instance = object.__new__(cls) 
    69  
    70         return cls.__instance 
    71  
    7278    def __init__(self): 
    73 #        print self.__configuration 
    74 #        self.__configuration = 1 
     79#        print __file__ 
    7580        pass 
    7681 
     
    141146 
    142147if __name__ == "__main__": 
    143     Settings = runtime() 
    144  
    145     print >> Settings.echo_channel, Settings.echo_channel 
    146     Settings.echo_channel = "TEST" 
    147     print >> Settings.echo_channel, Settings.echo_channel 
    148     Settings.echo_channel = "" 
    149     print >> Settings.echo_channel, Settings.echo_channel 
     148    import doctest 
     149    doctest.testmod(exclude_empty=True) 
  • SHX/trunk/src/SeismicHandler/modules/traces.py

    r256 r257  
    2121 
    2222import sys 
     23from SeismicHandler.basics.tools import Singleton 
    2324from obspy.core.stream import Stream 
    2425 
    2526class Traces(object): 
    2627    """ 
     28    Supply access for two stream of traces. One is used for plotting, the other 
     29    holds hidden traces. This will be enhanced later, but for compatibility 
     30    reasons to existing command procedures it's done that ways first. 
     31    """ 
     32    __metaclass__ = Singleton 
    2733 
    28     """ 
    2934    visible = None 
    3035    hidden = None 
    31  
    32     def __new__(cls): 
    33         """ 
    34         Ensure that there's only one instance of the traces class. 
    35  
    36         Technically this is a singleton. 
    37         """ 
    38  
    39         if not '__instance' in cls.__dict__: 
    40             cls.__instance = object.__new__(cls) 
    41  
    42         return cls.__instance 
    4336 
    4437    def __init__(self, *args, **kwargs): 
Note: See TracChangeset for help on using the changeset viewer.