]> jfr.im git - yt-dlp.git/blob - devscripts/utils.py
[devscripts] Create `utils` and refactor
[yt-dlp.git] / devscripts / utils.py
1 import argparse
2 import functools
3
4
5 def read_file(fname):
6 with open(fname, encoding='utf-8') as f:
7 return f.read()
8
9
10 def write_file(fname, content):
11 with open(fname, 'w', encoding='utf-8') as f:
12 return f.write(content)
13
14
15 # Get the version without importing the package
16 def read_version(fname='yt_dlp/version.py'):
17 exec(compile(read_file(fname), fname, 'exec'))
18 return locals()['__version__']
19
20
21 def get_filename_args(has_infile=False, default_outfile=None):
22 parser = argparse.ArgumentParser()
23 if has_infile:
24 parser.add_argument('infile', help='Input file')
25 kwargs = {'nargs': '?', 'default': default_outfile} if default_outfile else {}
26 parser.add_argument('outfile', **kwargs, help='Output file')
27
28 opts = parser.parse_args()
29 if has_infile:
30 return opts.infile, opts.outfile
31 return opts.outfile
32
33
34 def compose_functions(*functions):
35 return lambda x: functools.reduce(lambda y, f: f(y), functions, x)