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
AttrSetTask.py
Go to the documentation of this file.
1 '''
2 @package: dc
3 @author scorp
4 @link: http://hierarchical-cluster-engine.com/
5 @copyright: Copyright © 2013-2014 IOIX Ukraine
6 @license: http://hierarchical-cluster-engine.com/license/
7 @since: 0.1
8 '''
9 
10 from dtm.EventObjects import GeneralResponse
11 from dc_db.BaseTask import BaseTask
12 import dc_db.Constants as Constants
13 import app.Utils as Utils # pylint: disable=F0401
14 
15 logger = Utils.MPLogger().getLogger()
16 
17 # # AttrSetTask Class, implements AttributeSet task functionality
18 #
20 
21  CODE_GOOD_INSERT = 0
22  CODE_BAD_INSERT = 1
23  CODE_ALREADY_EXIST = 2
24  CODE_SITE_NOT_EXIST = 3
25 
26  # #Class constructor
27  #
28  def __init__(self):
29  super(AttrSetTask, self).__init__()
30 
31 
32  # #make all necessary actions to add new Attributes into mysql db
33  #
34  # @param attrs list of Attributes objects
35  # @param queryCallback function for queries execution
36  # @return generalResponse instance of GeneralResponse object
37  def process(self, attrs, queryCallback):
38  ret = GeneralResponse()
39  for attr in attrs:
40  if self.isSiteExist(attr.siteId, queryCallback):
41  if self.selectAttribute(attr, queryCallback):
42  ret.statuses.append(AttrSetTask.CODE_ALREADY_EXIST)
43  self.updateAttribute(attr, queryCallback)
44  elif self.addAttribute(attr, queryCallback):
45  ret.statuses.append(AttrSetTask.CODE_GOOD_INSERT)
46  else:
47  ret.statuses.append(AttrSetTask.CODE_BAD_INSERT)
48  else:
49  ret.statuses.append(AttrSetTask.CODE_SITE_NOT_EXIST)
50  return ret
51 
52 
53  # #select attribute
54  #
55  # @param attrObject instance of Attribute object
56  # @param queryCallback function for queries execution
57  def selectAttribute(self, attrObject, queryCallback):
58  ret = False
59  LOCAL_URL_CHECK_QUERY = "SELECT COUNT(*) FROM `att_%s` WHERE `Name` = '%s' AND `URLMd5` = '%s'"
60  query = LOCAL_URL_CHECK_QUERY % (attrObject.siteId, attrObject.name,
61  attrObject.urlMd5 if attrObject.urlMd5 is not None else "")
62  res = queryCallback(query, Constants.ATT_DB_ID)
63  if hasattr(res, '__iter__') and len(res) > 0 and len(res[0]) > 0 and res[0][0] > 0:
64  ret = True
65 
66  return ret
67 
68 
69  # #inserts new attribute
70  #
71  # @param attrObject instance of Attribute object
72  # @param queryCallback function for queries execution
73  def addAttribute(self, attrObject, queryCallback):
74  ret = False
75  fields, values = Constants.getFieldsValuesTuple(attrObject, Constants.AttrTableDict)
76  fieldValueString = Constants.createFieldsValuesString(fields, values)
77  if fieldValueString is not None and fieldValueString != "":
78  query = Constants.INSERT_COMMON_TEMPLATE % ((Constants.DC_ATT_TABLE_NAME_TEMPLATE % attrObject.siteId),
79  fieldValueString)
80  queryCallback(query, Constants.ATT_DB_ID, Constants.EXEC_NAME, True)
81  ret = True
82 
83  return ret
84 
85 
86  # # Update already exists attributes
87  #
88  # @param attrObject instance of Attribute object
89  # @param queryCallback function for queries execution
90  def updateAttribute(self, attrObject, queryCallback):
91  UPDATE_TEMPLATE = "UPDATE `att_%s` SET `Value` = '%s' WHERE `Name` = '%s' AND `URLMd5` = '%s'"
92  query = UPDATE_TEMPLATE % (attrObject.siteId, Utils.escape(attrObject.value), attrObject.name,
93  attrObject.urlMd5 if attrObject.urlMd5 is not None else "")
94  queryCallback(query, Constants.ATT_DB_ID, Constants.EXEC_NAME, True)
def isSiteExist(self, siteId, queryCallback, userId=None)
Definition: BaseTask.py:29
GeneralResponse event object, represents general state response for multipurpose usage.
def addAttribute(self, attrObject, queryCallback)
Definition: AttrSetTask.py:73
def selectAttribute(self, attrObject, queryCallback)
Definition: AttrSetTask.py:57
def updateAttribute(self, attrObject, queryCallback)
Definition: AttrSetTask.py:90
def process(self, attrs, queryCallback)
Definition: AttrSetTask.py:37