6 import copy, datetime, logging
9 from serverportfolio
import Utils
23 _logger = logging.getLogger(
"SP.StockTemplate")
24 _logger.debug(
"Load StockTemplate" )
34 _templ_fundamental = {
36 'MarketCapitalization' : float(0),
37 'SharesOwned' : int(0),
38 'DividendYield' : float(0),
40 'PriceBook' : float(0),
62 'StockExchange' : str(),
79 'first_date' : datetime.datetime.fromtimestamp(0),
80 'last_date' : datetime.datetime.fromtimestamp(0),
108 'first_date' : datetime.datetime.fromtimestamp(0),
109 'last_date' : datetime.datetime.fromtimestamp(0),
126 _templ_one_value_xml = {
128 'attrib' : {
'append' :
False,
'source' :
None,
'date' :
None }
142 _templ_static_invalid = {}
161 _templ_valid_xml_value = {
166 'date' : datetime.datetime.fromtimestamp(0),
188 'Static' : {
'template' : _templ_static,
191 'config' : {
'save' :
False }
199 'Info' : {
'template' : _templ_info,
202 'config' : {
'save' : [
'xml'],
'attrib': {
'append' :
False,
'source' :
None } }
209 'Fundamental' : {
'template' : _templ_fundamental,
212 'config' : {
'save' : [
'xml'],
'attrib':{
'append' :
True,
'source' :
None} },
218 'HistPrice' : {
'template' : _templ_hp,
219 'config' : {
'save' : [
'csv']}
222 'InstValue' : {
'template' : _templ_inst_value,
223 'config' : {
'save' : [
'csv']}
228 'DivSplit' : {
'template' : _templ_div,
229 'config' : {
'save' : [
'xml',
'csv'] }
233 'DatesIntro' : {
'template' : _templ_intro,
234 'config' : {
'save' :
False}
281 cls._logger.debug(
"_reset static template")
284 assert id(cls._templ_static) == id(cls._templ_action[
'Static'][
'template']), \
285 "Id templ_static differ before clear"
287 cls._templ_static.clear()
288 cls._templ_static_invalid.clear()
291 assert id(cls._templ_static) == id(cls._templ_action[
'Static'][
'template']), \
292 "Id templ_static differ after clear"
301 cls._logger.debug(
"Entry get_template, action: %s" % action)
303 e_action = Utils.stringToEAction( action )
306 if e_action.name
in cls._templ_action:
307 tmp_dict = copy.deepcopy( cls._templ_action[e_action.name] )
311 if 'attrib' in tmp_dict[
'config']:
312 del tmp_dict[
'config'][
'attrib']
319 raise PortfolioError(
"e_action %s has no template in StockTemplates")
330 tmp_dict = copy.deepcopy( cls._templ_parser)
333 assert action
is not None,
"get_template_parser called with action None"
337 e_action = Utils.stringToEAction( action )
339 tmp_dict[
'action_templ'][e_action.name] = copy.deepcopy(cls._templ_action[ e_action.name ][
'template'])
350 cls._logger.debug(
"Entry get_template_xml %s" % action)
354 tmp_xml = cls.get_template_stock( action, for_xml=
True )
357 del tmp_xml[
'config']
359 cls._logger.debug(
"template_xml: %s" % tmp_xml)
362 for name
in tmp_xml[
'template'].keys():
366 if (name ==
"other") | (name ==
"error" ):
368 tmp_xml[
'template'][name] = list()
369 cls._logger.debug(
"return template_xml %s" % tmp_xml)
376 return copy.deepcopy(cls._templ_one_value_xml)
381 return copy.deepcopy(cls._templ_valid_xml_value)
394 if any(cls._templ_static):
395 cls._logger.warning(
"_tmpl_static already loaded, do nothing, should not happen !")
405 for item
in list_key:
406 cls._templ_static[item] = str()
407 cls._templ_static_invalid[item] =
'invalid'
412 assert id(cls._templ_static) == id(cls._templ_action[
'Static'][
'template']), \
413 "templ_static not pointing to the same object"
419 return cls._templ_static_invalid
428 cls._logger.debug(
"Entry to_save_xml")
429 cls._logger.debug(
"to save action %s, %s" % (action, cls._templ_action[ action]) )
430 return cls._templ_action[ action ][
'config'][
'save']
437 cls._logger.debug(
"Entry saved_as")
440 return cls._templ_action[ action ][
'config'][
'save']
445 if cls._templ_action[ action ][
'config'][
'save'] ==
False:
447 if specific
in cls._templ_action[ action ][
'config'][
'save']:
451 self.logger.error(
"saved_as Should not arrive here")
452 assert 1==1,
"Should not arrive here"
460 cls._logger.debug(
"Entry create_default_template_xml")
462 for action
in cls._templ_action.keys():
463 print "call to_save_xml action ", action
465 if cls.saved_as(action,
'xml'):
469 default_xml[action] = cls.get_template_xml(action)
470 print "get_template_xml ", default_xml
474 for key_value
in default_xml[action][
'template'].keys():
476 if (key_value ==
'other') | (key_value ==
'error'):
481 default_xml[action][
'template'][key_value].append(cls.get_template_one_value_xml())
489 print key_value, default_xml[action][
'template'][key_value][0][
'attrib']
493 cls._logger.debug(
"default xml %s" % default_xml)
499 if (action_name ==
'InstValue'):
500 return "#name value variation time date volume ouverture +haut +bas state\n"
501 elif (action_name ==
'HistPrice'):
502 return "#name first_date last_date\n"
503 elif (action_name ==
'Info'):
504 return "# symbol name sector industry\n"
505 elif (action_name ==
'DatesIntro'):
506 return "# symbol first available data in Yahoo\n"
507 elif (action_name ==
'Fundamental'):
508 return "# symbol ....\n"
512 return "# header to write in StokTemplates for action %s\n" % action_name
520 if __name__ ==
"__main__":
522 print "Main StockTemplates"
525 d_static = StTmpl.get_dict_static()
526 print "d_static ", d_static
528 d_static = StTmpl.get_dict_static(
"CAC40" )
529 print "d_static", d_static
Base class of the custom exceptions.
def set_tmpl_static
Set the internal template for the static data, also for invalid stock.
def to_save_xml
Query to know if this template must be saved in XML file.
def get_template_xml
Specific for xml dictionary, replace each value by a list.
def saved_as
Query the format in which the data must be saved.
def get_template_stock
Return a copy of an action template for stocks, include 'template' and 'config', delete 'attrib' entr...
def create_default_template_xml
Create a default xml dictionary, called when creating a new XML file.
Define template dictionaries to describe the data structure.
def get_tmpl_static_invalid
Return the invalid static template.
Define custom and specific exceptions for the complete package.
def _reset
Reset the state of templates to the original.
def get_template_one_value_xml
Return a template for each xml value, include 'value' and 'atrib' for each one Or/And, with action, name can set default append/date.
def get_template_parser
Return the template for parser to be included by all parsers.
def get_template_valid_xml_value
Retrun a template for the validation template, include 'new_value','xml_value','repl_add','date'.