-
Notifications
You must be signed in to change notification settings - Fork 9
/
mql4_compiler.py
165 lines (115 loc) · 4.4 KB
/
mql4_compiler.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
import sublime, sublime_plugin
import os
import subprocess
import re
import sys
PLATFORM = sublime.platform()
METALANG = 'metalang.exe'
EXTENSION = '.mq4'
WINE = 'wine'
BASE_PATH = os.path.abspath(os.path.dirname(__file__))
METALANG_PATH = os.path.join(BASE_PATH, METALANG)
def which(file):
manual_path = os.path.join("/usr/bin", file)
if os.path.exists(manual_path):
return manual_path
manual_path = os.path.join("/usr/local/bin", file)
if os.path.exists(manual_path):
return manual_path
for dir in os.environ['PATH'].split(os.pathsep):
path = os.path.join(dir, file)
if os.path.exists(path):
return path
print ("PATH = {0}".format(os.environ['PATH']))
return None
class Mql4CompilerCommand(sublime_plugin.TextCommand):
def init(self):
view = self.view
if view.file_name() is not None :
self.dirname = os.path.realpath(os.path.dirname(view.file_name()))
self.filename = os.path.basename(view.file_name())
self.extension = os.path.splitext(self.filename)[1]
if PLATFORM != 'windows':
self.wine_path = which(WINE)
def isError(self):
iserror = False
if not os.path.exists(METALANG_PATH):
print (METALANG_PATH) # Debug
print ("Mqlcompiler | error: metalang.exe not found")
iserror = True
if PLATFORM != 'windows':
if not self.wine_path :
print ("Mqlcompiler | error: wine is not installed")
iserror = True
if self.view.file_name() is None :
# check if console..
print ("Mqlcompiler | error: Buffer has to be saved first")
iserror = True
else :
if self.extension != EXTENSION:
print ("Mqlcompiler | error: wrong file extension: ({0})".format(self.extension))
iserror = True
if self.view.is_dirty():
print ("Mqlcompiler | error: Save File before compiling")
iserror = True
return iserror
def runMetalang(self):
command = [METALANG_PATH,self.filename]
startupinfo = None
# hide pop-up window on windows
if PLATFORM == 'windows':
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
# executing exe files with wine on mac / linux
if PLATFORM != 'windows':
command.insert(0,self.wine_path)
# execution:
proc = subprocess.Popen(command,
cwd= self.dirname,
stdout=subprocess.PIPE,
shell=False,
startupinfo=startupinfo)
return proc.stdout.read()
def formatOutput(self , stdout):
output = ""
log_lines = re.split('\n',stdout)
group_files = []
for l in log_lines :
line = l.strip()
if not line:
continue
line_arr = re.split(';',line)
line_len = len(line_arr)
if line_len < 5 :
if re.match(r"^Exp file",line):
output+= "\n-----------------------\n"
output+= line + "\n"
if line_len == 5 :
fpath = line_arr[2].split("\\")[-1]
if fpath and not fpath in group_files:
group_files.append(fpath)
output += "\n-----------------------\n"
output += "file: {0}".format(fpath)
output += "\n-----------------------\n"
if line_arr[3]:
output+= "line {0} | {1}".format(line_arr[3],line_arr[4])
else:
output+= "{0}".format(line_arr[4])
output+= "\n"
return output
def newLogWindow(self, output):
window = self.view.window()
new_view = window.create_output_panel("mql4log")
new_view.run_command('erase_view')
new_view.run_command('append', {'characters': output})
window.run_command("show_panel", {"panel": "output.mql4log"})
sublime.status_message('Metalang')
pass
def run(self , edit):
self.init()
if self.isError():
return
stdout = self.runMetalang()
stdout = stdout.decode(encoding='UTF-8')
output = self.formatOutput(stdout)
self.newLogWindow(output)