def getFriendlyDataSize(self, blockData, bypassCheck = False): if not bypassCheck and not base.isString(blockData): return "unknown" dataSize = float(base.isString(blockData) and len(blockData) or blockData) return base.convertToHumanReadableDataUnit(dataSize)
def set_name(self, new_name): ''' Set the name of the element with new_name (as string or unicode). ''' if base.isString(new_name): self.name = new_name return self.name
def respond_status(self, code, message = None, break_now = True): ''' Set the response status. *code* is a HTTP status code in interger. *message* is a response reason. *break_now* is a boolean. If *True*, this method will raise CherryPy's HTTPError exception. Otherwise, return status. ''' if not base.isString(message) or len(message) == 0: message = None if 300 <= code < 400: del cherrypy.response.headers['Content-Type'] if code >= 400 and break_now: if message: raise cherrypy.HTTPError(code, message) else: raise cherrypy.HTTPError(code) else: if message: cherrypy.response.status = u"%d %s" % (code, message) else: cherrypy.response.status = code return base.convertToUnicode(cherrypy.response.status)
def read(self, source): """ Read the data (strictly well-formatted XML or Kotoba-compatible) from the source. Source can be a file name, XML-formatted string or DOMElement (Yotsuba 3's). """ # Load XML Document as a unicode file if base.isString(source) and os.path.exists(source): fp = codecs.open(source, 'r') self.__xmldoc = fp.read() fp.close() # XML document as string elif base.isString(source) and not os.path.exists(source): self.__xmldoc = source # Assign the root document with DOMElement object elif source is DOMElement: self.__root_element = source # Raise exception for unknown type of source else: raise KotobaSourceException("Unknown type of source. Cannot load the data from source. (Given: %s)" % type(source)) if base.isString(source): self.__graph()
def init(self, combinators): # Check type if not base.isString(combinators): raise KotobaInvalidSelection("Not a string") self.combinators = combinators # Prepare data raw_combinators = base.convertToUnicode(combinators) raw_combinators = re.split("\s*,\s*", raw_combinators.strip()) for raw_combinator in raw_combinators: if raw_combinator == '': raise KotobaInvalidSelectionException("Invalid multiple combinators. Perhaps there is a leading/tailing comma or an empty combinator/selector.") combinator = Combinator(raw_combinator) self.append(combinator)
def init(self, combinator): # Check type if not base.isString(combinator): raise KotobaInvalidSelectionException("Not a string") self.combinator = combinator # Check for invalid combinator (part 1: not start or end with combining instruction) if re.search("^(>|\+|~)", combinator) or re.search("(>|\+|~)$", combinator): raise KotobaInvalidSelectionException("Not start or end with combining instruction (>, +, ~)") # Prepare data raw_combinator = base.convertToUnicode(combinator) raw_combinator = re.split("\s+", raw_combinator.strip()) # Local buffer raise_on_next_combiner = False # Note: at this point, there is no combiners leading or tailing the combinator. for combo_block in raw_combinator: if combo_block in Selector.combination_map.keys(): # [If it is a registered combiner] if not raise_on_next_combiner: raise_on_next_combiner = True self[-1].combo_method = Selector.combination_map[combo_block] log.debug("Combinator.init: Reset the iteration method of the last selector (%s) to %s" % (self[-1].name, self[-1].combo_method)) else: raise KotobaInvalidSelectionException("No consecutive combiners allowed") else: # [Not registered combiner] raise_on_next_combiner = False selector = Selector(combo_block) self.append(selector) #print "Combinators >> init >> combo not registered" #print self[-1] self[-1].end_of_query = True
def getDataSize(self, blockData): if not base.isString(blockData): return 0 return len(blockData)
def __require_next_selector(self, single_selector, reference_node = None): ''' Test if the reference node is matched with the given selector. ''' test_node = reference_node is None and self or reference_node try: # Check if the name of the element is the same as what the selector # is looking for. Bypass the check if the selector is using wildcard. if base.isString(single_selector.name) and not single_selector.is_wildcard(): assert single_selector.name == test_node.name, "Expect: %s (Actual: %s)" % (single_selector.name, test_node.name) # @type single_selector Selector # @type test_node DOMElement #log.debug("DOMElement.__require_next_selector: Check node name > Passed (%s = %s)" % (single_selector.name, test_node.name)) #log.debug('DOMElement.__require_next_selector: Watch > test_node.attr = %s' % test_node.attrs) for attr_name, attr_value in single_selector.attributes.iteritems(): try: flag_begin_with = attr_name[-1] == '^' flag_end_with = attr_name[-1] == '$' flag_contain_substr = attr_name[-1] == '*' flag_whitespace_separated_value_partial_match = attr_name[-1] == '~' flag_hyphen_separated_value_partial_match = attr_name[-1] == '|' attr_name = re.sub("(\^|\$|\*|\~|\|)$", '', attr_name) assert attr_name in test_node.attrs, "Missing attribute" if attr_value: if flag_begin_with: log.debug("DOMElement.__require_next_selector: Test flag_begin_with for %s" % attr_name) assert re.search("^" + attr_value, test_node.attrs[attr_name]) is not None elif flag_end_with: log.debug("DOMElement.__require_next_selector: Test flag_end_with for %s" % attr_name) assert re.search(attr_value + "$", test_node.attrs[attr_name]) is not None elif flag_contain_substr: log.debug("DOMElement.__require_next_selector: Test flag_contain_substr for %s" % attr_name) assert re.search(attr_value, test_node.attrs[attr_name]) is not None elif flag_hyphen_separated_value_partial_match: log.debug("DOMElement.__require_next_selector: Test flag_whitespace_separated_value_partial_match for %s" % attr_name) assert re.match(attr_value + "-?", test_node.attrs[attr_name]) is not None or re.search("^" + attr_value + "-", test_node.attrs[attr_name], re.I) is not None elif flag_whitespace_separated_value_partial_match: log.debug("DOMElement.__require_next_selector: Test flag_hyphen_separated_value_partial_match for %s" % attr_name) keywords_found = False for token in re.split(' ', test_node.attrs[attr_name]): try: log.debug("DOMElement.__require_next_selector: Comparing (attr_v) %s and (token) %s " % (attr_value, token)) assert token == attr_value keywords_found = True except: pass # handled by keywords_found below finally: if keywords_found: break if not keywords_found: raise KotobaCheckpointScreeningException() else: log.debug("DOMElement.__require_next_selector: Test for the normal operator for %s" % attr_name) assert test_node.attrs[attr_name] == attr_value except: assert False, "Unexpected error" for pclass in single_selector.pseudo_classes: log.debug("DOMElement.__require_next_selector: Pseudo class > " + pclass) if pclass == "root": assert test_node.level == 0 elif pclass == "empty": assert len(test_node.children()) == 0 log.debug("DOMElement.__require_next_selector: Passed all assertions") return True except AssertionError: log.debug("DOMElement.__require_next_selector: Failed assertions") log.debug("Details: %s" % exc_info()[1]) return False except: log.debug("Unexpected error: %s" % exc_info()[0]) return False
def encrypt(obj_ref): if not base.isString(obj_ref): raise CryptoIOException() return base64.b64encode(obj_ref)