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
AttrFetchTask.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 import dc.EventObjects
11 import dc_db.Constants as Constants
12 from dc_db.BaseTask import BaseTask
13 import app.Utils as Utils # pylint: disable=F0401
14 
15 logger = Utils.MPLogger().getLogger()
16 
17 
18 # # AttrFetchTask Class, implements AttributeFetch task functionality
19 #
21 
22  ATTR_FETCH_TEMPLATE = "SELECT * FROM `%s` "
23 
24  # #Class constructor
25  #
26  def __init__(self):
27  super(AttrFetchTask, self).__init__()
28 
29 
30  # #make all necessary actions to fetch attributes from db
31  #
32  # @param attrFetches list of AttrFetche objects
33  # @param queryCallback function for queries execution
34  # @return list ofAttribute objects
35  def process(self, attrFetches, queryCallback):
36  ret = []
37  for attrFetch in attrFetches:
38  if self.isSiteExist(attrFetch.siteId, queryCallback):
39  try:
40  additionWhere = None
41  if attrFetch.name is None:
42  additionWhere = self.generateCriterionSQL(attrFetch.criterions)
43  else:
44  additionWhere = ("WHERE `Name` = '%s'" % attrFetch.name)
45  logger.debug(">>> additionWhere = " + str(additionWhere))
46  if additionWhere is not None and len(additionWhere) > 0:
47  query = AttrFetchTask.ATTR_FETCH_TEMPLATE % (Constants.DC_ATT_TABLE_NAME_TEMPLATE % attrFetch.siteId)
48  res = queryCallback(query + additionWhere, Constants.ATT_DB_ID, Constants.EXEC_NAME)
49  if res is not None and len(res) > 0:
50  for elem in res:
51  attribute = dc.EventObjects.Attribute(attrFetch.siteId, elem["Name"])
52  attribute.urlMd5 = elem["URLMd5"]
53  attribute.value = elem["Value"]
54  attribute.cDate = elem["CDate"]
55  ret.append(attribute)
56  except Exception as excp:
57  logger.debug(">>> [AttributeFetch] Some Exception = " + str(type(excp)) + " " + str(excp))
58 
59  return ret
60 
61 
62  # #fetchUrlsAttributes static method, fetches and returns attributes by urlMd5
63  #
64  # @param siteId - incoming siteId
65  # @param urlMd5 - incoming urlMd5
66  # @param queryCallback - function for queries execution
67  # @return list ofAttribute objects
68  @staticmethod
69  def fetchUrlsAttributes(siteId, urlMd5, queryCallback):
70  attrFetchTask = AttrFetchTask()
71  attrFetches = [dc.EventObjects.AttributeFetch(siteId, None, {"WHERE": ("`URLMd5` = '%s'" % urlMd5)})]
72  return attrFetchTask.process(attrFetches, queryCallback)
73 
74 
75  # #fetchUrlsAttributesByNames static method, fetches and returns attributes by urlMd5 and attribute names
76  #
77  # @param siteId - incoming siteId
78  # @param urlMd5 - incoming urlMd5
79  # @param queryCallback - function for queries execution
80  # @param attributeNames - attribute names list
81  # @return list ofAttribute objects
82  @staticmethod
83  def fetchUrlsAttributesByNames(siteId, urlMd5, queryCallback, attributeNames):
84  # variable for result
85  ret = []
86  # get full attributes list
87  attributes = AttrFetchTask.fetchUrlsAttributes(siteId, urlMd5, queryCallback)
88  if isinstance(attributeNames, list):
89  # default behavior (return all attributes)
90  if '*' in attributeNames:
91  ret = attributes
92  # search name form list
93  else:
94  localAttributes = []
95  for attribute in attributes:
96  if attribute.name in attributeNames:
97  localAttributes.append(attribute)
98  # save attributes list
99  ret = localAttributes
100 
101  return ret
def isSiteExist(self, siteId, queryCallback, userId=None)
Definition: BaseTask.py:29
def fetchUrlsAttributes(siteId, urlMd5, queryCallback)
def process(self, attrFetches, queryCallback)
def generateCriterionSQL(self, criterions, additionWhere=None, siteId=None)
Definition: BaseTask.py:46
def fetchUrlsAttributesByNames(siteId, urlMd5, queryCallback, attributeNames)