]>
Commit | Line | Data |
---|---|---|
1 | import argparse | |
2 | import functools | |
3 | import subprocess | |
4 | ||
5 | ||
6 | def read_file(fname): | |
7 | with open(fname, encoding='utf-8') as f: | |
8 | return f.read() | |
9 | ||
10 | ||
11 | def write_file(fname, content, mode='w'): | |
12 | with open(fname, mode, encoding='utf-8') as f: | |
13 | return f.write(content) | |
14 | ||
15 | ||
16 | def read_version(fname='yt_dlp/version.py'): | |
17 | """Get the version without importing the package""" | |
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) | |
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) |