parser.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import logging
  2. from argparse import ArgumentParser
  3. parser = ArgumentParser()
  4. parser.add_argument('-d', '--debug', action='store_true', help='print debug logging output to console')
  5. parser.add_argument('-f', '--feed', help='Atom feed file to test parsing')
  6. args = parser.parse_args()
  7. if args.debug:
  8. logging.basicConfig(level=logging.DEBUG)
  9. import pytz
  10. from datetime import datetime
  11. tz = pytz.utc
  12. from atomparser import atomprop
  13. if args.feed:
  14. with open(args.feed, 'r') as xml:
  15. atom = atomprop.AtomFeed(xml)
  16. else:
  17. atom = atomprop.AtomFeed(author={'name': 'Test Tester'})
  18. e = atomprop.AtomEntry(content='Something I wrote.')
  19. e.title = 'post 1'
  20. atom.entries.append(e)
  21. for propname in ['title', 'subtitle', 'id', 'author', 'updated', 'next', 'prev']:
  22. try:
  23. prop = getattr(atom, propname)
  24. if isinstance(prop, atomprop.AtomAuthor):
  25. print('\t{}:\t{}'.format(propname.capitalize(), prop.name.get_value()))
  26. else:
  27. print('\t{}:\t{}'.format(propname.capitalize(), prop.get_value()))
  28. except AttributeError as e:
  29. logging.debug('\t{}:\tnot set ({})'.format(propname.capitalize(), e))
  30. pass
  31. except KeyError as e:
  32. logging.debug('\t{}:\tnot set ({})'.format(propname.capitalize(), e))
  33. pass
  34. print('\tEntries: {}'.format(len(atom.entries)))
  35. for i in range(atom.entries.length):
  36. print('\tEntry {}'.format(i))
  37. for propname in ['title', 'id', 'author', 'published', 'updated', 'categories', 'summary', 'content']:
  38. try:
  39. prop = getattr(atom.entries[i], propname)
  40. if isinstance(prop, atomprop.DateConstruct):
  41. print('\t\t{}:\t{}'.format(propname.capitalize(), tz.normalize(prop.get_value())))
  42. elif isinstance(prop, atomprop.AtomAuthor):
  43. print('\t\t{}:\t{}'.format(propname.capitalize(), prop.name.get_value()))
  44. elif isinstance(prop, atomprop.AtomCategories) and prop.length > 0:
  45. print('\t\t{}:\t{}'.format(propname.capitalize(), ', '.join(val.get('term') for val in prop)))
  46. else:
  47. try:
  48. print('\t\t{}:\t{}'.format(propname.capitalize(), prop.get_value()))
  49. except AttributeError as e:
  50. pass
  51. except KeyError as e:
  52. logging.debug('\t\t{}:\tnot set ({})'.format(propname.capitalize(), e))
  53. pass