]>
Commit | Line | Data |
---|---|---|
115add43 | 1 | import argparse |
2 | import functools | |
392389b7 | 3 | import subprocess |
115add43 | 4 | |
5 | ||
6 | def read_file(fname): | |
7 | with open(fname, encoding='utf-8') as f: | |
8 | return f.read() | |
9 | ||
10 | ||
7d61d230 L |
11 | def write_file(fname, content, mode='w'): |
12 | with open(fname, mode, encoding='utf-8') as f: | |
115add43 | 13 | return f.write(content) |
14 | ||
15 | ||
115add43 | 16 | def read_version(fname='yt_dlp/version.py'): |
392389b7 | 17 | """Get the version without importing the package""" |
115add43 | 18 | exec(compile(read_file(fname), fname, 'exec')) |
19 | return locals()['__version__'] | |
20 | ||
21 | ||
22 | def get_filename_args(has_infile=False, default_outfile=None): | |
23 | parser = argparse.ArgumentParser() | |
24 | if has_infile: | |
25 | parser.add_argument('infile', help='Input file') | |
26 | kwargs = {'nargs': '?', 'default': default_outfile} if default_outfile else {} | |
27 | parser.add_argument('outfile', **kwargs, help='Output file') | |
28 | ||
29 | opts = parser.parse_args() | |
30 | if has_infile: | |
31 | return opts.infile, opts.outfile | |
32 | return opts.outfile | |
33 | ||
34 | ||
35 | def compose_functions(*functions): | |
36 | return lambda x: functools.reduce(lambda y, f: f(y), functions, x) | |
392389b7 | 37 | |
38 | ||
39 | def run_process(*args, **kwargs): | |
40 | kwargs.setdefault('text', True) | |
41 | kwargs.setdefault('check', True) | |
42 | kwargs.setdefault('capture_output', True) | |
43 | if kwargs['text']: | |
44 | kwargs.setdefault('encoding', 'utf-8') | |
45 | kwargs.setdefault('errors', 'replace') | |
46 | return subprocess.run(args, **kwargs) |