stashutils.extensions: now handle unicode properly; fixes
"""This module defines functions to interact with stash extensions."""
import os
import shutil
+import io
from stash.system.shcommon import _STASH_EXTENSION_BIN_PATH as EBP
from stash.system.shcommon import _STASH_EXTENSION_MAN_PATH as EMP
@@ -18,7 +19,7 @@ load_from_dir = load_from_dir
def create_file(dest, content):
"""
- creates a file at dest with content.
+ Creates a file at dest with content.
If content is a string or unicode, use it as the content.
Otherwise, use content.read() as the content.
"""
@@ -27,14 +28,18 @@ def create_file(dest, content):
parent = os.path.dirname(dest)
if not os.path.exists(parent):
os.makedirs(parent)
- with open(dest, "wb") as f:
+ if isinstance(content, binary_type):
+ with io.open(dest, "wb") as f:
+ f.write(content)
+ elif isinstance(content, text_type):
+ with io.open(dest, "w", encoding="utf-8") as f:
f.write(content)
return dest
def create_page(name, content):
"""
- creates a manpage with name filled with content.
+ Creates a manpage with name filled with content.
If content is a list or tuple, instead create a dir and fill it with pages
created from the elements of this list.
The list should consist of tuples of (ending, content)
