utils.py 993 B

123456789101112131415161718192021222324252627282930313233343536
  1. import argparse
  2. import functools
  3. def read_file(fname):
  4. with open(fname, encoding='utf-8') as f:
  5. return f.read()
  6. def write_file(fname, content, mode='w'):
  7. with open(fname, mode, encoding='utf-8') as f:
  8. return f.write(content)
  9. # Get the version without importing the package
  10. def read_version(fname='hypervideo_dl/version.py'):
  11. exec(compile(read_file(fname), fname, 'exec'))
  12. return locals()['__version__']
  13. def get_filename_args(has_infile=False, default_outfile=None):
  14. parser = argparse.ArgumentParser()
  15. if has_infile:
  16. parser.add_argument('infile', help='Input file')
  17. kwargs = {'nargs': '?', 'default': default_outfile} if default_outfile else {}
  18. parser.add_argument('outfile', **kwargs, help='Output file')
  19. opts = parser.parse_args()
  20. if has_infile:
  21. return opts.infile, opts.outfile
  22. return opts.outfile
  23. def compose_functions(*functions):
  24. return lambda x: functools.reduce(lambda y, f: f(y), functions, x)