Browse Source

parser: accept params instead of stdin

Fabian Peter Hammerle 8 years ago
parent
commit
7a4973fd2c
2 changed files with 28 additions and 4 deletions
  1. 10 3
      scripts/order-confirmation-mail-parser
  2. 18 1
      tests/test_integration.py

+ 10 - 3
scripts/order-confirmation-mail-parser

@@ -497,11 +497,17 @@ def parse(msg):
 
     raise Exception('failed to parse')
 
-def compute(register_path):
+def compute(mail_path, register_path):
 
-    msg = email.message_from_string(sys.stdin.read())
+    orders = []
 
-    orders = parse(msg)
+    if mail_path:
+        for p in mail_path:
+            with open(p, 'r') as mail:
+                orders += parse(email.message_from_file(mail))
+    else:
+        msg = email.message_from_string(sys.stdin.read())
+        orders += parse(msg)
 
     if register_path:
         with open(register_path, 'r') as register:
@@ -523,6 +529,7 @@ def _init_argparser():
 
     argparser = argparse.ArgumentParser(description = None)
     argparser.add_argument('--register', metavar = 'path', dest = 'register_path')
+    argparser.add_argument('mail_path', nargs = '*')
     return argparser
 
 def main(argv):

+ 18 - 1
tests/test_integration.py

@@ -9,7 +9,7 @@ project_root_path = os.path.realpath(os.path.join(__file__, '..', '..'))
 script_path = os.path.join(project_root_path, 'scripts', 'order-confirmation-mail-parser')
 test_data_path = os.path.join(project_root_path, 'tests', 'data')
 
-def test_integration():
+def test_integration_stdin():
     for mail_path in glob.glob(os.path.join(test_data_path, '*', 'mail_*.eml')):
         with open(mail_path, 'r') as mail_file:
             process = subprocess.Popen([script_path], stdin = subprocess.PIPE, stdout = subprocess.PIPE)
@@ -18,3 +18,20 @@ def test_integration():
             with open(mail_path.replace('.eml', '.yml'), 'r') as expected_result_file:
                 expected_result = yaml.load(expected_result_file.read())
                 assert result == expected_result, 'unexpected result for ' + os.path.basename(mail_path)
+
+def test_ingegration_path():
+    result = yaml.load(subprocess.check_output([
+        script_path,
+        os.path.join(test_data_path, 'amazon', 'mail_1.eml'),
+        os.path.join(test_data_path, 'oebb', 'mail_1.eml'),
+        os.path.join(test_data_path, 'oebb', 'mail_2.eml'),
+        ]))
+    expected_result = []
+    for result_path in [
+            os.path.join(test_data_path, 'amazon', 'mail_1.yml'),
+            os.path.join(test_data_path, 'oebb', 'mail_1.yml'),
+            os.path.join(test_data_path, 'oebb', 'mail_2.yml'),
+            ]:
+        with open(result_path) as yaml_file:
+            expected_result += yaml.load(yaml_file.read())
+    assert result == expected_result