This repository has been archived by the owner on Sep 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
draft.in
122 lines (101 loc) · 3.63 KB
/
draft.in
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python3
# Copyright (C) 2017 Saiful Bari Khan <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import locale
import gettext
import signal
import os
import logging
import argparse
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('GIRepository', '2.0')
from gi.repository import GIRepository, Gio, Gtk
SOURCE_DIR = os.path.abspath(os.path.dirname(__file__))
LOCALE_DIR = '@LOCALEDIR@'
PKG_DATA_DIR = '@PKGDATADIR@'
DATA_DIR = '@DATADIR@'
GSETTINGS_SCHEMA_DIR = '@GSETTINGSSCHEMADIR@'
PYTHON_DIR = '@PYTHONDIR@'
LOG_FORMAT = '%(asctime)s %(levelname)s\t%(message)s'
LOG_DATE_FORMAT = '%H:%M:%S'
def set_exception_hook():
"""Configures sys.excepthook to enforce Gtk application exiting."""
def new_hook(etype, evalue, etb):
old_hook(etype, evalue, etb)
while Gtk.main_level():
Gtk.main_quit()
sys.exit()
old_hook = sys.excepthook
sys.excepthook = new_hook
def set_up_parser():
"""Sets up the commandline argument parser"""
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--debug', action='store_true',
default=False, help="enable debugging the program")
parser.add_argument("-t", "--test", action="store_true",
default=False, help="enable code testing")
return parser
def set_log_level(parser):
"""Sets application log level according to debug value."""
args = parser.parse_args()
if args.debug:
logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT,
datefmt=LOG_DATE_FORMAT)
# Gtk hates "-d" switch, so lets drop it
if '-d' in sys.argv:
sys.argv.remove('-d')
if '--debug' in sys.argv:
sys.argv.remove('--debug')
else:
logging.basicConfig(level=logging.WARN, format=LOG_FORMAT,
datefmt=LOG_DATE_FORMAT)
# TODO: Add test harness
def set_internationalization():
"""Set application internationalization"""
locale.bindtextdomain('draft', LOCALE_DIR)
locale.textdomain('draft')
gettext.bindtextdomain('draft', LOCALE_DIR)
gettext.textdomain('draft')
def set_resources():
"""Sets application ressource file."""
resource = Gio.resource_load(os.path.join(DATA_DIR, 'draft.gresource'))
Gio.resources_register(resource)
def run_application():
"""Runs the application and returns its exit code."""
from draftsrc.app import Application
from draftsrc import db
application = Application()
signal.signal(signal.SIGINT, signal.SIG_DFL)
try:
ret = application.run(sys.argv)
except SystemExit as e:
ret = e.code
finally:
db.final_text_updater.activate()
return ret
def main():
"""Set up the environment and run Draft"""
set_exception_hook()
parser = set_up_parser()
set_log_level(parser)
set_internationalization()
set_resources()
return run_application()
if __name__ == "__main__":
sys.path.insert(1, PYTHON_DIR)
exit_status = main()
sys.exit(exit_status)