-
Notifications
You must be signed in to change notification settings - Fork 5
/
__init__.py
152 lines (121 loc) · 3.63 KB
/
__init__.py
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
# This file is part of Xpra.
# Copyright (C) 2010 Nathaniel Smith <njs@pobox.com>
# Copyright (C) 2011-2014 Antoine Martin <antoine@devloop.org.uk>
# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
# later version. See the file COPYING for details.
import os as os
import sys as sys
_init_done = False
def init(prgname=None, appname=None):
""" do whatever is needed to prepare an application for running,
some platforms may initialize logging to file, etc
If the names are supplied, we call set_name()
"""
global _init_done
if prgname is not None or appname is not None:
set_default_name(prgname, appname)
set_name()
if not _init_done:
_init_done = True
do_init()
#platforms can override this
def do_init():
pass
_prgname = None
_appname = None
def set_default_name(prgname=None, appname=None):
#sets the default prg and app names
global _prgname, _appname
if prgname is not None:
_prgname = prgname
if appname is not None:
_appname = appname
#platforms can override this
def command_error(message):
print(message)
def command_info(message):
print(message)
_clean_done = False
def clean():
global _clean_done
if not _clean_done:
_clean_done = True
do_clean()
#platforms can override this
def do_clean():
pass
_name_set = False
def set_name(prgname=_prgname, appname=_appname):
global _name_set
if not _name_set:
_name_set = True
set_prgname(prgname)
set_application_name(appname)
def _glib():
from xpra.gtk_common.gobject_compat import import_glib
return import_glib()
#platforms can override this
def set_prgname(name):
try:
_glib().set_prgname(name)
except:
pass
def get_prgname():
global _prgname
return _prgname
#platforms can override this
def set_application_name(name):
try:
_glib().set_application_name(name)
except:
pass
def get_application_name():
global _appname
return _appname
def get_main_fallback():
#this is used on some platforms (win32),
#when the user tries to run "xpra" without arguments
#returns the function to run as fallback (or None)
return None
def get_username():
return do_get_username()
def do_get_username():
try:
import pwd
return pwd.getpwuid(os.getuid()).pw_name
except:
try:
import getpass
return getpass.getuser()
except:
pass
return ""
def platform_import(where, pm, required, *imports):
if os.name == "nt":
p = "win32"
elif sys.platform.startswith("darwin"):
p = "darwin"
elif os.name == "posix":
p = "xposix"
else:
raise OSError("Unknown OS %s" % (os.name))
module = "xpra.platform.%s" % p
if pm:
module += ".%s" % pm
#cannot log this early! (win32 needs log to file redirection..)
#log = Logger("platform", "import")
#log("importing %s from %s (required=%s)" % (imports, module, required))
platform_module = __import__(module, {}, {}, imports)
assert platform_module
for x in imports:
found = hasattr(platform_module, x)
if not found:
if required:
raise Exception("could not find %s in %s" % (x, module))
else:
continue
v = getattr(platform_module, x)
where[x] = v
platform_import(globals(), None, True, "do_init", "do_clean")
platform_import(globals(), None, False, "set_prgname", "set_application_name",
"command_error", "command_info", "get_main_fallback", "do_get_username")