dolibarrWriter.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import settings
  2. import os
  3. import codecs
  4. class Writer(object):
  5. output_files = settings.get('OUTPUT_FILES')
  6. output_dir = settings.get('OUTPUT_DIR')
  7. @staticmethod
  8. def write(journal, entries):
  9. filename = Writer.output_files[journal]
  10. output = os.path.join(Writer.output_dir, filename)
  11. entries_by_year = entries.get_by_year()
  12. for year in entries_by_year:
  13. output_file = output.replace("%year%", year)
  14. output_dir = os.path.dirname(output_file)
  15. if not os.path.exists(output_dir):
  16. os.makedirs(os.path.dirname(output_file))
  17. elif not os.path.isdir(output_dir):
  18. print "Error: %s is not a dir\n" % (output_dir)
  19. raise os.error()
  20. f = codecs.open(output_file, 'w', 'utf-8')
  21. for entry in entries_by_year[year]:
  22. f.write(entry.get_ledger())
  23. f.write("\n")
  24. f.close()
  25. @staticmethod
  26. def write_hreport_plan():
  27. pc_names = settings.get('PC_NAMES')
  28. pc_descriptions = settings.get('PC_DESCRIPTIONS')
  29. filename = Writer.output_files['pc']
  30. output_file = os.path.join(Writer.output_dir, filename)
  31. f = codecs.open(output_file, 'w', 'utf-8')
  32. for pc in sorted(pc_names.keys()):
  33. name = pc_names[pc]
  34. if pc in pc_descriptions:
  35. desc = pc_descriptions[pc]
  36. else:
  37. desc = name
  38. s = "%s %s %s\n" % (name.ljust(80), pc.ljust(12), desc)
  39. f.write(s)
  40. f.close()