# coding=utf-8 import logging import re log = logging.getLogger(__name__) def is_email(element): """ >>> is_email('username@example.com') True >>> is_email('example.com') False >>> is_email('firstname.lastname@domain.co.uk') True """ email_regex = r"^[A-Za-z0-9\.\+_-]+@[A-Za-z0-9\._-]+\.[a-zA-Z]*$" return re.match(email_regex, str(element)) def html_email(email, title=None): """ >>> html_email('username@example.com') 'username@example.com' """ if not title: title = email return '{title}'.format(email=email, title=title) def html_list(data): """ >>> html_list(['example.com', 'admin1@example.com', 'admin2@example.com']) '' """ html = "" def html_table_header_row(data): """ >>> html_table_header_row(['administrators', 'key', 'leader', 'project']) '\\n\\tAdministratorsKeyLeaderProject' >>> html_table_header_row(['key', 'project', 'leader', 'administrators']) '\\n\\tKeyProjectLeaderAdministrators' """ html = "\n\t" for th in data: title = th.replace("_", " ").title() html += "{}".format(title) return html + "" def html_row_with_ordered_headers(data, col_headers, row_header=None): """ >>> headers = ['administrators', 'key', 'leader', 'project'] >>> data = {'key': 'DEMO', 'project': 'Demonstration', 'leader': 'leader@example.com', 'administrators': ['admin1@example.com', 'admin2@example.com']} >>> html_row_with_ordered_headers(data, headers) '\\n\\tDEMOleader@example.comDemonstration' >>> headers = ['key', 'project', 'leader', 'administrators'] >>> html_row_with_ordered_headers(data, headers) '\\n\\tDEMODemonstration leader@example.com ' """ html = "\n\t" if row_header: html += "{}".format(row_header.replace("_", " ").title()) for header in col_headers: element = data[header] if isinstance(element, list): element = html_list(element) if is_email(element): element = html_email(element) html += "{}".format(element) return html + "" def html_table_from_dict(data, ordering): """ >>> ordering = ['administrators', 'key', 'leader', 'project'] >>> data = [ \ {'key': 'DEMO', 'project': 'Demo project', 'leader': 'lead@example.com', \ 'administrators': ['admin@example.com', 'root@example.com']},] >>> html_table_from_dict(data, ordering) '\\n \\n \\n
Administrators Key Leader Project
DEMO lead@example.com Demo project
' >>> ordering = ['key', 'project', 'leader', 'administrators'] >>> html_table_from_dict(data, ordering) '\\n \\n \\n
Key Project Leader Administrators
DEMO Demo project lead@example.com
' """ html = "" html += html_table_header_row(ordering) for row in data: html += html_row_with_ordered_headers(row, ordering) return html + "\n
" def html_table_from_nested_dict(data, ordering): """ >>> ordering = ['manager', 'admin', 'employee_count'] >>> data = { 'project_A': {'manager': 'John', 'admin': 'admin1@example.com', 'employee_count': '4'}, 'project_B': {'manager': 'Jane', 'admin': 'admin2@example.com', 'employee_count': '7'} } >>> html_table_from_nested_dict(data, ordering)
Manager Admin Employee Count
Project A John admin1@example.com 4
Project B Jane admin2@example.com 7
""" html = "" # Add an empty first cell for the row header column header_row = [""] header_row.extend(ordering) html += html_table_header_row(header_row) for row_header, row in data.items(): html += html_row_with_ordered_headers(row, ordering, row_header) return html + "\n
" def block_code_macro_confluence(code, lang=None): """ Wrap into code block macro :param code: :param lang: :return: """ if not lang: lang = "" return ( """\ {lang} """ ).format(lang=lang, code=code) def html_code__macro_confluence(text): """ Wrap into html macro :param text: :return: """ return ( """\ """ ).format(text=text) def noformat_code_macro_confluence(text, nopanel=None): """ Wrap into code block macro :param text: :param nopanel: (bool) True or False Removes the panel around the content. :return: """ if not nopanel: nopanel = False return ( """\ {nopanel} """ ).format(nopanel=nopanel, text=text) def symbol_normalizer(text): if not text: return "" result = text result = result.replace("Ä", u"Ä") result = result.replace("ä", u"ä") result = result.replace("Ë", u"Ë") result = result.replace("ë", u"ë") result = result.replace("Ï", u"Ï") result = result.replace("ï", u"ï") result = result.replace("Ö", u"Ö") result = result.replace("ö", u"ö") result = result.replace("Ü", u"Ü") result = result.replace("ü", u"ü") result = result.replace("Á", u"Á") result = result.replace("á", u"á") result = result.replace("É", u"É") result = result.replace("é", u"é") result = result.replace("Í", u"Í") result = result.replace("í", u"í") result = result.replace("Ó", u"Ó") result = result.replace("ó", u"ó") result = result.replace("Ú", u"Ú") result = result.replace("ú", u"ú") result = result.replace("À", u"À") result = result.replace("à", u"à") result = result.replace("È", u"È") result = result.replace("è", u"è") result = result.replace("Ì", u"Ì") result = result.replace("ì", u"ì") result = result.replace("Ò", u"Ò") result = result.replace("ò", u"ò") result = result.replace("Ù", u"Ù") result = result.replace("ù", u"ù") result = result.replace("Â", u"Â") result = result.replace("â", u"â") result = result.replace("Ê", u"Ê") result = result.replace("ê", u"ê") result = result.replace("Î", u"Î") result = result.replace("î", u"î") result = result.replace("Ô", u"Ô") result = result.replace("ô", u"ô") result = result.replace("Û", u"Û") result = result.replace("û", u"û") result = result.replace("Å", u"Å") result = result.replace("å", u"å") result = result.replace("°", u"°") return result def parse_cookie_file(cookie_file): """ Parse a cookies.txt file (Netscape HTTP Cookie File) return a dictionary of key value pairs compatible with requests. :param cookie_file: a cookie file :return dict of cookies pair """ cookies = {} with open(cookie_file, "r") as fp: for line in fp: if not re.match(r"^(#|$)", line): line_fields = line.strip().split("\t") try: cookies[line_fields[5]] = line_fields[6] except IndexError as e: log.error(e) return cookies