diff --git a/README.md b/README.md index de1cf18..242aed2 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,10 @@ for jsx_path, js_path in my_paths: # For a single file, you can use a shortcut method. jsx.transform('path/to/input/file.jsx', js_path='path/to/output/file.js') + +# You can also define the path to react. +transformer = jsx.JSXTransformer('path/to/react') +jsx.transform('path/to/input/file.jsx', js_path='path/to/output/file.js', react_path='...') ``` You can also use ``transform_string(jsx)`` method to transform strings: diff --git a/react/__init__.py b/react/__init__.py index 09a9e4d..cb412de 100644 --- a/react/__init__.py +++ b/react/__init__.py @@ -1,3 +1,5 @@ +# -*- encoding: utf-8 -*- + # Copyright 2013 Facebook, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/react/jsx.py b/react/jsx.py index 070c6f4..cdf93a7 100644 --- a/react/jsx.py +++ b/react/jsx.py @@ -1,3 +1,5 @@ +# -*- encoding: utf-8 -*- + # Copyright 2013 Facebook, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -12,17 +14,20 @@ # See the License for the specific language governing permissions and # limitations under the License. +import os + import execjs -import react.source class JSXTransformer(object): JSX_TRANSFORMER_JS_EXPR = '(global.JSXTransformer || module.exports)' + react_path = os.path.abspath(os.path.join(__file__, 'js', 'react')) - def __init__(self): - path = react.source.path_for('JSXTransformer.js') - with open(path, 'rU') as f: + def __init__(self, react_path=None): + if react_path is not None: + self.react_path = react_path + with open(os.path.join(self.react_path, 'JSXTransformer.js'), 'rU') as f: self.context = execjs.compile(f.read()) def transform_string(self, jsx, harmony=False, strip_types=False): @@ -56,12 +61,14 @@ def transform(self, jsx_path, js_path=None, **kwargs): class TransformError(Exception): - def __init__(self, message): - Exception.__init__(self, message) + pass def transform(jsx_path, **opts): - return JSXTransformer().transform(jsx_path, **opts) + react_path = opts.pop('react_path', None) + return JSXTransformer(react_path=react_path).transform(jsx_path, **opts) + def transform_string(jsx, **opts): - return JSXTransformer().transform_string(jsx, **opts) + react_path = opts.pop('react_path', None) + return JSXTransformer(react_path=react_path).transform_string(jsx, **opts) diff --git a/react/source.py b/react/source.py deleted file mode 100644 index d93b71d..0000000 --- a/react/source.py +++ /dev/null @@ -1,25 +0,0 @@ -# Copyright 2013 Facebook, Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from os.path import abspath, dirname, isfile, join - - -JS_ROOT = abspath(join(dirname(__file__), 'js/react')) - -def path_for(react_file): - path = join(JS_ROOT, react_file) - if isfile(path): - return path - else: - raise IOError('%s: Could not find specified React file.' % path) diff --git a/react/test/__init__.py b/react/test/__init__.py index 064259a..a371e2c 100644 --- a/react/test/__init__.py +++ b/react/test/__init__.py @@ -1,3 +1,5 @@ +# -*- encoding: utf-8 -*- + # Copyright 2013 Facebook, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -12,7 +14,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from os.path import dirname, abspath - +import os -TEST_ROOT = abspath(dirname(__file__)) +TEST_ROOT = os.path.abspath(os.path.dirname(__file__)) diff --git a/react/test/__main__.py b/react/test/__main__.py index 13be1bd..9240bf8 100644 --- a/react/test/__main__.py +++ b/react/test/__main__.py @@ -1,3 +1,5 @@ +# -*- encoding: utf-8 -*- + # Copyright 2013 Facebook, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -15,8 +17,6 @@ from jsx import * from source import * -import unittest - - if __name__ == '__main__': - unittest.main() \ No newline at end of file + import unittest + unittest.main() diff --git a/react/test/jsx.py b/react/test/jsx.py index bd749bf..e4ba3c4 100644 --- a/react/test/jsx.py +++ b/react/test/jsx.py @@ -1,3 +1,5 @@ +# -*- encoding: utf-8 -*- + # Copyright 2013 Facebook, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -12,24 +14,25 @@ # See the License for the specific language governing permissions and # limitations under the License. -from os.path import join +import os +import unittest + from react import jsx from react.test import TEST_ROOT -from unittest import TestCase -class TestJSXTransformer(TestCase): +class TestJSXTransformer(unittest.TestCase): def test_transform(self): - jsx_path = join(TEST_ROOT, 'files/test.jsx') - js_path = join(TEST_ROOT, 'files/test.js') + jsx_path = os.path.join(TEST_ROOT, 'files/test.jsx') + js_path = os.path.join(TEST_ROOT, 'files/test.js') with open(js_path, 'rU') as js: self.assertEquals( jsx.transform(jsx_path), unicode(js.read())) - malformed_path = join(TEST_ROOT, 'files/malformed.jsx') + malformed_path = os.path.join(TEST_ROOT, 'files/malformed.jsx') self.assertRaises( jsx.TransformError, lambda: jsx.transform(malformed_path)) diff --git a/react/test/source.py b/react/test/source.py deleted file mode 100644 index 7ae3b4c..0000000 --- a/react/test/source.py +++ /dev/null @@ -1,31 +0,0 @@ -# Copyright 2013 Facebook, Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from os.path import abspath, join -from react import source -from unittest import TestCase - - -class TestSource(TestCase): - - def test_path_for(self): - JS_ROOT = source.JS_ROOT - - self.assertEquals( - source.path_for('JSXTransformer.js'), - abspath(join(JS_ROOT, 'JSXTransformer.js'))) - - self.assertRaises( - IOError, - lambda: source.path_for('NonexistantFile.js')) diff --git a/react/utils/pipeline.py b/react/utils/pipeline.py index 7fc8932..6591027 100644 --- a/react/utils/pipeline.py +++ b/react/utils/pipeline.py @@ -1,3 +1,5 @@ +# -*- encoding: utf-8 -*- + # Copyright 2013 Facebook, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,20 +20,21 @@ from pipeline.exceptions import CompilerError from react.jsx import JSXTransformer, TransformError + class JSXCompiler(CompilerBase): + output_extension = 'js' def __init__(self, *args, **kwargs): - CompilerBase.__init__(self, *args, **kwargs) + super(JSXCompiler, self).__init__(*args, **kwargs) self.transformer = JSXTransformer() def match_file(self, path): return path.endswith('.jsx') def compile_file(self, infile, outfile, outdated=False, force=False): - if not outdated and not force: - return - try: - return self.transformer.transform(infile, outfile) - except TransformError as e: - raise CompilerError(str(e)) + if outdated or force: + try: + return self.transformer.transform(infile, outfile) + except TransformError as e: + raise CompilerError(str(e)) diff --git a/setup.py b/setup.py index 59ab67b..1125b89 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,7 @@ +# -*- encoding: utf-8 -*- + from setuptools import setup, find_packages + from react import VERSION @@ -11,7 +14,7 @@ license='Apache-2.0', description='Python bridge to JSX & the React JavaScript library.', long_description=open('DESCRIPTION').read(), - classifiers =[ + classifiers=[ 'Development Status :: 4 - Beta', 'Intended Audience :: Developers', 'License :: OSI Approved :: Apache Software License',