2016-03-03 22:08:51 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-01-05 15:50:57 +00:00
|
|
|
# Copyright (C) 2012-2014 Bastian Kleineidam
|
2016-04-03 19:31:56 +00:00
|
|
|
# Copyright (C) 2015-2016 Tobias Gruetzmacher
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
|
2012-12-19 19:42:53 +00:00
|
|
|
import json
|
2016-03-31 21:25:53 +00:00
|
|
|
import codecs
|
|
|
|
|
2016-03-03 22:08:51 +00:00
|
|
|
from dosagelib.util import unescape, 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
|
|
|
|
|
|
|
def capfirst(text):
|
|
|
|
"""Uppercase the first character of text."""
|
|
|
|
if not text:
|
|
|
|
return text
|
|
|
|
return text[0].upper() + text[1:]
|
|
|
|
|
|
|
|
|
2012-12-19 19:42:53 +00:00
|
|
|
def save_result(res, json_file):
|
|
|
|
"""Save result to file."""
|
2016-03-31 21:25:53 +00:00
|
|
|
with codecs.open(json_file, 'wb', 'utf-8') as f:
|
2016-03-03 22:08:51 +00:00
|
|
|
json.dump(res, f, sort_keys=True, indent=2, separators=(',', ': '))
|
2012-12-19 19:42:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
def load_result(json_file):
|
2013-01-09 21:20:03 +00:00
|
|
|
"""Load contents of a json file."""
|
2016-03-31 21:25:53 +00:00
|
|
|
with codecs.open(json_file, 'rb', 'utf-8') as f:
|
2012-12-19 19:42:53 +00:00
|
|
|
return json.load(f)
|
2013-01-09 21:20:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def truncate_name(text):
|
2016-03-03 22:08:51 +00:00
|
|
|
"""Ensure the comic name does not exceed 50 characters."""
|
|
|
|
return text[:50]
|
2013-02-13 19:02:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def format_name(text):
|
|
|
|
"""Format a comic name."""
|
|
|
|
name = unescape(text)
|
2016-04-03 19:31:56 +00:00
|
|
|
name = "".join(capfirst(x) for x in name.split(" "))
|
2013-04-29 18:24:54 +00:00
|
|
|
name = asciify(name.replace(u'&', u'And').replace(u'@', u'At'))
|
2013-02-13 19:02:47 +00:00
|
|
|
return name
|