Better unicode mock support

This commit is contained in:
Manuel Ebert 2014-06-27 14:49:45 +02:00
parent 6e6462de0c
commit da0e74ff5f

View file

@ -49,9 +49,11 @@ def get_password(validator, keychain=None, max_attempts=3):
prompt("Extremely wrong password.")
sys.exit(1)
def get_keychain(journal_name):
return keyring.get_password('jrnl', journal_name)
def set_keychain(journal_name, password):
if password is None:
try:
@ -61,39 +63,50 @@ def set_keychain(journal_name, password):
elif not TEST:
keyring.set_password('jrnl', journal_name, password)
def u(s):
"""Mock unicode function for python 2 and 3 compatibility."""
return s if PY3 or type(s) is unicode else unicode(s.encode('string-escape'), "unicode_escape")
if PY3:
return str(s)
elif isinstance(s, basestring) and type(s) is not unicode:
return unicode(s.encode('string-escape'), "unicode_escape")
return unicode(s)
def py2encode(s):
"""Encode in Python 2, but not in python 3."""
return s.encode("utf-8") if PY2 and type(s) is unicode else s
def prompt(msg):
"""Prints a message to the std err stream defined in util."""
if not msg.endswith("\n"):
msg += "\n"
STDERR.write(u(msg))
def py23_input(msg=""):
STDERR.write(u(msg))
return STDIN.readline().strip()
def py23_read(msg=""):
return STDIN.read()
def yesno(prompt, default=True):
prompt = prompt.strip() + (" [Y/n]" if default else " [y/N]")
raw = py23_input(prompt)
return {'y': True, 'n': False}.get(raw.lower(), default)
def load_and_fix_json(json_path):
"""Tries to load a json object from a file.
If that fails, tries to fix common errors (no or extra , at end of the line).
"""
with open(json_path) as f:
json_str = f.read()
config = fixed = None
config = None
try:
return json.loads(json_str)
except ValueError as e:
@ -112,6 +125,7 @@ def load_and_fix_json(json_path):
prompt("[Entry was NOT added to your journal]")
sys.exit(1)
def get_text_from_editor(config, template=""):
tmpfile = os.path.join(tempfile.mktemp(prefix="jrnl"))
with codecs.open(tmpfile, 'w', "utf-8") as f:
@ -125,10 +139,12 @@ def get_text_from_editor(config, template=""):
prompt('[Nothing saved to file]')
return raw
def colorize(string):
"""Returns the string wrapped in cyan ANSI escape"""
return u"\033[36m{}\033[39m".format(string)
def slugify(string):
"""Slugifies a string.
Based on public domain code from https://github.com/zacharyvoase/slugify
@ -140,6 +156,7 @@ def slugify(string):
slug = re.sub(r'[-\s]+', '-', no_punctuation)
return u(slug)
def int2byte(i):
"""Converts an integer to a byte.
This is equivalent to chr() in Python 2 and bytes((i,)) in Python 3."""