Parcourir la source

ajout de l'option --year

Philippe Le Brouster il y a 9 ans
Parent
commit
d69bc30c3b
2 fichiers modifiés avec 25 ajouts et 21 suppressions
  1. 10 7
      bin/himport
  2. 15 14
      himports/dolibarrWriter.py

+ 10 - 7
bin/himport

@@ -21,11 +21,11 @@ sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout)
 
 def process_args(argv):
     options = {}
-    usage = u'himport -a -p -d -v'
+    usage = u'himport -a -p -d -v -y'
     try:
         opts, args = getopt.getopt(
-            argv, "hp:P:adv",
-            ["mysql-password=", "mysql-port"]
+            argv, "hp:P:advy:",
+            ["mysql-password=", "mysql-port", "year="]
         )
     except getopt.GetoptError:
         print usage
@@ -33,6 +33,7 @@ def process_args(argv):
 
     options['alchemy'] = False
     options['pdb'] = False
+    options['years'] = list()
     options['verbose'] = 0
 
     for opt, arg in opts:
@@ -49,6 +50,8 @@ def process_args(argv):
             options['verbose'] += 1
         elif opt in ("-d", "--pdb"):
             options['pdb'] = True
+        elif opt in ("-y", "--year"):
+            options['years'].append(str(arg))
 
     return options
 
@@ -87,10 +90,10 @@ def do_sqlalchemy(options):
             sys.stdout.write("%s\n" % (pc))
 
     # On ecrie les fichiers hledger
-    Writer.write("bank", bank_journal)
-    Writer.write("sells", sell_journal)
-    Writer.write("suppliers", supplier_journal)
-    Writer.write("social", social_journal)
+    Writer.write("bank", bank_journal, options['years'])
+    Writer.write("sells", sell_journal, options['years'])
+    Writer.write("suppliers", supplier_journal, options['years'])
+    Writer.write("social", social_journal, options['years'])
     Writer.write_hreport_plan()
 
     dolibarr.disconnect()

+ 15 - 14
himports/dolibarrWriter.py

@@ -8,25 +8,26 @@ class Writer(object):
     output_dir = settings.get('OUTPUT_DIR')
 
     @staticmethod
-    def write(journal, entries):
+    def write(journal, entries, years):
         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()
+            if year in years:
+                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():