mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
more py3 porting
Also DRYer the socket addresses creation
This commit is contained in:
parent
8d73d33733
commit
8dc5c82fa0
@ -1,6 +1,6 @@
|
|||||||
#!/usr/bin/env python2
|
#!/usr/bin/env python2
|
||||||
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
|
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
|
||||||
from __future__ import with_statement, print_function
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
|
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||||
@ -12,6 +12,7 @@ from threading import Thread
|
|||||||
from calibre import force_unicode
|
from calibre import force_unicode
|
||||||
from calibre.constants import iswindows, get_windows_username, islinux, filesystem_encoding, ispy3
|
from calibre.constants import iswindows, get_windows_username, islinux, filesystem_encoding, ispy3
|
||||||
from calibre.utils.filenames import ascii_filename
|
from calibre.utils.filenames import ascii_filename
|
||||||
|
from polyglot.functools import lru_cache
|
||||||
|
|
||||||
VADDRESS = None
|
VADDRESS = None
|
||||||
|
|
||||||
@ -26,61 +27,38 @@ def eintr_retry_call(func, *args, **kwargs):
|
|||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
||||||
def gui_socket_address():
|
@lru_cache()
|
||||||
if gui_socket_address.ans is None:
|
def socket_address(which):
|
||||||
if iswindows:
|
if iswindows:
|
||||||
gui_socket_address.ans = r'\\.\pipe\CalibreGUI'
|
ans = r'\\.\pipe\Calibre' + which
|
||||||
try:
|
try:
|
||||||
user = get_windows_username()
|
user = get_windows_username()
|
||||||
except:
|
except Exception:
|
||||||
user = None
|
user = None
|
||||||
|
if user:
|
||||||
|
user = ascii_filename(user).replace(' ', '_')
|
||||||
if user:
|
if user:
|
||||||
user = ascii_filename(user).replace(' ', '_')
|
ans += '-' + user[:100] + 'x'
|
||||||
if user:
|
else:
|
||||||
gui_socket_address.ans += '-' + user[:100] + 'x'
|
user = force_unicode(os.environ.get('USER') or os.path.basename(os.path.expanduser('~')), filesystem_encoding)
|
||||||
|
sock_name = '{}-calibre-{}.socket'.format(ascii_filename(user).replace(' ', '_'), which)
|
||||||
|
if islinux:
|
||||||
|
ans = '\0' + sock_name
|
||||||
else:
|
else:
|
||||||
user = os.environ.get('USER', '')
|
from tempfile import gettempdir
|
||||||
if not user:
|
tmp = force_unicode(gettempdir(), filesystem_encoding)
|
||||||
user = os.path.basename(os.path.expanduser('~'))
|
ans = os.path.join(tmp, sock_name)
|
||||||
if islinux:
|
if not ispy3 and not isinstance(ans, bytes):
|
||||||
gui_socket_address.ans = (u'\0%s-calibre-gui.socket' % ascii_filename(force_unicode(user)))
|
ans = ans.encode(filesystem_encoding)
|
||||||
else:
|
return ans
|
||||||
from tempfile import gettempdir
|
|
||||||
tmp = gettempdir()
|
|
||||||
gui_socket_address.ans = os.path.join(tmp, user+'-calibre-gui.socket')
|
def gui_socket_address():
|
||||||
if not ispy3 and not isinstance(gui_socket_address.ans, bytes):
|
return socket_address('GUI' if iswindows else 'gui')
|
||||||
gui_socket_address.ans = gui_socket_address.ans.encode(filesystem_encoding)
|
|
||||||
return gui_socket_address.ans
|
|
||||||
|
|
||||||
|
|
||||||
def viewer_socket_address():
|
def viewer_socket_address():
|
||||||
if viewer_socket_address.ans is None:
|
return socket_address('Viewer' if iswindows else 'viewer')
|
||||||
if iswindows:
|
|
||||||
viewer_socket_address.ans = r'\\.\pipe\CalibreViewer'
|
|
||||||
try:
|
|
||||||
user = get_windows_username()
|
|
||||||
except:
|
|
||||||
user = None
|
|
||||||
if user:
|
|
||||||
user = ascii_filename(user).replace(' ', '_')
|
|
||||||
if user:
|
|
||||||
viewer_socket_address.ans += '-' + user[:100] + 'x'
|
|
||||||
else:
|
|
||||||
user = os.environ.get('USER', '')
|
|
||||||
if not user:
|
|
||||||
user = os.path.basename(os.path.expanduser('~'))
|
|
||||||
if islinux:
|
|
||||||
viewer_socket_address.ans = (u'\0%s-calibre-viewer.socket' % ascii_filename(force_unicode(user)))
|
|
||||||
else:
|
|
||||||
from tempfile import gettempdir
|
|
||||||
tmp = gettempdir()
|
|
||||||
viewer_socket_address.ans = os.path.join(tmp, user+'-calibre-viewer.socket')
|
|
||||||
if not ispy3 and not isinstance(viewer_socket_address.ans, bytes):
|
|
||||||
viewer_socket_address.ans = viewer_socket_address.ans.encode(filesystem_encoding)
|
|
||||||
return viewer_socket_address.ans
|
|
||||||
|
|
||||||
|
|
||||||
gui_socket_address.ans = viewer_socket_address.ans = None
|
|
||||||
|
|
||||||
|
|
||||||
class RC(Thread):
|
class RC(Thread):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user