def raise_for_response(self, response, valid_response): if response.status != valid_response: error_msg = response.body try: body = parse_xml(error_msg) if iselement(body): error_msg = parse_error(body) except XMLParseError: pass values = (response.error, error_msg, response.status) message = 'Message: %s, Body: %s, Status code: %s' % (values) raise LibcloudError(message, driver=self)
def get_ispdb_confg(domain: str) -> [dict, dict]: if domain in COMMON_ISPDB_DOMAINS: logger.debug(f'Got hardcoded autoconfig for {domain}') return ( COMMON_ISPDB_DOMAINS[domain]['imap_connection'], COMMON_ISPDB_DOMAINS[domain]['smtp_connection'], ) logger.debug(f'Looking up thunderbird autoconfig for {domain}') ispdb_url = ISPDB_URL_FORMATTER.format(domain=domain) try: response = requests.get(ispdb_url) except requests.RequestException as e: logger.warning( f'Failed to fetch ISPDB settings for domain: {domain}: {e}') return if response.status_code == 200: imap_settings = {} smtp_settings = {} # Parse the XML et = parse_xml(response.content) provider = et.find('emailProvider') for incoming in provider.findall('incomingServer'): if incoming.get('type') != 'imap': continue imap_settings['host'] = incoming.find('hostname').text imap_settings['port'] = int(incoming.find('port').text) imap_settings['ssl'] = incoming.find('socketType').text == 'SSL' break for outgoing in provider.findall('outgoingServer'): if outgoing.get('type') != 'smtp': continue smtp_settings['host'] = outgoing.find('hostname').text smtp_settings['port'] = int(outgoing.find('port').text) socket_type = outgoing.find('socketType').text smtp_settings['ssl'] = socket_type == 'SSL' smtp_settings['tls'] = socket_type == 'STARTTLS' break logger.debug((f'Autoconf settings for {domain}: ' f'imap={imap_settings}, smtp={smtp_settings}')) return imap_settings, smtp_settings
def read_records_from_input(self, input_stream: BinaryIO) -> Iterator[dict]: """Read records from an input stream in xml format. :param input_stream: The input stream of the records. :type input_stream: BinaryIO :yield: A record. :rtype: Iterator[dict] """ self.reader = parse_xml(input_stream).getroot() for record in self.reader: yield {attribute.tag: attribute.text for attribute in record}
def load_cache(root, db_file, cache, loader): # Read from json cache if one is present. cache_path = os.path.join(root, cache) db = {} if os.path.exists(cache_path): try: with open(cache_path) as f: db = json.load(f) except json.decoder.JSONDecodeError as e: print("Failed to decode {0}; deleting.".format(cache)) os.remove(cache_path) if not db: db = loader(parse_xml(os.path.join(root, db_file)).getroot()) with open(cache_path, 'w') as f: json.dump(db, f) return db
def clean_file(self): file = self.cleaned_data.get("file") if file: try: tree = parse_xml(file) root = tree.getroot() msg = root.find('{*}BkToCstmrStmt') if 'camt.053.001.04' not in str(root.tag) or msg is None: raise forms.ValidationError( "Die Datei liegt nicht im korrekten Format vor!") self.cleaned_data["camt.053.001.04"] = msg except ParseError as error: raise forms.ValidationError( f"Die Datei kann nicht verarbeitet werden! {error}") return file
def get_ispdb_confg(domain): logger.debug(f'Looking up thunderbird autoconfig for {domain}') response = requests.get(f'{ISPDB_URL}/{domain}') if response.status_code == 200: imap_settings = {} smtp_settings = {} # Parse the XML et = parse_xml(response.content) provider = et.find('emailProvider') for incoming in provider.findall('incomingServer'): if incoming.get('type') != 'imap': continue imap_settings['host'] = incoming.find('hostname').text imap_settings['port'] = int(incoming.find('port').text) imap_settings['ssl'] = incoming.find('socketType').text == 'SSL' break for outgoing in provider.findall('outgoingServer'): if outgoing.get('type') != 'smtp': continue smtp_settings['host'] = outgoing.find('hostname').text smtp_settings['port'] = int(outgoing.find('port').text) socket_type = outgoing.find('socketType').text smtp_settings['ssl'] = socket_type == 'SSL' smtp_settings['tls'] = socket_type == 'STARTTLS' break logger.debug(( f'Autoconf settings for {domain}: ' f'imap={imap_settings}, smtp={smtp_settings}' )) return imap_settings, smtp_settings