From fce3b268f389c127bd99da23d10dea6fde103cde Mon Sep 17 00:00:00 2001 From: Florian Finke Date: Sat, 14 Jul 2012 14:59:20 +0200 Subject: [PATCH 1/4] added standalone coffee compiler --- coffeescript/scripts/coffee.py | 14 +++++++++ coffeescript/scripts/console.py | 55 +++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 coffeescript/scripts/coffee.py create mode 100644 coffeescript/scripts/console.py diff --git a/coffeescript/scripts/coffee.py b/coffeescript/scripts/coffee.py new file mode 100644 index 0000000..f707b88 --- /dev/null +++ b/coffeescript/scripts/coffee.py @@ -0,0 +1,14 @@ +import console +import coffeescript +import optparse +import sys + + +def main(): + usage = "usage: %prog [source [destination]]" + parser = optparse.OptionParser(usage=usage) + (options, argv) = parser.parse_args() + source = open(argv[0]) if len(argv) > 0 else sys.stdin + destination = argv[1] if len(argv) > 1 else sys.stdout + output = console.Writer(destination) + output.write(coffeescript.compile(source.read())) diff --git a/coffeescript/scripts/console.py b/coffeescript/scripts/console.py new file mode 100644 index 0000000..98cde84 --- /dev/null +++ b/coffeescript/scripts/console.py @@ -0,0 +1,55 @@ +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +""" + +Copyright (c) 2011 Evgeny V. Generalov. +mailto:e.generalov@gmail.com +""" +import sys +import locale + + +class Writer(object): + """text writer""" + + def __init__(self, output=None, out_encoding=None): + self.out = None + self.out_encoding = out_encoding + self.set_output(output) + + def set_output(self, output=None): + """set output stream""" + self.out = output or sys.stdout + + def write(self, string): + """write a string to the output""" + self.out.write(self._encode(string)) + + def writeln(self, string): + """write a line to the output""" + print >> self.out, self._encode(string) + + def _encode(self, data): + """encode data to string""" + # py3k streams handle their encoding : + if sys.version_info >= (3, 0): + return data + if not isinstance(data, unicode): + return data + # data is string + encoding = (self.out_encoding or + getattr(self.out, 'encoding', None) or + locale.getdefaultlocale()[1] or + sys.getdefaultencoding()) + return data.encode(encoding) + From 00190ccbcad8d01324ed9adfb62e17ccf39e632f Mon Sep 17 00:00:00 2001 From: Florian Finke Date: Sat, 14 Jul 2012 15:01:44 +0200 Subject: [PATCH 2/4] added coffeescript.scripts to packages --- setup.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/setup.py b/setup.py index 26135eb..dd62df3 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,6 @@ #!python3 #encoding: ascii from setuptools import setup -import sys import io with io.open('README.rst', encoding='ascii') as fp: @@ -12,13 +11,17 @@ version="1.0.3", author='OMOTO Kenji', description='A bridge to the JS CoffeeScript compiler', - - packages=['coffeescript'], + + packages=['coffeescript', 'coffeescript.scripts'], package_dir={'coffeescript': 'coffeescript'}, package_data={ 'coffeescript': ['coffee-script.js'], }, - + entry_points='''\ + [console_scripts] + coffee = coffeescript.scripts.coffee:main + ''', + long_description=long_description, url='https://github.com/doloopwhile/Python-CoffeeScript', author_email='doloopwhile@gmail.com', From 027aedb23c5a860311efb2a988a8b0cafe43afa2 Mon Sep 17 00:00:00 2001 From: Florian Finke Date: Sat, 14 Jul 2012 15:32:20 +0200 Subject: [PATCH 3/4] check if self.out is str --- coffeescript/scripts/console.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/coffeescript/scripts/console.py b/coffeescript/scripts/console.py index 98cde84..17b41cf 100644 --- a/coffeescript/scripts/console.py +++ b/coffeescript/scripts/console.py @@ -30,6 +30,8 @@ def __init__(self, output=None, out_encoding=None): def set_output(self, output=None): """set output stream""" self.out = output or sys.stdout + if type(self.out) == 'str': + self.out = open(self.out, 'w') def write(self, string): """write a string to the output""" From 8f8b65f1ee4035a0c6742a952a686b6bc12ca625 Mon Sep 17 00:00:00 2001 From: Florian Finke Date: Sat, 14 Jul 2012 15:38:37 +0200 Subject: [PATCH 4/4] small bugfix --- coffeescript/scripts/console.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coffeescript/scripts/console.py b/coffeescript/scripts/console.py index 17b41cf..cd10705 100644 --- a/coffeescript/scripts/console.py +++ b/coffeescript/scripts/console.py @@ -30,7 +30,7 @@ def __init__(self, output=None, out_encoding=None): def set_output(self, output=None): """set output stream""" self.out = output or sys.stdout - if type(self.out) == 'str': + if type(self.out) is str or type(self.out) is unicode: self.out = open(self.out, 'w') def write(self, string):