Changeset 116


Ignore:
Timestamp:
04/30/09 13:31:55 (11 years ago)
Author:
marcus
Message:
  • refactoring for use as later python module
Location:
SHX/trunk/src
Files:
3 added
3 deleted
1 edited
12 copied
6 moved

Legend:

Unmodified
Added
Removed
  • SHX/trunk/src/SeismicHandler/SHX

    r115 r116  
    11#! /bin/bash 
    22 
    3 export PYTHONSTARTUP="startup.py" 
     3path=`dirname $0` 
     4export PYTHONSTARTUP="${path}/startup.py" 
    45python 
  • SHX/trunk/src/SeismicHandler/config/options.py

    r111 r116  
    66import os 
    77 
    8 from core.log import logging 
     8from SeismicHandler.core.log import logging 
    99logger = logging.getLogger("config.options") 
    1010 
  • SHX/trunk/src/SeismicHandler/core/__init__.py

    r115 r116  
    11# -*- coding: utf-8 -*- 
    22 
    3 from core.history import History as history 
    4 from core import codes 
     3from SeismicHandler.core.history import History as history 
     4from SeismicHandler.core import codes 
  • SHX/trunk/src/SeismicHandler/core/commands/__init__.py

    r113 r116  
    66import fnmatch 
    77 
    8 from core.log import logging 
    9 from config.options import options, environment 
     8from SeismicHandler.core.log import logging 
     9from SeismicHandler.config.options import options, environment 
    1010 
    11 class commandClass(object): 
     11class Commands(object): 
    1212    """ read available commands """ 
    1313     
     
    5454 
    5555# shift classes into local namespace 
    56 commands = commandClass().commands 
     56commands = Commands().commands 
    5757for cmd in commands: 
    58     locals()[cmd.capitalize()] = commands[cmd] 
     58    locals()[cmd.lower().capitalize()] = commands[cmd] 
     59    del locals()[cmd] 
     60 
     61# clean up     
     62del os, Commands, fnmatch, cmd 
  • SHX/trunk/src/SeismicHandler/core/commands/quit.py

    r115 r116  
    11# -*- coding: utf-8 -*- 
    22 
    3 from core.log import logging 
    4 from core.shlib import shlib 
    5 from core.modules import Command 
     3from SeismicHandler.core.log import logging 
     4from SeismicHandler.core.shlib import shlib 
     5from SeismicHandler.core.modules import Command 
    66 
    77class Quit(Command): 
  • SHX/trunk/src/SeismicHandler/core/log.py

    r111 r116  
    22 
    33import logging 
     4import os 
    45 
    5 class loggingClass(object): 
     6class Logging(object): 
    67    logging = None 
    78     
     
    1819         
    1920        if not self.logging: 
    20             logging.basicConfig(level=logging.DEBUG, 
    21                                 format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', 
    22                                 datefmt='%Y-%m-%d %H:%M:%S', 
    23                                 filename='sh.log', 
    24                                 filemode='w') 
     21            # get temp directory for logfile 
     22            try: 
     23                # mostly set on windows 
     24                dir = os.environ["TEMP"] 
     25            except KeyError: 
     26                # assume linux system  
     27                dir = "/tmp/" 
     28                 
     29            try: 
     30                logging.basicConfig(level=logging.DEBUG, 
     31                                    format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', 
     32                                    datefmt='%Y-%m-%d %H:%M:%S', 
     33                                    filename=os.path.join(dir, 'sh.log'), 
     34                                    filemode='w') 
     35            except IOError: 
     36                logging.warning("Cannot log to disc, path %s not writable! Logging everything to console." % dir) 
     37                 
     38                logging.basicConfig(level=logging.DEBUG, 
     39                                    format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', 
     40                                    datefmt='%Y-%m-%d %H:%M:%S') 
    2541             
    26             # additional to console 
    27             console = logging.StreamHandler() 
    28             console.setLevel(logging.INFO) 
    29             #console.setLevel(logging.DEBUG) 
    30              
    31             # set a format which is simpler for console use 
    32             formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s') 
    33              
    34             # tell the handler to use this format 
    35             console.setFormatter(formatter) 
    36              
    37             # add the handler to the root logger 
    38             logging.getLogger('').addHandler(console) 
     42            else: 
     43                # additional to console 
     44                console = logging.StreamHandler() 
     45                console.setLevel(logging.ERROR) 
     46                #console.setLevel(logging.DEBUG) 
     47                 
     48                # set a format which is simpler for console use 
     49                formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s') 
     50                 
     51                # tell the handler to use this format 
     52                console.setFormatter(formatter) 
     53                 
     54                # add the handler to the root logger 
     55                logging.getLogger('').addHandler(console) 
    3956             
    4057            self.logging = logging 
    4158 
    42 loggingClass() 
     59Logging() 
     60 
     61# clean up 
     62# logging must be kept since it's imported from other modules 
     63del os 
  • SHX/trunk/src/SeismicHandler/core/modules/Command.py

    r113 r116  
    11# -*- coding: utf-8 -*- 
    22 
    3 from core import history, codes 
    4 from core.shlib import shlib 
    5 from core.log import logging 
     3from SeismicHandler.core import history, codes 
     4from SeismicHandler.core.shlib import shlib 
     5from SeismicHandler.core.log import logging 
    66 
    77class Command(object): 
  • SHX/trunk/src/SeismicHandler/core/modules/__init__.py

    r111 r116  
    66import fnmatch 
    77 
    8 from core.log import logging 
     8from SeismicHandler.core.log import logging 
    99#from config.options import options, environment 
    1010 
     
    4848for i in modulesClass().modules: 
    4949    locals()[i] = modulesClass().modules[i] 
     50 
     51# clean up 
     52del os, fnmatch 
  • SHX/trunk/src/SeismicHandler/core/shheaders.py

    r115 r116  
    1 #! /usr/bin/env python 
     1# -*- coding: utf-8 -*- 
    22 
    3 import ctypes as CT 
     3import ctypes as C 
    44 
    55# command parameter structure 
     
    2121""" 
    2222 
    23 class PARAM(CT.Structure): 
     23class PARAM(C.Structure): 
    2424    _fields_ = [ 
    25         ("pno", CT.c_int), 
    26         ("p", CT.c_int), 
    27         ("qno", CT.c_int), 
    28         ("q", CT.c_int), 
    29         ("query", CT.c_int), 
    30         ("qchecked", CT.c_int), 
     25        ("pno", C.c_int), 
     26        ("p", (C.c_char * 2049)*16), 
     27        ("qno", C.c_int), 
     28        ("q", (C.c_char * 2049)*5), 
     29        ("query", C.c_int), 
     30        ("qchecked", C.c_int), 
    3131    ] 
  • SHX/trunk/src/SeismicHandler/core/shlib.py

    r113 r116  
    22 
    33""" wrapper for seismic handler c library 
    4     implemented as singleton""" 
     4    implemented as multiton """ 
    55 
    6 import ctypes as CT 
     6import ctypes as C 
    77import sys 
    88import os 
    99 
    10 from core.log import logging 
    11 from config.options import environment 
    12 from core.shheaders import PARAM  
     10from SeismicHandler.core.log import logging 
     11from SeismicHandler.config.options import environment 
     12from SeismicHandler.core.shheaders import PARAM 
    1313 
    1414logger = logging.getLogger("core.shlib") 
    1515     
    1616class shlib(object): 
    17     sh = None 
     17    _instances = {} 
    1818     
    1919    def __new__(type, *args, **kwargs): 
    20         """ ensure single instance """ 
    21         if not '_the_instance' in type.__dict__: 
    22             type._the_instance = object.__new__(type) 
     20        """ ensure single instance with identifier "xyz" """ 
     21        try: 
     22            inst = args[0] 
     23        except IndexError: 
     24            inst = "_standard" 
     25         
     26        if not args[0] in type._instances: 
     27            type._instances[args[0]] = object.__new__(type) 
    2328             
    24         return type._the_instance 
     29        return type._instances[args[0]] 
    2530 
    2631    def __init__(self, *args, **kwargs): 
    27         if not self.sh: 
    28             # load shared lib (once) 
     32        if not "sh" in self.__dict__: 
     33            # load shared lib (once per identifier) 
    2934            try: 
    30                 self.sh = CT.cdll.LoadLibrary(os.path.join(environment["root"], "libshc.so")) 
     35                self.sh = C.cdll.LoadLibrary(os.path.join(environment["root"], "libshc.so")) 
    3136            except Exception, e: 
    3237                logger.critical(("error loading shared lib", e)) 
    3338                sys.exit(1) 
     39                 
     40    def call(self, cmd): 
     41        """ call original c lib function to execute SH command in cmd 
     42         
     43            @param cmd: string containing SH command   
     44        """ 
     45        pass 
  • SHX/trunk/src/SeismicHandler/shx.py

    r115 r116  
    99import textwrap 
    1010 
    11 from core.log import logging 
    12 from core.commands import Quit 
    13 from core.shlib import shlib 
    14 from core.codes import VERSION 
     11from SeismicHandler.core.log import logging 
     12from SeismicHandler.core.commands import Quit 
     13from SeismicHandler.core.shlib import shlib 
     14from SeismicHandler.core.codes import VERSION 
    1515 
    1616class shx(): 
  • SHX/trunk/src/SeismicHandler/startup.py

    r115 r116  
    33""" start python shell adopted to seismic handler style """ 
    44 
    5 from core.codes import VERSION 
    6 from core.log import logging 
    7 from core.commands import * 
     5import sys 
     6import os 
     7import textwrap 
     8 
     9# append path to pythonpath 
     10sys.path.append(os.sep.join(os.path.split(__file__)[0].split(os.sep)[:-1])) 
    811 
    912# change prompt style 
    10 import sys 
    1113sys.ps1 = "|sh> " 
    1214sys.ps2 = ".... " 
    13 del sys 
     15 
     16# import Seismic Handler related modules 
     17from SeismicHandler.core.codes import VERSION 
     18from SeismicHandler.core.log import logging 
     19from SeismicHandler.core.commands import * 
     20from SeismicHandler.core.codes import LICENCE, VERSION 
    1421 
    1522# enable history  
     
    2027else: 
    2128    import atexit 
    22     import os 
    2329    import rlcompleter 
    2430 
     
    3541    atexit.register(save_history) 
    3642 
    37     del os, atexit, readline, rlcompleter, save_history, historyPath 
     43    del atexit, readline, rlcompleter, save_history, historyPath 
    3844 
    39 from core.codes import LICENCE, VERSION 
    40 import textwrap 
    41  
     45# print greeting and licence 
    4246print "\n", textwrap.fill(textwrap.dedent(LICENCE % VERSION), 75).strip(), "\n" 
    4347 
    44 del textwrap 
     48# clean up 
     49del textwrap, sys, os 
Note: See TracChangeset for help on using the changeset viewer.