def test_mutator_binary_mutator(self):
        self.settings['assetmutator.remutate_check'] = 'exists'
        path = 'pyramid_assetmutator.tests:fixtures/test.json'
        src_fullpath = get_abspath(path)
        mutant = Mutator(self.request, path,
                         mutator=dict(cmd='gzip --stdout', ext='json.gz'))
        mutant.mutate()

        filename = '%s/fixtures/_test.%s.json.gz' % (self.here,
                                                     hexhashify(src_fullpath))
        self.assertTrue(os.path.exists(filename))

        import gzip
        f = gzip.open(filename)
        content = f.read()
        f.close()

        if PY3:
            content = content.decode('utf-8')

        self.assertEqual(
            content,
            '{"spam": "lorem", "eggs": "鸡蛋"}\n'
        )

        os.remove(filename)
    def assetmutator_source(self, path, **kw):
        """
        Returns the source data/contents of the mutated asset (and mutates the
        asset if needed). This is useful when you want to output inline data
        (e.g. for inline JavaScript blocks).

        :param path: The Pyramid asset path to process.
        :type path: string - Required

        :type mutator: dict or string - Optional
        :param mutator: Allows you to override/specify a specific mutator to use
                         (e.g. ``coffee``), or assign a brand new mutator
                         dictionary to be used (e.g. ``{'cmd': 'lessc', 'ext':
                         'css'}``)

        .. note:: Many template packages escape output by default. Consult your
                  template language's syntax to output an unescaped string.
        """
        request = self.request

        mutant = Mutator(request, path, rendering_val=self.rendering_val, **kw)

        if not request.registry.settings['assetmutator.each_request']:
            if not mutant.mutated:
                # TODO: Error?
                return None

            return mutant.mutated_data()
        else:
            if mutant.mutated:
                return mutant.mutated_data()
            else:
                mutant.mutate()
                return mutant.mutated_data()
    def test_mutator_mutated_always_remutate_json(self):
        self.settings['assetmutator.remutate_check'] = 'exists'
        self.settings['assetmutator.mutated_path'] = \
            'pyramid_assetmutator.tests:cache'
        self.settings['assetmutator.always_remutate'] = ['*.json']
        path = 'pyramid_assetmutator.tests:fixtures/test.json'
        src_fullpath = get_abspath(path)
        mutant = Mutator(self.request, path)
        mutant.mutate()

        self.assertEqual(
            mutant.mutated_data(),
            '{"spam": "lorem", "eggs": "鸡蛋"}\n'
        )

        dirname = '%s/cache' % self.here
        filename = '%s/_test.%s.txt' % (dirname, hexhashify(src_fullpath))
        self.assertTrue(os.path.exists(filename))
        stat = get_stat(filename)

        # Pause, remutate, and verify file was changed
        time.sleep(0.1)
        mutant.mutate()
        self.assertNotEqual(stat, get_stat(filename))

        os.remove(filename)
Beispiel #4
0
def applicationcreated_subscriber(event):
    app = event.app
    app.registry.settings['assetmutator.mutators'] = mutators

    if app.registry.settings['assetmutator.mutated_path'] \
       and app.registry.settings['assetmutator.purge_mutated_path']:
        path = get_abspath(app.registry.settings['assetmutator.mutated_path'])

        if os.path.isdir(path):
            for file in os.listdir(path):
                try:
                    file_path = os.path.join(path, file)

                    if os.path.isfile(file_path):
                        os.unlink(file_path)
                except:
                    pass


    if app.registry.settings['assetmutator.each_boot']:
        request = app.request_factory.blank('/')
        asset_paths = app.registry.settings['assetmutator.asset_paths']

        for asset_path in asset_paths:
            mutant = Mutator(request, asset_path, registry=app.registry,
                             batch=True)
            mutant.mutate()
def applicationcreated_subscriber(event):
    app = event.app
    app.registry.settings['assetmutator.mutators'] = mutators

    if app.registry.settings['assetmutator.each_boot']:
        request = app.request_factory.blank('/')
        asset_paths = app.registry.settings['assetmutator.asset_paths']

        for asset_path in asset_paths:
            mutant = Mutator(request, asset_path, registry=app.registry,
                             batch=True)
            mutant.mutate()
    def test_mutator_source_mtime(self):
        mutant = Mutator(self.request,
                         'pyramid_assetmutator.tests:fixtures/test.json')
        mutant.mutate()

        self.assertEqual(
            mutant.mutated_data(),
            '{"spam": "lorem", "eggs": "鸡蛋"}\n'
        )

        mtime = os.path.getmtime('%s/fixtures/test.json' % self.here)
        filename = '%s/fixtures/_test.%s.txt' % (self.here, mtime)
        self.assertTrue(os.path.exists(filename))

        os.remove(filename)
    def assetmutator_url(self, path, **kw):
        """
        Returns a Pyramid :meth:`~pyramid.request.Request.static_url` of the
        mutated asset (and mutates the asset if needed).

        :param path: The Pyramid asset path to process.
        :type path: string - Required

        :type mutator: dict or string - Optional
        :param mutator: Allows you to override/specify a specific mutator to use
                         (e.g. ``coffee``), or assign a brand new mutator
                         dictionary to be used (e.g. ``{'cmd': 'lessc', 'ext':
                         'css'}``)
        """
        request = self.request

        mutant = Mutator(request, path, rendering_val=self.rendering_val, **kw)

        if not request.registry.settings['assetmutator.each_request']:
            if not mutant.mutated:
                # TODO: Error?
                pass

            return request.static_url(mutant.new_path)
        else:
            if mutant.mutated:
                return request.static_url(mutant.new_path)
            else:
                return request.static_url(mutant.mutate())
    def test_mutator_source_specified_mutator(self):
        self.settings['assetmutator.remutate_check'] = 'exists'
        mutant = Mutator(self.request,
                         'pyramid_assetmutator.tests:fixtures/test.json',
                         mutator='json')
        mutant.mutate()

        self.assertEqual(
            mutant.mutated_data(),
            '{"spam": "lorem", "eggs": "鸡蛋"}\n'
        )

        filename = '%s/fixtures/_test.txt' % self.here
        self.assertTrue(os.path.exists(filename))

        os.remove(filename)
    def test_mutator_specified_mutator(self):
        self.settings['assetmutator.remutate_check'] = 'exists'
        path = 'pyramid_assetmutator.tests:fixtures/test.json'
        src_fullpath = get_abspath(path)
        mutant = Mutator(self.request, path, mutator='json')
        mutant.mutate()

        self.assertEqual(
            mutant.mutated_data(),
            '{"spam": "lorem", "eggs": "鸡蛋"}\n'
        )

        filename = '%s/fixtures/_test.%s.txt' % (self.here,
                                                 hexhashify(src_fullpath))
        self.assertTrue(os.path.exists(filename))

        os.remove(filename)
    def test_mutator_source_checksum(self):
        self.settings['assetmutator.remutate_check'] = 'checksum'

        mutant = Mutator(self.request,
                         'pyramid_assetmutator.tests:fixtures/test.json')
        mutant.mutate()

        self.assertEqual(
            mutant.mutated_data(),
            '{"spam": "lorem", "eggs": "鸡蛋"}\n'
        )

        checksum = compute_md5('%s/fixtures/test.json' % self.here)
        filename = '%s/fixtures/_test.%s.txt' % (self.here, checksum)
        self.assertTrue(os.path.exists(filename))

        os.remove(filename)
    def test_mutator_source_stat(self):
        path = 'pyramid_assetmutator.tests:fixtures/test.json'
        src_fullpath = get_abspath(path)
        mutant = Mutator(self.request, path)
        mutant.mutate()

        self.assertEqual(
            mutant.mutated_data(),
            '{"spam": "lorem", "eggs": "鸡蛋"}\n'
        )

        size = str(os.path.getsize('%s/fixtures/test.json' % self.here))
        mtime = str(os.path.getmtime('%s/fixtures/test.json' % self.here))
        fingerprint = hexhashify(src_fullpath) + hexhashify(size + '.' + mtime)
        filename = '%s/fixtures/_test.%s.txt' % (self.here, fingerprint)
        self.assertTrue(os.path.exists(filename))

        os.remove(filename)
    def assetmutator_assetpath(self, path, **kw):
        """
        Returns a Pyramid `asset specification`_ such as
        ``pkg:static/path/to/file.ext`` (and mutates the asset if needed).

        :param path: The Pyramid asset path to process.
        :type path: string - Required

        :type mutator: dict or string - Optional
        :param mutator: Allows you to override/specify a specific mutator to use
                         (e.g. ``coffee``), or assign a brand new mutator
                         dictionary to be used (e.g. ``{'cmd': 'lessc', 'ext':
                         'css'}``)

        This function could be used to nest ``pyramid_assetmutator`` calls. e.g.
        ``assetmutator_path(assetmutator_assetpath('pkg:static/js/script.coffee'))``
        could compile a CoffeeScript file into JS, and then further minify the
        JS file if your mutator configuration looked something like::

            config.assign_assetmutator('coffee', 'coffee -c -p', 'js')
            config.assign_assetmutator('js', 'uglifyjs', 'js')

        .. _asset specification: http://pyramid.readthedocs.org/en/latest/
                                 glossary.html#term-asset-specification
        """
        request = self.request

        mutant = Mutator(request, path, rendering_val=self.rendering_val, **kw)

        if not request.registry.settings['assetmutator.each_request']:
            if not mutant.mutated:
                # TODO: log an error?
                pass

            return mutant.new_path
        else:
            if mutant.mutated:
                return mutant.new_path
            else:
                return mutant.mutate()