@@ -126,7 +126,7 @@ def k_assert(self, command):
126
126
command ['with' ] = self .nextValue ()
127
127
else :
128
128
command ['with' ] = None
129
- self .addCommand (command )
129
+ self .add (command )
130
130
return True
131
131
132
132
def r_assert (self , command ):
@@ -143,7 +143,7 @@ def k_begin(self, command):
143
143
cmd ['keyword' ] = 'end'
144
144
cmd ['debug' ] = True
145
145
cmd ['lino' ] = command ['lino' ]
146
- self .addCommand (cmd )
146
+ self .add (cmd )
147
147
return self .nextPC ()
148
148
else :
149
149
return self .compileFromHere (['end' ])
@@ -441,7 +441,7 @@ def k_get(self, command):
441
441
timeout ['type' ] = 'int'
442
442
timeout ['content' ] = 5
443
443
command ['timeout' ] = timeout
444
- self .addCommand (command )
444
+ self .add (command )
445
445
if self .peek () == 'or' :
446
446
self .nextToken ()
447
447
self .nextToken ()
@@ -453,7 +453,7 @@ def k_get(self, command):
453
453
cmd ['goto' ] = 0
454
454
cmd ['debug' ] = False
455
455
skip = self .getPC ()
456
- self .addCommand (cmd )
456
+ self .add (cmd )
457
457
# Process the 'or'
458
458
self .getCommandAt (get )['or' ] = self .getPC ()
459
459
self .compileOne ()
@@ -535,7 +535,7 @@ def r_gosub(self, command):
535
535
# if <condition> <action> [else <action>]
536
536
def k_if (self , command ):
537
537
command ['condition' ] = self .nextCondition ()
538
- self .addCommand (command )
538
+ self .add (command )
539
539
self .nextToken ()
540
540
pcElse = self .getPC ()
541
541
cmd = {}
@@ -544,7 +544,7 @@ def k_if(self, command):
544
544
cmd ['keyword' ] = 'gotoPC'
545
545
cmd ['goto' ] = 0
546
546
cmd ['debug' ] = False
547
- self .addCommand (cmd )
547
+ self .add (cmd )
548
548
# Get the 'then' code
549
549
self .compileOne ()
550
550
if self .peek () == 'else' :
@@ -557,7 +557,7 @@ def k_if(self, command):
557
557
cmd ['keyword' ] = 'gotoPC'
558
558
cmd ['goto' ] = 0
559
559
cmd ['debug' ] = False
560
- self .addCommand (cmd )
560
+ self .add (cmd )
561
561
# Fixup the link to the 'else' branch
562
562
self .getCommandAt (pcElse )['goto' ] = self .getPC ()
563
563
# Process the 'else' branch
@@ -603,7 +603,7 @@ def k_import(self, command):
603
603
variable ['keyword' ] = keyword
604
604
variable ['import' ] = None
605
605
variable ['used' ] = False
606
- self .addCommand (variable )
606
+ self .add (variable )
607
607
if self .peek () != 'and' :
608
608
break
609
609
self .nextToken ()
@@ -732,17 +732,37 @@ def k_load(self, command):
732
732
if record ['keyword' ] == 'ssh' :
733
733
command ['ssh' ] = record ['name' ]
734
734
command ['path' ] = self .nextValue ()
735
- self .add (command )
736
- return True
737
-
738
- command ['file' ] = self .getValue ()
735
+ else :
736
+ command ['file' ] = self .getValue ()
737
+ else :
738
+ command ['file' ] = self .getValue ()
739
+ command ['or' ] = None
740
+ load = self .getPC ()
739
741
self .add (command )
742
+ if self .peek () == 'or' :
743
+ self .nextToken ()
744
+ self .nextToken ()
745
+ # Add a 'goto' to skip the 'or'
746
+ cmd = {}
747
+ cmd ['lino' ] = command ['lino' ]
748
+ cmd ['domain' ] = 'core'
749
+ cmd ['keyword' ] = 'gotoPC'
750
+ cmd ['goto' ] = 0
751
+ cmd ['debug' ] = False
752
+ skip = self .getPC ()
753
+ self .add (cmd )
754
+ # Process the 'or'
755
+ self .getCommandAt (load )['or' ] = self .getPC ()
756
+ self .compileOne ()
757
+ # Fixup the skip
758
+ self .getCommandAt (skip )['goto' ] = self .getPC ()
740
759
return True
741
760
else :
742
761
FatalError (self .compiler , f'I don\' t understand \' { self .getToken ()} \' ' )
743
762
return False
744
763
745
764
def r_load (self , command ):
765
+ errorReason = None
746
766
target = self .getVariable (command ['target' ])
747
767
if 'ssh' in command :
748
768
ssh = self .getVariable (command ['ssh' ])
@@ -751,17 +771,24 @@ def r_load(self, command):
751
771
try :
752
772
with sftp .open (path , 'r' ) as remote_file : content = remote_file .read ().decode ()
753
773
except :
754
- RuntimeError ( self . program , 'Unable to read data' )
774
+ errorReason = f 'Unable to read from { path } '
755
775
else :
756
776
filename = self .getRuntimeValue (command ['file' ])
757
777
try :
758
778
with open (filename ) as f : content = f .read ()
779
+ try :
780
+ if filename .endswith ('.json' ): content = json .loads (content )
781
+ except :
782
+ errorReason = 'Bad or null JSON string'
759
783
except :
760
- content = ''
761
- try :
762
- if filename .endswith ('.json' ): content = json .loads (content )
763
- except :
764
- RuntimeError (self .program , 'Bad or null JSON string' )
784
+ errorReason = f'Unable to read from { filename } '
785
+
786
+ if errorReason :
787
+ if command ['or' ] != None :
788
+ print (f'Exception "{ errorReason } ": Running the "or" clause' )
789
+ return command ['or' ]
790
+ else :
791
+ RuntimeError (self .program , f'Error: { errorReason } ' )
765
792
value = {}
766
793
value ['type' ] = 'text'
767
794
value ['content' ] = content
@@ -888,15 +915,15 @@ def k_on(self, command):
888
915
cmd ['keyword' ] = 'gotoPC'
889
916
cmd ['goto' ] = 0
890
917
cmd ['debug' ] = False
891
- self .addCommand (cmd )
918
+ self .add (cmd )
892
919
# Add the action and a 'stop'
893
920
self .compileOne ()
894
921
cmd = {}
895
922
cmd ['domain' ] = 'core'
896
923
cmd ['lino' ] = command ['lino' ]
897
924
cmd ['keyword' ] = 'stop'
898
925
cmd ['debug' ] = False
899
- self .addCommand (cmd )
926
+ self .add (cmd )
900
927
# Fixup the link
901
928
command ['goto' ] = self .getPC ()
902
929
return True
@@ -990,7 +1017,7 @@ def k_post(self, command):
990
1017
command ['result' ] = None
991
1018
command ['or' ] = None
992
1019
post = self .getPC ()
993
- self .addCommand (command )
1020
+ self .add (command )
994
1021
if self .peek () == 'or' :
995
1022
self .nextToken ()
996
1023
self .nextToken ()
@@ -1002,7 +1029,7 @@ def k_post(self, command):
1002
1029
cmd ['goto' ] = 0
1003
1030
cmd ['debug' ] = False
1004
1031
skip = self .getPC ()
1005
- self .addCommand (cmd )
1032
+ self .add (cmd )
1006
1033
# Process the 'or'
1007
1034
self .getCommandAt (post )['or' ] = self .getPC ()
1008
1035
self .compileOne ()
@@ -1055,10 +1082,10 @@ def r_print(self, command):
1055
1082
program = command ['program' ]
1056
1083
code = program .code [program .pc ]
1057
1084
lino = str (code ['lino' ] + 1 )
1058
- while len (lino ) < 5 : lino = f' { lino } '
1085
+ # while len(lino) < 5: lino = f' {lino}'
1059
1086
if value == None : value = '<empty>'
1060
1087
if 'log' in command :
1061
- print (f'{ datetime .now ().time ()} :{ lino } -> { value } ' )
1088
+ print (f'{ datetime .now ().time ()} :{ self . program . name } : { lino } ->{ value } ' )
1062
1089
else :
1063
1090
print (value )
1064
1091
return self .nextPC ()
@@ -1262,17 +1289,65 @@ def k_script(self, command):
1262
1289
# Save a value to a file
1263
1290
def k_save (self , command ):
1264
1291
command ['content' ] = self .nextValue ()
1265
- if self .nextIs ('to' ):
1266
- command ['file' ] = self .nextValue ()
1267
- self .add (command )
1268
- return True
1269
- return False
1292
+ self .skip ('to' )
1293
+ if self .nextIsSymbol ():
1294
+ record = self .getSymbolRecord ()
1295
+ if record ['keyword' ] == 'ssh' :
1296
+ command ['ssh' ] = record ['name' ]
1297
+ command ['path' ] = self .nextValue ()
1298
+ self .add (command )
1299
+ else :
1300
+ command ['file' ] = self .getValue ()
1301
+ else :
1302
+ command ['file' ] = self .getValue ()
1303
+ command ['or' ] = None
1304
+ save = self .getPC ()
1305
+ self .add (command )
1306
+ if self .peek () == 'or' :
1307
+ self .nextToken ()
1308
+ self .nextToken ()
1309
+ # Add a 'goto' to skip the 'or'
1310
+ cmd = {}
1311
+ cmd ['lino' ] = command ['lino' ]
1312
+ cmd ['domain' ] = 'core'
1313
+ cmd ['keyword' ] = 'gotoPC'
1314
+ cmd ['goto' ] = 0
1315
+ cmd ['debug' ] = False
1316
+ skip = self .getPC ()
1317
+ self .add (cmd )
1318
+ # Process the 'or'
1319
+ self .getCommandAt (save )['or' ] = self .getPC ()
1320
+ self .compileOne ()
1321
+ # Fixup the skip
1322
+ self .getCommandAt (skip )['goto' ] = self .getPC ()
1323
+ return True
1270
1324
1271
1325
def r_save (self , command ):
1326
+ errorReason = None
1272
1327
content = self .getRuntimeValue (command ['content' ])
1273
- filename = self .getRuntimeValue (command ['file' ])
1274
- if filename .endswith ('.json' ): content = json .dumps (content )
1275
- with open (filename , 'w' ) as f : f .write (content )
1328
+ if 'ssh' in command :
1329
+ ssh = self .getVariable (command ['ssh' ])
1330
+ path = self .getRuntimeValue (command ['path' ])
1331
+ sftp = ssh ['sftp' ]
1332
+ if path .endswith ('.json' ): content = json .dumps (content )
1333
+ try :
1334
+ with sftp .open (path , 'w' ) as remote_file : remote_file .write (content )
1335
+ except :
1336
+ errorReason = 'Unable to write to {path}'
1337
+ else :
1338
+ filename = self .getRuntimeValue (command ['file' ])
1339
+ if filename .endswith ('.json' ): content = json .dumps (content )
1340
+ try :
1341
+ with open (filename , 'w' ) as f : f .write (content )
1342
+ except :
1343
+ errorReason = f'Unable to write to { filename } '
1344
+
1345
+ if errorReason :
1346
+ if command ['or' ] != None :
1347
+ print (f'Exception "{ errorReason } ": Running the "or" clause' )
1348
+ return command ['or' ]
1349
+ else :
1350
+ RuntimeError (self .program , f'Error: { errorReason } ' )
1276
1351
return self .nextPC ()
1277
1352
1278
1353
# Send a message to a module
@@ -1746,7 +1821,7 @@ def k_while(self, command):
1746
1821
# token = self.getToken()
1747
1822
command ['condition' ] = code
1748
1823
test = self .getPC ()
1749
- self .addCommand (command )
1824
+ self .add (command )
1750
1825
# Set up a goto for when the test fails
1751
1826
fail = self .getPC ()
1752
1827
cmd = {}
@@ -1755,7 +1830,7 @@ def k_while(self, command):
1755
1830
cmd ['keyword' ] = 'gotoPC'
1756
1831
cmd ['goto' ] = 0
1757
1832
cmd ['debug' ] = False
1758
- self .addCommand (cmd )
1833
+ self .add (cmd )
1759
1834
# Do the body of the while
1760
1835
self .nextToken ()
1761
1836
if self .compileOne () == False :
@@ -1767,7 +1842,7 @@ def k_while(self, command):
1767
1842
cmd ['keyword' ] = 'gotoPC'
1768
1843
cmd ['goto' ] = test
1769
1844
cmd ['debug' ] = False
1770
- self .addCommand (cmd )
1845
+ self .add (cmd )
1771
1846
# Fixup the 'goto' on completion
1772
1847
self .getCommandAt (fail )['goto' ] = self .getPC ()
1773
1848
return True
0 commit comments