def test__wrap(self):
        source = "test"
        wrap = "test_wrap({0})"
        expected = "test_wrap(test)"
        ep = EP(wrap=wrap)

        wrapped = ep._wrap(source)

        assert_equal(expected, wrapped)
    def test__namespace(self):
        source = "test"
        namespace = "TEMPLATES"
        name = "app_template"
        expected = 'TEMPLATES["app_template"] = test;'
        ep = EP(namespace=namespace)

        wrapped = ep._namespace(name, source)

        assert_equal(expected, wrapped)
    def test_compile_disable_wrap_and_namespace(self):
        source = "{{test}}"
        expected_re = r"^function .+}$"
        ep = EP(wrap=False, namespace=False)

        compiled = ep.compile(None, source)

        matches = re.match(expected_re, compiled, flags=re.DOTALL)

        msg = "{0} doesn't match RE {1}".format(compiled, expected_re)
        assert_not_equal(matches, None, msg)
    def test_compile_with_wrap_and_namespace(self):
        source = "{{test}}"
        wrap = "template({0})"
        namespace = "T"
        name = "t"
        expected_re = r'^T\["t"\] = template\(function .+\);$'
        ep = EP(wrap=wrap, namespace=namespace)

        compiled = ep.compile(name, source)

        matches = re.match(expected_re, compiled, flags=re.DOTALL)

        msg = "{0} doesn't match RE {1}".format(compiled, expected_re)
        assert_not_equal(matches, None, msg)
class EmberHandlebarsCompiler(FilterBase):
    """EmberHandlebarsCompiler

    wrap: if specified use as wrapper
        false to disable wrapping

    namespace: if specified use as namespace
        false to disable namespacing
    """

    wrap = None
    namespace = None
    parent_dir = 'templates'
    extensions = ['.hbs', '.handlebars']

    def __init__(self, content, attrs, **kwargs):
        super(EmberHandlebarsCompiler, self).__init__(content, **kwargs)
        self.ep = EmberPrecompiler(wrap=self.wrap, namespace=self.namespace)
        self.attrs = attrs

    def strip_extensions(self, basename):
        for ext in self.extensions:
            if basename.endswith(ext):
                return basename[:-len(ext)]
        return basename

    def strip_parent_dir(self, basename):
        basename = basename.lstrip('/')
        segments = basename.split('/')

        for segment in segments:
            if segment == self.parent_dir:
                idx = segments.index(segment)
                return '/'.join(segments[idx+1:])

        return basename

    def name_from_basename(self, basename):
        basename = self.strip_parent_dir(basename)
        basename = self.strip_extensions(basename)
        return basename

    def determine_name(self, **kwargs):
        name = self.attrs.get('data-template-name', None)

        if not name and 'basename' in kwargs:
            name = self.name_from_basename(kwargs['basename'])

        if not name:
            raise FilterError('template name could not be determined')

        return name

    def input(self, **kwargs):
        name = self.determine_name(**kwargs)
        try:
            return self.ep.compile(name, self.content)
        except CompilerError as ex:
            raise FilterError(ex)
 def __init__(self, content, attrs, **kwargs):
     super(EmberHandlebarsCompiler, self).__init__(content, **kwargs)
     self.ep = EmberPrecompiler(wrap=self.wrap, namespace=self.namespace)
     self.attrs = attrs
    def test__compile(self):
        source = ""
        ep = EP()

        compiled = ep._compile(source)
        assert_true(compiled.startswith("function"))