-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEditra.edit.py
More file actions
executable file
·148 lines (101 loc) · 3.5 KB
/
Editra.edit.py
File metadata and controls
executable file
·148 lines (101 loc) · 3.5 KB
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
#!/usr/bin/env python
import socket
import sys
import os
import subprocess
import threading
import time
import json
PORT = 35033
try:
import renpy
from renpy.editor import Editor as EditorBase
from renpy.exports import fsencode
except ImportError:
class EditorBase(object):
pass
class Renpy(object):
pass
renpy = Renpy()
renpy.windows = False
renpy.macintosh = False
renpy.linux = True
class Editor(EditorBase):
def begin(self, new_window=False, **kwargs):
self.command = { "new_window" : new_window, "files" : [ ] }
def open(self, filename, line=None, **kwargs):
d = { "name" : os.path.abspath(filename) }
if line is not None:
d["line"] = line
self.command["files"].append(d)
def send_command(self):
"""
Tries connecting to editra and sending the command.
Returns true if the command has been sent, and false if the command
was not sent.
"""
try:
s = socket.socket()
s.connect(("127.0.0.1", PORT))
except socket.error:
s.close()
return False
f = s.makefile("w+")
json.dump(self.command, f)
f.write("\n")
f.flush()
result = f.readline()
if not result:
raise Exception("Editra closed control channel.")
result = json.loads(result)
if "error" in result:
raise Exception("Editra error: {0}".format(result["error"]))
return True
def launch_editra(self):
"""
Tries to launch Editra.
"""
DIR = os.path.abspath(os.path.dirname(__file__))
if renpy.windows:
config_dir = os.path.join(DIR, ".Editra")
elif renpy.macintosh:
config_dir = os.path.join(DIR, "Editra-mac.app/Contents/Resources/.Editra")
else:
config_dir = os.path.join(DIR, ".Editra")
if not os.path.exists(config_dir):
os.mkdir(config_dir)
plugin_cfg = os.path.join(config_dir, "plugin.cfg")
if not os.path.exists(plugin_cfg):
with open(plugin_cfg, "w") as f:
f.write("renpy_editra=True")
if renpy.windows:
env = os.environ.copy()
env[b'PYENCHANT_LIBRARY_PATH'] = fsencode(os.path.join(DIR, "lib", "windows-i686", "libenchant-1.dll"))
subprocess.Popen([
os.path.join(DIR, "lib", "windows-i686", "pythonw.exe"),
"-EOO",
os.path.join(DIR, "Editra/editra"),
], cwd=DIR, env=env)
elif renpy.macintosh:
subprocess.Popen([ "open", "-a", fsencode(os.path.join(DIR, "Editra-mac.app")) ])
else:
subprocess.Popen([ fsencode(os.path.join(DIR, "Editra/editra")) ])
def end(self, **kwargs):
if self.send_command():
return
self.command["new_window"] = False
self.launch_editra()
deadline = time.time() + 10.0
while time.time() < deadline:
if self.send_command():
return
time.sleep(.1)
raise Exception("Launching Editra failed.")
def main():
e = Editor()
e.begin()
for i in sys.argv[1:]:
e.open(i)
e.end()
if __name__ == "__main__":
main()