15 import os, re, types, logging
23 from serverportfolio
import Utils
46 if cls
not in cls._instances:
48 cls.logger=logging.getLogger(
"SP.Singleton")
49 cls.logger.debug(
"create new instance singleton")
51 cls._instances[cls] = super(Singleton, cls).
__call__(*args, **kwargs)
53 cls.logger.debug(
"return already created instance")
54 return cls._instances[cls]
81 __metaclass__ = Singleton
90 def __init__(self, file_stocks=GlobalDicts.DEFAULT_DICTIONARY, load_all_stocks = False):
91 self.
logger = logging.getLogger(
"SP.DictStocks")
92 self.logger.debug(
"Init DictionaryStocks")
111 except PortfolioError:
112 self.logger.error(
"Caught PortfolioError in _read_config_txt. Should not happen !")
116 self.logger.error(
"Catch Exception in _read_config_txt. Should not happen !")
119 self.logger.debug(
"list of stocks loaded by configuration %s" % list_stocks)
123 if load_all_stocks ==
True:
149 assert isinstance(value,Stock),
'Only Stock object can be assigned'
164 self.logger.debug(
"get_stocks: %s" % input_stock)
166 list_stock = Utils.to_list( input_stock )
175 for symbol
in list_stock:
182 list_obj_stock.append( self[symbol] )
185 if len(list_stock) == 1:
186 return list_obj_stock[0]
188 return list_obj_stock
194 self.logger.debug(
"get_stock_config.keys :%s", self._dictstocks_config.keys())
195 if all_stocks ==
True:
196 return self._dictstocks_config.keys()
198 return self._dictstocks.keys()
207 return StTmpl.get_template_stock(
'Static')
218 def print_dict_stocks( self, action, input_stocks = None, opt_all_stocks = False, opt_header = False ):
219 self.logger.debug(
"Entry print_dict_stocks, action: %s" % action )
220 self.logger.debug(
"list stock: %s" % input_stocks)
223 if opt_all_stocks ==
True:
224 list_stock = self._dictstocks_config.keys()
228 if input_stocks ==
None:
229 list_stock = self._dictstocks.keys()
232 list_stock = Utils.to_list( input_stocks )
237 stock_lines += StTmpl.make_header(action)
240 for stock
in list_stock:
242 stock_lines += self.
get_stocks(stock).print_stock( action )
267 if stock_symbol ==
None :
268 self.logger.info(
"Load all Stocks from the configuration file")
270 "Option stock_symbol = None, but dictionary not empty"
271 list_symbol = self._dictstocks_config.keys()
273 list_symbol = Utils.to_list( stock_symbol )
275 for symbol
in list_symbol:
278 "Try to create an already loaded Stock"
282 stock =
InvalidStock( symbol, StTmpl.get_tmpl_static_invalid() )
299 self.logger.debug(
"read_config_txt file: %s" % file_stocks)
301 file_stocks = GlobalDicts.PATH_DICT + os.sep + self.
_file_stocks
302 self.logger.debug(
"file_stocks: %s" % file_stocks)
319 self.logger.debug(
"open file %s" % file_stocks)
320 file=open(file_stocks,
"r")
322 self.logger.error(
"file: %s cannot be opened, throw PortfolioError" % file_stocks )
327 line = file.readline()[:-1]
329 list_key = line.split()
333 self.logger.debug(
"list_key in config %s" % list_key)
337 StTmpl.set_tmpl_static( list_key[1:] )
343 for line
in file.readlines():
350 if data[0][0] ==
"#":
357 list_new.append(data[0])
359 tmpl_static = StTmpl.get_template_stock(
'Static')
361 for i
in range(1,len(data)):
363 if list_key[i]==
'market':
365 data[i] = GlobalDicts.MarketMySQL_to_DictStock(data[i])
370 tmpl_static[
'template'][ list_key[i] ] = data[i]
389 print "Activate TCPClient"
395 if ( tcp_client.GetActivity() ==
False ):
396 self.logger.info(
"activate tcp_client")
398 tcp_client.active =
True
400 self.logger.info(
"tcp_client already activated")
405 print "Desactivate TCPClient"
409 if ( tcp_client.GetActivity() ==
True ):
411 tcp_client.active =
False
412 self.logger.info(
"desactivate tcp_client")
414 self.logger.info(
"tcp_client already desactivated")
Define a metaclass Singleton object.
def DesactivateTCPClient
Used by C++ to desactivate the client.
def __call__
Executed at the object creation, created only once.
def get_stocks
Return a Stock (or a list of Stock) object(s) from a symbol (or a list of symbols).
def __getitem__
Access by key.
Base class of the custom exceptions.
def _create_stocks
Create and initialise Stock objects.
Make a connection to the root application server to send directly new data.
def __init__
Initialise the singleton dictionary and read the configuration text file.
def ActivateTCPClient
Used by C++ to activate / desactivate the client socket.
_dictstocks_config
Store the content of the static stocks definitions (dictstocks.txt).
Make a class for sending new data to the server of ROOT_application.
_dictstocks
Dictionary containing Stock objects with key/value = 'symbol' : Stock object.
def get_dict_static
Return the static template from a stock symbol.
Define the global variable StockTemplates.StTmpl and dictionary templates.
def __setitem__
Set with key / value.
Class template for invalid stocks, if a stock symbol is not found in the configuration file...
Define custom and specific exceptions for the complete package.
def get_stock_keys
Return the list of stock symbols loaded from the configuration file, do not create the Stock objects...
Store data and functions related to one stock.
_file_stocks
Store the name of the configuration file.
Container of all Stocks objects, it also reads the static stocks configuration file "dictstocks...
Define the classes Stock and InvalidStock.
Global variables for configuration: paths, TCP ports and generic definitions.
def _read_config_txt
Read static definition of the stocks from the configuration text file.
_tcp_client
Access to the TCPClient for sending update to the GUIServer.
def print_dict_stocks
Print (a nice) output of the data.