Exemplo n.º 1
0
 def output(self, _in, out, **kw):
     content = _in.read()
     transformer = JSXTransformer()
     js = transformer.transform_string(content)
     out.write(self._moduleIntro())
     out.write(js)
     out.write(self._moduleOutro())
Exemplo n.º 2
0
 def __init__(self,
              content,
              attrs=None,
              filter_type=None,
              charset=None,
              filename=None):
     super(JSXCompiler, self).__init__(content, filter_type, filename)
     self.transformer = JSXTransformer()
Exemplo n.º 3
0
class JSXCompiler(FilterBase):

    def __init__(self, content, attrs=None, filter_type=None, charset=None,
                 filename=None):
        super(JSXCompiler, self).__init__(content, filter_type, filename)
        self.transformer = JSXTransformer()

    def input(self, **kwargs):
        if self.filename:
            return self.transformer.transform(self.filename)
        else:
            return self.transformer.transform_string(self.content)
Exemplo n.º 4
0
def jsx(filename: str) -> Response:
    # Figure out what our update time is to namespace on
    jsxfile = os.path.join(static_location, filename)
    mtime = os.path.getmtime(jsxfile)
    namespace = f'{mtime}.{jsxfile}'
    jsx = g.cache.get(namespace)
    if jsx is None:
        with open(jsxfile, 'rb') as f:
            transformer = JSXTransformer()
            jsx = transformer.transform_string(f.read().decode('utf-8'))
        # Set the cache to one year, since we namespace on this file's update time
        g.cache.set(namespace, jsx, timeout=86400 * 365)
    return Response(jsx, mimetype='application/javascript')
Exemplo n.º 5
0
def jsx(filename: str) -> Response:
    # Figure out what our update time is to namespace on
    jsxfile = os.path.join(static_location, filename)
    mtime = os.path.getmtime(jsxfile)
    namespace = '{}.{}'.format(mtime, jsxfile)
    jsx = g.cache.get(namespace)
    if jsx is None:
        transformer = JSXTransformer()
        f = open(jsxfile, 'r')
        jsx = transformer.transform_string(f.read())
        f.close()
        # Set the cache to one year, since we namespace on this file's update time
        g.cache.set(namespace, jsx, timeout=86400 * 365)
    return Response(jsx, mimetype='application/javascript')
Exemplo n.º 6
0
class ReactHandler(StaticFileHandler):
    def initialize(self, path):
        self.root = os.path.abspath(path) + os.path.sep
        self.jsx = JSXTransformer()
        self.settings.setdefault('compiled_template_cache', False)

    def try_transform(self, abspath):
        try:
            self.jsx.transform(abspath + "x", js_path=abspath)
        except Exception as e:
            raise HTTPError(
                500, "Could not transform %s into a JS file. %s" %
                (os.path.basename(abspath + "x"), str(e)))

    def get(self, path, **kwargs):
        """
        Transform the JSX online as they change.
        """
        if os.path.sep != "/":
            path = path.replace("/", os.path.sep)
        abspath = os.path.abspath(os.path.join(self.root, path))
        self.absolute_path = abspath
        # some default errors:
        if not (abspath + os.path.sep).startswith(self.root):
            raise HTTPError(403, "%s is not in root static directory", path)
        if not os.path.exists(abspath):
            # special case, check if jsx file exists.
            if abspath.endswith(".js") and os.path.exists(abspath + "x"):
                self.try_transform(abspath)
                # transformation to JS was successful.
            else:
                raise HTTPError(404)
        if not os.path.isfile(abspath):
            raise HTTPError(403, "%s is not a file", path)

        if abspath.endswith(".js") and os.path.exists(abspath + "x"):
            if os.path.getmtime(abspath + "x") > os.path.getmtime(abspath):
                # more recent JSX file than JS
                self.try_transform(abspath)
            # else the generated file is recent enough
        template_path = self.get_template_path()
        if not template_path:
            template_path = os.path.dirname(os.path.abspath(__file__))
        loader = RequestHandler._template_loaders[template_path]
        return super(ReactHandler, self).get(path, **kwargs)

    @classmethod
    def _get_cached_version(cls, abs_path):
        return None
Exemplo n.º 7
0
class JSXCompiler(FilterBase):
    def __init__(self,
                 content,
                 attrs=None,
                 filter_type=None,
                 charset=None,
                 filename=None):
        super(JSXCompiler, self).__init__(content, filter_type, filename)
        self.transformer = JSXTransformer()

    def input(self, **kwargs):
        if self.filename:
            return self.transformer.transform(self.filename)
        else:
            return self.transformer.transform_string(self.content)
 def input(self, **kwargs):
     if REACT_DEBUG:
         print("Process INPUT with :: {}".format(kwargs))
     try:
         t = JSXTransformer()
         rtn_str = t.transform_string(self.content)
     except Exception as e:
         """
             Given the way this can work for on the fly compression
             we'll print it out here and then raise the error; that way
             when using GUNICORN we can still see the error
         """
         print("Unable to process JSX :: {}".format(e))
         raise
     return rtn_str
Exemplo n.º 9
0
class JSXCompiler(CompilerBase):
    output_extension = 'js'

    def __init__(self, *args, **kwargs):
        CompilerBase.__init__(self, *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:
            harmony = settings.REACT_HARMONY
        except KeyError:
            harmony = False

        try:
            strip_types = settings.REACT_STRIP_TYPES
        except KeyError:
            strip_types = False

        try:
            return self.transformer.transform(
                infile,
                outfile,
                harmony=harmony,
                strip_types=strip_types,
            )
        except TransformError as e:
            raise CompilerError(str(e))
Exemplo n.º 10
0
class JSXCompiler(CompilerBase):
    output_extension = 'js'

    def __init__(self, *args, **kwargs):
        CompilerBase.__init__(self, *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:
            harmony = settings.REACT_HARMONY
        except KeyError:
            harmony = False

        try:
            strip_types = settings.REACT_STRIP_TYPES
        except KeyError:
            strip_types = False

        try:
            return self.transformer.transform(
                infile,
                outfile,
                harmony=harmony,
                strip_types=strip_types,
            )
        except TransformError as e:
            raise CompilerError(str(e))
Exemplo n.º 11
0
class ReactHandler(StaticFileHandler):
    def initialize(self, path):
        self.root = os.path.abspath(path) + os.path.sep
        self.jsx  = JSXTransformer()
        self.settings.setdefault('compiled_template_cache', False)

    def try_transform(self, abspath):
        try:
            self.jsx.transform(abspath + "x", js_path=abspath)
        except Exception as e:
            raise HTTPError(500, "Could not transform %s into a JS file. %s" % (os.path.basename(abspath + "x"), str(e)))

    def get(self, path, **kwargs):
        """
        Transform the JSX online as they change.
        """
        if os.path.sep != "/":
            path = path.replace("/", os.path.sep)
        abspath = os.path.abspath(os.path.join(self.root, path))
        self.absolute_path = abspath
        # some default errors:
        if not (abspath + os.path.sep).startswith(self.root):
            raise HTTPError(403, "%s is not in root static directory", path)
        if not os.path.exists(abspath):
            # special case, check if jsx file exists.
            if abspath.endswith(".js") and os.path.exists(abspath + "x"):
                self.try_transform(abspath)
                # transformation to JS was successful.
            else:
                raise HTTPError(404)
        if not os.path.isfile(abspath):
            raise HTTPError(403, "%s is not a file", path)

        if abspath.endswith(".js") and os.path.exists(abspath + "x"):
            if os.path.getmtime(abspath + "x") > os.path.getmtime(abspath):
                # more recent JSX file than JS
                self.try_transform(abspath)
            # else the generated file is recent enough
        template_path = self.get_template_path()
        if not template_path:
            template_path = os.path.dirname(os.path.abspath(__file__))
        loader = RequestHandler._template_loaders[template_path]
        return super(ReactHandler, self).get(path, **kwargs)

    @classmethod
    def _get_cached_version(cls, abs_path):
        return None
Exemplo n.º 12
0
class JSXCompiler(CompilerBase):
    output_extension = 'js'

    def __init__(self, *args, **kwargs):
        CompilerBase.__init__(self, *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(e.message)
Exemplo n.º 13
0
class JSXCompiler(CompilerBase):
    output_extension = ".js"

    def __init__(self):
        CompilerBase.__init__(self)
        self.transformer = JSXTransformer()

    def match_file(self, filename):
        return path.endswith(".jsx")

    def compile(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(e.message)
Exemplo n.º 14
0
class JSXCompiler(CompilerBase):
    output_extension = 'js'

    def __init__(self, *args, **kwargs):
        CompilerBase.__init__(self, *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))
Exemplo n.º 15
0
class JSXCompiler(CompilerBase):

    output_extension = 'js'

    def __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 outdated or force:
            try:
                return self.transformer.transform(infile, outfile)
            except TransformError as e:
                raise CompilerError(str(e))
Exemplo n.º 16
0
 def input(self, _in, out, **kw):
     content = _in.read()
     transformer = JSXTransformer()
     js = transformer.transform_string(content)
     out.write(js)
Exemplo n.º 17
0
 def __init__(self, *args, **kwargs):
     CompilerBase.__init__(self, *args, **kwargs)
     self.transformer = JSXTransformer()
Exemplo n.º 18
0
 def __init__(self, content, attrs=None, filter_type=None, charset=None,
              filename=None):
     super(JSXCompiler, self).__init__(content, filter_type, filename)
     self.transformer = JSXTransformer()
Exemplo n.º 19
0
 def __init__(self, *args, **kwargs):
     super(JSXCompiler, self).__init__(*args, **kwargs)
     self.transformer = JSXTransformer()
Exemplo n.º 20
0
 def initialize(self, path):
     self.root = os.path.abspath(path) + os.path.sep
     self.jsx = JSXTransformer()
     self.settings.setdefault('compiled_template_cache', False)
Exemplo n.º 21
0
 def __init__(self):
     CompilerBase.__init__(self)
     self.transformer = JSXTransformer()
Exemplo n.º 22
0
 def initialize(self, path):
     self.root = os.path.abspath(path) + os.path.sep
     self.jsx  = JSXTransformer()
     self.settings.setdefault('compiled_template_cache', False)
Exemplo n.º 23
0
 def __init__(self):
     CompilerBase.__init__(self)
     self.transformer = JSXTransformer()
Exemplo n.º 24
0
 def __init__(self, *args, **kwargs):
     CompilerBase.__init__(self, *args, **kwargs)
     self.transformer = JSXTransformer()
Exemplo n.º 25
0
 def input(self, _in, out, **kw):
     content = _in.read()
     transformer = JSXTransformer()
     js = transformer.transform_string(content)
     out.write(js)