Python django.contrib.staticfiles.finders.get_finders() Examples

The following are 9 code examples of django.contrib.staticfiles.finders.get_finders(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.contrib.staticfiles.finders , or try the search function .
Example #1
Source File: checks.py    From bioforum with MIT License 5 votes vote down vote up
def check_finders(app_configs=None, **kwargs):
    """Check all registered staticfiles finders."""
    errors = []
    for finder in get_finders():
        try:
            finder_errors = finder.check()
        except NotImplementedError:
            pass
        else:
            errors.extend(finder_errors)
    return errors 
Example #2
Source File: checks.py    From Hands-On-Application-Development-with-PyCharm with MIT License 5 votes vote down vote up
def check_finders(app_configs=None, **kwargs):
    """Check all registered staticfiles finders."""
    errors = []
    for finder in get_finders():
        try:
            finder_errors = finder.check()
        except NotImplementedError:
            pass
        else:
            errors.extend(finder_errors)
    return errors 
Example #3
Source File: utils.py    From django-pyscss with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def find_all_files(glob):
    """
    Finds all files in the django finders for a given glob,
    returns the file path, if available, and the django storage object.
    storage objects must implement the File storage API:
    https://docs.djangoproject.com/en/dev/ref/files/storage/
    """
    for finder in finders.get_finders():
        for path, storage in finder.list([]):
            if fnmatch.fnmatchcase(os.path.join(getattr(storage, 'prefix', '')
                                                or '', path),
                                   glob):
                yield path, storage 
Example #4
Source File: storage.py    From Kiwi with GNU General Public License v2.0 5 votes vote down vote up
def find_files():
    # copied from django.contrib.staticfiles.management.commands.collectstatic
    found_files = OrderedDict()
    for finder in get_finders():
        for path, storage in finder.list([]):
            path = path.replace('\\', '/')
            if path not in found_files:
                found_files[path] = (storage, path)

    return found_files 
Example #5
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_override_staticfiles_finders(self):
        """
        Overriding the STATICFILES_FINDERS setting should be reflected in
        the return value of django.contrib.staticfiles.finders.get_finders.
        """
        current = get_finders()
        self.assertGreater(len(list(current)), 1)
        finders = ['django.contrib.staticfiles.finders.FileSystemFinder']
        with self.settings(STATICFILES_FINDERS=finders):
            self.assertEqual(len(list(get_finders())), len(finders)) 
Example #6
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_override_staticfiles_finders(self):
        """
        Overriding the STATICFILES_FINDERS setting should be reflected in
        the return value of django.contrib.staticfiles.finders.get_finders.
        """
        current = get_finders()
        self.assertGreater(len(list(current)), 1)
        finders = ['django.contrib.staticfiles.finders.FileSystemFinder']
        with self.settings(STATICFILES_FINDERS=finders):
            self.assertEqual(len(list(get_finders())), len(finders)) 
Example #7
Source File: collectstatic.py    From GTDWeb with GNU General Public License v2.0 4 votes vote down vote up
def collect(self):
        """
        Perform the bulk of the work of collectstatic.

        Split off from handle() to facilitate testing.
        """
        if self.symlink and not self.local:
            raise CommandError("Can't symlink to a remote destination.")

        if self.clear:
            self.clear_dir('')

        if self.symlink:
            handler = self.link_file
        else:
            handler = self.copy_file

        found_files = OrderedDict()
        for finder in get_finders():
            for path, storage in finder.list(self.ignore_patterns):
                # Prefix the relative path if the source storage contains it
                if getattr(storage, 'prefix', None):
                    prefixed_path = os.path.join(storage.prefix, path)
                else:
                    prefixed_path = path

                if prefixed_path not in found_files:
                    found_files[prefixed_path] = (storage, path)
                    handler(path, prefixed_path, storage)

        # Here we check if the storage backend has a post_process
        # method and pass it the list of modified files.
        if self.post_process and hasattr(self.storage, 'post_process'):
            processor = self.storage.post_process(found_files,
                                                  dry_run=self.dry_run)
            for original_path, processed_path, processed in processor:
                if isinstance(processed, Exception):
                    self.stderr.write("Post-processing '%s' failed!" % original_path)
                    # Add a blank line before the traceback, otherwise it's
                    # too easy to miss the relevant part of the error message.
                    self.stderr.write("")
                    raise processed
                if processed:
                    self.log("Post-processed '%s' as '%s'" %
                             (original_path, processed_path), level=1)
                    self.post_processed_files.append(original_path)
                else:
                    self.log("Skipped post-processing '%s'" % original_path)

        return {
            'modified': self.copied_files + self.symlinked_files,
            'unmodified': self.unmodified_files,
            'post_processed': self.post_processed_files,
        } 
Example #8
Source File: collectstatic.py    From Hands-On-Application-Development-with-PyCharm with MIT License 4 votes vote down vote up
def collect(self):
        """
        Perform the bulk of the work of collectstatic.

        Split off from handle() to facilitate testing.
        """
        if self.symlink and not self.local:
            raise CommandError("Can't symlink to a remote destination.")

        if self.clear:
            self.clear_dir('')

        if self.symlink:
            handler = self.link_file
        else:
            handler = self.copy_file

        found_files = OrderedDict()
        for finder in get_finders():
            for path, storage in finder.list(self.ignore_patterns):
                # Prefix the relative path if the source storage contains it
                if getattr(storage, 'prefix', None):
                    prefixed_path = os.path.join(storage.prefix, path)
                else:
                    prefixed_path = path

                if prefixed_path not in found_files:
                    found_files[prefixed_path] = (storage, path)
                    handler(path, prefixed_path, storage)
                else:
                    self.log(
                        "Found another file with the destination path '%s'. It "
                        "will be ignored since only the first encountered file "
                        "is collected. If this is not what you want, make sure "
                        "every static file has a unique path." % prefixed_path,
                        level=1,
                    )

        # Storage backends may define a post_process() method.
        if self.post_process and hasattr(self.storage, 'post_process'):
            processor = self.storage.post_process(found_files,
                                                  dry_run=self.dry_run)
            for original_path, processed_path, processed in processor:
                if isinstance(processed, Exception):
                    self.stderr.write("Post-processing '%s' failed!" % original_path)
                    # Add a blank line before the traceback, otherwise it's
                    # too easy to miss the relevant part of the error message.
                    self.stderr.write("")
                    raise processed
                if processed:
                    self.log("Post-processed '%s' as '%s'" %
                             (original_path, processed_path), level=2)
                    self.post_processed_files.append(original_path)
                else:
                    self.log("Skipped post-processing '%s'" % original_path)

        return {
            'modified': self.copied_files + self.symlinked_files,
            'unmodified': self.unmodified_files,
            'post_processed': self.post_processed_files,
        } 
Example #9
Source File: sync_page_themes.py    From django-leonardo with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def collect(self):
        """
        Load and save ``PageColorScheme`` for every ``PageTheme``

        .. code-block:: bash

            static/themes/bootswatch/united/variables.scss
            static/themes/bootswatch/united/styles.scss

        """

        self.ignore_patterns = [
            '*.png', '*.jpg', '*.js', '*.gif', '*.ttf', '*.md', '*.rst',
            '*.svg']
        page_themes = PageTheme.objects.all()

        for finder in get_finders():
            for path, storage in finder.list(self.ignore_patterns):
                for t in page_themes:
                    static_path = 'themes/{0}'.format(t.name.split('/')[-1])
                    if static_path in path:
                        try:
                            page_theme = PageTheme.objects.get(id=t.id)
                        except PageTheme.DoesNotExist:
                            raise Exception(
                                "Run sync_themes before this command")
                        except Exception as e:
                            self.stdout.write(
                                "Cannot load {} into database original error: {}".format(t, e))

                        # find and load skins
                        skins_path = os.path.join(
                            storage.path('/'.join(path.split('/')[0:-1])))
                        for dirpath, skins, filenames in os.walk(skins_path):
                            for skin in [s for s in skins if s not in ['fonts']]:
                                for skin_dirpath, skins, filenames in os.walk(os.path.join(dirpath, skin)):
                                    skin, created = PageColorScheme.objects.get_or_create(
                                        theme=page_theme, label=skin, name=skin.title())
                                    for f in filenames:
                                        if 'styles' in f:
                                            with codecs.open(os.path.join(skin_dirpath, f)) as style_file:
                                                skin.styles = style_file.read()
                                        elif 'variables' in f:
                                            with codecs.open(os.path.join(skin_dirpath, f)) as variables_file:
                                                skin.variables = variables_file.read()
                                    skin.save()
                                    self.skins_updated += 1

        self.page_themes_updated += len(page_themes)