Browse Source

parser: added catalogue option

Fabian Peter Hammerle 8 years ago
parent
commit
e367369564
2 changed files with 28 additions and 3 deletions
  1. 12 2
      scripts/order-confirmation-mail-parser
  2. 16 1
      tests/test_integration.py

+ 12 - 2
scripts/order-confirmation-mail-parser

@@ -9,6 +9,7 @@ import os
 import sys
 import yaml
 import email
+import shutil
 import pprint
 import random
 import locale
@@ -497,14 +498,22 @@ def parse(msg):
 
     raise Exception('failed to parse')
 
-def compute(mail_path, register_path):
+def compute(mail_path, catalogue, register_path):
 
     orders = []
 
     if mail_path:
         for p in mail_path:
             with open(p, 'r') as mail:
-                orders += parse(email.message_from_file(mail))
+                mail_orders = parse(email.message_from_file(mail))
+                orders += mail_orders
+            if catalogue:
+                for order in mail_orders:
+                    order_dir_path = os.path.join(order.platform, order.order_id)
+                    if not os.path.isdir(order_dir_path):
+                        os.makedirs(order_dir_path)
+                    shutil.copyfile(p, os.path.join(order_dir_path, os.path.basename(p)))
+                os.remove(p)
     else:
         msg = email.message_from_string(sys.stdin.read())
         orders += parse(msg)
@@ -529,6 +538,7 @@ def _init_argparser():
 
     argparser = argparse.ArgumentParser(description = None)
     argparser.add_argument('--register', metavar = 'path', dest = 'register_path')
+    argparser.add_argument('--catalogue', action='store_true')
     argparser.add_argument('mail_path', nargs = '*')
     return argparser
 

+ 16 - 1
tests/test_integration.py

@@ -19,7 +19,7 @@ def test_integration_stdin():
                 expected_result = yaml.load(expected_result_file.read())
                 assert result == expected_result, 'unexpected result for ' + os.path.basename(mail_path)
 
-def test_ingegration_path():
+def test_integration_path():
     result = yaml.load(subprocess.check_output([
         script_path,
         os.path.join(test_data_path, 'amazon', 'mail_1.eml'),
@@ -35,3 +35,18 @@ def test_ingegration_path():
         with open(result_path) as yaml_file:
             expected_result += yaml.load(yaml_file.read())
     assert result == expected_result
+
+def test_catalogue(tmpdir):
+    os.chdir(tmpdir.strpath)
+    import shutil
+    shutil.copyfile(os.path.join(test_data_path, 'amazon', 'mail_8.eml'), 'mail.eml')
+    assert os.listdir('.') == ['mail.eml']
+    orders = yaml.load(subprocess.check_output([
+        script_path, 
+        '--catalogue',
+        'mail.eml',
+        ]))
+    assert os.listdir('.') == ['amazon.de']
+    assert os.listdir('amazon.de') == ['028-6176648-7157123',  '028-0273468-3485109']
+    assert os.listdir(os.path.join('amazon.de', '028-6176648-7157123')) == ['mail.eml']
+    assert os.listdir(os.path.join('amazon.de', '028-0273468-3485109')) == ['mail.eml']