-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_komunitin_lite
executable file
·98 lines (78 loc) · 2.96 KB
/
run_komunitin_lite
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python3
import os
import sys
import inspect
import configparser
import logging
import gettext
from glob import glob
from komunitin_lite import get_base_path
from komunitin_lite.core.local_storage import USER_LOCAL_DIR, get_local_data
def get_app_config(base_path):
config_file = os.path.join(base_path, "config.ini")
if not os.path.isfile(config_file):
config_file = os.path.join(base_path, "config_default.ini")
config = configparser.ConfigParser()
try:
config.read(config_file)
except Exception as e:
print("Error reading configuration data: {}".format(e))
sys.exit()
return config
def get_user_local_config():
# Create user local dir if doesn't exist (only first time)
if not os.path.exists(USER_LOCAL_DIR):
try:
os.makedirs(USER_LOCAL_DIR)
except Exception as e:
print("Error creating user local directory: {}".format(e))
sys.exit()
# Read user local config
try:
local_config = get_local_data(config=True)
except Exception as e:
print("Error reading user local configuration file: {}".format(e))
sys.exit()
return local_config
def get_available_languages(base_path):
langs_paths = glob(os.path.join(base_path, 'po', '*', 'LC_MESSAGES',
'base.mo'))
return [pth.split(os.path.sep)[-3] for pth in langs_paths]
if __name__ == "__main__":
BASE_PATH = os.path.dirname(inspect.getfile(get_base_path))
config = get_app_config(BASE_PATH)
user_local_config = get_user_local_config()
# Logging to local file
log_level = config["logger"].get("logging_level", "ERROR")
logger = logging.getLogger('KomLite')
logger.setLevel(getattr(logging, log_level))
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler = logging.FileHandler(
os.path.join(USER_LOCAL_DIR, 'komunitin_lite.log'))
file_handler.setLevel(log_level)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
# console_handler = logging.StreamHandler()
# console_handler.setLevel(log_level)
# console_handler.setFormatter(formatter)
# logger.addHandler(console_handler)
# Install language
langs_available = get_available_languages(BASE_PATH)
lang = user_local_config.get('language', 'en')
language = gettext.translation(
'base', localedir=os.path.join(BASE_PATH, 'po'), languages=[lang])
language.install()
# Insert data in config to be available in the whole app
config["app_data"] = {
"languages": ",".join(langs_available),
"base_path": BASE_PATH
}
if "--cli" in sys.argv:
from komunitin_lite.cli.cli import CommandLineInterface
cli = CommandLineInterface(config=config)
cli.run()
else:
from komunitin_lite.gtk3.application import Application
app = Application(config=config)
app.run(sys.argv)