1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
|
#line 34 "interscript/src/input_frame.ipk"
import string
import re
import traceback
import sys
import os
import tempfile
import types
from interscript.frames.processf import process_fault
# these imports _should_ come from the global frame!
from interscript.drivers.sources.base import eof, eoi
from interscript.drivers.sources.disk import named_file_source
from interscript.drivers.sources.disk import parse_source_filename
from interscript.drivers.sources.disk import loadfile
from interscript.drivers.sources.cache import cache_source
from interscript.drivers.sinks.bufdisk import named_file_sink
from interscript.drivers.sinks.disk import simple_named_file_sink
from interscript.drivers.sinks.cache import cache_sink
from interscript.drivers.storage.memory import memory
from interscript.languages.interscript_languages import add_translation
from interscript.tanglers.data import data_tangler
from interscript.core.protocols import has_protocol
from interscript.parsers.html import sgml_wrapper, html_filter
try:
import interscript.utilities.diff
except:
try: raise "dummy" # correct stupid python bug
except: pass
def compile_parse_tab(res):
return map(lambda x: [re.compile(x[0]), x[1]], res)
class deduce: pass
#line 78 "interscript/src/input_frame.ipk"
class input_frame:
def __init__(self, pass_frame, src, reg_list, weaver, userdict, depth):
# the display
self.inhibit_sref = pass_frame.inhibit_sref
self.pass_frame = pass_frame
self.master = pass_frame.master
self.process = self.master.process
self.global_frame = self.process.global_frame
self.current_weaver = weaver
self.current_weaver_stack = []
self.depth = depth
self.source = src
self.userdict = userdict
self.reg_list = reg_list
self.read_buffer = []
self.current_tangler_stack = []
self.current_tangler = None
self.line_offset = 0
self.original_filename = src.get_source_name()
self.original_count = self.line_offset
self.current_weaver.set_original_filename(self.original_filename)
self.head_offset = 0
self.tabwidth = self.master.tabwidth
self.cont_re = re.compile('^$|^ (.*)$')
self.any_line_re = re.compile('^(.*)$')
if 'frames' in self.process.trace:
self.process.acquire_object(self, 'INPUT FRAME['+str(depth)+']='+src.get_source_name())
self.post_methods()
def __del__(self):
if 'frames' in self.process.trace:
self.process.release_object(self)
def get_input_frame(self): return self
#line 124 "interscript/src/input_frame.ipk"
def post_methods(self):
# input frame methods
self.userdict.update(self.process.global_frame.__dict__)
method_names = self.__class__.__dict__.keys()
is_begin_or_end_method = lambda x: x[:3]=='end' or x[:5]=='begin'
method_names = filter(is_begin_or_end_method, method_names)
method_names = method_names + [
'head','heading','push_head','pop_head','set_head',
'line_break','page_break',
'set_warning_character',
'doc','p','eop','cite_url',
'data_output','c_output','cpp_output',
'python_output','perl_output',
'interscript_output',
'tangler',
'push','pop','select','comment','resume_code',
'tangler_push','tangler_pop','tangler_set', # temporarily!
'get_tangler',
'untangle',
'weave','weave_line','tangle','sink_line',
'print_identifier_cross_reference',
'print_contents',
'print_file_list',
'print_source_list',
'print_include_list',
'print_class_reference',
'print_file_status',
'get_weaver',
'raw_if','enable','enable_if','disable',
'get_input_frame',
'table_row', 'table_rule',
'begin_table_row','end_table_row',
'begin_table_cell','end_table_cell',
'item',
'include_file','include_source',
'include_code','insert_code','display_code',
'sink_verbatim','expand','define',
'include_html','html',
'capture_output','print_output',
'capture_python_output','print_python_output',
'print_python_test_output',
'set_weaver','get_weaver', 'push_weaver','pop_weaver',
'get_attribute',
'interscript_from_options',
'test_interscript',
'python','test_python',
'register_test','set_test_result',
'set_anchor','ref_anchor',
'post_notice',
'skip_upto','skip_upto_if',
'set_encoding','get_encoding',
'help', 'weave_help'
]
for m in method_names:
self.userdict[m]=eval('self.'+m)
# pass frame methods
pass_frame_method_names = [
'get_pass_frame'
]
for m in pass_frame_method_names:
self.userdict[m]=eval('self.pass_frame.'+m)
# processs frame methods
process_frame_method_names = ['get_process_frame']
for m in process_frame_method_names:
self.userdict[m]=eval('self.process.'+m)
#master frame methods
mastrer_frame_method_names = [
'get_master_frame',
'set_title', 'get_title',
'add_author',
'set_native_language','get_native_language',
'set_document_data', 'get_document_data']
for m in mastrer_frame_method_names:
self.userdict[m]=eval('self.master.'+m)
#functions (non methods)
# NOTE: most functions are made available thru the global frame!
function_names = []
for f in function_names:
self.userdict[f]=eval(f)
#line 210 "interscript/src/input_frame.ipk"
def help(self):
"Command help"
print "Command Help"
d = self.userdict
keys = d.keys()
keys.sort()
for key in keys:
routine = d[key]
typ = type(routine)
doc = getattr(routine,'__doc__','')
if typ.__name__ in ['module']:
print typ.__name__,key
else:
print typ.__name__, key + "->",doc
def weave_help(self, level):
"Weave help"
self.head(level,"Command Help")
d = self.userdict
keys = d.keys()
keys.sort()
for key in keys:
routine = d[key]
typ = type(routine)
doc = getattr(routine,'__doc__','')
if doc is None: doc = "No documentation"
self.head(level+1, typ.__name__+ " "+ key)
if typ is types.MethodType:
self.weave_line("Method of class " + routine.im_class.__name__+".")
self.begin_displayed_code()
self.weave(doc)
self.end_displayed_code()
#line 246 "interscript/src/input_frame.ipk"
def close(self):
if 'frames' in self.process.trace:
print 'closing frame',self.source.name
self.userdict.clear()
del self.userdict
del self.current_tangler
del self.current_weaver
del self.reg_list
while self.current_tangler_stack: del self.current_tangler_stack[-1]
while self.current_weaver_stack: del self.current_weaver_stack[-1]
#line 259 "interscript/src/input_frame.ipk"
def file_pass(self):
while 1:
try:
file,count,line = self.readline()
self.echo = 'input' in self.process.trace
if self.echo:
print '%s %6s: %s' % (file,count,line)
for r in self.reg_list:
match = r[0].match(line)
if match:
r[1](match,file,count,self.userdict)
break
except eoi:
if 'frames' in self.process.trace:
print 'EOI detected'
if self.current_tangler:
self.select(None)
self.close()
return
except KeyboardInterrupt:
print '!!!!!!!!! KEYBOARD INTERRUPT !!!!!!!!!'
self.process.update_files = 0
self.close()
raise KeyboardInterrupt
except process_fault,value:
print '!!!!!!!!! PROCESS FAULT ',value,' !!!!!!!!!'
self.process.update_files = 0
self.close()
raise
except SystemExit,value:
print '!!!!!!!!! SYSTEM EXIT !!!!!!!!!'
self.process.update_files = 0
self.close()
raise SystemExit,value
except:
print '!!!!!!!!! PROGRAM ERROR !!!!!!!!!'
traceback.print_exc()
self.process.update_files = 0
self.close()
sys.exit(1)
#line 308 "interscript/src/input_frame.ipk"
def interscript_from_options(self,*args):
"Run interscript from with the given command line options"
from interscript import run_from_options
svdin = sys.stdin
try:
try:
run_from_options(args)
except KeyboardInterrupt: raise
except SystemExit: raise
except:
print 'Error running embedded interscript from options'
print 'options were', args
traceback.print_exc()
finally:
sys.stdin = svdin
# Note: this routine is ONLY useful for testing interscript itself,
# because it always puts the test code into the interscript directory!
# This needs to be fixed!!! It is ALSO platform dependent!
def test_interscript(self, description, source_terminator, *args, **kwds):
"Run interscript on the following embedded test script"
testno = self.register_test(description, 'interscript')
testlabel = 'test_'+str(testno)
self.set_anchor(testlabel)
self.current_weaver.writeline(
'On-the-fly interscript for test '+str(testno)+' follows.')
source_origin_line = self.original_count
source_origin_file = self.original_filename
test_code = self.collect_lines_upto(source_terminator)
self.current_weaver.script_head('interscript',source_origin_file)
for i in range(len(test_code)):
self.current_weaver.echotangle(source_origin_line+i+1,test_code[i])
self.current_weaver.script_foot('interscript',source_origin_file)
try:
os.mkdir('interscript/tests')
except:
pass
our_source_filename = 'interscript/tests/test_'+str(testno)+'.tpk'
f = open(our_source_filename,'w')
f.write(string.join(test_code,'\n')+'\n')
f.close()
logfile='interscript/tests/output/test_'+str(testno)+'.log'
kargs = []
for key in kwds.keys():
if key not in ['description','source_terminator']:
kargs.append('--' + key + '=' + repr(kwds[key]))
newargs = args + tuple(kargs) + (
'--weaver=html',
'--weaver-prefix=interscript/tests/output/',
'--new-logfile='+logfile,
'--title=Test '+str(testno)+': '+description,
our_source_filename)
apply(self.interscript_from_options, newargs)
self.set_test_result(testno,'inspect')
self.current_weaver.doc()
self.current_weaver.writeline('Test output at')
self.current_weaver.cite_url('../tests/output/test_'+str(testno)+'.html')
self.current_weaver.writeline('. Logfile at')
self.current_weaver.cite_url('../tests/output/test_'+str(testno)+'.log')
self.current_weaver.writeline('.')
self.current_weaver.par()
#line 384 "interscript/src/input_frame.ipk"
def get_attribute(self,name,default=None):
"Get a variable, default None"
if self.userdict.has_key(name):
return self.userdict[name]
else:
return default
#line 403 "interscript/src/input_frame.ipk"
def begin(self):
"Begin a block"
ho = self.head_offset
self.select(None)
inpt = input_frame(
self.pass_frame,
self.source,
[],
self.current_weaver,
self.userdict.copy(),
self.depth)
inpt.head_offset = ho
inpt.set_warning_character(python=self.python_warn)
inpt.file_pass()
def end(self):
"end a block"
self.select(None)
raise eoi
#line 477 "interscript/src/input_frame.ipk"
def include_file(self,name,encoding=None):
"Include an interscruot file"
if 'input' in self.process.trace:
print 'input from',name
file_signature = (self.depth+1,'interscript',name)
if file_signature in self.pass_frame.skiplist:
print 'SKIPPING INCLUDE FILE',file_signature
i = 0
t = self.master.src_tree
n = len(t)
while i<n:
if file_signature == tuple(t[i][0:3]): break
i = i + 1
if i == n:
print 'COULD NOT FIND SKIP FILE',file_signature,'in',t
else:
self.pass_frame.include_files.append(file_signature)
i = i + 1
lev = file_signature[0]
while i<n:
if t[i][0] >= lev: break
print 'INSERTING',t[i][2],'into include file list (cheating)'
self.pass_frame.include_files.append(tuple(t[i][0:3]))
i = i + 1
else:
self.pass_frame.include_files.append(file_signature)
if encoding is None:
encoding = self.source.encoding_name
self.include_source(named_file_source(
self.pass_frame,name, self.source.directory, encoding=encoding))
def include_source(self,source):
"Include an interscript source"
self.select(None)
ho = self.head_offset
inpt = input_frame(
self.pass_frame,
source,
[],
self.current_weaver,
self.userdict.copy(),
self.depth+1)
inpt.head_offset = ho
inpt.set_warning_character(python='@')
inpt.file_pass()
self.current_weaver.set_original_filename (self.original_filename)
#line 527 "interscript/src/input_frame.ipk"
def set_encoding(self, encoding):
"Set the current encoding"
self.source.set_encoding(encoding)
def get_encoding(self):
"Get the current encoding"
return self.source.get_encoding()
#line 558 "interscript/src/input_frame.ipk"
def insert_code(self,name):
"Insert code in an external file into the tangler stream"
ifdata = (self.depth+1,'code: '+self.current_tangler.language,name)
self.pass_frame.include_files.append(ifdata)
r = []
source = named_file_source(self.pass_frame,name, self.source.directory)
inpt = input_frame(
self.pass_frame,
source,
r,
self.current_weaver,
self.userdict.copy(),
self.depth+1)
r.append([inpt.any_line_re, inpt.do_web])
inpt.select(self.current_tangler)
inpt.file_pass()
def include_code(self,name,current_tangler):
"Insert code in an external file into a nominated tangler stream"
ifdata = (self.depth+1,'code: '+current_tangler.language,name)
self.pass_frame.include_files.append(ifdata)
r = []
source = named_file_source(
self.pass_frame,
name,
self.source.directory)
inpt = input_frame(
self.pass_frame,
source,
r,
self.current_weaver,
self.userdict.copy(),
self.depth+1)
r.append([inpt.any_line_re, inpt.do_web])
inpt.select(current_tangler)
inpt.file_pass()
def sink_verbatim(self,filename):
"Write code in an external file directly to the tanglers driver"
self.current_weaver.label_chunk(filename)
source = named_file_source(
self.pass_frame,
filename,
self.source.directory)
data = source.readlines()
self.current_tangler.sink.writelines(data)
def define(self, macroname, language='data'):
"Name a chunk of code"
self.select(self.tangler(cache_sink(macroname, self.master), language))
def expand(self,macroname):
"Write named chunk directly to tanglers driver"
self.current_weaver.label_chunk(macroname)
source = cache_source(macroname, self.master)
data = source.readlines()
self.current_tangler.sink.writelines(data)
#line 637 "interscript/src/input_frame.ipk"
#line 654 "interscript/src/input_frame.ipk"
def include_html(source):
"Include a HTML file as input. Translate to interscript."
self.select(None)
r = []
self.pass_frame.include_files.append((self.depth+1,'html: '+self.current_tangler.language,name))
inpt = input_frame(
self.pass_frame,
source,
r,
self.current_weaver,
self.userdict.copy(),
self.depth+1)
inpt.html_parser = sgml_wrapper(html_filter(inpt))
r.append((inpt.any_line_re,inpt.do_html))
inpt.file_pass()
def html(self):
"Begin processesing embedded HTML. Translate to interscript."
self.select(None)
r = []
inpt = input_frame(
self.pass_frame,
self.source,
r,
self.current_weaver,
self.userdict.copy(),
self.depth)
inpt.html_parser = sgml_wrapper(html_filter(inpt))
r.append((inpt.any_line_re,inpt.do_html))
inpt.file_pass()
#line 689 "interscript/src/input_frame.ipk"
def get_weaver(self):
"Get the current weaver"
return self.current_weaver
def set_weaver(self,w):
"set the current weaver"
tmp = self.current_weaver
self.current_weaver = w
return tmp
def push_weaver(self,w):
"Push the current weaver onto the weaver stack, and set new current weaver"
self.current_weaver_stack.append(self.current_weaver)
self.current_weaver = w
def pop_weaver(self):
"Set the current weaver to the weaver on the weaver stack and pop it."
self.current_weaver = self.current_weaver_stack[-1]
del self.current_weaver_stack[-1]
def raw_if(self,tag):
"Set the weaver in raw mode if it has the given tag"
self.current_weaver.raw_if(tag)
def enable_if(self,tag):
"Enable the current weaver if it has the given tag"
self.current_weaver.enable_if(tag)
def enable(self):
"Enable the current weaver"
self.current_weaver.enable()
def disable(self):
"Disable the current weaver"
self.current_weaver.disable()
#line 727 "interscript/src/input_frame.ipk"
def set_anchor(self,label):
"Set the argument label as a document anchor"
if self.current_tangler:
self.current_tangler.weaver.set_anchor(label)
else:
self.current_weaver.set_anchor(label)
def ref_anchor(self,label):
"Generate a reference to the given anchor label"
self.current_weaver.ref_anchor(label)
#line 740 "interscript/src/input_frame.ipk"
def set_warning_character(self,python=None):
"Set the interscript warning character (usually @)"
res = self.make_parse_tab(pywarn=python)
res = compile_parse_tab(res)
self.reg_list = res
self.python_warn = python
def normal_line(self,data,file,count):
weaver = self.get_weaver()
if self.current_tangler:
self.current_tangler.writeline(data,file,count)
else:
weaver.writeline(data)
#line 758 "interscript/src/input_frame.ipk"
def enqueue_input(self,file, count, line):
"""Enqueue a line with cross reference information
into the input stream."""
self.read_buffer.append((file,count,line))
def dequeue_input(self):
"Read a line out of the input stream"
data = self.read_buffer[0]
del self.read_buffer[0]
return data
# This is the interscript version of a #line directive
def line(self, number, filename):
"Reset interscript's source reference data"
self.inpt.original_file = filename
self.inpt.line_offset = number - inpt.src.get_lines_read()
def readline(self):
while 1:
if self.read_buffer:
return self.dequeue_input()
try:
line = self.source.readline()
self.real_filename = self.source.get_source_name()
self.real_count = self.source.get_lines_read()
self.original_count = self.real_count + self.line_offset
line = string.rstrip(line)
self.line = string.expandtabs(line,self.tabwidth)
return (self.original_filename,self.original_count,self.line)
except KeyboardInterrupt:
# should inhibit output for process, not globally
self.process.update_files = 0
raise KeyboardInterrupt
except eof:
if 'input' in self.process.trace:
print 'readline: EOF'
self.line = None
raise eoi
else:
print 'program error in readline:',sys.exc_info()
self.process.update_files = 0
#line 820 "interscript/src/input_frame.ipk"
def untangle(self,name):
"""Wrap an external file up as an interscript package, the wrapped
file is written to the current tangler."""
if not self.current_tangler:
raise 'untangle without active tangler'
f = open(name)
data = f.readlines()
f.close()
self.current_tangler.sink.writeline('@select(output("'+name+'"))')
for line in data:
l = string.rstrip(line)
if len(l):
if l[0]=='@': l = '@'+l
self.inpt.tangler.sink.writeline(l)
self.current_tangler.sink.writeline('@select(None)')
self.current_tangler.weaver.begin_small()
self.current_tangler.weaver.writeline('Included '+name+', '+str(len(data))+' lines.')
self.current_tangler.weaver.end_small()
self.current_tangler.weaver.line_break()
#line 867 "interscript/src/input_frame.ipk"
# regexp's for the main functions
def make_parse_tab(self, pywarn = None):
res = []
if pywarn:
res = res + [
['^'+pywarn+'('+pywarn+')(.*)$',self.do_quote_at],
['^'+pywarn+'(.*[-+*/%:,\\\\([{]) *(#.*)?$', self.do_exec_suite],
['^'+pywarn+'(.*)$',self.do_exec_line]
]
res = res + [
['^(.*)$',self.do_web]
]
return res
#line 894 "interscript/src/input_frame.ipk"
def collect_stuff(self,prefix, cont_re, echo):
saved = prefix
try:
file2,count2,line = self.readline()
match = cont_re.match(line)
while match:
if echo:
print '%s %6s: %s' % (file2,count2,line)
body = match.group(1)
if not body: body = ''
saved = saved+'\n'+body
file2,count2,line = self.readline()
match = cont_re.match(line)
self.enqueue_input(file2,count2,line)
except eoi:
pass
saved = saved + '\n'
return saved
def collect_lines_upto(self,terminal, keep=0):
"Collect lines up to marker line"
term_re = re.compile('^'+terminal+'$')
saved = []
file,count,line = self.readline()
match = term_re.match(line)
while not match:
saved.append(line)
file,count,line = self.readline()
match = term_re.match(line)
return saved
def skip_upto(self,terminal):
"Skip up to marker line"
term_re = re.compile('^'+terminal+'$')
file,count,line = self.readline()
match = term_re.match(line)
while not match:
file,count,line = self.readline()
match = term_re.match(line)
def skip_upto_if(self,terminal,condition):
"if condition is true, skip up to marker line"
if condition: self.skip_upto(terminal)
def collect_upto(self,terminal, keep=0):
"Collect text up to marker line"
return string.join(self.collect_lines_upto(terminal,keep), '\n')+'\n'
#line 944 "interscript/src/input_frame.ipk"
def python(self, terminal, keep=0):
"Execute embedded python script"
file = self.original_filename
count = self.original_count
data = self.collect_upto(terminal)
self.process.py_exec(data,file,count,self.userdict)
#line 963 "interscript/src/input_frame.ipk"
def print_diff_table(self, comparison,
actual_heading='Actual', expected_heading='Expected',
ok_message='Data compared equal.',
diff_message='Differential follows.'):
equal = len(comparison) == 0
our_weaver = self.get_weaver()
if not equal:
if diff_message:
our_weaver.writeline(diff_message)
our_weaver.begin_table('Actual','Expected', CLASS='DIFF')
for section in comparison:
left = section[0][1:]
right = section[1][1:]
left = string.join(left,'\n')
right = string.join(right,'\n')
our_weaver.table_row([left, right])
our_weaver.end_table()
else:
if ok_message:
our_weaver.writeline(ok_message)
#line 987 "interscript/src/input_frame.ipk"
def register_test(self, description, kind):
"Allocate a sequence number for a test case as described and return it."
testno = self.pass_frame.get_new_test_number()
testlabel = 'test_'+str(testno)
self.pass_frame.tests[testno]=\
[description,testlabel,kind,'Aborted']
return testno
def set_test_result(self, testno, result):
"Set the result of the given test number"
self.pass_frame.tests[testno][3]=result
#line 1001 "interscript/src/input_frame.ipk"
def test_python(self,
hlevel=None,
descr=None,
source_filename=None,
source_terminator=None,
expect_filename=None,
expect_terminator=None,
diff_context=0):
"""Test a python script. The script may be an external file, or
embedded, expected output may be an external file,
embedded, or omitted, if provided a context diff will be generated
with the nominated number of lines of context.
"""
testno = self.pass_frame.get_new_test_number()
testlabel = 'test_'+str(testno)
test_record = self.pass_frame.tests[testno]=\
[descr,testlabel,'python','Aborted']
expect = expect_filename or expect_terminator
# print heading
if hlevel: our_hlevel = hlevel
else: our_hlevel = self.last_head+1
if descr == None: descr = 'Test'
self.head(our_hlevel,'Test '+str(testno)+': '+descr)
self.set_anchor(testlabel)
our_weaver = self.get_weaver()
if source_terminator:
our_weaver.writeline('On-the-fly python test script follows.')
source_origin_line = self.original_count
source_origin_file = self.original_filename
test_code = self.collect_lines_upto(source_terminator)
our_weaver.script_head('python',source_origin_file)
for i in range(len(test_code)):
our_weaver.echotangle(source_origin_line+i+1,test_code[i])
our_weaver.script_foot('python',source_origin_file)
elif source_filename:
our_weaver.writeline('Python test script from file '+source_filename+'.')
if expect_terminator:
expected_origin_line = self.original_count
expected_origin_file = self.original_filename
expected_output = self.collect_lines_upto(expect_terminator)
elif expect_filename:
# FIX to make document relative
our_weaver.writeline('Expected output from file '+expected_filename+'.')
expected_lines = loadfile(expect_filename)
# execute the test code
if source_filename:
our_source_filename = source_filename
description = None
else:
our_source_filebase = tempfile.mktemp()
our_source_filename = our_source_filebase + '_test.py'
f = open(our_source_filename,'w')
f.write(string.join(test_code,'\n')+'\n')
f.close()
description = 'python <<temporary>>'
our_weaver.writeline('Actual output follows.')
status, actual_output = self.print_python_output(
our_source_filename,
description)
cmd_ok = status == 0
# delete the file if it was created anonymously
if not source_filename:
os.remove(our_source_filename)
if expect:
try:
diff_lines = interscript.utilities.diff.diff_lines
comparison = diff_lines(actual_output, expected_output, context=diff_context)
equal = len(comparison)==0
self.pass_frame.tests[testno][2]= 'diff'
self.pass_frame.tests[testno][3]= ('Fail','Ok')[equal]
if not equal:
our_weaver.writeline('On-the-fly expected output follows.')
our_weaver.expected_head(expected_origin_file)
for i in range(len(expected_output)):
our_weaver.echotangle(expected_origin_line+i+1,expected_output[i])
our_weaver.expected_foot(expected_origin_file)
self.print_diff_table(comparison)
except ImportError:
our_weaver.writeline('Unable to import diff to perform comparison.')
except KeyboardInterrupt: raise
except SystemExit: raise
except:
traceback.print_exc()
else:
self.pass_frame.tests[testno][3]='Inspect'
#line 1116 "interscript/src/input_frame.ipk"
def do_exec_line(self,match, file,count,dict):
self.process.py_exec(match.group(1),file,count,dict)
#line 1121 "interscript/src/input_frame.ipk"
def do_exec_suite(self,match,file,count,dict):
saved = self.collect_stuff(match.group(1), self.cont_re, self.echo)
self.process.py_exec(saved,file,count,dict)
#line 1127 "interscript/src/input_frame.ipk"
def do_web(self,match,file,count,dict):
self.normal_line(match.group(1),file,count)
#line 1132 "interscript/src/input_frame.ipk"
def do_quote_at(self,match,file,count,dict):
self.normal_line(match.group(1)+match.group(2),file,count)
#line 1137 "interscript/src/input_frame.ipk"
def do_html(self,match,file,count,dict):
self.html_parser.writeline(match.group(1),file,count)
#line 1150 "interscript/src/input_frame.ipk"
def tangler_push(self,f):
"Push the current tangler onto the tangler stack then set it to the given tangler"
self.current_tangler_stack.append(self.current_tangler)
self.current_tangler = f
def tangler_pop(self):
"Set the current tangler to the top of the tangler stack and pop it."
self.current_tangler = self.current_tangler_stack[-1]
del self.current_tangler_stack[-1]
def tangler_set(self,f):
"Set the current tangler"
self.current_tangler = f
def get_tangler(self):
"Get the current tangler (may be None)"
return self.current_tangler
#line 1174 "interscript/src/input_frame.ipk"
def data_output(self,f): return self.tangler(f,'data')
def c_output(self,f): return self.tangler(f,'c')
def cpp_output(self,f): return self.tangler(f,'cpp')
def python_output(self,f): return self.tangler(f,'python')
def perl_output(self,f): return self.tangler(f,'perl')
# temporarily, we'll use a data tangler
def interscript_output(self,f):
filename = self.master.tangler_directory+f
sink = named_file_sink(self.pass_frame,filename,self.master.tangler_prefix)
return self.tangler(sink,'data')
def tangler(self,device, language=deduce, *args, **kwds):
"Create a tangle object from a dvice specification (either a filename or sink object"
if has_protocol(device, 'filename'):
filename = self.master.tangler_directory+str(device)
sink = named_file_sink(
self.pass_frame,
filename,
self.master.tangler_prefix)
elif has_protocol(device, 'sink'):
sink = device
else: raise TypeError,'tangler device argument must be string or sink'
if language is None: language = 'data'
if language is deduce:
try:
splitup = string.split(sink.name,'.')
if len(splitup)>1:
extension = splitup[-1]
#language = extension_table [extension]
language = extension
else: language = 'data'
except KeyError: language = 'data'
except IndexError: language = 'data'
language = string.lower(language)
language = string.replace(language,'++','pp') # C++ hack
language = string.replace(language,'-','_') # obj-C etc
try:
import imp
import interscript.tanglers
try:
file,pathname,description = imp.find_module(language,interscript.tanglers.__path__)
except:
#print "imp.find_module failed for '"+language+'"'
raise exceptions.UserWarning("Can't load tangler")
try:
tmod = imp.load_module(language,file,pathname,description)
except:
print "imp.load_module failed for '"+pathname+"'"
raise exceptions.UserWarning("Can't load tangler")
try:
class_name = language + "_tangler"
tglr = getattr(tmod,class_name)
except:
print "tangler class '"+class_name+"' not found in module "
raise exceptions.UserWarning("Can't load tangler")
t = tglr(sink,self.current_weaver,self.inhibit_sref)
except:
print 'Unable to load',language,'tangler for "'+device+'": using data'
t=data_tangler(sink,self.current_weaver)
return t
#line 1289 "interscript/src/input_frame.ipk"
def push(self,f):
"Push tangler onto tangler stack"
if self.current_tangler: self.code_foot()
self.tangler_push(f)
if self.current_tangler: self.code_head()
def pop(self):
"Pop tangler from tangler stack"
if self.current_tangler: self.code_foot()
self.tangler_pop()
if self.current_tangler: self.code_head()
#line 1302 "interscript/src/input_frame.ipk"
def select(self, *args, **kwds):
"Select the nominated object as the current tangler or weaver"
for arg in args:
self.select1(arg)
if kwds.has_key('tangler'):
self.select_tangler(kwds['tangler'])
if kwds.has_key('weaver'):
self.set_weaver(kwds['weaver'])
def select1(self, arg):
if has_protocol(arg,'tangler'):
self.select_tangler(arg)
elif has_protocol(arg, 'weaver'):
self.set_weaver(arg)
elif arg is None:
self.select_tangler(None)
else:
pass #permissive
def select_tangler(self,f):
if self.current_tangler: self.code_foot()
self.tangler_set(f)
if self.current_tangler: self.code_head()
def code_head(self):
dst_filename = self.current_tangler.sink.name
dst_lineno = self.current_tangler.sink.lines_written
src_filename = self.original_filename
src_lineno = self.original_count
index = self.pass_frame.section_index
list = index.get(dst_filename, [])
list.append((dst_lineno, src_filename, src_lineno))
index[dst_filename]=list
secno = len(list)
self.current_weaver.code_head(self.current_tangler, secno)
def code_foot(self):
dst_filename = self.current_tangler.sink.name
index = self.pass_frame.section_index
list = index.get(dst_filename, [])
secno = len(list)
self.current_weaver.code_foot(self.current_tangler, secno)
def begin_comments(self):
"Begin tangling lines as comments"
if self.current_tangler:
self.current_tangler_push(self.current_tangler.get_comment_tangler())
else:
self.current_tangler_push(None)
def end_comments(self):
"End comment tangler"
self.current_tangler_pop()
def resume_code(self):
"Pop the current tangler, use after starting string or comment tangler"
self.current_tangler_pop()
def comment(self,v):
"Begin tangling an embedded comment."
self.get_weaver().write_comment(v)
def begin_string(self,eol = ' ', width = 0):
"Begin tangling an embedded string."
if self.current_tangler:
self.current_tangler_push(self.current_tangler.get_string_tangler(eol,width))
else:
self.current_tangler_push(None)
def end_string(self):
"Terminate a string tangler"
tangler_pop()
def weave(self,s):
"Weave a string of text"
weaver = self.get_weaver()
weaver.write(s)
def weave_line(self,s):
"Weave a line of text"
weaver = self.get_weaver()
weaver.writeline(s)
def _tangle_line(self,s, inhibit_sref=None):
"Tangle one line of code"
if inhibit_sref == None: inhibit_sref = self.inhibit_sref
if self.current_tangler:
line = self.original_count
file = self.original_filename
self.current_tangler.writeline(s,file,line,inhibit_sref)
else:
print "tangle: No tangler for",s
def tangle(self,s, inhibit_sref=None):
"Tangle lines of code"
if inhibit_sref == None: inhibit_sref = self.inhibit_sref
lines = string.split(s,'\n')
for line in lines:
self._tangle_line(line,inhibit_sref)
def sink_line(self,s):
if self.current_tangler:
snk = self.current_tangler.sink
snk.writeline(s)
else:
print "tangle: No tangler for",s
#line 1413 "interscript/src/input_frame.ipk"
def print_contents(self,*args, **kwds):
"Print table of contents"
self.select(None)
weaver = self.get_weaver()
apply(weaver.print_contents, args, kwds)
def print_file_list(self,*args,**kwds):
"Print complete list of all files"
self.select(None)
weaver = self.get_weaver()
apply(weaver.print_file_list, args, kwds)
def print_file_status(self,*args,**kwds):
"""Weave status information. Warning: this information
changes from pass to pass, and will prevent your document
converging to a fixpoint if the output is woven to a
named file sink. Make sure the weaver driver is a
simple file sink, which is included in convergence checks."""
self.select(None)
weaver = self.get_weaver()
apply(weaver.print_file_status, args, kwds)
def print_source_list(self, *args, **kwds):
"Weave the interscript source tree"
self.select(None)
weaver = self.get_weaver()
apply(weaver.print_source_list, args, kwds)
def print_include_list(self, *args, **kwds):
"Weave the include file list"
self.select(None)
weaver = self.get_weaver()
apply(weaver.print_include_list, args, kwds)
def macro(self,name):
self.select(None)
weaver = self.get_weaver()
return data_tangler(memory(name),weaver)
def print_identifier_cross_reference(self, *args, **kwds):
"Weave the identifier cross reference table"
self.select(None)
weaver = self.get_weaver()
apply(weaver.identifier_reference, args, kwds)
def print_class_reference(self, *args, **kwds):
"Weave the class cross reference table"
self.select(None)
weaver = self.get_weaver()
apply(weaver.class_reference, args, kwds)
#line 1467 "interscript/src/input_frame.ipk"
def capture_output(self,command):
"Capture the output from executing the shell command"
commands = self.global_frame.commands
status, output = commands.getstatusoutput(command)
data = string.split(output,'\n')
return (status,data)
def print_output(self,command,description=None):
"Weave output from executing the script, register it as a test case."
status, data = self.capture_output(command)
weaver = self.get_weaver()
if description: cmd = description
else: cmd = command
weaver.test_output_head(cmd, status)
for i in range(len(data)):
line = data[i]
l = string.rstrip(line)
weaver.echotangle(i+1,l)
weaver.test_output_foot(cmd, status)
return (status, data)
def capture_python_output(self,script):
"Capture the output from executing the python script externally"
return self.capture_output('"'+sys.executable+'" '+script)
def print_python_output(self,script, description=None):
"Weave output from executing the script externally"
return self.print_output(
'"'+sys.executable+'" '+script,
description)
def print_python_test_output(self,script, descr):
"Weave output from executing the python script, register it as a test case."
testno = self.pass_frame.get_new_test_number()
testlabel = 'test_'+str(testno)
self.pass_frame.tests[testno]=[descr,testlabel,'python','Unknown']
self.set_anchor(testlabel)
return self.print_python_output(script,descr)
#line 1514 "interscript/src/input_frame.ipk"
def head(self, level, text, **kwds):
level = int(level)
level = level + self.head_offset
self.last_head = level
if 'headings' in self.process.trace:
print (' '*(level-1))+'"'+text+'"'
self.pass_frame.toc.append((level,text, kwds))
if self.current_tangler: self.code_foot()
self.tangler_set(None)
apply(add_translation,(text,),kwds.get('translations',{}))
apply(self.current_weaver.head,(level,text),kwds)
# like heading, but to be used in code as well:
# doesn't switch to document mode, doesn't do
# code headings and footings.
# deprecated form client interface, but required for perl tangler
def heading(self, level, text, **kwds):
"Weave a heading"
level = int(level)
level = level + self.head_offset
self.last_head = level
if 'headings' in self.process.trace:
print (' '*(level-1))+'"'+text+'"'
self.pass_frame.toc.append((level,text, kwds))
apply(self.current_weaver.head,(level,text),kwds)
def push_head(self, amt=1):
"Push the heading level onto the heading level stack"
self.head_offset = self.head_offset + amt
def pop_head(self, amt=1):
"Pop the heading level from the heading level stack"
self.push_head(-amt)
def set_head(self, amt=None):
"Set the heading level"
if amt != None:
self.head_offset = amt - 1
else:
self.head_offset = self.last_head - 1
#line 1558 "interscript/src/input_frame.ipk"
def doc(self):
"Begin documentation mode"
if self.current_tangler: self.code_foot()
self.tangler_set(None)
def p(self): # end a paragraph and start a new one
"Paragraph serapator"
self.current_weaver.par()
def eop(self): # end a paragraph without starting a new one
"Paragraph terminator"
self.current_weaver.eop()
def line_break(self):
"Break a line"
self.current_weaver.line_break()
def page_break(self):
"Break a page"
self.current_weaver.page_break()
#line 1580 "interscript/src/input_frame.ipk"
def cite_url(self, url):
self.current_weaver.cite_url(url)
#line 1585 "interscript/src/input_frame.ipk"
def begin_table(self, *headings, **kwds):
"Begin a table"
apply(self.get_weaver().begin_table, headings, kwds)
def table_row(self, *data):
"Weave a table row"
self.current_weaver.table_row(data)
def end_table(self):
"End the current table"
self.current_weaver.end_table()
def table_rule(self):
"Draw a horizontal rule across a table"
self.current_weaver.table_rule()
def begin_table_row(self):
"Begin a table row"
self.current_weaver.begin_table_row()
def end_table_row(self):
"End a table row"
self.current_weaver.end_table_row()
def begin_table_cell(self):
"Begin a table cell"
self.current_weaver.begin_table_cell()
def end_table_cell(self):
"End a table cell"
self.current_weaver.end_table_cell()
#line 1619 "interscript/src/input_frame.ipk"
def begin_list(self, style):
"Begin a list of the nominated style"
self.current_weaver.begin_list(style)
def end_list(self):
"End a list"
self.current_weaver.end_list()
def item(self,*args, **kwds):
"Start a new list item"
apply(self.current_weaver.item, args, kwds)
def begin_numbered_list(self, start=1):
"Start a new numbered list item"
self.current_weaver.begin_numbered_list(start)
def end_numbered_list(self):
self.current_weaver.end_numbered_list()
def begin_numbered_list_item(self):
self.current_weaver.begin_numbered_list_item()
def end_numbered_list_item(self):
self.current_weaver.end_numbered_list_item()
def begin_bullet_list(self):
self.current_weaver.begin_bullet_list()
def end_bullet_list(self):
self.current_weaver.end_bullet_list()
def begin_bullet_list_item(self):
self.current_weaver.begin_bullet_list_item()
def end_bullet_list_item(self):
self.current_weaver.end_bullet_list_item()
def begin_keyed_list(self):
self.current_weaver.begin_keyed_list()
def end_keyed_list(self):
self.current_weaver.end_keyed_list()
def begin_keyed_list_item(self, key):
self.current_weaver.begin_keyed_list_item(key)
def end_keyed_list_item(self):
self.current_weaver.end_keyed_list_item()
#line 1670 "interscript/src/input_frame.ipk"
def begin_emphasize(self):
"Begin setting text in emphasis font"
self.current_weaver.begin_emphasize()
def end_emphasize(self):
"End setting text in emphasis font"
self.current_weaver.end_emphasize()
def begin_strong(self):
"Begin setting text in strong font"
self.current_weaver.begin_strong()
def end_strong(self):
"End setting text in strong font"
self.current_weaver.end_strong()
def begin_italic(self):
"Begin setting text in italic font"
self.current_weaver.begin_italic()
def end_italic(self):
"End setting text in italic font"
self.current_weaver.end_italic()
def begin_bold(self):
"Begin setting text in bold font"
self.current_weaver.begin_bold()
def end_bold(self):
"End setting text in bold font"
self.current_weaver.end_bold()
def begin_big(self):
"End setting text in big font"
self.current_weaver.begin_big()
def end_big(self):
"End setting text in big font"
self.current_weaver.end_big()
def begin_small(self):
"Begin setting text in small font"
self.current_weaver.begin_small()
def end_small(self):
self.current_weaver.end_small()
def begin_code(self):
"Begin setting text in monospaced font"
self.current_weaver.begin_code()
def end_code(self):
"Begin setting text in monospaced font"
self.current_weaver.end_code()
#line 1727 "interscript/src/input_frame.ipk"
def begin_displayed_text(self):
"Begin text display"
self.current_weaver.begin_displayed_text()
def end_displayed_text(self):
self.current_weaver.end_displayed_text()
def begin_displayed_code(self):
"Begin display of verbatim code"
self.current_weaver.begin_displayed_code()
def end_displayed_code(self):
self.current_weaver.end_displayed_code()
# this command is used to print out a code file 'verbatim'
# without line numbers!
def display_code(self,name,kind='code'):
"Display the external file as code"
self.pass_frame.include_files.append((self.depth+1,kind,name))
self.begin_displayed_code()
filename = parse_source_filename(name, self.source.directory)[3]
f = open(filename)
data = f.readlines()
f.close()
weaver = self.get_weaver()
for line in data:
l = string.rstrip(line)
weaver.writeline(l)
self.end_displayed_code()
#line 1761 "interscript/src/input_frame.ipk"
def post_notice(self, key, value):
"Post a copyright, licence, or credit notice"
self.master.noticedict[key]=value
#line 1765 "interscript/src/input_frame.ipk"
|