-
Notifications
You must be signed in to change notification settings - Fork 0
/
itrade_ext.py
264 lines (218 loc) · 8.97 KB
/
itrade_ext.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/env python
# ============================================================================
# Project Name : iTrade
# Module Name : itrade_ext.py
#
# Description: Extensions and Plugins
#
# The Original Code is iTrade code (http://itrade.sourceforge.net).
#
# The Initial Developer of the Original Code is Gilles Dumortier.
#
# Portions created by the Initial Developer are Copyright (C) 2004-2008 the
# Initial Developer. All Rights Reserved.
#
# Contributor(s):
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see http://www.gnu.org/licenses/gpl.html
#
# History Rev Description
# 2007-01-21 dgil Wrote it from scratch (inspired from leoPlugins.py)
# ============================================================================
# ============================================================================
# Imports
# ============================================================================
# python system
import logging
import glob
import os
import re
import imp
# iTrade system
from itrade_logging import *
import itrade_config
from itrade_market import market2place
from itrade_login import loggedLoginConnector
from itrade_defs import *
# ============================================================================
# globals
# ============================================================================
loadedModules = {} # keys are module names, values are modules ref
# ============================================================================
# ConnectorRegistry
# ============================================================================
class ConnectorRegistry(object):
def __init__(self):
self.m_conn = []
def register(self,market,place,qlist,qtag,connector,bDefault=True):
self.m_conn.append((market,place,bDefault,connector,qlist,qtag))
#print 'Register %s for market :' % self, market,' qlist:',qlist,' qtag:',qtag
return True
def get(self,market,qlist,qtag,place=None,name=None):
if place==None:
place = market2place(market)
if name:
for amarket,aplace,adefault,aconnector,aqlist,aqtag in self.m_conn:
#print amarket,aqlist,aqtag,aplace,aconnector.name(),adefault
if market==amarket and place==aplace and aconnector.name()==name and (aqlist==QLIST_ANY or aqlist==qlist) and (qtag==QTAG_ANY or aqtag==qtag):
return aconnector
else:
for amarket,aplace,adefault,aconnector,aqlist,aqtag in self.m_conn:
if market==amarket and place==aplace and (aqlist==QLIST_ANY or qlist==aqlist) and adefault and (qtag==QTAG_ANY or aqtag==qtag):
return aconnector
return None
def list(self,market,qlist,place):
lst = []
for amarket,aplace,adefault,aconnector,aqlist,aqtag in self.m_conn:
if amarket==market and aplace==place and (aqlist==QLIST_ANY or aqlist==qlist):
lst.append((aconnector.name(),amarket,aplace,adefault,aconnector,aqlist,aqtag))
return lst
# ============================================================================
# Export Live and Import Registries
# ============================================================================
try:
ignore(gLiveRegistry)
except NameError:
gLiveRegistry = ConnectorRegistry()
registerLiveConnector = gLiveRegistry.register
getLiveConnector = gLiveRegistry.get
listLiveConnector = gLiveRegistry.list
def getDefaultLiveConnector(market,list,place=None):
# try live connector
ret = getLiveConnector(market,list,QTAG_LIVE,place)
if ret:
# check live connector is logged
if loggedLoginConnector(ret.name()):
return ret
# no live connector or not logged : fall-back to differed connector
ret = getLiveConnector(market,list,QTAG_DIFFERED,place)
if ret==None:
print 'No default connector %s for market :' % market,' qlist:',list
return ret
try:
ignore(gImportRegistry)
except NameError:
gImportRegistry = ConnectorRegistry()
registerImportConnector = gImportRegistry.register
getImportConnector = gImportRegistry.get
listImportConnector = gImportRegistry.list
# ============================================================================
# Export ListSymbol Registry
# ============================================================================
try:
ignore(gListSymbolRegistry)
except NameError:
gListSymbolRegistry = ConnectorRegistry()
registerListSymbolConnector = gListSymbolRegistry.register
getListSymbolConnector = gListSymbolRegistry.get
listListSymbolConnector = gListSymbolRegistry.list
# ============================================================================
# loadExtensions()
#
# load all enabled extensions from the extension folder
#
# file: name of the file to manage the extension
# folder: path of the folder to load the extension from
# ============================================================================
def loadExtensions(file,folder):
# file to manage list of extensions
extFile = os.path.join(folder,file)
if not os.path.exists(extFile):
print 'Load (%s) : %s file not found !' % (folder,file)
return False
# list of potential files to load
files = glob.glob(os.path.join(folder,"*.py"))
if not files:
print 'Load (%s) : no extension file found !' % folder
return False
# list of enabled / disabled Files
enabledFiles = []
disabledFiles = []
try:
ext = open(extFile)
lines = ext.readlines()
ext.close()
except IOError:
print "Load (%s) : can't open %s file !" % (folder,extFile)
for s in lines:
#print s
s = s.strip()
if s and s[0] == '#':
s = s[1:].strip()
if s and s.find(' ') == -1 and s[-3:] == '.py':
# file commented -> disabled
path = os.path.join(folder,s)
if path not in enabledFiles and path not in disabledFiles:
disabledFiles.append(path)
else:
if s and s.find(' ') == -1 and s[-3:] == '.py':
path = os.path.join(folder,s)
if path not in enabledFiles and path not in disabledFiles:
#print 'add:',path
enabledFiles.append(path)
# load extensions in the order they appear in the enabledFiles list
if files and enabledFiles:
for f in enabledFiles:
if f in files:
loadOneExtension(f,folder)
return True
def loadOneExtension(ext,folder):
global loadedModules
# extract module name
if ext[-3:] == ".py":
moduleName = ext[:-3]
else:
moduleName = ext
moduleName = os.path.basename(moduleName)
# check module not loaded
if isLoaded(moduleName):
module = loadedModules.get(moduleName)
print 'Extension (%s) %s already loaded' % (folder,moduleName)
return module
# import the module
module = importFromPath (moduleName,folder)
# return the moduler reference
if not module:
print "Can't load (%s) %s" % (folder,moduleName)
else:
if itrade_config.verbose:
print 'Load (%s) %s' % (folder,moduleName)
return module
def isLoaded (name):
return name in loadedModules
def importFromPath(moduleName,path):
module = sys.modules.get(moduleName)
if not module:
#
f = None
# find the module
data = imp.find_module(moduleName,[path])
if data:
# import the module
f,path,desc = data
module = imp.load_module(moduleName,f,path,desc)
if module:
loadedModules[moduleName] = module
if f: f.close()
return module
# ============================================================================
# Test Extensions
# ============================================================================
if __name__=='__main__':
setLevel(logging.INFO)
loadExtensions(itrade_config.fileExtData,itrade_config.dirExtData)
loadExtensions(itrade_config.fileIndData,itrade_config.dirIndData)
# ============================================================================
# That's all folks !
# ============================================================================