Changeset 236
- Timestamp:
- 07/27/2010 05:08:56 PM (12 years ago)
- Location:
- SHX/trunk/src/SeismicHandler
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
SHX/trunk/src/SeismicHandler/core/parser.py
r234 r236 28 28 """ 29 29 30 import os 30 31 import re 31 32 import inspect … … 220 221 """ 221 222 222 def __init__(self, input , symbols, parameters=None):223 def __init__(self, inputdata, symbols, parameters=None, **kwargs): 223 224 """ 224 225 Read in stream, skip empty lines and comments. Remember GOTO targets. … … 240 241 self.parameters = translate(parameters, self) 241 242 242 try: 243 if hasattr(input, "read"): 244 stream = input 245 elif type(input) == str: 246 stream = open("input", "r") 243 if "searchpath" in kwargs: 244 self.searchpath = kwargs["searchpath"] 245 else: 246 # XXX use default search path from configuration 247 self.searchpath = [""] 248 249 try: 250 if hasattr(inputdata, "read"): 251 stream = inputdata 252 elif type(inputdata) == str: 253 if not inputdata.endswith(".SHC"): 254 inputdata = inputdata + ".SHC" 255 256 if Settings.swCapconv: 257 inputdata = inputdata.upper() 258 259 # look for file in search path 260 for folder in self.searchpath: 261 try: 262 stream = open(os.path.join(folder, inputdata), "r") 263 except IOError: 264 pass 265 else: 266 break 267 247 268 _ = stream 248 269 except: … … 256 277 commands.append(i[0][7:].lower()) 257 278 258 self. commands = commands279 self.internal_commands = commands 259 280 260 281 self.feed(stream) … … 304 325 cmd = parse(cmd).parsed 305 326 306 # Execute command... 307 if cmd["shx_command"] in commands.list: 327 # Check for internal command ... 328 if cmd["shx_command"] in self.internal_commands: 329 try: 330 x = getattr(self, "command"+cmd["shx_command"].capitalize()) 331 x(*cmd["shx_parameter"], **cmd["shx_qualifiers"]) 332 except Exception, e: 333 print e 334 335 # or execute command... 336 elif cmd["shx_command"] in commands.list: 308 337 # translate variables 309 338 _ = translate(cmd, self) … … 320 349 symb = symbol() 321 350 try: 322 ns = script(cmd["shx_command"], symb, parameters=cmd) 351 ns = script( 352 cmd["shx_command"], 353 symb, 354 parameters=cmd, 355 searchpath=self.searchpath, 356 ) 323 357 ns.run() 324 except Exception , e:358 except Exception: 325 359 msg = "Cannot run script '%s'!" % cmd["shx_command"] 326 360 … … 359 393 self.pointer -= 1 360 394 raise StopIteration 395 396 def commandReturn(self): 397 # jump to end of script 398 self.pointer = len(self.content) 361 399 362 400 def commandGoto(self, target): … … 451 489 452 490 # XXX todo 453 "DSPTRCS": lambda:None,454 "TOTTRCS": lambda:None,455 "STATUS": lambda:None,456 "SYSTIME": lambda:None,457 "VERSION": lambda:None,458 "DSP_X": lambda:None,459 "DSP_Y": lambda:None,460 "DSP_W": lambda:None,461 "DSP_H": lambda:None,462 "DSP_XMAX": lambda:None,463 "DSP_YMAX": lambda:None,464 "TITLESTYLE": lambda:None,465 "TRCINFOSTYLE": lambda:None,466 "ZEROTRCSTYLE": lambda:None,467 "TIMEAXISSTYLE": lambda:None,468 "MARKSTYLE": lambda:None,469 "PMSTYLE": lambda:None,470 "SH_ID": lambda:None,491 "DSPTRCS": None, 492 "TOTTRCS": None, 493 "STATUS": None, 494 "SYSTIME": None, 495 "VERSION": None, 496 "DSP_X": None, 497 "DSP_Y": None, 498 "DSP_W": None, 499 "DSP_H": None, 500 "DSP_XMAX": None, 501 "DSP_YMAX": None, 502 "TITLESTYLE": None, 503 "TRCINFOSTYLE": None, 504 "ZEROTRCSTYLE": None, 505 "TIMEAXISSTYLE": None, 506 "MARKSTYLE": None, 507 "PMSTYLE": None, 508 "SH_ID": None, 471 509 472 510 # special treatment necessary 473 511 # syntax is HEXCHAR3B 474 "HEXCHAR": lambda:None,512 "HEXCHAR": None, 475 513 476 514 # no docs … … 554 592 raise ValueError("Invalid hexadecimal value: %s" % name[-2:]) 555 593 594 if x == None: 595 raise NotImplementedError 596 556 597 if callable(x): 557 598 return x() -
SHX/trunk/src/SeismicHandler/tests/data/SCRIPT1.SHC
r234 r236 4 4 ! 5 5 6 sdef/global bar 42 7 echo $pi 8 switch capconv off 9 sdef foo 23 10 echo "foo 11 switch capconv on 12 sdel foo 13 echo test /NO_LF 14 echo_ch xxx 15 echo test2 6 echo_ch PARSERTESTCASE 7 sdef /global foo 23 8 script2 bar /qux=1 9 echo script1: "bar 10 ! close output stream 16 11 echo_ch 17 @switch echo on18 switch echo off19 echo "bar20 ! this will echo ";"21 echo $hexchar3b -
SHX/trunk/src/SeismicHandler/tests/test_parser.py
r230 r236 23 23 24 24 import unittest 25 import os 26 from StringIO import StringIO 25 27 26 28 class parserTestCase(unittest.TestCase): … … 32 34 def tearDown(self): 33 35 try: 34 os.unlink(self.outfile )36 os.unlink(self.outfile + ".STX") 35 37 except: 36 38 pass 37 39 40 def addEchoChannel(self, data): 41 data.insert(0, "echo_ch %s" % self.outfile) 42 data.append("echo_ch") 43 44 return data 45 46 def callScript(self, data): 47 data = "\n".join(self.addEchoChannel(data.splitlines())) 48 49 from SeismicHandler.core.parser import script, symbol 50 51 s = symbol() 52 x = script(StringIO(data), s) 53 x.run() 54 55 return self.getResults() 56 57 def getResults(self): 58 return open(self.outfile + ".STX").read() 59 60 def testEcho1(self): 61 script = """ 62 echo foo /NO_CRLF 63 @echo bar 64 """ 65 expect = "FOO bar\n" 66 67 res = self.callScript(script) 68 self.assertEqual(res, expect) 69 70 def testEcho2(self): 71 script = """ 72 echo "foo 73 """ 74 75 self.assertRaises(Exception, self.callScript, script) 76 77 def testSdef(self): 78 script = """ 79 sdef foo 1 80 sdef bar /GLOBAL 2 81 sdef bar 3 82 echo "foo 83 echo "bar 84 sdel bar 85 echo "bar 86 """ 87 expect = "1\n3\n2\n" 88 89 res = self.callScript(script) 90 self.assertEqual(res, expect) 91 92 93 def testSystem(self): 94 script = """ 95 echo $DOLLAR /NO_CRLF 96 echo $HAT /NO_CRLF 97 echo $PI /NO_CRLF 98 echo $HEXCHAR3B /NO_CRLF 99 """ 100 101 expect = "$ ^ 3.1415926535897931 ;" 102 103 res = self.callScript(script) 104 self.assertEqual(res, expect) 105 106 def testSwitch1(self): 107 script = """ 108 switch cmderrstop off 109 foobar 110 switch cmderrstop on 111 foobar 112 """ 113 114 # suppress warning 115 import warnings 116 warnings.simplefilter("ignore", UserWarning) 117 118 self.assertRaises(Exception, self.callScript, script) 119 120 def testSwitch2(self): 121 script = """ 122 switch capconv off 123 echo test 124 switch capconv on 125 echo test 126 """ 127 expect = "test\nTEST\n" 128 129 res = self.callScript(script) 130 self.assertEqual(res, expect) 131 132 def testSwitch3(self): 133 script = """ 134 switch foo on 135 """ 136 137 self.assertRaises(Exception, self.callScript, script) 138 139 def testReturn(self): 140 # the explicit "echo_ch" closes output stream in order to flush contents 141 script = """ 142 echo foo /NO_CRLF 143 echo_ch 144 return 145 echo bar 146 """ 147 148 expect = "FOO" 149 150 res = self.callScript(script) 151 self.assertEqual(res, expect) 152 38 153 def testScript1(self): 154 """ 155 This test loads a script from file which calls another script. 156 157 The scripts are saved in data directory: 158 data/SCRIPT1.SHC 159 data/SCRIPT2.SHC 160 161 Global variables and parameters are passed. 162 """ 163 39 164 try: 40 165 scpt = open("data/SCRIPT1.SHC", "r") … … 45 170 46 171 s = symbol() 47 x = script(scpt, s )172 x = script(scpt, s, searchpath=["data"]) 48 173 x.run() 174 175 expect = "SCRIPT2: 23\nSCRIPT1: 42\n" 176 177 res = self.getResults() 178 self.assertEqual(res, expect) 49 179 50 180 def suite():
Note: See TracChangeset
for help on using the changeset viewer.