-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
172 lines (139 loc) · 5.04 KB
/
main.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python2
import sys
if sys.platform == "win32":
import ctypes
SEM_FAILCRITICALERRORS = 1
ctypes.windll.kernel32.SetErrorMode(SEM_FAILCRITICALERRORS)
ctypes.windll.user32.SetProcessDPIAware()
import os, getopt, traceback, warnings
if not hasattr(sys, "frozen"):
module_dir = os.path.dirname(os.path.realpath(__file__))
os.chdir(module_dir)
sys.path.append(os.path.join(module_dir, "..", "fsmonitor"))
else:
warnings.filterwarnings("ignore")
import wx
from async import coroutine
class DevoAppHandler(object):
def __init__(self, app):
self.app = app
@coroutine
def process_args(self, args, cwd):
try:
mainframe = self.app.mainframe
if mainframe.IsIconized():
mainframe.Iconize(False)
mainframe.Raise()
args = DevoArgs(args, cwd)
if args.project:
yield mainframe.OpenProject(args.project)
for filename in args.filenames:
yield mainframe.OpenEditor(filename)
except Exception:
pass
yield True
class DevoArgs(object):
def __init__(self, args, cwd):
self.raw_args = args
opts, args = getopt.gnu_getopt(args, "", ["project=", "new-instance", "no-fork"])
self.project = None
self.new_instance = False
self.fork = True
for opt, arg in opts:
if opt == "--project":
self.project = arg
elif opt == "--new-instance":
self.new_instance = True
elif opt == "--no-fork":
self.fork = False
self.filenames = [os.path.join(cwd, filename) for filename in args]
class DevoApp(wx.App):
def __init__(self):
wx.App.__init__(self, redirect=False)
self.SetAppName("Devo")
self.first_drop = True
self.mainframe = None
self.listener = None
wx.Log_SetActiveTarget(wx.LogStderr())
def OnInit(self):
return True
def Startup(self, args):
from dialogs import dialogs
try:
import async_wx
from app_instance import AppListener, get_app_instance
from fileutil import get_user_config_dir, mkpath
from log_file import get_log_file
async_wx.set_wx_scheduler()
config_dir = get_user_config_dir("devo")
try:
mkpath(config_dir)
except OSError as e:
message = "Failed to create Devo configuration directory:\n\n" + str(e)
dialogs.error(None, message, "Initialization Error")
return False
if not args.new_instance:
instance = get_app_instance("devo")
if instance:
try:
if instance.call("process_args", args.raw_args, os.getcwd()):
return False
except Exception:
pass
self.listener = AppListener("devo", DevoAppHandler(self))
if hasattr(sys, "frozen"):
log_filename = os.path.join(config_dir, "errors.log")
self.log_file = get_log_file(log_filename)
sys.stdout, self.stdout = self.log_file, sys.stdout
sys.stderr, self.stderr = self.log_file, sys.stderr
from mainframe import MainFrame
self.mainframe = MainFrame.__new__(MainFrame, args)
self.mainframe.__init__(args)
self.SetTopWindow(self.mainframe)
self.Bind(wx.EVT_END_SESSION, self.OnEndSession)
return True
except Exception:
message = "Devo failed to initialize due to the following error:\n\n" + traceback.format_exc()
if self.mainframe:
try:
self.mainframe.Destroy()
except Exception:
pass
dialogs.error(None, message, "Initialization Error")
return False
def MacOpenFile(self, filename):
if not self.first_drop or hasattr(sys, "frozen"):
self.mainframe.OpenEditor(filename)
self.first_drop = False
def MacReopenApp(self):
if self.mainframe.IsIconized():
self.mainframe.Restore()
def OnEndSession(self, evt):
if self.mainframe:
self.mainframe.OnClose(evt)
def Shutdown(self):
if self.listener:
self.listener.shutdown()
if hasattr(sys, "frozen"):
self.log_file.flush()
def main():
if sys.platform == "darwin" and hasattr(sys, "frozen"):
args = []
else:
args = sys.argv[1:]
try:
args = DevoArgs(args, os.getcwd())
except getopt.GetoptError as e:
sys.stderr.write("devo: error: %s\n" % e)
sys.exit(1)
if args.fork and sys.platform not in ("win32", "darwin"):
if os.fork() != 0:
os._exit(0)
app = DevoApp()
if app.Startup(args):
app.MainLoop()
app.Shutdown()
else:
sys.exit(1)
if __name__ == "__main__":
main()