From 1d1bb71203fc4a963b8bdaeca97094c95656e62b Mon Sep 17 00:00:00 2001 From: Collin Watson Date: Sat, 14 Jan 2012 12:17:46 -0800 Subject: [PATCH] Added unicode file and input examples --- examples/unicode.json | 4 ++++ examples/unicode.py | 33 ++++++++++++++++++++++++++++++--- examples/unicode.sh | 2 +- 3 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 examples/unicode.json diff --git a/examples/unicode.json b/examples/unicode.json new file mode 100644 index 0000000..94f9554 --- /dev/null +++ b/examples/unicode.json @@ -0,0 +1,4 @@ +{ + "title": "Bashō's 'old pond'", + "text": "古池や蛙飛込む水の音" +} diff --git a/examples/unicode.py b/examples/unicode.py index 3df92fc..8168fde 100644 --- a/examples/unicode.py +++ b/examples/unicode.py @@ -1,11 +1,18 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- - import os import sys +import codecs + sys.path.insert(0, os.path.abspath('..')) +try: + import json +except: + import simplejson as json + from clint import args +from clint import piped_in from clint.textui import colored, puts, indent if __name__ == '__main__': @@ -28,5 +35,25 @@ if __name__ == '__main__': puts('') puts('Arguments:') with indent(4): - for arg in args.all: - puts('%s' % colored.red(arg)) + puts('%s' % colored.red(args[0])) + + puts('') + puts('File:') + with indent(4): + f = args.files[0] + puts(colored.yellow('%s:' % f)) + with indent(2): + fd = codecs.open(f, encoding='utf-8') + for line in fd: + line = line.strip('\n\r') + puts(colored.yellow(' %s' % line)) + fd.close() + + puts('') + puts('Input:') + with indent(4): + in_data = json.loads(piped_in()) + title = in_data['title'] + text = in_data['text'] + puts(colored.blue('Title: %s' % title)) + puts(colored.magenta('Text: %s' % text)) diff --git a/examples/unicode.sh b/examples/unicode.sh index 47a95e9..fc93c67 100755 --- a/examples/unicode.sh +++ b/examples/unicode.sh @@ -1,3 +1,3 @@ #!/usr/bin/env sh -python unicode.py こんにちは。 +python unicode.py こんにちは。 unicode.json < unicode.json