Changeset 230


Ignore:
Timestamp:
07/20/2010 03:59:07 PM (13 years ago)
Author:
marcus
Message:
  • parser work (solves tickets #15,16,22)
  • new testcases
  • some refactoring
Location:
SHX/trunk/src/SeismicHandler
Files:
3 added
8 edited

Legend:

Unmodified
Added
Removed
  • SHX/trunk/src/SeismicHandler/commands/__init__.py

    r224 r230  
    2020import os 
    2121 
    22 path = os.path.split(__file__) 
     22path = os.path.split(__file__)[0] 
    2323list = {} 
    2424 
    2525# XXX This should respect a user defined command directory. 
    26 for root, dirs, files in os.walk(path[0]): 
     26for root, dirs, files in os.walk(path): 
    2727    for f in files: 
    2828        if not f.endswith(".py"): 
     
    3232 
    3333        a = __import__(f[:-3], globals(), locals(), ["SeismicHandler.commands"]) 
    34         for k in a.provides: 
    35             list[k] = a.provides[k] 
     34 
     35        for k in a.provides:     
     36            list[k] = getattr(a, a.provides[k]) 
    3637 
    3738    # only visit current directory 
  • SHX/trunk/src/SeismicHandler/commands/echo.py

    r224 r230  
    7373    'foo bar qux\\n' 
    7474    >>> Settings.echo_channel = oldout   # restore output channel 
     75 
     76    URI:http://www.seismic-handler.org/portal/wiki/ShEcho 
    7577    """ 
    7678 
  • SHX/trunk/src/SeismicHandler/commands/echo_ch.py

    r224 r230  
    6161       ECHO_CH                ! closes a previously opened ECHO-file and 
    6262                              ! switches output back to screen 
    63 """ 
     63 
     64    URI:http://www.seismic-handler.org/portal/wiki/ShEchoCh 
     65    """ 
    6466 
    6567    known_qualifiers = [ 
  • SHX/trunk/src/SeismicHandler/core/__init__.py

    r221 r230  
    2222from SeismicHandler.core.history import History as history 
    2323from SeismicHandler.core import codes 
    24  
     24from SeismicHandler.core.log import Logging 
    2525from SeismicHandler.core.runtime import runtime 
    2626 
  • SHX/trunk/src/SeismicHandler/core/parser.py

    r224 r230  
    3030import re 
    3131import inspect 
    32 from SeismicHandler.core import Settings 
     32from SeismicHandler.core import Settings, Logging 
    3333import SeismicHandler.commands as commands 
    3434 
     
    7979    >>> x["shx_qualifier"]["FOO"] 
    8080    '1' 
    81      
    82     Please not that the "suspected" file name is also present as switch: 
     81 
     82    Please note that the "suspected" file name is also present as switch: 
    8383    >>> sorted(x["shx_qualifier"].keys()) 
    8484    ['BLA', 'FOO', 'SW', 'TEST', 'TMP'] 
     
    9797    >>> x["shx_parameter"] 
    9898    ['', '2', '3', '4', '', '6', '7'] 
    99      
     99 
    100100    >>> x = parse('ECHO;;;;foo;bar;;').parsed 
    101101    >>> x["shx_parameter"] 
     
    111111        self.input = input 
    112112 
    113         if Settings.CapConv: 
     113        if Settings.swCapconv: 
    114114            input = input.upper() 
    115115 
     
    199199    >>> strm = StringIO("echo line1\\necho line2\\n") 
    200200    >>> symb = symbol() 
    201     >>> Settings.Echo = True 
     201    >>> Settings.swEcho = True 
    202202    >>> x = script(strm, symb) 
    203203    >>> x.run() 
    204204    echo line1 
    205205    echo line2 
    206      
    207     It's possible to add more commands at runtime. The script will continue  
     206 
     207    It's possible to add more commands at runtime. The script will continue 
    208208    there. 
    209209    >>> x.feed(StringIO("echo line3\\n")) 
     
    286286                cmd = self.next() 
    287287 
    288                 if Settings.Echo: 
     288                if Settings.swEcho: 
    289289                    print cmd 
    290290 
    291291                cmd = parse(cmd).parsed 
    292292 
    293                 # Start script if unknown command found: 
    294                 if not cmd["shx_command"] in commands.list: 
     293                 
     294                # Execute command... 
     295                if cmd["shx_command"] in commands.list: 
     296                    commands.list[cmd["shx_command"]](*cmd["shx_parameter"], **cmd["shx_qualifier"]) 
     297                     
     298                # .. or start script. 
     299                else: 
    295300                    symb = symbol() 
    296301                    try: 
    297302                        ns = script(cmd["shx_command"], symb) 
    298303                        ns.run() 
    299                     except: 
    300                          # XXX behaviour must respect global settings 
    301                         raise Exception("Cannot run script '%s'!" % cmd["shx_command"]) 
     304                    except Exception, e: 
     305                        msg = "Cannot run script '%s'!" % cmd["shx_command"] 
     306 
     307                        # Respect global settings: 
     308                        if Settings.swSherrstop: 
     309                            import sys 
     310                            print >> sys.stderr, msg 
     311                            quit() 
     312 
     313                        if Settings.swCmderrstop: 
     314                            raise Exception(msg) 
     315 
     316                        if not Settings.swNoerrmsg: 
     317                            import warnings 
     318                            warnings.warn(msg) 
     319 
    302320            except StopIteration: 
    303321                break 
     
    370388 
    371389class translate(object): 
     390    """ 
     391    Translate variables in command. 
     392    """ 
    372393    def __init__(self): 
    373394        pass 
     
    422443    def __setattr__(self, name, value): 
    423444        name = name.upper() 
    424          
     445 
    425446        self.__dict__[name] = value 
    426447 
     
    447468    def setGlobal(self, name, value): 
    448469        name = name.upper() 
    449          
     470 
    450471        self.__dict__["__globals"][name] = value 
    451472 
  • SHX/trunk/src/SeismicHandler/core/runtime.py

    r224 r230  
    2929    __echo_channel = sys.stdout 
    3030 
    31     # convert to upper case 
    32     CapConv = True 
    33     # echo command before execution 
    34     Echo = False 
    35     # verify command 
    36     Verify = False 
    37     # cancel script on error 
    38     CmdErrStop = True 
    39     # exit SH on error 
    40     ShErrStop = False 
     31    # global switches (see http://www.seismic-handler.org/portal/wiki/ShSwitch) 
     32    _Switches = { 
     33        # convert to upper case 
     34        "Capconv": True, 
     35        # echo command before execution 
     36        "Echo": False, 
     37        # verify command 
     38        "Verify": False, 
     39        # cancel script on error 
     40        "Cmderrstop": True, 
     41        # exit SH on error 
     42        "Sherrstop": False, 
     43        # no error message at all 
     44        "Noerrmsg": False, 
     45        # verbose 
     46        "Chatty": True, 
     47        # indicate startup file 
     48        "Startup": False, 
     49    } 
    4150 
    4251    # dictionary of global variables 
     
    5665 
    5766        return cls.__instance 
     67 
     68    def __getattr__(self, name): 
     69        if name.startswith("sw"): 
     70            return getattr(self, "_Switches")[name[2:]] 
     71        else: 
     72#            return self.__class__.__dict__[name] 
     73            return object.__getattribute__(self, name) 
     74 
     75    def __setattr__(self, name, value): 
     76        if name.startswith("sw"): 
     77            if not name[2:] in self.__class__.__dict__["_Switches"].keys(): 
     78                raise Exception("Unknown switch!") 
     79             
     80            self.__class__.__dict__["_Switches"][name[2:]] = value 
     81        elif name == "echo_channel": 
     82            object.__setattr__(self, "echo_channel", value) 
     83        else: 
     84            self.__dict__[name] = value 
    5885 
    5986    @Property 
     
    106133if __name__ == "__main__": 
    107134    Settings = runtime() 
    108      
     135 
    109136    print >> Settings.echo_channel, Settings.echo_channel 
    110137    Settings.echo_channel = "TEST" 
  • SHX/trunk/src/SeismicHandler/tests/data

    • Property svn:ignore set to
      .SCRIPT1.SHC.swp
  • SHX/trunk/src/SeismicHandler/tests/test_base.py

    r175 r230  
    2222""" 
    2323 
    24 from SeismicHandler.core.base.command import BaseCommand 
     24from SeismicHandler.core.command import BaseCommand 
    2525import unittest 
    2626import sys 
     
    3636    def testBaseCommand1(self): 
    3737        """ 
    38         Check for proper test of initialization. 
    39         """ 
    40  
    41         # assertRaise needs a callable - not just a class init 
    42         try: 
    43             cmd = BaseCommand() 
    44         except NotImplementedError: 
    45             pass 
    46         else: 
    47             self.fail("check failed") 
    48  
    49     def testBaseCommand2(self): 
    50         """ 
    5138        Check dummy command 
    5239        """ 
     
    5643            test help text 
    5744            """ 
     45            known_qualifiers = [] 
     46            expectFilename = False 
    5847            def run(self): 
    5948                self.x = "test" 
Note: See TracChangeset for help on using the changeset viewer.