File size: 11,009 Bytes
56d74b6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | # 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')
'<a href="mailto:username@example.com">username@example.com</a>'
"""
if not title:
title = email
return '<a href="mailto:{email}">{title}</a>'.format(email=email, title=title)
def html_list(data):
"""
>>> html_list(['example.com', 'admin1@example.com', 'admin2@example.com'])
'<ul>
<li>example.com</li>
<li><a href="mailto:admin1@example.com">admin1@example.com</a></li>
<li><a href="mailto:admin2@example.com">admin2@example.com</a></li>
</ul>'
"""
html = "<ul>"
for item in data:
if isinstance(item, dict):
if item.get("email"):
item = html_email(item.get("email"), item.get("name", None))
elif item.get("name"):
item = item.get("name")
if is_email(item):
item = html_email(item, item)
html += "<li>{}</li>".format(item)
return html + "</ul>"
def html_table_header_row(data):
"""
>>> html_table_header_row(['administrators', 'key', 'leader', 'project'])
'\\n\\t<tr><th>Administrators</th><th>Key</th><th>Leader</th><th>Project</th></tr>'
>>> html_table_header_row(['key', 'project', 'leader', 'administrators'])
'\\n\\t<tr><th>Key</th><th>Project</th><th>Leader</th><th>Administrators</th></tr>'
"""
html = "\n\t<tr>"
for th in data:
title = th.replace("_", " ").title()
html += "<th>{}</th>".format(title)
return html + "</tr>"
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\\t<tr><td><ul>
<li><a href="mailto:admin1@example.com">admin1@example.com</a></li>
<li><a href="mailto:admin2@example.com">admin2@example.com</a></li>
</ul></td><td>DEMO</td><td>leader@example.com</td><td>Demonstration</td></tr>'
>>> headers = ['key', 'project', 'leader', 'administrators']
>>> html_row_with_ordered_headers(data, headers)
'\\n\\t<tr><td>DEMO</td><td>Demonstration</td>
<td>leader@example.com</td><td>
<ul>
<li><a href="mailto:admin1@example.com">admin1@example.com</a></li>
<li><a href="mailto:admin2@example.com">admin2@example.com</a></li>
</ul></td></tr>'
"""
html = "\n\t<tr>"
if row_header:
html += "<th>{}</th>".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 += "<td>{}</td>".format(element)
return html + "</tr>"
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)
'<table>
<tbody>\\n
<tr>
<th>Administrators</th>
<th>Key</th>
<th>Leader</th>
<th>Project</th>
</tr>\\n
<tr>
<td>
<ul>
<li><a href="mailto:admin@example.com">admin@example.com</a></li>
<li><a href="mailto:root@example.com">root@example.com</a></li>
</ul>
</td>
<td>DEMO</td>
<td>lead@example.com</td>
<td>Demo project</td>
</tr>\\n
</tbody>
</table>'
>>> ordering = ['key', 'project', 'leader', 'administrators']
>>> html_table_from_dict(data, ordering)
'<table>
<tbody>\\n
<tr>
<th>Key</th>
<th>Project</th>
<th>Leader</th>
<th>Administrators</th>
</tr>\\n
<tr>
<td>DEMO</td>
<td>Demo project</td>
<td>lead@example.com</td>
<td>
<ul>
<li><a href="mailto:admin@example.com">admin@example.com</a></li>
<li><a href="mailto:root@example.com">root@example.com</a></li>
</ul>
</td>
</tr>\\n
</tbody>
</table>'
"""
html = "<table><tbody>"
html += html_table_header_row(ordering)
for row in data:
html += html_row_with_ordered_headers(row, ordering)
return html + "\n</tbody></table>"
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)
<table>
<tbody>
<tr>
<th></th>
<th>Manager</th>
<th>Admin</th>
<th>Employee Count</th>
</tr>
<tr>
<th>Project A</th>
<td>John</td>
<td><a href="mailto:admin1@example.com">admin1@example.com</a></td>
<td>4</td>
</tr>
<tr>
<th>Project B</th>
<td>Jane</td>
<td><a href="mailto:admin2@example.com">admin2@example.com</a></td>
<td>7</td>
</tr>
</tbody>
</table>
"""
html = "<table><tbody>"
# 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</tbody></table>"
def block_code_macro_confluence(code, lang=None):
"""
Wrap into code block macro
:param code:
:param lang:
:return:
"""
if not lang:
lang = ""
return (
"""\
<ac:structured-macro ac:name="code" ac:schema-version="1">
<ac:parameter ac:name="language">{lang}</ac:parameter>
<ac:plain-text-body><![CDATA[{code}]]></ac:plain-text-body>
</ac:structured-macro>
"""
).format(lang=lang, code=code)
def html_code__macro_confluence(text):
"""
Wrap into html macro
:param text:
:return:
"""
return (
"""\
<ac:structured-macro ac:name="html" ac:schema-version="1">
<ac:plain-text-body><![CDATA[{text}]]></ac:plain-text-body>
</ac:structured-macro>
"""
).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 (
"""\
<ac:structured-macro ac:name="noformat" ac:schema-version="1">
<ac:parameter ac:name="nopanel">{nopanel}</ac:parameter>
<ac:plain-text-body><![CDATA[{text}]]></ac:plain-text-body>
</ac:structured-macro>
"""
).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
|