-
Notifications
You must be signed in to change notification settings - Fork 2
/
chirpw
executable file
·167 lines (143 loc) · 4.58 KB
/
chirpw
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python3
#
# Copyright 2014 Dan Smith <[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 glob
import os
import six
import sys
import os
import locale
import gettext
import argparse
import logging
import urllib
if six.PY3:
from gi import pygtkcompat
pygtkcompat.enable()
pygtkcompat.enable_gtk(version='3.0')
from chirp import chirp_common
from chirp import directory
from chirp import logger
from chirp import elib_intl
from chirp import platform
from chirp.ui import config
directory.safe_import_drivers()
LOG = logging.getLogger("chirpw")
# Does not work with python3 chirpw
# urllib.URLopener.version = chirp_common.http_user_agent()
localepath = platform.get_platform().find_resource("locale")
conf = config.get()
manual_language = conf.get("language", "state")
langs = []
if manual_language and manual_language != "Auto":
lang_codes = {"English": "en_US",
"Polish": "pl",
"Italian": "it",
"Dutch": "nl",
"German": "de",
"Hungarian": "hu",
"Russian": "ru",
"Portuguese (BR)": "pt_BR",
"French": "fr",
"Spanish": "es_ES",
}
try:
LOG.info("Language: %s", lang_codes[manual_language])
langs = [lang_codes[manual_language]]
except KeyError:
LOG.error("Unsupported language `%s'" % manual_language)
else:
lc, encoding = locale.getdefaultlocale()
if (lc):
langs = [lc]
try:
langs += os.getenv("LANG").split(":")
except:
pass
try:
if os.name == "nt":
elib_intl._putenv("LANG", langs[0])
else:
os.putenv("LANG", langs[0])
except IndexError:
pass
path = "locale"
gettext.bindtextdomain("CHIRP", localepath)
gettext.textdomain("CHIRP")
lang = gettext.translation("CHIRP", localepath, languages=langs,
fallback=True)
# Python <2.6 does not have str.format(), which chirp uses to make translation
# strings nicer. So, instead of installing the gettext standard "_()" function,
# we can install our own, which returns a string of the following class,
# which emulates the new behavior, thus allowing us to run on older Python
# versions.
class CompatStr(str):
def format(self, **kwargs):
base = lang.gettext(self)
for k, v in kwargs.items():
base = base.replace("{%s}" % k, str(v))
return base
pyver = sys.version.split()[0]
try:
vmaj, vmin, vrel = pyver.split(".", 3)
except:
vmaj, vmin = pyver.split(".", 2)
vrel = 0
if int(vmaj) == 2 and int(vmin) < 6:
# Python <2.6, emulate str.format()
import __builtin__
def lang_with_format(string):
return CompatStr(string)
__builtin__._ = lang_with_format
else:
# Python >=2.6, use normal gettext behavior
lang.install()
parser = argparse.ArgumentParser()
parser.add_argument("files", metavar="file", nargs='*', help="File to open")
parser.add_argument("--module", metavar="module",
help="Load module on startup")
logger.add_version_argument(parser)
parser.add_argument("--profile", action="store_true",
help="Enable profiling")
logger.add_arguments(parser)
args = parser.parse_args()
logger.handle_options(args)
a = None
if True:
from chirp.ui import mainapp
a = mainapp.ChirpMain()
# Be sure to load module before opening files
if args.module:
a.load_module(args.module)
for i in args.files:
LOG.info("Opening %s", i)
a.do_open(i)
a.show()
if args.profile:
import cProfile
import pstats
import gtk
cProfile.run("gtk.main()", "chirpw.stats")
p = pstats.Stats("chirpw.stats")
p.sort_stats("cumulative").print_stats(10)
else:
import gtk
gtk.main()
if config._CONFIG:
config._CONFIG.set("last_dir",
platform.get_platform().get_last_dir(),
"state")
config._CONFIG.save()