code_junkie

Elegant structured text file parsing

Nov 11th, 2011
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. Chat Transcript
  2.  
  3. Visitor: Random Website Visitor
  4. Operator: Milton
  5. Company: Initech
  6. Started: 16 Oct 2008 9:13:58
  7. Finished: 16 Oct 2008 9:45:44
  8.  
  9. Random Website Visitor: Where do i get the cover sheet for the TPS report?
  10. * There are no operators available at the moment. If you would like to leave a message, please type it in the input field below and click "Send" button
  11. * Call accepted by operator Milton. Currently in room: Milton, Random Website Visitor.
  12. Milton: Y-- Excuse me. You-- I believe you have my stapler?
  13. Random Website Visitor: I really just need the cover sheet, okay?
  14. Milton: it's not okay because if they take my stapler then I'll, I'll, I'll set the building on fire...
  15. Random Website Visitor: oh i found it, thanks anyway.
  16. * Random Website Visitor is now off-line and may not reply. Currently in room: Milton.
  17. Milton: Well, Ok. But… that's the last straw.
  18. * Milton has left the conversation. Currently in room: room is empty.
  19.  
  20. Visitor Details
  21. ---------------
  22. Your Name: Random Website Visitor
  23. Your Question: Where do i get the cover sheet for the TPS report?
  24. IP Address: 255.255.255.255
  25. Host Name: 255.255.255.255
  26. Referrer: Unknown
  27. Browser/OS: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; InfoPath.1; .NET CLR 2.0.50727)
  28.  
  29. visitor = text.find(/Visitor:(.*)/)
  30. operator = text.find(/Operator:(.*)/)
  31. body = text.find(/whatever....)
  32.  
  33. text.match(/Visitor:(.*)nOperator:(.*)...whatever to giant regex/m) do
  34. visitor = $1
  35. operator = $2
  36. etc.
  37. end
  38.  
  39. from pprint import pprint
  40. from lepl import AnyBut, Drop, Eos, Newline, Separator, SkipTo, Space
  41.  
  42. # field = name , ":" , value
  43. name, value = AnyBut(':n')[1:,...], AnyBut('n')[::'n',...]
  44. with Separator(~Space()[:]):
  45. field = name & Drop(':') & value & ~(Newline() | Eos()) > tuple
  46.  
  47. header_start = SkipTo('Chat Transcript' & Newline()[2])
  48. header = ~header_start & field[1:] > dict
  49. server_message = Drop('* ') & AnyBut('n')[:,...] & ~Newline() > 'Server'
  50. conversation = (server_message | field)[1:] > list
  51. footer_start = 'Visitor Details' & Newline() & '-'*15 & Newline()
  52. footer = ~footer_start & field[1:] > dict
  53. chat_log = header & ~Newline() & conversation & ~Newline() & footer
  54.  
  55. pprint(chat_log.parse_file(open('chat.log')))
  56.  
  57. from pprint import pprint
  58. from lepl import And, Drop, Newline, Or, Regexp, SkipTo
  59.  
  60. def Field(name, value=Regexp(r's*(.*?)s*?n')):
  61. """'name , ":" , value' matcher"""
  62. return name & Drop(':') & value > tuple
  63.  
  64. Fields = lambda names: reduce(And, map(Field, names))
  65.  
  66. header_start = SkipTo(Regexp(r'^Chat Transcript$') & Newline()[2])
  67. header_fields = Fields("Visitor Operator Company Started Finished".split())
  68. server_message = Regexp(r'^* (.*?)n') > 'Server'
  69. footer_fields = Fields(("Your Name, Your Question, IP Address, "
  70. "Host Name, Referrer, Browser/OS").split(', '))
  71.  
  72. with open('chat.log') as f:
  73. # parse header to find Visitor and Operator's names
  74. headers, = (~header_start & header_fields > dict).parse_file(f)
  75. # only Visitor, Operator and Server may take part in the conversation
  76. message = reduce(Or, [Field(headers[name])
  77. for name in "Visitor Operator".split()])
  78. conversation = (message | server_message)[1:]
  79. messages, footers = ((conversation > list)
  80. & Drop('nVisitor Detailsn---------------n')
  81. & (footer_fields > dict)).parse_file(f)
  82.  
  83. pprint((headers, messages, footers))
  84.  
  85. ({'Company': 'Initech',
  86. 'Finished': '16 Oct 2008 9:45:44',
  87. 'Operator': 'Milton',
  88. 'Started': '16 Oct 2008 9:13:58',
  89. 'Visitor': 'Random Website Visitor'},
  90. [('Random Website Visitor',
  91. 'Where do i get the cover sheet for the TPS report?'),
  92. ('Server',
  93. 'There are no operators available at the moment. If you would like to leave a message, please type it in the input field below and click "Send" button'),
  94. ('Server',
  95. 'Call accepted by operator Milton. Currently in room: Milton, Random Website Visitor.'),
  96. ('Milton', 'Y-- Excuse me. You-- I believe you have my stapler?'),
  97. ('Random Website Visitor', 'I really just need the cover sheet, okay?'),
  98. ('Milton',
  99. "it's not okay because if they take my stapler then I'll, I'll, I'll set the building on fire..."),
  100. ('Random Website Visitor', 'oh i found it, thanks anyway.'),
  101. ('Server',
  102. 'Random Website Visitor is now off-line and may not reply. Currently in room: Milton.'),
  103. ('Milton', "Well, Ok. But… that's the last straw."),
  104. ('Server',
  105. 'Milton has left the conversation. Currently in room: room is empty.')],
  106. {'Browser/OS': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; InfoPath.1; .NET CLR 2.0.50727)',
  107. 'Host Name': '255.255.255.255',
  108. 'IP Address': '255.255.255.255',
  109. 'Referrer': 'Unknown',
  110. 'Your Name': 'Random Website Visitor',
  111. 'Your Question': 'Where do i get the cover sheet for the TPS report?'})
Add Comment
Please, Sign In to add comment