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
JsonChecker.py
Go to the documentation of this file.
1 '''
2 Created on Apr 2, 2014
3 
4 @package: dtm
5 @author: scorp
6 @link: http://hierarchical-cluster-engine.com/
7 @copyright: Copyright © 2013-2014 IOIX Ukraine
8 @license: http://hierarchical-cluster-engine.com/license/
9 @since: 0.1
10 '''
11 
12 from cement.core import foundation
13 import logging
14 import Constants as CONSTANTS
15 import app.Utils as Utils
16 import sys
17 import app.Consts as APP_CONSTS
18 
19 
20 # Logger initialization
21 logger = logging.getLogger(APP_CONSTS.LOGGER_NAME)
22 
23 
24 logging.basicConfig()
25 #logger = logging.getLogger(CONSTANTS.APP_NAME)
26 
27 
28 
30 class JsonChecker(foundation.CementApp):
31 
32  class Meta:
33  label = CONSTANTS.APP_NAME
34 
35 
36 
38  def __init__(self):
39  foundation.CementApp.__init__(self)
40  self.errorCode = CONSTANTS.ERROR_NOERROR
41  self.errorStr = ""
42 
43 
44 
46  def fillError(self, errorStr, errorCode):
47  self.errorCode = errorCode
48  self.errorStr = errorStr
49  logger.error(self.errorStr)
50 
51 
52 
54  def readJSONValue(self, fileName, path):
55  fd = None
56  value = None
57  try:
58  fd = open(fileName, 'r')
59  except IOError:
60  fd = None
61  if fd != None:
62  jsonString = fd.read()
63  try:
64  value = Utils.getPath(None, jsonString, path)
65  value = str(value)
66  except (ValueError):
67  self.fillError(CONSTANTS.ERROR_STR3, CONSTANTS.ERROR_BAD_JSON)
68  except (TypeError, KeyError, IndexError):
69  self.fillError(CONSTANTS.ERROR_STR4, CONSTANTS.ERROR_PATH_NOT_FOUND)
70  else:
71  self.fillError(CONSTANTS.ERROR_STR2, CONSTANTS.ERROR_FILE)
72  return value
73 
74 
75 
78  def valueChecker(self, jsonValue, value):
79  isFind = False
80  valueList = value.split(CONSTANTS.VALUE_SEPARATOR)
81  for item in valueList:
82  if item == jsonValue:
83  isFind = True
84  break
85  if isFind == False:
86  self.fillError(CONSTANTS.ERROR_STR5, CONSTANTS.ERROR_VALUE_NOT_FOUND)
87 
88 
89 
91  def setup(self):
92  foundation.CementApp.setup(self)
93  self.args.add_argument("-path", "--path", action="store")
94  self.args.add_argument("-value", "--value", action="store")
95  self.args.add_argument("-file", "--file", action="store")
96 
97 
98 
100  def run(self):
101  foundation.CementApp.run(self)
102  jsonValue = None
103  if self.pargs.path == None or self.pargs.file == None:
104  self.fillError(CONSTANTS.ERROR_STR1, CONSTANTS.ERROR_ARG1)
105  else:
106  jsonValue = self.readJSONValue(self.pargs.file, self.pargs.path)
107  if jsonValue != None and self.pargs.value != None:
108  self.valueChecker(jsonValue, self.pargs.value)
109  sys.exit(self.errorCode)
110 
111 
112 
114  def close(self):
115  foundation.CementApp.close(self)
JsonChecker Class contents main functional of JsonChecker application, class inherits from foundation...
Definition: JsonChecker.py:30
def valueChecker(self, jsonValue, value)
valueChecker method Method compares incoming jsonValue with value, or find jsonValue in value if valu...
Definition: JsonChecker.py:78
def __init__(self)
constructor initialise all class variable
Definition: JsonChecker.py:38
def run(self)
run method Method contains main application functionality
Definition: JsonChecker.py:100
def setup(self)
setup method Method calls before run application
Definition: JsonChecker.py:91
def fillError(self, errorStr, errorCode)
fillError method calls from error-code point from main processing
Definition: JsonChecker.py:46
def readJSONValue(self, fileName, path)
readJSONValue method Method reads json string from file and search alue by path
Definition: JsonChecker.py:54
def close(self)
close method Method calls after application run
Definition: JsonChecker.py:114