ServerPortfolio  2.0
Python parsers and server
 All Classes Namespaces Files Functions Variables Properties Pages
Server_Portfolio.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 
3 ## @package Server_Portfolio
4 # @brief Main executable for running the portfolio python service.
5 #
6 # LastChanged $Id: Server_Portfolio.py 524 2015-02-24 10:18:50Z martinml $
7 
8 # Main program :
9 # Run the Parser: AutoParser
10 # Run the Server: SocketServer_Server ( in the main thread, may change )
11 #
12 # Rewrite the log file: ServerPortfolio.log
13 
14 import sys, time
15 import logging, logging.config
16 import threading
17 
18 # global definition of the server
19 from serverportfolio.GlobalDicts import HOST,PORT, DEBUG_MODE
20 
21 # dependent to set-up the singleton
22 from serverportfolio.DictionaryStocks import DictionaryStocks
23 
24 import serverportfolio.AutoParser as AutoParser
25 import serverportfolio.SocketServer_Server as SocketServer_Server
26 
27 ################# functions
28 
29 def print_help():
30  print "./Server_Portfolio dictstock.txt / help "
31  print " default ./dictstock.txt"
32 
33 ################ main code
34 
35 # optimisation, do not want to collect process information, but thread yes !
36 logging.logProcesses = 0
37 
38 # setup a new logger SP
39 # load from a file, regroup all configurations there
40 logging.config.fileConfig('/home/michael/workspace_kepler/ServerPortfolio/trunk/ServerPortfolio/logging.conf')
41 # want module information, name the logger but CANNOT call basicConfig, need explicit setup
42 logger = logging.getLogger('SP')
43 
44 # ok for this logger, not for derived
45 if DEBUG_MODE :
46  print "only test "
47  logger.debug("test debug")
48  logger.info("test info")
49  logger.error("test error")
50 
51 ################# Main program
52 
53 ## do not work as expected, sys.exit certainly send an exception
54 try:
55  fstock=sys.argv[1]
56  print "fstock ", fstock
57  if ( fstock == "help" ) | ( fstock == "-h" ) | ( fstock == "-help") :
58  print_help()
59  sys.exit(1)
60  # Load the dictionnary and add the "volatile" part
61  dict_stocks = DictionaryStocks( fstock )
62 
63 # no argument will use the default
64 except : #IndexError:
65  #dict_stocks=DictStocks_Parser.Init()
66  print "no argument, use default dictstocks.txt"
67  #dict_stocks = DictionaryStocks( load_all_stocks=True )
68  dict_stocks = DictionaryStocks()
69  dict_stocks.get_stocks()
70  #dict_stocks.get_stocks('SP500')
71  #dict_stocks.get_stocks(['FSLR','EURUS'])
72  #dict_stocks.get_stocks('FAZ') #Problem with FAZ, but RunParsers FORCE is working !!, in parse cannot extract table
73  #dict_stocks.get_stocks(['SP500','CAC40'])
74  #dict_stocks.get_stocks(['SP500','CAC40','FSLR','DJ'])
75 
76 ## could use a return dict ( alias, only for testing ok )
77 
78 #print dict_stocks
79 
80 ## Load AutoParser
81 # Run thread queue handler
82 thq=AutoParser.Thread_Handle_Queue()
83 thq.start()
84 
85 print "threading.enumerate() ", threading.enumerate()
86 
87 ## Load Server
88 
89 # here run server in a different thread, works but main wait until join....
90 #server_thread = SocketServer_Server.threading.Thread(target=SocketServer_Server.run_MyServer,
91 # name="t-server",
92 # args=(HOST,PORT,thq))
93 
94 ## If True, exit the server thread when the main thread terminates
95 # Here False (default)
96 #server_thread.setDaemon(False)
97 #server_thread.start()
98 #print "enumerate",threading.enumerate()
99 #print "Server loop running in thread:", server_thread.name
100 #
101 # wait for TCPServer thread to stop
102 # server_thread.join()
103 
104 # simple to run like this ! The MainThread will be the server
105 SocketServer_Server.run_MyServer( HOST, PORT, thq)
106 # exit only when server will be turned off
107 
108 # kill thq
109 thq._running = False
110 # ? certainly better, should join the thread ?
111 del thq
112 
113 print "AT THE END: enumerate thread ",threading.enumerate()
114 
Container of all Stocks objects, it also reads the static stocks configuration file "dictstocks...
def print_help
functions
Global variables for configuration: paths, TCP ports and generic definitions.
Definition: GlobalDicts.py:1
Module for updating the dictionary of stocks using independent threads every TIMEPARSER sec...
Definition: AutoParser.py:1
Server of the application, wait for requests: provide data, force update, etc.
Define singleton class DictionaryStocks, act as the main container of Stocks objects.