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.is_mutated:
                logger.error(
                    '"%s" does not appear to have been mutated yet.' % path
                )
                return None

            return mutant.mutated_data()
        else:
            mutant.mutate()
            return mutant.mutated_data()
Esempio n. 2
0
def assetmutator_source(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 = get_current_request()
    settings = request.registry.settings
    
    mutant = Mutator(settings, path, **kw)
    
    if not 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.process()
            return mutant.mutated_data()
    def test_mutator_source_not_found(self):
        self.settings['assetmutator.remutate_check'] = 'exists'

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

        self.assertRaises(RuntimeError, mutant.mutated_data)

        if sys.version_info[:2] > (2, 6):
            with self.assertRaises(RuntimeError) as exc:
                mutant.mutated_data()
            self.assertEqual('%s' % exc.exception,
                             'Source not found. Has it been mutated?')
    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)
Esempio n. 5
0
    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)
Esempio n. 6
0
    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)