-
Notifications
You must be signed in to change notification settings - Fork 1
/
glader_core.py
61 lines (50 loc) · 1.62 KB
/
glader_core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
""" Glader - Core
Functions and globals that are vital to other Glader modules.
-Christopher Welborn 03-22-20
"""
import os
import sys
NAME = 'Glader'
__version__ = '0.2.5'
VERSIONSTR = '{} v. {}'.format(NAME, __version__)
# Set with -D,--debug command-line options.
DEBUG = ('-D' in sys.argv) or ('--debug' in sys.argv)
# Config file should always liver under /home/..
CONFIGDIR = os.path.expanduser('~/.config/glader')
CONFIGFILE = os.path.join(CONFIGDIR, 'glader.conf')
SCRIPT = os.path.split(os.path.abspath(sys.argv[0]))[1]
SCRIPTDIR = os.path.abspath(sys.path[0])
def debug(*args, **kwargs):
""" Prints to stderr, if DEBUG is truthy. """
if not DEBUG:
return None
kwargs['file'] = sys.stderr
print(*args, **kwargs)
def ensure_config_dir():
""" Ensure the config dir exists. Create it if needed. """
if os.path.isdir(CONFIGDIR):
debug(f'Config dir: {CONFIGDIR}')
return None
try:
os.makedirs(CONFIGDIR)
debug(f'Created directory: {CONFIGDIR}')
except EnvironmentError as ex:
print(
f'\nUnable to create config dir: {CONFIGDIR}\n{ex}',
file=sys.stderr,
)
sys.exit(1)
def import_fail(err):
""" Fail with a friendlier message when imports fail. """
msglines = (
'\n{namever} requires some third-party libraries.',
'Please install requirements using \'pip\' or your package manager.',
'The import error was:',
' {err}\n'
)
print(
'\n'.join(msglines).format(namever=VERSIONSTR, err=err),
file=sys.stderr,
)
sys.exit(1)