Changeset 224


Ignore:
Timestamp:
07/14/2010 05:26:23 PM (12 years ago)
Author:
marcus
Message:

Parser work including more doctests.

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

Legend:

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

    r173 r224  
     1import os.path 
    12# -*- coding: utf-8 -*- 
    23 
     
    1516#    You should have received a copy of the GNU Lesser General Public License 
    1617#    along with SHX.  If not, see <http://www.gnu.org/licenses/>. 
     18 
     19# automatically import commands 
     20import os 
     21 
     22path = os.path.split(__file__) 
     23list = {} 
     24 
     25# XXX This should respect a user defined command directory. 
     26for root, dirs, files in os.walk(path[0]): 
     27    for f in files: 
     28        if not f.endswith(".py"): 
     29            continue 
     30        if f.startswith("__"): 
     31            continue 
     32 
     33        a = __import__(f[:-3], globals(), locals(), ["SeismicHandler.commands"]) 
     34        for k in a.provides: 
     35            list[k] = a.provides[k] 
     36 
     37    # only visit current directory 
     38    break 
     39 
     40del os 
  • SHX/trunk/src/SeismicHandler/commands/echo.py

    r221 r224  
    1919from SeismicHandler.core import Settings 
    2020 
     21provides = {"echo": "echo"} 
    2122class echo(BaseCommand): 
    2223    """ 
  • SHX/trunk/src/SeismicHandler/commands/echo_ch.py

    r221 r224  
    1919from SeismicHandler.core import Settings 
    2020 
     21provides = {"echo_ch": "echo_ch"} 
    2122class echo_ch(BaseCommand): 
    2223    """ 
  • SHX/trunk/src/SeismicHandler/core/parser.py

    r222 r224  
    3131import inspect 
    3232from SeismicHandler.core import Settings 
     33import SeismicHandler.commands as commands 
    3334 
    3435class parse(object): 
     
    4647    - class attribute "parsed" (dictionary) 
    4748    - class attribute with "shx_" prefix (e.g. shx_input) 
     49 
     50    The parser analyses the command string: 
     51    >>> cmd = 'COMMAND /SWITCH1 PARAMETER1 /SWITCH2=4 PARAMETER2' 
     52    >>> x = parse(cmd).parsed 
     53 
     54    The parsed command name is always lower case. It's used for comparision with 
     55    the "provides" attribute of the command classes. 
     56    >>> x["shx_command"] 
     57    'command' 
     58 
     59    Switches may occur at any position, but are commonly placed following the 
     60    command name: 
     61    >>> sorted(x["shx_qualifier"].keys()) 
     62    ['SWITCH1', 'SWITCH2'] 
     63    >>> x["shx_qualifier"]["SWITCH1"] 
     64    True 
     65    >>> x["shx_qualifier"]["SWITCH2"] 
     66    '4' 
     67 
     68    Parameter are returned as list in the order of occurrence: 
     69    >>> x["shx_parameter"] 
     70    ['PARAMETER1', 'PARAMETER2'] 
     71 
     72    One new feature is that the algorithm guesses a file name (slashes normally 
     73    indicate qualifiers). This only works, if the switches follow directly after 
     74    the command name! 
     75    >>> cmd = 'DELAY+SUM/BLA/FOO=1 BAR /tmp/test /SW FOO' 
     76    >>> x = parse(cmd).parsed 
     77    >>> x["shx_parameter"] 
     78    ['BAR', 'FOO'] 
     79    >>> x["shx_qualifier"]["FOO"] 
     80    '1' 
     81     
     82    Please not that the "suspected" file name is also present as switch: 
     83    >>> sorted(x["shx_qualifier"].keys()) 
     84    ['BLA', 'FOO', 'SW', 'TEST', 'TMP'] 
     85    >>> x["shx_suspectedFilename"] 
     86    '/TMP/TEST' 
     87 
     88    If more than one "possible filename is given, only the first one is 
     89    recognized. All following are only processed as qualifiers. 
     90    >>> x = parse('COMMAND /tmp/x /tmp/y').parsed 
     91    >>> x["shx_suspectedFilename"] 
     92    '/TMP/X' 
     93 
     94    The semicolon is an alternative separator that is mostly used to skip 
     95    parameters: 
     96    >>> x = parse("ECHO;;2 3 4 ;; 6 7").parsed 
     97    >>> x["shx_parameter"] 
     98    ['', '2', '3', '4', '', '6', '7'] 
     99     
     100    >>> x = parse('ECHO;;;;foo;bar;;').parsed 
     101    >>> x["shx_parameter"] 
     102    ['', '', '', 'FOO', 'BAR', ''] 
    48103    """ 
    49104 
     
    137192 
    138193    All other commands are promoted to their handler or treated as new script. 
    139     """ 
    140  
    141     def __init__(self, stream, symbols): 
     194 
     195    For testing purposes we use a stream and fill it with commands. Also a 
     196    symbol set is needed. To echo the commands, the global "Echo" switch must 
     197    be turned on. 
     198    >>> from StringIO import StringIO 
     199    >>> strm = StringIO("echo line1\\necho line2\\n") 
     200    >>> symb = symbol() 
     201    >>> Settings.Echo = True 
     202    >>> x = script(strm, symb) 
     203    >>> x.run() 
     204    echo line1 
     205    echo line2 
     206     
     207    It's possible to add more commands at runtime. The script will continue  
     208    there. 
     209    >>> x.feed(StringIO("echo line3\\n")) 
     210    >>> x.run() 
     211    echo line3 
     212    """ 
     213 
     214    def __init__(self, input, symbols, parameters=None): 
    142215        """ 
    143216        Read in stream, skip empty lines and comments. Remember GOTO targets. 
     
    154227        self.symbols = symbols 
    155228 
    156         # cache self-handled commands 
     229        try: 
     230            if hasattr(input, "read"): 
     231                stream = input 
     232            elif type(input) == str: 
     233                stream = open("input", "r") 
     234            _ = stream 
     235        except: 
     236            # cannot open 
     237            raise Exception("Input not readable!") 
     238 
     239        # Cache self-handled commands. These methods begin with "command". 
    157240        commands = [] 
    158241        for i in inspect.getmembers(self, lambda x: inspect.ismethod(x)): 
     
    162245        self.commands = commands 
    163246 
    164         self._feed(stream) 
    165  
    166     def _feed(self, stream): 
     247        self.feed(stream) 
     248 
     249    def feed(self, stream): 
    167250        """ 
    168251        Method for populating command list. 
     
    201284        while True: 
    202285            try: 
    203                 print self.next(), "<-" 
     286                cmd = self.next() 
     287 
     288                if Settings.Echo: 
     289                    print cmd 
     290 
     291                cmd = parse(cmd).parsed 
     292 
     293                # Start script if unknown command found: 
     294                if not cmd["shx_command"] in commands.list: 
     295                    symb = symbol() 
     296                    try: 
     297                        ns = script(cmd["shx_command"], symb) 
     298                        ns.run() 
     299                    except: 
     300                         # XXX behaviour must respect global settings 
     301                        raise Exception("Cannot run script '%s'!" % cmd["shx_command"]) 
    204302            except StopIteration: 
    205303                break 
     
    355453    import doctest 
    356454    doctest.testmod(exclude_empty=True) 
    357      
    358     quit() 
    359  
    360     from StringIO import StringIO 
    361     test = StringIO("echo line1\necho line2\n") 
    362     s = symbol() 
    363  
    364     x = script(test, s) 
    365     x.run() 
    366     x._feed(StringIO("echo line3\n")) 
    367     x.run() 
    368  
    369     print parse('DELAY+SUM/BLA/FOO=1 BAR /tmp/test /SW FOO').parsed 
    370     print parse('ECHO;;;;bla;blub;;').parsed 
    371     print parse('LULU /tmp/x /tmp/y').parsed 
    372     p = parse("param;;2 3 4 5 6 7") 
    373     print p.input_conv 
  • SHX/trunk/src/SeismicHandler/core/runtime.py

    r222 r224  
    3131    # convert to upper case 
    3232    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 
    3341 
    3442    # dictionary of global variables 
Note: See TracChangeset for help on using the changeset viewer.