forked from pychess/pychess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pychess
executable file
·270 lines (231 loc) · 8.07 KB
/
pychess
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/env python3
import os
import sys
# Fix environment to let pychess run in place
if getattr(sys, 'frozen', False):
this_dir = os.path.dirname(sys.executable)
else:
this_dir = os.path.dirname(os.path.abspath(__file__))
if os.path.isdir(os.path.join(this_dir, "lib/pychess")) and \
os.path.join(this_dir, "lib") not in sys.path:
sys.path = [os.path.join(this_dir, "lib")] + sys.path
# Create splash screen
try:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
splash = Gtk.Window()
splash.props.type_hint = Gdk.WindowTypeHint.SPLASHSCREEN
splash.set_decorated(False)
splash.set_position(Gtk.WindowPosition.CENTER)
from pychess.System import prefix
splash.add(Gtk.Image().new_from_file(prefix.addDataPrefix("glade/about.png")))
splash.show_all()
while Gtk.events_pending():
Gtk.main_iteration()
except:
print("Failed to create splash screen")
import argparse
import asyncio
import gettext
import locale
import logging
if not getattr(sys, 'frozen', False):
try:
import faulthandler
faulthandler.enable()
except ImportError:
pass
# Check requirements
if sys.version_info < (3, 4, 2):
print('ERROR: PyChess requires Python >= 3.4.2')
sys.exit(1)
try:
import cairo
cairo.version
except ImportError:
print("ERROR: PyChess requires python-cairo to be installed.")
sys.exit(1)
try:
import gi
except ImportError:
print("ERROR: PyChess requires pygobject to be installed.")
sys.exit(1)
try:
gi.require_version("cairo", "1.0")
gi.require_version("GLib", "2.0")
gi.require_version("Gdk", "3.0")
gi.require_version("GdkPixbuf", "2.0")
gi.require_version("GObject", "2.0")
gi.require_version("Gtk", "3.0")
gi.require_version("GtkSource", "3.0")
gi.require_version("Pango", "1.0")
gi.require_version("PangoCairo", "1.0")
gi.require_version("Rsvg", "2.0")
from gi.repository import GLib
except ValueError as e:
print("ERROR: Not all dependencies installed! You can find them in INSTALL")
print(e)
sys.exit(1)
try:
import sqlalchemy
sqlalchemy.__version__
except ImportError:
print("ERROR: PyChess requires sqlalchemy to be installed")
sys.exit(1)
try:
import psutil
psutil.__version__
except ImportError:
print("ERROR: PyChess requires psutil to be installed")
sys.exit(1)
# Ensure access to data store
try:
import pychess
from pychess.System.prefix import addDataPrefix, getDataPrefix, isInstalled
except ImportError:
print("ERROR: Could not import modules.")
print("Please try to run pychess as stated in the INSTALL file")
sys.exit(1)
# Parse command line arguments
try:
from pychess.System.Log import log, LoggerWriter, setup_glib_logging
except ImportError:
pass
if getattr(sys, 'frozen', False):
sys.stdout = LoggerWriter(logging.getLogger("STDOUT"), logging.INFO)
sys.stderr = LoggerWriter(logging.getLogger("STDERR"), logging.ERROR)
log_viewer = False
chess_file = sys.argv[1] if len(sys.argv) > 1 else None
ics_host = None
ics_port = None
version = "%s (%s)" % (pychess.VERSION, pychess.VERSION_NAME)
description = "The PyChess chess client, version %s." % version
parser = argparse.ArgumentParser(description=description)
parser.add_argument('--version', action='version',
version="%(prog)s" + " %s" % version)
parser.add_argument('--log-debug', action='store_true',
help='change default logging level from INFO to DEBUG')
parser.add_argument('--no-gettext', action='store_true',
help='turn off locale translations')
parser.add_argument('--log-viewer', action='store_true',
help='enable Log Viewer menu')
parser.add_argument('--purge-recent', action='store_true',
help='purge recent games menu')
parser.add_argument('--ics-host', action='store',
help='the hostname of internet chess server (default is freechess.org)')
parser.add_argument('--ics-port', action='store', type=int,
help='the connection port of internet chess server (default is 5000)')
parser.add_argument('chess_file', nargs='?', metavar='chessfile',
help='a chess file in PGN, EPD, FEN, or HTML (Chess Alpha 2 Diagram) format')
parser.add_argument('--gbulb-loop', action='store_true',
help='use gbulb event loop based on GLib')
parser.add_argument('--no-version-check', action='store_true',
help='disable online version check')
args = parser.parse_args()
log_debug = args.log_debug
no_gettext = args.no_gettext
log_viewer = args.log_viewer
purge_recent = args.purge_recent
chess_file = args.chess_file
ics_host = args.ics_host
ics_port = args.ics_port
version_check = not args.no_version_check
gbulb_loop = args.gbulb_loop
if gbulb_loop:
try:
from pychess.external import gbulb
gbulb.install(gtk=True)
except ImportError:
print("ERROR: PyChess requires gbulb to be installed.")
sys.exit(1)
# Set sqlite temp dir path
os.environ["SQLITE_TMPDIR"] = os.path.expanduser("~")
# Set up translations
if no_gettext:
os.environ['LANG'] = "C"
locale.setlocale(locale.LC_ALL, 'C')
else:
locale.setlocale(locale.LC_ALL, '')
# http://stackoverflow.com/questions/3678174/python-gettext-doesnt-load-translations-on-windows
if sys.platform.startswith('win'):
if os.getenv('LANG') is None:
lang, enc = locale.getdefaultlocale()
os.environ['LANG'] = lang
locale.setlocale(locale.LC_ALL, '')
domain = "pychess"
if isInstalled():
if sys.platform == "win32":
locale_dir = os.path.join(os.path.dirname(getDataPrefix()), "locale")
else:
locale_dir = None
else:
locale_dir = addDataPrefix("lang")
gettext.install(domain, localedir=locale_dir, names=('ngettext',))
# http://stackoverflow.com/questions/10094335/how-to-bind-a-text-domain-to-a-local-folder-for-gettext-under-gtk3
if sys.platform == "win32":
from ctypes import cdll
libintl = cdll.LoadLibrary("libintl-8")
libintl.bindtextdomain(domain, locale_dir)
libintl.bind_textdomain_codeset(domain, 'UTF-8')
else:
locale.bindtextdomain(domain, locale_dir)
try:
from pychess.compat import create_task
from pychess.System.LogEmitter import GLogHandler, logemitter
from pychess.System.prefix import getUserDataPrefix, addUserDataPrefix
from pychess.System import conf
from pychess.Main import PyChess
except ImportError:
raise
pass
conf.no_gettext = no_gettext
# Start logging
if log_debug:
setup_glib_logging()
if log_viewer:
log.logger.addHandler(GLogHandler(logemitter))
log.logger.setLevel(logging.DEBUG if log_debug is True else logging.INFO)
oldlogs = [l for l in os.listdir(getUserDataPrefix())
if l.endswith(".log")]
conf.set("max_log_files", conf.get("max_log_files"))
oldlogs.sort()
lel_oldlogs = len(oldlogs)
while lel_oldlogs > conf.get("max_log_files"):
try:
os.remove(addUserDataPrefix(oldlogs[0]))
del oldlogs[0]
except OSError:
pass
lel_oldlogs -= 1
@asyncio.coroutine
def start(gtk_app):
yield from asyncio.sleep(0)
gtk_app.register()
gtk_app.activate()
def glib_update(main_context, loop):
while main_context.pending():
main_context.iteration(False)
loop.call_later(.01, glib_update, main_context, loop)
if gbulb_loop:
loop = asyncio.get_event_loop()
elif sys.platform == "win32":
from asyncio.windows_events import ProactorEventLoop
loop = ProactorEventLoop()
asyncio.set_event_loop(loop)
else:
loop = asyncio.SelectorEventLoop()
asyncio.set_event_loop(loop)
gtk_app = PyChess(log_viewer, purge_recent, chess_file, ics_host, ics_port, loop, splash, version_check)
if log_debug:
loop.set_debug(enabled=True)
try:
if gbulb_loop:
loop.run_forever(application=gtk_app)
else:
main_context = GLib.MainContext.default()
create_task(start(gtk_app))
glib_update(main_context, loop)
loop.run_forever()
finally:
loop.close()