HCE Project Python language Distributed Tasks Manager Application, Distributed Crawler Application and client API bindings.  2.0.0-chaika
Hierarchical Cluster Engine Python language binding
PostProcessingApplicationClass.py
Go to the documentation of this file.
1 # coding: utf-8
2 
3 """
4 HCE project, Python bindings, Distributed Tasks Manager application.
5 Base template class from cement application for easy implementation derived classes.
6 
7 @package: dc_postprocessor
8 @file PostProcessingApplicationClass.py
9 @author Alexander Vybornyh <alexander.hce.cluster@gmail.com>
10 @link: http://hierarchical-cluster-engine.com/
11 @copyright: Copyright &copy; 2013-2017 IOIX Ukraine
12 @license: http://hierarchical-cluster-engine.com/license/
13 @since: 0.1
14 """
15 
16 try:
17  import cPickle as pickle
18 except ImportError:
19  import pickle
20 
21 import sys
22 import logging.config
23 import ConfigParser
24 from cement.core import foundation
25 
26 import app.Utils as Utils
27 import app.Consts as APP_CONSTS
28 
29 POST_PROCESSING_APPLICATION_NAME = 'PostProcessingApplicationClass'
30 
31 # This object is a run at once application
32 class PostProcessingApplicationClass(foundation.CementApp):
33  # # Constants error messages
34  MSG_ERROR_EMPTY_CONFIG_FILE_NAME = "Config file name is empty."
35  MSG_ERROR_WRONG_CONFIG_FILE_NAME = "Config file name is wrong"
36  MSG_ERROR_LOAD_APP_CONFIG = "Error loading application config file."
37  MSG_ERROR_READ_LOG_CONFIG = "Error read log config file."
38  MSG_ERROR_MISSED_SECTION = "Missed mandatory section '%s'"
39 
40  # # Constants debug messages
41  MSG_DEBUG_INPUT_PICKLE = "Input pickle: "
42  MSG_DEBUG_INPUT_BATCH = "Input batch: "
43  MSG_DEBUG_OUTPUT_BATCH = "Output batch: "
44  MSG_DEBUG_OUTPUT_PICKLE = "Output pickle: "
45  MSG_DEBUG_SEND_PICKLE = "Send pickle. Done."
46 
47 
48  # # Constants use in class
49  CONFIG_OPTION_LOG = "log"
50 
51  # Mandatory
52  class Meta(object):
53  label = POST_PROCESSING_APPLICATION_NAME
54  def __init__(self):
55  pass
56 
57 
58  # #Constructor
59  # initialize default fields
60  def __init__(self):
61  # call base class __init__ method
62  foundation.CementApp.__init__(self)
63  self.exitCode = APP_CONSTS.EXIT_SUCCESS
64  self.logger = None
65  self.configParser = None
66  self.inputFile = None
67  self.batch = None
68 
69 
70  # # setup application, necessary recall in derived classes
71  def setup(self):
72  # call base class setup method
73  foundation.CementApp.setup(self)
74 
75 
76  # # run application, necessary recall in derived classes
77  #
78  # @param - None
79  # @return - None
80  def run(self):
81  # call base class run method
82  foundation.CementApp.run(self)
83 
84  # call initialization application
85  self.__initApp()
86 
87 
88 # #load log config file
89  #
90  # @return - None
91  def __loadLogConfig(self, configName):
92 
93  try:
94  if not isinstance(configName, str) or len(configName) == 0:
95  raise Exception(self.MSG_ERROR_EMPTY_CONFIG_FILE_NAME)
96 
97  logging.config.fileConfig(configName)
98 
99  # call rotation log files and initialization logger
100  self.logger = Utils.MPLogger().getLogger()
101 
102  except Exception, err:
103  raise Exception(self.MSG_ERROR_READ_LOG_CONFIG + ' ' + str(err))
104 
105 
106  # #load application config file
107  #
108  # @param configName - name of application config file
109  # @return - instance of ConfigOptions class
110  def __loadAppConfig(self, configName):
111  # variable for result
112  configParser = None
113  try:
114  config = ConfigParser.ConfigParser()
115  config.optionxform = str
116 
117  readOk = config.read(configName)
118 
119  if len(readOk) == 0:
120  raise Exception(self.MSG_ERROR_WRONG_CONFIG_FILE_NAME + ": " + configName)
121 
122  if not config.has_section(APP_CONSTS.CONFIG_APPLICATION_SECTION_NAME):
123  raise Exception(self.MSG_ERROR_MISSED_SECTION % str(APP_CONSTS.CONFIG_APPLICATION_SECTION_NAME))
124 
125  # load logger config file and instantiate logger instance
126  self.__loadLogConfig(config.get(APP_CONSTS.CONFIG_APPLICATION_SECTION_NAME, self.CONFIG_OPTION_LOG))
127 
128  configParser = config
129  except Exception, err:
130  raise Exception(self.MSG_ERROR_LOAD_APP_CONFIG + ' ' + str(err))
131 
132  return configParser
133 
134 
135  # #initialize application from config files
136  #
137  # @param - None
138  # @return - None
139  def __initApp(self):
140  if self.pargs.config:
141  # load app config
142  self.configParser = self.__loadAppConfig(self.pargs.config)
143  # load input file if necessary
144  if self.pargs.inputFile:
145  self.inputFile = str(self.pargs.inputFile)
146  else:
147  raise Exception(self.MSG_ERROR_LOAD_APP_CONFIG)
148 
149 
150  # # get input pickle from stdin or input file
151  #
152  # @param - None
153  # @return inputPickle - input pickle object
154  def __getInputPickle(self):
155 
156  if self.inputFile is None:
157  inputPickle = sys.stdin.read()
158  else:
159  f = open(self.inputFile, 'r')
160  inputPickle = f.read()
161  f.close()
162  # self.logger.debug(self.MSG_DEBUG_INPUT_PICKLE + '\n' + str(inputPickle))
163 
164  return inputPickle
165 
166 
167  # # unpikle input object
168  #
169  # @param inputPickle - input pickle object
170  def __unpickleInput(self, inputPickle):
171  inputBatch = pickle.loads(inputPickle)
172  # self.logger.debug(self.MSG_DEBUG_INPUT_BATCH + Utils.varDump(inputBatch))
173 
174  return inputBatch
175 
176 
177  # # create output pickle object
178  #
179  # @param outputBatch - output batch
180  # @return outputPickle - output pickle object
181  def __createOutputPickle(self, outputBatch):
182  # self.logger.debug(self.MSG_DEBUG_OUTPUT_BATCH + str(outputBatch))
183  outputPickle = pickle.dumps(outputBatch)
184  # self.logger.debug(self.MSG_DEBUG_OUTPUT_PICKLE + str(outputPickle))
185 
186  return outputPickle
187 
188 
189  # # send pickle to stdout
190  #
191  # @param outputPickle - output pickle object
192  # @return - None
193  def __sendPickle(self, outputPickle):
194  sys.stdout.write(outputPickle)
195  # self.logger.debug(self.MSG_DEBUG_SEND_PICKLE)
196 
197 
198  # # extracting receive input batch object
199  #
200  # @param - None
201  # @return - None
202  def inputBatch(self):
203  self.batch = self.__unpickleInput(self.__getInputPickle())
204 
205 
206  # # sending result output batch object
207  #
208  # @param - None
209  # @return - None
210  def outputBatch(self):
211  self.__sendPickle(self.__createOutputPickle(self.batch))
212 
213 
214  # # get config option use config parser
215  # @param sectionName -section name
216  # @param optionName - option name
217  # @param defaultValue - default value
218  # @return optionValue - option value as is
219  def getConfigOption(self, sectionName, optionName, defaultValue=None):
220  # variable for result
221  ret = defaultValue
222  try:
223  if self.configParser is not None:
224  ret = self.configParser.get(sectionName, optionName)
225  except Exception, err:
226  raise Exception(str(err))
227 
228  return ret
def inputBatch(self)
def __initApp(self)
def __init__(self)
def __sendPickle(self, outputPickle)
batch
string MSG_ERROR_WRONG_CONFIG_FILE_NAME
def __getInputPickle(self)
def outputBatch(self)
def __loadLogConfig(self, configName)
string MSG_ERROR_MISSED_SECTION
exitCode
string MSG_ERROR_EMPTY_CONFIG_FILE_NAME
string CONFIG_OPTION_LOG
logger
inputFile
def getConfigOption(self, sectionName, optionName, defaultValue=None)
def __unpickleInput(self, inputPickle)
string MSG_ERROR_READ_LOG_CONFIG
string MSG_ERROR_LOAD_APP_CONFIG
def __createOutputPickle(self, outputBatch)
def __loadAppConfig(self, configName)
configParser
def setup(self)
def run(self)
def __init__(self)