From b7df06299a516e904aa46c832eb3bb3c8d16c0a8 Mon Sep 17 00:00:00 2001 From: David Fischer Date: Sat, 17 Oct 2015 23:03:07 +0200 Subject: [PATCH 01/10] Fix PEP-8 warnings --- react/jsx.py | 1 + react/source.py | 2 +- react/test/__main__.py | 2 +- react/test/source.py | 2 +- react/utils/pipeline.py | 1 + setup.py | 2 +- 6 files changed, 6 insertions(+), 4 deletions(-) diff --git a/react/jsx.py b/react/jsx.py index 070c6f4..245bd35 100644 --- a/react/jsx.py +++ b/react/jsx.py @@ -63,5 +63,6 @@ def __init__(self, message): def transform(jsx_path, **opts): return JSXTransformer().transform(jsx_path, **opts) + def transform_string(jsx, **opts): return JSXTransformer().transform_string(jsx, **opts) diff --git a/react/source.py b/react/source.py index d93b71d..ef424ce 100644 --- a/react/source.py +++ b/react/source.py @@ -14,9 +14,9 @@ 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): diff --git a/react/test/__main__.py b/react/test/__main__.py index 13be1bd..cd848cb 100644 --- a/react/test/__main__.py +++ b/react/test/__main__.py @@ -19,4 +19,4 @@ if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main() diff --git a/react/test/source.py b/react/test/source.py index 7ae3b4c..993f696 100644 --- a/react/test/source.py +++ b/react/test/source.py @@ -18,7 +18,7 @@ class TestSource(TestCase): - + def test_path_for(self): JS_ROOT = source.JS_ROOT diff --git a/react/utils/pipeline.py b/react/utils/pipeline.py index 7fc8932..4f2f534 100644 --- a/react/utils/pipeline.py +++ b/react/utils/pipeline.py @@ -18,6 +18,7 @@ from pipeline.exceptions import CompilerError from react.jsx import JSXTransformer, TransformError + class JSXCompiler(CompilerBase): output_extension = 'js' diff --git a/setup.py b/setup.py index 59ab67b..b87abbb 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,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', From f76b7cd97a568fb84149d9cb7c0fc4da0fd8cbc6 Mon Sep 17 00:00:00 2001 From: David Fischer Date: Sat, 17 Oct 2015 23:07:24 +0200 Subject: [PATCH 02/10] Follow import style recommendations --- react/source.py | 8 ++++---- react/test/__init__.py | 5 ++--- react/test/jsx.py | 13 +++++++------ react/test/source.py | 9 +++++---- setup.py | 1 + 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/react/source.py b/react/source.py index ef424ce..bf1b784 100644 --- a/react/source.py +++ b/react/source.py @@ -12,14 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -from os.path import abspath, dirname, isfile, join +import os -JS_ROOT = abspath(join(dirname(__file__), 'js/react')) +JS_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), 'js/react')) def path_for(react_file): - path = join(JS_ROOT, react_file) - if isfile(path): + path = os.path.join(JS_ROOT, react_file) + if os.path.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..e635143 100644 --- a/react/test/__init__.py +++ b/react/test/__init__.py @@ -12,7 +12,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/jsx.py b/react/test/jsx.py index bd749bf..d70cd49 100644 --- a/react/test/jsx.py +++ b/react/test/jsx.py @@ -12,24 +12,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 index 993f696..f2db304 100644 --- a/react/test/source.py +++ b/react/test/source.py @@ -12,19 +12,20 @@ # See the License for the specific language governing permissions and # limitations under the License. -from os.path import abspath, join +import os +import unittest + from react import source -from unittest import TestCase -class TestSource(TestCase): +class TestSource(unittest.TestCase): def test_path_for(self): JS_ROOT = source.JS_ROOT self.assertEquals( source.path_for('JSXTransformer.js'), - abspath(join(JS_ROOT, 'JSXTransformer.js'))) + os.path.abspath(os.path.join(JS_ROOT, 'JSXTransformer.js'))) self.assertRaises( IOError, diff --git a/setup.py b/setup.py index b87abbb..f3bf1af 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,5 @@ from setuptools import setup, find_packages + from react import VERSION From b78bdb7f35549bd984a9cd1d6da1ff1cb21b3f13 Mon Sep 17 00:00:00 2001 From: David Fischer Date: Sat, 17 Oct 2015 23:09:02 +0200 Subject: [PATCH 03/10] Add encoding header --- react/__init__.py | 2 ++ react/jsx.py | 2 ++ react/source.py | 2 ++ react/test/__init__.py | 2 ++ react/test/__main__.py | 2 ++ react/test/jsx.py | 2 ++ react/test/source.py | 2 ++ react/utils/pipeline.py | 2 ++ setup.py | 2 ++ 9 files changed, 18 insertions(+) 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 245bd35..6892fa6 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"); diff --git a/react/source.py b/react/source.py index bf1b784..ae9b0be 100644 --- a/react/source.py +++ b/react/source.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/test/__init__.py b/react/test/__init__.py index e635143..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"); diff --git a/react/test/__main__.py b/react/test/__main__.py index cd848cb..0d9399d 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"); diff --git a/react/test/jsx.py b/react/test/jsx.py index d70cd49..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"); diff --git a/react/test/source.py b/react/test/source.py index f2db304..9523eda 100644 --- a/react/test/source.py +++ b/react/test/source.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/utils/pipeline.py b/react/utils/pipeline.py index 4f2f534..05f778b 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"); diff --git a/setup.py b/setup.py index f3bf1af..1125b89 100644 --- a/setup.py +++ b/setup.py @@ -1,3 +1,5 @@ +# -*- encoding: utf-8 -*- + from setuptools import setup, find_packages from react import VERSION From d2ae11b982976baf2c27bcb20efae3d0d9d0b42a Mon Sep 17 00:00:00 2001 From: David Fischer Date: Sat, 17 Oct 2015 23:09:32 +0200 Subject: [PATCH 04/10] Import unittest if necessary --- react/test/__main__.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/react/test/__main__.py b/react/test/__main__.py index 0d9399d..9240bf8 100644 --- a/react/test/__main__.py +++ b/react/test/__main__.py @@ -17,8 +17,6 @@ from jsx import * from source import * -import unittest - - if __name__ == '__main__': + import unittest unittest.main() From 12eb47773b2b3c177c9f200f10d6eb4887cc012b Mon Sep 17 00:00:00 2001 From: David Fischer Date: Sat, 17 Oct 2015 23:14:57 +0200 Subject: [PATCH 05/10] Remove useless code --- react/jsx.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/react/jsx.py b/react/jsx.py index 6892fa6..9919826 100644 --- a/react/jsx.py +++ b/react/jsx.py @@ -58,8 +58,7 @@ 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): From e9bde2d6f331ecf04c9b790f3806beb1eeaf0b4b Mon Sep 17 00:00:00 2001 From: David Fischer Date: Sat, 17 Oct 2015 23:15:21 +0200 Subject: [PATCH 06/10] Use super --- react/utils/pipeline.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/react/utils/pipeline.py b/react/utils/pipeline.py index 05f778b..bfd8424 100644 --- a/react/utils/pipeline.py +++ b/react/utils/pipeline.py @@ -22,10 +22,11 @@ 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): From e1449ceff0413270b6e7a24f519f4b88612a4c84 Mon Sep 17 00:00:00 2001 From: David Fischer Date: Sat, 17 Oct 2015 23:19:54 +0200 Subject: [PATCH 07/10] One less line --- react/utils/pipeline.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/react/utils/pipeline.py b/react/utils/pipeline.py index bfd8424..6591027 100644 --- a/react/utils/pipeline.py +++ b/react/utils/pipeline.py @@ -33,9 +33,8 @@ 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)) From 27a38086adf038803671b7db758f4d0ed4f14a13 Mon Sep 17 00:00:00 2001 From: David Fischer Date: Sat, 17 Oct 2015 23:20:48 +0200 Subject: [PATCH 08/10] One less line --- react/source.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/react/source.py b/react/source.py index ae9b0be..a3428fa 100644 --- a/react/source.py +++ b/react/source.py @@ -23,5 +23,4 @@ def path_for(react_file): path = os.path.join(JS_ROOT, react_file) if os.path.isfile(path): return path - else: - raise IOError('%s: Could not find specified React file.' % path) + raise IOError('%s: Could not find specified React file.' % path) From 988f0a9fccb2a7977e97cb445beb6fbaaffe9244 Mon Sep 17 00:00:00 2001 From: David Fischer Date: Sat, 17 Oct 2015 23:21:26 +0200 Subject: [PATCH 09/10] Few less lines --- react/test/source.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/react/test/source.py b/react/test/source.py index 9523eda..6bdbd5a 100644 --- a/react/test/source.py +++ b/react/test/source.py @@ -23,11 +23,9 @@ class TestSource(unittest.TestCase): def test_path_for(self): - JS_ROOT = source.JS_ROOT - self.assertEquals( source.path_for('JSXTransformer.js'), - os.path.abspath(os.path.join(JS_ROOT, 'JSXTransformer.js'))) + os.path.abspath(os.path.join(source.JS_ROOT, 'JSXTransformer.js'))) self.assertRaises( IOError, From 33de70d54ff29209ed417f73564a9e11a4d7ab23 Mon Sep 17 00:00:00 2001 From: David Fischer Date: Sat, 17 Oct 2015 23:52:37 +0200 Subject: [PATCH 10/10] Make react path overridable both at usage and in subclasses --- README.md | 4 ++++ react/jsx.py | 17 +++++++++++------ react/source.py | 26 -------------------------- react/test/source.py | 32 -------------------------------- 4 files changed, 15 insertions(+), 64 deletions(-) delete mode 100644 react/source.py delete mode 100644 react/test/source.py 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/jsx.py b/react/jsx.py index 9919826..cdf93a7 100644 --- a/react/jsx.py +++ b/react/jsx.py @@ -14,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): @@ -62,8 +65,10 @@ class TransformError(Exception): 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 a3428fa..0000000 --- a/react/source.py +++ /dev/null @@ -1,26 +0,0 @@ -# -*- encoding: utf-8 -*- - -# 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. - -import os - -JS_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), 'js/react')) - - -def path_for(react_file): - path = os.path.join(JS_ROOT, react_file) - if os.path.isfile(path): - return path - raise IOError('%s: Could not find specified React file.' % path) diff --git a/react/test/source.py b/react/test/source.py deleted file mode 100644 index 6bdbd5a..0000000 --- a/react/test/source.py +++ /dev/null @@ -1,32 +0,0 @@ -# -*- encoding: utf-8 -*- - -# 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. - -import os -import unittest - -from react import source - - -class TestSource(unittest.TestCase): - - def test_path_for(self): - self.assertEquals( - source.path_for('JSXTransformer.js'), - os.path.abspath(os.path.join(source.JS_ROOT, 'JSXTransformer.js'))) - - self.assertRaises( - IOError, - lambda: source.path_for('NonexistantFile.js'))