args.py (3128B) - raw


      1 import argparse
      2 import sys
      3 from os.path import isfile, exists
      4 
      5 
      6 def get_arguments():
      7     parser = argparse.ArgumentParser(
      8         prog=sys.argv[0],
      9         description='A simple implementation of ledger-cli. Supports only the \
     10             following commands and flags: `balance`, `register`, `print`, \
     11             `--sort`, `--price-db` and `--file`.',
     12         epilog='This implementation is inspired in \
     13             [ledger-cli](https://ledger-cli.org/).')
     14     parser.add_argument('-v', '--version',
     15                         action='version', version='%(prog)s 1.0')
     16 
     17     parser.add_argument('verb', metavar='Action',
     18                         action='store', nargs='+',
     19                         help='Specify an action between balance, report and \
     20                             print.')
     21     parser.add_argument('-S', '--sort', metavar='value-expression',
     22                         action='store', dest='sort',
     23                         help='Sort postings by evaluating the given \
     24                             value-expression.')
     25     parser.add_argument('--price-db', metavar='FILE',
     26                         action='store', dest='price_db',
     27                         help='Set the FILE that is used for recording \
     28                             downloaded commodity prices')
     29     parser.add_argument('-f', '--file', metavar='FILE',
     30                         action='append', dest='files',
     31                         help='Read journal data from FILE.')
     32 
     33     my_args = parser.parse_args()
     34     return my_args
     35 
     36 
     37 def test_args(my_args):
     38     # Test if verb is valid.
     39     valid_verbs = [
     40         'balance', 'bal', 'b',
     41         'register', 'reg', 'r',
     42         'print'
     43     ]
     44     if my_args.verb[0] not in valid_verbs:
     45         raise Exception(f'{my_args.verb[0]} is NOT a valid verb! Valid verbs are: {", ".join(valid_verbs)}.')
     46 
     47     # Test if expression for sorting is valid.
     48     # TODO: How can we validate an expression???
     49 
     50     # Test if price_db file exists and is a file.
     51     if my_args.price_db and not exists(my_args.price_db):
     52         raise Exception(f'Command: `--price-db {my_args.price_db}`. File {my_args.price_db} does NOT exist.')
     53     elif my_args.price_db and not isfile(my_args.price_db):
     54         raise Exception(f'Command: `--price-db {my_args.price_db}`. {my_args.price_db} is NOT a file.')
     55 
     56     # Test files.
     57     if my_args.files:
     58         if len(my_args.files) == 1 and my_args.files[0] == '-':
     59             # Check if there's data in stdin.
     60             #
     61             # https://stackoverflow.com/questions/53541223/read-python-stdin-from-pipe-without-blocking-on-empty-input
     62             if sys.stdin.isatty():
     63                 raise Exception(f'Command: --file -. No info from STDIN.')
     64 
     65         else:
     66             for file in my_args.files:
     67                 if not exists(file):
     68                     raise Exception(f'Command: `--file {file}`. File {file} does NOT exist.')
     69                 elif not isfile(file):
     70                     raise Exception(f'Command: `--file {file}`. {file} is NOT a file.')
     71     else:
     72         if sys.stdin.isatty():
     73             raise Exception(f'No info from STDIN. Please specify a file with `--file FILE`.')