Changeset 268


Ignore:
Timestamp:
09/14/2010 08:22:39 PM (12 years ago)
Author:
marcus
Message:

working on configuration parser

Location:
SHX/trunk/src/SeismicHandler
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • SHX/trunk/src/SeismicHandler

    • Property svn:ignore
      •  

        old new  
        33.pydevproject 
        44.shx.cfg.swp 
         5.shx.conf.swp 
  • SHX/trunk/src/SeismicHandler/modules/runtime.py

    r258 r268  
    2121E.g. Echo Channel, Cap Conversion, ... 
    2222""" 
    23 import os.path 
    2423 
    2524import sys 
    2625import os 
     26import ConfigParser 
    2727from SeismicHandler.basics import Property, Singleton, AttribDict 
    2828 
     
    9090        """ 
    9191 
    92         dirs = [ 
    93             "/etc", 
    94             # some weird fall-back if $HOME does not exists 
    95             os.getenv("HOME", "/etc"), 
    96             os.path.join(os.path.split(__file__)[0], ".."), 
    97         ] 
    98  
    99         for d in dirs: 
    100             if not os.path.exists(os.path.join(d, "shx.conf")): 
    101                 continue 
    102  
    103             print "found in", d 
    104  
     92        dirs = { 
     93            "SHX": os.path.abspath(__file__).rsplit(os.path.sep, 2)[0], 
     94            "ETC": "/etc/SeismicHandler", 
     95        } 
     96 
     97        dir_home = os.getenv("HOME", None) 
     98 
     99        if dir_home: 
     100            dirs["PRIVATE"] = os.path.join(dir_home, ".shx") 
     101 
     102        for d in dirs.values(): 
     103            src = os.path.join(d, "shx.conf") 
     104            if os.path.exists(src): 
     105                break 
     106 
     107        config = ConfigParser.SafeConfigParser() 
     108        if not config.read(src): 
     109            raise Exception("configuration file not found!") 
     110 
     111        conf = AttribDict() 
     112        for s in config.sections(): 
     113            conf[s] = AttribDict() 
     114            for i in config.items(s): 
     115                conf[s][i[0]] = self.__handlePlaceholders(map(str.strip, \ 
     116                                                         i[1].split(",")), dirs) 
     117 
     118            # check for include statement 
     119            if "include" in conf[s]: 
     120 
     121                # include sub configfile if requested 
     122                extra = ConfigParser.SafeConfigParser() 
     123                for e in conf[s]["include"]: 
     124                    extra.read(e) 
     125 
     126                    # only include current section 
     127                    if not extra.has_section(s): 
     128                        break 
     129 
     130                    for ie in extra.items(s): 
     131                        conf[s][ie[0]] = self.__handlePlaceholders(ie[1].split(","), dirs) 
     132 
     133        print conf 
     134 
     135    def __handlePlaceholders(self, values, dirs): 
     136        """ 
     137        Replace placeholders in config file. 
     138        """ 
     139 
     140        ret = [] 
     141        for v in values: 
     142            if v.startswith("["): 
     143                # replacement requested 
     144                for d in dirs.keys(): 
     145                    if v.startswith("[%s]" % d): 
     146                        # do replacement 
     147                        ret.append(v.replace("[%s]" % d, dirs[d])) 
     148 
     149            else: 
     150                ret.append(v) 
     151 
     152        return ret 
    105153 
    106154    def __getattr__(self, name): 
  • SHX/trunk/src/SeismicHandler/modules/traces.py

    r258 r268  
    2626class Traces(object): 
    2727    """ 
    28     Supply access for two stream of traces. One is used for plotting, the other 
     28    Supply access for two streams of traces. One is used for plotting, the other 
    2929    holds hidden traces. This will be enhanced later, but for compatibility 
    3030    reasons to existing command procedures it's done that ways first. 
  • SHX/trunk/src/SeismicHandler/shx.conf

    r258 r268  
    22# Main configuration file for Seismic Handler 
    33# 
    4 # The string [SHX] is replaced by installation directory, 
    5 # [PRIVATE] is replaced by $HOME/.shx/ 
     4# Following replacements are performed: 
     5# 
     6# [SHX] is replaced by installation directory, 
     7# [PRIVATE] is replaced by $HOME/.shx, 
     8# [ETC] is replaced by /etc/SeismicHandler 
    69# 
    710 
     
    912scripts = [SHX]/scripts/,[PRIVATE]/scripts/ 
    1013filters = [SHX]/filters/,[PRIVATE]/filters/ 
    11 stations = [SHX]/stations/,[PRIVATE]/stations/ 
     14stations = [SHX]/stations/,[PRIVATE]/stations/,/mnt/stations/ 
    1215 
    1316[miniseed] 
     
    1518 
    1619[graphics] 
    17 include = [PRIVATE]/graphics.cfg 
     20height = 768 
     21width = 1024 
     22 
     23# It is possible to include another configuration file. It has to 
     24# contain the section name to which information should be attached. 
     25# Any include statement will be treated last regardless of it's position 
     26# in the section! 
     27include = [PRIVATE]/graphics.cfg, [PRIVATE]/graphics2.cfg 
     28# If the included configuration file is not found, the error will be 
     29# passed silently. 
Note: See TracChangeset for help on using the changeset viewer.