]>
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 | ||
1d03633c | 16 | def read_version(fname='yt_dlp/version.py', varname='__version__'): |
392389b7 | 17 | """Get the version without importing the package""" |
1d03633c | 18 | items = {} |
19 | exec(compile(read_file(fname), fname, 'exec'), items) | |
20 | return items[varname] | |
115add43 | 21 | |
22 | ||
23 | def get_filename_args(has_infile=False, default_outfile=None): | |
24 | parser = argparse.ArgumentParser() | |
25 | if has_infile: | |
26 | parser.add_argument('infile', help='Input file') | |
27 | kwargs = {'nargs': '?', 'default': default_outfile} if default_outfile else {} | |
28 | parser.add_argument('outfile', **kwargs, help='Output file') | |
29 | ||
30 | opts = parser.parse_args() | |
31 | if has_infile: | |
32 | return opts.infile, opts.outfile | |
33 | return opts.outfile | |
34 | ||
35 | ||
36 | def compose_functions(*functions): | |
37 | return lambda x: functools.reduce(lambda y, f: f(y), functions, x) | |
392389b7 | 38 | |
39 | ||
40 | def run_process(*args, **kwargs): | |
41 | kwargs.setdefault('text', True) | |
42 | kwargs.setdefault('check', True) | |
43 | kwargs.setdefault('capture_output', True) | |
44 | if kwargs['text']: | |
45 | kwargs.setdefault('encoding', 'utf-8') | |
46 | kwargs.setdefault('errors', 'replace') | |
47 | return subprocess.run(args, **kwargs) |