Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.93 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # BGI script dumper
  4.  
  5. import glob
  6. import os
  7. import struct
  8. import sys
  9. import io
  10.  
  11. import bgi_common
  12. import bgi_setup
  13.  
  14. def dump_text(fo, marker, id, text, comment):
  15.     fo.write(u'//%s\n' % comment)
  16.     fo.write(u'<%s%s%04d>%s\n' % (bgi_setup.slang,marker,id,text))
  17.     for lang in bgi_setup.dlang:
  18.         if bgi_setup.dcopy:
  19.             fo.write(u'<%s%s%04d>%s\n' % (lang,marker,id,text))
  20.         else:
  21.             fo.write(u'<%s%s%04d>\n' % (lang,marker,id))
  22.     fo.write(u'\n')
  23.    
  24. def dump_unique(fo, code_section, imarker):
  25.     text_set = set()
  26.     for addr in sorted(code_section):
  27.         text, id, marker, comment = code_section[addr]
  28.         if marker == imarker and text not in text_set:
  29.             dump_text(fo, marker, id, bgi_common.escape(text), comment)
  30.             text_set.add(text)
  31.  
  32. def dump_sequential(fo, code_section, imarker):
  33.     for addr in sorted(code_section):
  34.         text, id, marker, comment = code_section[addr]
  35.         if marker == imarker:
  36.             dump_text(fo, marker, id, bgi_common.escape(text), comment)
  37.    
  38. def dump_script(script):
  39.     data = open(script, 'rb').read()
  40.     hdr_bytes, code_bytes, text_bytes, config = bgi_common.split_data(data)
  41.     text_section = bgi_common.get_text_section(text_bytes)
  42.     code_section = bgi_common.get_code_section(code_bytes, text_section, config)
  43.     fo = io.open(script+bgi_setup.dext, 'w', encoding=bgi_setup.denc)
  44.     dump_unique(fo, code_section, 'N')        # names
  45.     fo.write(u'///'+u'='*80+u'\n\n')
  46.     dump_sequential(fo, code_section, 'T')    # text
  47.     fo.write(u'///'+u'='*80+u'\n\n')
  48.     dump_unique(fo, code_section, 'Z')        # other
  49.     fo.close()
  50.  
  51. if __name__ == '__main__':
  52.     if len(sys.argv) < 2:
  53.         print('Usage: bgi_dump.py <file(s)>')
  54.         print('(only extension-less files amongst <file(s)> will be processed)')
  55.         sys.exit(1)
  56.     for arg in sys.argv[1:]:
  57.         for script in glob.glob(arg):
  58.             base, ext = os.path.splitext(script)
  59.             if not ext and os.path.isfile(script):
  60.                 print('Dumping %s...' % script)
  61.                 dump_script(script)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement