Ejemplo n.º 1
0
 def inject(self, html, resources, context):
     if self.tag is None:
         tag = context.get('context')
     else:
         tag = self.tag
     return html.replace(
         compat.as_bytestring(tag),
         compat.as_bytestring(tag + '\n'+ resources))
Ejemplo n.º 2
0
 def __call__(self, html, needed, request=None, response=None):
     # seperate inclusions in top and bottom inclusions if this is needed
     top, bottom = self.group(needed)
     if top:
         html = html.replace(
             compat.as_bytestring('</head>'),
             compat.as_bytestring('%s</head>' % top.render()), 1)
     if bottom:
         html = html.replace(
             compat.as_bytestring('</body>'),
             compat.as_bytestring('%s</body>' % bottom.render()), 1)
     return html
Ejemplo n.º 3
0
 def __call__(self, html, needed, request=None, response=None):
     # seperate inclusions in top and bottom inclusions if this is needed
     top, bottom = self.group(needed)
     if top:
         html = html.replace(
             compat.as_bytestring('</head>'),
             compat.as_bytestring('%s</head>' % top.render()), 1)
     if bottom:
         html = html.replace(
             compat.as_bytestring('</body>'),
             compat.as_bytestring('%s</body>' % bottom.render()), 1)
     return html
    def __call__(self, html, needed, request=None, response=None):
        # seperate inclusions in top and bottom inclusions if this is needed
        top, bottom = self.group(needed)
        if top:
            html = _head_regex.sub('\\1\n    %s\n' % (top.render()), html.decode(), count=1)
#             html = html.replace(
#                 compat.as_bytestring('</head>'),
#                 compat.as_bytestring('%s</head>' % top.render()), 1)
        if bottom:
            html = html.replace(
                compat.as_bytestring('</body>'),
                compat.as_bytestring('%s</body>' % bottom.render()), 1)
        return html
Ejemplo n.º 5
0
def test_injector_based_on_injectorplugin():
    foo = Library('foo', '')
    a = Resource(foo, 'a.css')
    b = Resource(foo, 'b.css', bottom=True)
    needed = init_needed(resources=[a, b])

    inj = TopInjector({})

    html = b'<html><head></head><body></body></html>'

    assert inj(html, needed) == \
        compat.as_bytestring('''<html><head><link rel="stylesheet" type="text/css" href="/fanstatic/foo/a.css" />
<link rel="stylesheet" type="text/css" href="/fanstatic/foo/b.css" /></head><body></body></html>''')
Ejemplo n.º 6
0
def test_injector_based_on_injectorplugin():
    foo = Library('foo', '')
    a = Resource(foo, 'a.css')
    b = Resource(foo, 'b.css', bottom=True)
    needed = NeededResources(resources=[a,b])

    inj = TopInjector({})

    html = b'<html><head></head><body></body></html>'

    assert inj(html, needed) == \
        compat.as_bytestring('''<html><head><link rel="stylesheet" type="text/css" href="/fanstatic/foo/a.css" />
<link rel="stylesheet" type="text/css" href="/fanstatic/foo/b.css" /></head><body></body></html>''')
Ejemplo n.º 7
0
def test_custom_sdist_command_runs_compiler_beforehand(tmpdir, monkeypatch):
    pkgdir = _copy_testdata(tmpdir)
    monkeypatch.chdir(pkgdir)
    p = subprocess.Popen(
        [sys.executable, 'setup.py', 'sdist', '--formats', 'zip'],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE)
    stdout, _ = p.communicate()
    p.wait()
    assert compat.as_bytestring(
        'hard linking src/somepackage/resources/style.min.css') in stdout
    dist = ZipFile(str(pkgdir / 'dist' / 'somepackage-1.0dev.zip'))
    assert (
        'somepackage-1.0dev/src/somepackage/resources/style.min.css'
        in dist.namelist())
Ejemplo n.º 8
0
def md5(path):
    chcksm = hashlib.md5()
    for path in sorted(list(list_directory(path, include_directories=False))):
        chcksm.update(compat.as_bytestring(path))
        try:
            f = open(path, 'rb')
            while True:
                # 256kb chunks.
                # XXX how to optimize chunk size?
                chunk = f.read(0x40000)
                if not chunk:
                    break
                chcksm.update(chunk)
        finally:
            f.close()
    return chcksm.hexdigest()
Ejemplo n.º 9
0
def test_converts_placeholders_to_arguments(tmpdir):
    from fanstatic.compiler import SOURCE, TARGET

    source = str(tmpdir / 'source')
    with open(source, 'w') as f:
        f.write('source')
    target = str(tmpdir / 'target')
    with open(target, 'w') as f:
        f.write('target')

    class Echo(fanstatic.compiler.CommandlineBase):
        command = 'echo'
        arguments = ['-n', SOURCE, TARGET]

        def process(self, source, target):
            p = super(Echo, self).process(source, target)
            return p.stdout.read()

    assert Echo().process(source, target) == compat.as_bytestring(
        '%s %s' % (source, target))
def test_converts_placeholders_to_arguments(tmpdir):
    from fanstatic.compiler import SOURCE, TARGET

    source = str(tmpdir / 'source')
    with open(source, 'w') as f:
        f.write('source')
    target = str(tmpdir / 'target')
    with open(target, 'w') as f:
        f.write('target')

    class Echo(fanstatic.compiler.CommandlineBase):
        command = 'echo'
        arguments = ['-n', SOURCE, TARGET]

        def process(self, source, target):
            p = super(Echo, self).process(source, target)
            return p.stdout.read()

    # `-n` removes trailing newlines and is therefore not shown.
    assert Echo().process(source, target) == compat.as_bytestring(
        '%s %s' % (source, target))
Ejemplo n.º 11
0
 def process(self, source, target):
     with open(target, 'wb') as output:
         output.write(compat.as_bytestring(
             open(source, 'r').read().replace(' ', '')))
Ejemplo n.º 12
0
 def process(self, source, target):
     with open(target, 'wb') as output:
         output.write(compat.as_bytestring('dummy'))
Ejemplo n.º 13
0
 def process(self, source, target):
     jsmin = self._import()
     with open(target, 'wb') as output:
         js = open(source, 'r').read()
         output.write(compat.as_bytestring(jsmin.jsmin(js)))
Ejemplo n.º 14
0
 def process(self, source, target):
     jsmin = self._import()
     with open(target, 'wb') as output:
         js = open(source, 'r').read()
         output.write(compat.as_bytestring(jsmin.jsmin(js)))
Ejemplo n.º 15
0
 def __call__(self, html, needed):
     needed_html = self.make_inclusion(needed).render()
     return html.replace(
         compat.as_bytestring('<head>'),
         compat.as_bytestring('<head>%s' % needed_html), 1)
Ejemplo n.º 16
0
 def process(self, source, target):
     with open(target, 'wb') as output:
         output.write(compat.as_bytestring('dummy'))
Ejemplo n.º 17
0
 def __call__(self, html, needed):
     needed_html = self.make_inclusion(needed).render()
     return html.replace(compat.as_bytestring('<head>'),
                         compat.as_bytestring('<head>%s' % needed_html), 1)
 def process(self, source, target):
     with open(target, 'wb') as output:
         output.write(
             compat.as_bytestring(
                 open(source, 'r').read().replace(' ', '')))