Moved console script entrypoint to its own module

This commit is contained in:
Raoul Snyman 2021-07-28 22:13:12 -07:00
parent 6e96953ed8
commit 7541b88101
3 changed files with 48 additions and 37 deletions

View File

@ -18,7 +18,10 @@ classifiers =
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10
Topic :: Utilities
keywords = music, chords, guitar
keywords =
music
chords
guitar
[options]
package_dir =
@ -35,7 +38,7 @@ where = src
[options.entry_points]
console_scripts =
python-chordpro = chordpro
python-chordpro = chordpro.cli:main
[bdist_wheel]
universal = 1

View File

@ -1,35 +1,2 @@
from argparse import ArgumentParser
from chordpro.base import Song
from chordpro.renderers.html import render as render_html
from chordpro.renderers.text import render as render_text
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('input', metavar='FILENAME', help='Input ChordPro file')
parser.add_argument('-o', '--output', metavar='FILENAME', help='Output to a file')
parser.add_argument('-f', '--format', metavar='FORMAT', choices=['html', 'text'], default='html',
help='Output format')
parser.add_argument('-p', '--param', metavar='KEY=VALUE', action='append', help='Parameter to send to renderer')
args = parser.parse_args()
render_params = {kv.split('=')[0]: kv.split('=')[-1] for kv in args.param} if args.param else {}
# Prepare the params
for key, value in render_params.items():
if value.lower() == "true":
render_params[key] = True
elif value.lower() == "false":
render_params[key] = False
elif value.isdigit():
render_params[key] = int(value)
song = Song(args.input)
if args.format == 'text':
output = render_text(song, **render_params)
else:
output = render_html(song, **render_params)
if args.output:
with open(args.output, 'w') as html_file:
html_file.write(output)
else:
print(output)
from chordpro.cli import main
main()

41
src/chordpro/cli.py Normal file
View File

@ -0,0 +1,41 @@
from argparse import ArgumentParser
from chordpro.base import Song
from chordpro.renderers.html import render as render_html
from chordpro.renderers.text import render as render_text
def get_args():
"""Parse the command line arguments"""
parser = ArgumentParser()
parser.add_argument('input', metavar='FILENAME', help='Input ChordPro file')
parser.add_argument('-o', '--output', metavar='FILENAME', help='Output to a file')
parser.add_argument('-f', '--format', metavar='FORMAT', choices=['html', 'text'], default='html',
help='Output format')
parser.add_argument('-p', '--param', metavar='KEY=VALUE', action='append', help='Parameter to send to renderer')
return parser.parse_args()
def main():
"""The command line entrypoint"""
args = get_args()
render_params = {kv.split('=')[0]: kv.split('=')[-1] for kv in args.param} if args.param else {}
# Prepare the params
for key, value in render_params.items():
if value.lower() == "true":
render_params[key] = True
elif value.lower() == "false":
render_params[key] = False
elif value.isdigit():
render_params[key] = int(value)
song = Song(args.input)
if args.format == 'text':
output = render_text(song, **render_params)
else:
output = render_html(song, **render_params)
if args.output:
with open(args.output, 'w') as html_file:
html_file.write(output)
else:
print(output)