txt2odc.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #! /usr/bin/env python2.7
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Alexander Shiryaev, 2020.01
  5. #
  6. # Documents.ExportDocument
  7. #
  8. # ASCII only
  9. import sys, re
  10. def Int (x):
  11. assert x >= 0
  12. assert x < 0x100000000
  13. return chr(x % 0x100) + chr((x >> 8) % 0x100) + chr((x >> 16) % 0x100) + chr((x >> 24) % 0x100)
  14. hdr0 = "CDOo" + Int(0) + \
  15. "\x82\xf1" + "Documents.StdDocumentDesc" + \
  16. "\x00\xf1" + "Documents.DocumentDesc" + \
  17. "\x00\xf1" + "Containers.ViewDesc" + \
  18. "\x00\xf1" + "Views.ViewDesc" + \
  19. "\x00\xf0" + "Stores.StoreDesc" + \
  20. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x07\x00\x00\x00"
  21. hdr1 = "\x00\x00\x00\x83\xf1" + "Documents.ModelDesc" + \
  22. "\x00\xf1" + "Containers.ModelDesc" + \
  23. "\x00\xf1" + "Models.ModelDesc" + \
  24. "\x00\xf1" + "Stores.ElemDesc" + \
  25. "\x00\xf2\x04\x00\x00\x00\x00\x00\x00\x00"
  26. hdr2 = "\x09\x00\x00\x00"
  27. hdr3 = "\x00\x00\x00\x00\x00\x82\xf1" + "TextViews.StdViewDesc" + \
  28. "\x00\xf1" + "TextViews.ViewDesc" + \
  29. "\x00\xf2\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x07\x00\x00\x00"
  30. hdr4 = "\x00\x00\x00\x83\xf1" + "TextModels.StdModelDesc" + \
  31. "\x00\xf1" + "TextModels.ModelDesc" + \
  32. "\x00\xf2\x06\x00\x00\x00\x00\x00\x00\x00"
  33. hdr5 = "\x0f\x00\x00\x00"
  34. hdr6 = "\x00\x00\x00\x00\x00\x00\x51\x00\x00\x00\x00\x82\xf1" + "TextModels.AttributesDesc" + \
  35. "\x00\xf2\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1a\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x2a\x00\x18\xf0\x01\x00\x00\x00\x00\x00\x90\x01\x00\x00\x00\x00"
  36. hdr7 = "\xff"
  37. trl = "\x82\xf1" + "TextControllers.StdCtrlDesc" + \
  38. "\x00\xf1" + "TextControllers.ControllerDesc" + \
  39. "\x00\xf1" + "Containers.ControllerDesc" + \
  40. "\x00\xf1" + "Controllers.ControllerDesc" + \
  41. "\x00\xf2\x04\x00\x00\x00\x00\x00\x00\x00\x14\x00\x00\x00\x00\x00\x00\x00\x09\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x82\xf1" + "TextRulers.StdRulerDesc" + \
  42. "\x00\xf1" + "TextRulers.RulerDesc" + \
  43. "\x00\xf2\x03\x00\x00\x00\x00\x00\x00\x00\xab\x00\x00\x00\x07\x00\x00\x00\xa3\x00\x00\x00\x00\x00\x00\x83\xf1" + "TextRulers.StdStyleDesc" + \
  44. "\x00\xf1" + "TextRulers.StyleDesc" + \
  45. "\x00\xf2\x07\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x5a\x00\x00\x00\x00\x00\x00\x00\x82\xf1" + "TextRulers.AttributesDesc" + \
  46. "\x00\xf2\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x24\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x20\xa3\x5a\x00\x00\x00\x00\x00\xbb\xf2\x01\x00\x2c\x68\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x82\xf2\x0d\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1a\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x2a\x00\x18\xf0\x01\x00\x00\x00\x00\x00\x90\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0c\xf8\x00\x00\x0c\xf8\x00\x00\x2c\x9b\x5b\x00\x0b\xf8\x00\x40\x82\xf1" + "Documents.ControllerDesc" + \
  47. "\x00\xf2\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x09\x00\x00\x00\x00\x40\x5b\x73\x00\x00\x99\xa2\x00\x80\xfc\x0a\x00\x80\xfc\x0a\x00\xc0\x5e\x68\x00\x80\x9c\x97\x00\x00"
  48. def splitToLines (text):
  49. lines = []
  50. [ [ lines.append(l1) for l1 in re.split( '[\r\n]', l ) ] for l in text.split('\r\n') ]
  51. return lines
  52. def txt2odc (text):
  53. # text = text.replace('\n', chr(0x0d))
  54. text = chr(0x0d).join(splitToLines(text))
  55. l = len(text)
  56. return hdr0 + Int(l + 873) + hdr1 + Int(l + 695) + hdr2 + Int(l + 687) + hdr3 + Int(l + 601) + hdr4 + Int(l + 99) + hdr5 + Int(l + 91) + hdr6 + Int(l) + hdr7 + text + trl
  57. def main ():
  58. if len(sys.argv) == 3:
  59. fh = open(sys.argv[1], 'rb')
  60. text = fh.read()
  61. fh.close()
  62. fh = open(sys.argv[2], 'wb')
  63. fh.write(txt2odc(text))
  64. fh.close()
  65. else:
  66. print("usage: %s file.txt file.odc" % (sys.argv[0],))
  67. if __name__ == '__main__':
  68. main()