]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blame - helpers/__init__.py
Use argparse for parser
[perso/Immae/Projets/Python/MusicSampler.git] / helpers / __init__.py
CommitLineData
1df30f07 1# -*- coding: utf-8 -*-
71715c04 2import argparse
bb69f62e
IB
3import sys
4import os
5
9b9dd12a
IB
6class Config:
7 def __init__(self, **kwargs):
8 for arg in kwargs:
9 setattr(self, arg, kwargs[arg])
10
11config = Config(yml_file="config.yml")
12
bb69f62e
IB
13def path():
14 if getattr(sys, 'frozen', False):
15 return sys._MEIPASS + "/"
16 else:
17 path = os.path.dirname(os.path.realpath(__file__))
18 return path + "/../"
19
cc008de4 20def parse_args():
71715c04
IB
21 argv = sys.argv[1:]
22 sys.argv = sys.argv[:1]
23 if "--" in argv:
24 index = argv.index("--")
25 kivy_args = argv[index+1:]
26 argv = argv[:index]
27
28 sys.argv.extend(kivy_args)
29
30 parser = argparse.ArgumentParser(description="A Music Sampler application.")
31 parser.add_argument("-c", "--config",
32 default="config.yml",
33 required=False,
34 help="Config file to load")
35 parser.add_argument("-V", "--version",
36 action="version",
37 help="Displays the current version and exits. Only use in bundled package",
38 version=show_version())
39 parser.add_argument('--',
40 dest="args",
41 help="Kivy arguments. All arguments after this are interpreted by Kivy. Pass \"-- --help\" to get Kivy's usage.")
42 args = parser.parse_args(argv)
43
44 config.yml_file = args.config
45
46def show_version():
47 if getattr(sys, 'frozen', False):
48 with open(path() + ".pyinstaller_commit", "r") as f:
49 return f.read()
50 else:
51 return "option '-v' can only be used in bundled package"
cc008de4 52
9b9dd12a
IB
53def yml_file():
54 return config.yml_file
55
98ff4305
IB
56def duration_to_min_sec(duration):
57 minutes = int(duration / 60)
58 seconds = int(duration) % 60
59 if minutes < 100:
60 return "{:2}:{:0>2}".format(minutes, seconds)
61 else:
62 return "{}:{:0>2}".format(minutes, seconds)