Changeset 233


Ignore:
Timestamp:
07/21/2010 05:19:53 PM (13 years ago)
Author:
marcus
Message:

Parser work. Translation of system variables (not all covered yet) and symbols work now.

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

Legend:

Unmodified
Added
Removed
  • SHX/trunk/src/SeismicHandler/core/parser.py

    r232 r233  
    111111        self.input = input 
    112112 
     113        converted = False 
    113114        if Settings.swCapconv: 
    114             input = input.upper() 
     115            # If a command starts with @, no conversion to upper case is 
     116            # performed but the indicator char is removed. 
     117            if input.startswith("@"): 
     118                input = input[1:] 
     119            else: 
     120                input = input.upper() 
     121                converted = True 
    115122 
    116123        c = re.compile(self.re_cmd) 
     
    162169                "shx_input_conv": input, 
    163170                "shx_command": cmd.lower(), # command always in lower case 
     171                "shx_converted": converted, # indicates cap conversion 
    164172                "shx_parameter": parameter, 
    165173                "shx_qualifiers": qualifiers, 
     
    227235        self.symbols = symbols 
    228236 
     237        # If a script is initially called there might be parameters and 
     238        # also qualifiers. 
     239        if parameters: 
     240            self.parameters = translate(parameters, self) 
     241 
    229242        try: 
    230243            if hasattr(input, "read"): 
     
    293306                # Execute command... 
    294307                if cmd["shx_command"] in commands.list: 
     308                    # translate variables 
     309                    _ = translate(cmd, self) 
     310 
     311                    if Settings.swVerify: 
     312                        print cmd["shx_translated"] 
     313                     
    295314                    # also supply recent symbolset 
    296315                    commands.list[cmd["shx_command"]](shx_symbols=self.symbols, \ 
     
    301320                    symb = symbol() 
    302321                    try: 
    303                         ns = script(cmd["shx_command"], symb) 
     322                        ns = script(cmd["shx_command"], symb, parameters=cmd) 
    304323                        ns.run() 
    305324                    except Exception, e: 
     
    390409class translate(object): 
    391410    """ 
    392     Translate variables in command. 
    393     """ 
    394     def __init__(self): 
    395         pass 
     411    Translate variables in command. If a script class is used as second 
     412    parameter, it's symbol set will be used (e.g. for global symbols). 
     413 
     414    There are five basic types of variables: 
     415    1. user-defined symbols start with a quote: "foo 
     416    2. system variables start with a dollar sign: $DSPTRCS 
     417    3. trace variables start with a caret: ^delta(3) 
     418    4. passed options to command procedures start with a hash: #1 
     419    5. data from file access start with a percent: %filename(1) 
     420 
     421    After translation all parameter and qualifiers are replaced. 
     422     
     423    If the global option "Verify" is set the translated command string is saved 
     424    into "shx_translated". This string is rebuild from the translated parts, 
     425    so qualifiers may appear not in original order. 
     426 
     427    In order to test this class, we define a dummy command object. 
     428    >>> Settings.swVerify = True 
     429    >>> cmd = { 
     430    ...    'shx_command': 'echo', 
     431    ...    'shx_converted': True, 
     432    ...    'shx_parameter': ['$PI', '$EXCLAMATION'], 
     433    ...    'shx_qualifiers': {'FOO': '$DOLLAR', 'BAR': True}, 
     434    ... } 
     435    >>> _ = translate(cmd) 
     436    >>> cmd['shx_translated'] 
     437    'ECHO 3.1415926535897931 ! /FOO=$ /BAR' 
     438    """ 
     439 
     440    system = { 
     441        "DOLLAR": "$", 
     442        "PI": "3.1415926535897931", 
     443        "SLASH": "/", 
     444        "HAT": "^", 
     445        "EXCLAMATION": "!", 
     446 
     447        # XXX todo 
     448        "DSPTRCS": lambda: None, 
     449        "TOTTRCS": lambda: None, 
     450    } 
     451 
     452    def __init__(self, cmd, script=None): 
     453        self.script = script 
     454         
     455        # translate parameters 
     456        for i, p in enumerate(cmd["shx_parameter"]): 
     457            if p[0] not in '"$^#%': 
     458                continue 
     459 
     460            id = p[0] 
     461            # system 
     462            if id == "$": 
     463                cmd["shx_parameter"][i] = self.handleSystem(p[1:]) 
     464            # symbols 
     465            elif id == '"': 
     466                cmd["shx_parameter"][i] = self.handleSymbol(p[1:]) 
     467            # trace XXX 
     468            elif id == '^': 
     469                cmd["shx_parameter"][i] = self.handleTrace(p[1:]) 
     470            # options at startup XXX 
     471            elif id == '#': 
     472                cmd["shx_parameter"][i] = self.handleOption(p[1:]) 
     473            # file XXX 
     474            elif id == '%': 
     475                cmd["shx_parameter"][i] = self.handleFile(p[1:]) 
     476 
     477        # translate qualifiers 
     478        for q in cmd["shx_qualifiers"]: 
     479            p = cmd["shx_qualifiers"][q] 
     480 
     481            # Skip qualifiers that act as switches. 
     482            if type(p) == bool: 
     483                continue 
     484 
     485            id = p[0] 
     486 
     487            if id == "$": 
     488                cmd["shx_qualifiers"][q] = self.handleSystem(p[1:]) 
     489            elif id == '"': 
     490                cmd["shx_qualifiers"][q] = self.handleSymbol(p[1:]) 
     491            elif id == '^': 
     492                cmd["shx_qualifiers"][q] = self.handleTrace(p[1:]) 
     493            elif id == '#': 
     494                cmd["shx_qualifiers"][q] = self.handleOption(p[1:]) 
     495            elif id == '%': 
     496                cmd["shx_qualifiers"][q] = self.handleFile(p[1:]) 
     497 
     498        # Actually this is only for debugging purposes. 
     499        if not Settings.swVerify: 
     500            return 
     501         
     502        qual = [] 
     503        for q in cmd["shx_qualifiers"]: 
     504            if type(cmd["shx_qualifiers"][q]) == bool: 
     505                qual.append(q) 
     506            else: 
     507                qual.append("%s=%s" % (q, cmd["shx_qualifiers"][q])) 
     508             
     509        cmd["shx_translated"] = " ".join([ 
     510            cmd["shx_converted"] and cmd["shx_command"].upper() or cmd["shx_command"], 
     511            " ".join(cmd["shx_parameter"]), 
     512            len(qual) and "/" + " /".join(qual) or "", 
     513        ]) 
     514 
     515    def handleSystem(self, name): 
     516        try: 
     517            x = self.system[name.upper()] 
     518        except KeyError: 
     519            raise NameError("System variable '%s' not found!" % name) 
     520 
     521        if callable(x): 
     522            return x() 
     523        else: 
     524            return x 
     525 
     526    def handleSymbol(self, name): 
     527        try: 
     528            return getattr(self.script.symbols, name) 
     529        except: 
     530            raise NameError("Symbol '%s' not found!" % name) 
     531 
     532    def handleTrace(self, name): 
     533        raise NotImplementedError 
     534 
     535    def handleOption(self, name): 
     536        raise NotImplementedError 
     537 
     538    def handleFile(self, name): 
     539        raise NotImplementedError 
    396540 
    397541class symbol(object): 
  • SHX/trunk/src/SeismicHandler/tests/data/SCRIPT1.SHC

    r232 r233  
    44! 
    55 
    6 switch echo on 
    7 switch echo off 
     6sdef/global bar 42 
     7echo $pi 
    88switch capconv off 
    9 sdef foo 3 
     9sdef foo 23 
     10echo "foo 
    1011switch capconv on 
    1112sdel foo 
     
    1415echo test2 
    1516echo_ch 
     17@switch echo on 
     18switch echo off 
     19echo "bar 
Note: See TracChangeset for help on using the changeset viewer.