Fix encoding issues of boilerplate.py

* Allow writing a file that has unicode characters
* Allow unicode parameters in the command line (author in git config was
  fine, but not on command-line)
This commit is contained in:
Laurent Bachelier 2013-07-19 10:53:59 +02:00 committed by Romain Bignon
commit 9d9daa0f75

View file

@ -23,6 +23,7 @@ import subprocess
import datetime import datetime
import os import os
import sys import sys
import codecs
from mako.lookup import TemplateLookup from mako.lookup import TemplateLookup
MODULE_PATH = os.getenv( MODULE_PATH = os.getenv(
@ -36,8 +37,12 @@ VERSION = '0.g'
TEMPLATES = TemplateLookup(directories=[TEMPLATE_PATH]) TEMPLATES = TemplateLookup(directories=[TEMPLATE_PATH])
def u8(s):
return s.decode('utf-8')
def gitconfig(entry): def gitconfig(entry):
return subprocess.check_output('git config -z --get %s' % entry, shell=True)[:-1].decode('utf-8') return u8(subprocess.check_output('git config -z --get %s' % entry, shell=True)[:-1])
def write(target, contents): def write(target, contents):
@ -46,7 +51,7 @@ def write(target, contents):
if os.path.exists(target): if os.path.exists(target):
print >>sys.stderr, "%s already exists." % target print >>sys.stderr, "%s already exists." % target
sys.exit(4) sys.exit(4)
with open(target, mode='w') as f: with codecs.open(target, mode='w', encoding='utf-8') as f:
f.write(contents) f.write(contents)
print 'Created %s' % target print 'Created %s' % target
@ -123,10 +128,10 @@ def main():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument( parser.add_argument(
'-a', '--author', '-a', '--author',
default=gitconfig('user.name')) default=gitconfig('user.name'), type=u8)
parser.add_argument( parser.add_argument(
'-e', '--email', '-e', '--email',
default=gitconfig('user.email')) default=gitconfig('user.email'), type=u8)
subparsers = parser.add_subparsers() subparsers = parser.add_subparsers()
recipes = [BaseRecipe, ComicRecipe, ComicTestRecipe] recipes = [BaseRecipe, ComicRecipe, ComicTestRecipe]