def format(self, obj, selected_fields=None): """ Format an object to be human-readable. An object has fields which can be selected. If the object provides an iter_fields() method, the formatter will call it. It can be used to specify the fields order. @param obj [object] object to format @param selected_fields [tuple] fields to display. If None, all fields are selected @return a string of the formatted object """ assert isinstance(obj, (dict, CapBaseObject, tuple)) if isinstance(obj, dict): item = obj elif isinstance(obj, tuple): item = OrderedDict([(k, v) for k, v in obj]) else: item = self.to_dict(obj, selected_fields) if item is None: return None if self.MANDATORY_FIELDS: missing_fields = set(self.MANDATORY_FIELDS) - set(item.keys()) if missing_fields: raise MandatoryFieldsNotFound(missing_fields) formatted = self.format_dict(item=item) if formatted: self.after_format(formatted) return formatted
def on_loaded(self): for script in self.document.xpath('//script'): text = script.text if text is None: continue m = re.search("window.location.replace\('([^']+)'\);", text) if m: self.browser.location(m.group(1)) try: self.browser.select_form(name='banque') except FormNotFoundError: pass else: self.browser.set_all_readonly(False) accounts = OrderedDict() for tr in self.document.getroot().cssselect( 'table.compteTable > tbody > tr'): if len(tr.findall('td')) == 0: continue attr = tr.xpath('.//a')[0].attrib.get('onclick', '') m = re.search( "value = '(\w+)';(checkAndSubmit\('\w+','(\w+)','(\w+)'\))?", attr) if m: typeCompte = m.group(1) tagName = m.group(3) if tagName is not None: value = self.document.xpath( '//input[@name="%s"]' % m.group(3))[int( m.group(4))].attrib['value'] else: value = typeCompte accounts[value] = (typeCompte, tagName) try: typeCompte, tagName = accounts[self.browser.accnum] value = self.browser.accnum except KeyError: accnums = ', '.join(accounts.keys()) if self.browser.accnum != '00000000000': self.logger.warning( u'Unable to find account "%s". Available ones: %s' % (self.browser.accnum, accnums)) elif len(accounts) > 1: self.logger.warning( 'There are several accounts, please use "accnum" backend parameter to force the one to use (%s)' % accnums) value, (typeCompte, tagName) = accounts.popitem(last=False) self.browser['typeCompte'] = typeCompte if tagName is not None: self.browser[tagName] = [value] self.browser.submit()
def on_loaded(self): for script in self.document.xpath('//script'): text = script.text if text is None: continue m = re.search("window.location.replace\('([^']+)'\);", text) if m: self.browser.location(m.group(1)) try: self.browser.select_form(name='banque') except FormNotFoundError: pass else: self.browser.set_all_readonly(False) accounts = OrderedDict() for tr in self.document.getroot().cssselect('table.compteTable > tbody > tr'): if len(tr.findall('td')) == 0: continue attr = tr.xpath('.//a')[0].attrib.get('onclick', '') m = re.search("value = '(\w+)';(checkAndSubmit\('\w+','(\w+)','(\w+)'\))?", attr) if m: typeCompte = m.group(1) tagName = m.group(3) if tagName is not None: value = self.document.xpath('//input[@name="%s"]' % m.group(3))[int(m.group(4))].attrib['value'] else: value = typeCompte accounts[value] = (typeCompte, tagName) try: typeCompte, tagName = accounts[self.browser.accnum] value = self.browser.accnum except KeyError: accnums = ', '.join(accounts.keys()) if self.browser.accnum != '00000000000': self.logger.warning(u'Unable to find account "%s". Available ones: %s' % (self.browser.accnum, accnums)) elif len(accounts) > 1: self.logger.warning('There are several accounts, please use "accnum" backend parameter to force the one to use (%s)' % accnums) value, (typeCompte, tagName) = accounts.popitem(last=False) self.browser['typeCompte'] = typeCompte if tagName is not None: self.browser[tagName] = [value] self.browser.submit()