dolibarrWriter.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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, years):
  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. if year in years:
  14. output_file = output.replace("%year%", year)
  15. output_dir = os.path.dirname(output_file)
  16. if not os.path.exists(output_dir):
  17. os.makedirs(os.path.dirname(output_file))
  18. elif not os.path.isdir(output_dir):
  19. print "Error: %s is not a dir\n" % (output_dir)
  20. raise os.error()
  21. f = codecs.open(output_file, 'w', 'utf-8')
  22. for entry in entries_by_year[year]:
  23. f.write(entry.get_ledger())
  24. f.write("\n")
  25. f.close()
  26. @staticmethod
  27. def write_hreport_plan():
  28. pc_names = settings.get('PC_NAMES')
  29. pc_descriptions = settings.get('PC_DESCRIPTIONS')
  30. filename = Writer.output_files['pc']
  31. output_file = os.path.join(Writer.output_dir, filename)
  32. f = codecs.open(output_file, 'w', 'utf-8')
  33. for pc in sorted(pc_names.keys()):
  34. name = pc_names[pc]
  35. if pc in pc_descriptions:
  36. desc = pc_descriptions[pc]
  37. else:
  38. desc = name
  39. s = "%s %s %s\n" % (name.ljust(80), pc.ljust(12), desc)
  40. f.write(s)
  41. f.close()