import settings import os import codecs class Writer(object): output_files = settings.get('OUTPUT_FILES') output_dir = settings.get('OUTPUT_DIR') @staticmethod def write(journal, entries): filename = Writer.output_files[journal] output = os.path.join(Writer.output_dir, filename) entries_by_year = entries.get_by_year() for year in entries_by_year: output_file = output.replace("%year%", year) output_dir = os.path.dirname(output_file) if not os.path.exists(output_dir): os.makedirs(os.path.dirname(output_file)) elif not os.path.isdir(output_dir): print "Error: %s is not a dir\n" % (output_dir) raise os.error() f = codecs.open(output_file, 'w', 'utf-8') for entry in entries_by_year[year]: f.write(entry.get_ledger()) f.write("\n") f.close() @staticmethod def write_hreport_plan(): pc_names = settings.get('PC_NAMES') pc_descriptions = settings.get('PC_DESCRIPTIONS') filename = Writer.output_files['pc'] output_file = os.path.join(Writer.output_dir, filename) f = codecs.open(output_file, 'w', 'utf-8') for pc in sorted(pc_names.keys()): name = pc_names[pc] if pc in pc_descriptions: desc = pc_descriptions[pc] else: desc = name s = "%s %s %s\n" % (name.ljust(80), pc.ljust(12), desc) f.write(s) f.close()