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..cd10705 --- /dev/null +++ b/coffeescript/scripts/console.py @@ -0,0 +1,57 @@ +# 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 + if type(self.out) is str or type(self.out) is unicode: + self.out = open(self.out, 'w') + + 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) + 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',