dosage/scripts/scriptutil.py

67 lines
1.6 KiB
Python
Raw Normal View History

2013-01-09 21:21:19 +00:00
# Copyright (C) 2012-2013 Bastian Kleineidam
2012-12-12 16:41:29 +00:00
import re
2012-12-19 19:42:53 +00:00
import json
2013-02-13 19:02:47 +00:00
from dosagelib.util import unescape, unquote, asciify
2012-11-29 05:46:58 +00:00
def contains_case_insensitive(adict, akey):
2012-12-19 19:42:53 +00:00
"""Check if key is in adict. The search is case insensitive."""
2012-11-29 05:46:58 +00:00
for key in adict:
if key.lower() == akey.lower():
return True
return False
2012-12-12 16:41:29 +00:00
_tagre = re.compile(r"<.+?>")
def remove_html_tags(text):
2012-12-19 19:42:53 +00:00
"""Remove all HTML tags from text."""
2012-12-12 16:41:29 +00:00
return _tagre.sub("", text)
def capfirst(text):
"""Uppercase the first character of text."""
if not text:
return text
return text[0].upper() + text[1:]
_ws = re.compile(r"\s+")
def compact_whitespace(text):
2012-12-19 19:42:53 +00:00
"""Compact all subsequent whitespace to a single space."""
2012-12-12 16:41:29 +00:00
if not text:
return text
return _ws.sub(" ", text)
2012-12-19 19:42:53 +00:00
def save_result(res, json_file):
"""Save result to file."""
with open(json_file, 'wb') as f:
json.dump(res, f, sort_keys=True)
def load_result(json_file):
2013-01-09 21:20:03 +00:00
"""Load contents of a json file."""
2012-12-19 19:42:53 +00:00
with open(json_file, "rb") as f:
return json.load(f)
2013-01-09 21:20:03 +00:00
def truncate_name(text):
"""Ensure the comic name does not exceed 100 characters."""
return text[:100]
2013-02-13 19:02:47 +00:00
def format_name(text):
"""Format a comic name."""
name = unescape(text)
name = asciify(name.replace('&', 'And').replace('@', 'At'))
name = capfirst(name)
return name
def format_description(text):
"""Format a comic description."""
desc = remove_html_tags(text)
desc = unescape(desc)
desc = unquote(desc)
desc = compact_whitespace(desc).strip()
return desc