def parse_pob(author, content, minify=False): """ Trigger the parsing of the pastebin link, pass it to the output creating object and send a message back :param channel: receiving channel :param author: user sending the message :param paste_key: pastebin paste key :param argument: optional: arguments to determine the output :return: """ paste_key = pastebin.fetch_paste_key(content) if paste_key: xml = None log.info("Parsing pastebin with key={}".format(paste_key)) try: xml = pastebin.get_as_xml(paste_key) except HTTPError as err: log.error("Invalid pastebin-url msg={}".format(err)) if xml: parser = Parser() build = parser.parse_build(xml) # print(build) embed = pob_output.generate_response(author, build, minified=minify) log.debug("embed={}; length={}".format(embed, embed.__sizeof__())) return embed
def decode_base64_and_inflate(b64string): try: decoded_data = base64.b64decode(b64string) return zlib.decompress(decoded_data) except zlib.error as err: log.error("ZLib Error in paste: err={}".format(err)) except ValueError as err: log.error("Value Error in paste: err={}".format(err))
def get_tree_link(tree): tree_index = Parser.get_attrib_if_exists(tree, 'activeSpec') if tree_index: # when a tree was selected, get the corresponding url selected_tree = tree[int(tree_index) - 1].find('URL').text try: return shrink_tree_url(selected_tree) except ValueError as err: log.error("Tree shrinking failed... err={}".format(err)) return selected_tree
def get_sage_cost_price(stock_code: str) -> Optional[float]: try: payload = {"select": "cost", "format": "json", "where": f"reference eq '{stock_code}'"} payload["where"] = urllib.parse.quote(payload["where"]) res = _call_sage(sage_stock_uri, payload) except Exception as err: log.error(f"Exception retrieving Sage stock ", exc_info=1) raise err return res
def _call_sage(endpoint: str, payload: dict) -> Optional[float]: log.debug("Calling sage") try: r = requests.get( endpoint, auth=(sage_user, sage_password), params=payload, timeout=sage_timeout, verify=False, ) except ConnectTimeout: log.error("Connection timeout connecting to Sage") raise SageException("Timed out connecting to Sage") except TimeoutError: log.error("Connection timeout connecting to Sage") raise SageException("Timed out communicating with Sage") except Exception as e: log.error(f"Error communicating with Sage: {e}") raise SageException(f"Error communicating with Sage: {e}") log.debug(f"Sage returned a status of {r.status_code}") if r.status_code != 200: log.error(f"Sage returned an error status of: ({r.status_code}) {r.reason}") raise SageException(f"Sage returned an error status of: ({r.status_code}) {r.reason}") try: r.encoding = 'utf-8-sig' j = r.json() except Exception as e: log.error(f"Sage did not return a valid json response: {e}") raise SageException(f"Sage did not return a valid json response: {e}") try: sage_response = SageResponse(**j) except ValidationError as e: log.error(f"Validation of Sage response failed: {e}") raise SageException(f"Validation of Sage response failed: {e}") if len(sage_response.resources) == 0: return None return sage_response.resources[0].cost
def urllib_error_retry(attempt_number, ms_since_first_attempt): delay = 1 * (2 ** (attempt_number - 1)) log.error("An error occurred during get_url_data(). Sleeping for {:.0f}s before retrying...".format(delay)) return delay * 1000