1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # PYTHON_ARGCOMPLETE_OK
- import dingguo.parser
- import email
- import os
- import shutil
- import sys
- import yaml
- def compute(mail_path, catalogue, register_path):
- orders = []
- if mail_path:
- for p in mail_path:
- with open(p, 'r') as mail:
- mail_orders = dingguo.parser.parse_order_confirmation_mail(
- 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 += dingguo.parser.parse_order_confirmation_mail(msg)
- if register_path:
- if os.path.exists(register_path):
- with open(register_path, 'r') as register_file:
- registry = yaml.load(register_file.read().decode('utf-8'))
- else:
- registry = dingguo.OrderRegistry()
- for order in orders:
- registry.register(order)
- with open(register_path, 'w') as register:
- register.write(yaml.dump(registry, default_flow_style = False, allow_unicode = True))
-
- print(yaml.dump(orders, default_flow_style = False, allow_unicode = True))
- def _init_argparser():
- import argparse
- 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
- def main(argv):
- argparser = _init_argparser()
- try:
- import argcomplete
- argcomplete.autocomplete(argparser)
- except ImportError:
- pass
- args = argparser.parse_args(argv)
- compute(**vars(args))
- return 0
- if __name__ == "__main__":
- sys.exit(main(sys.argv[1:]))
|