Skip to content

Commit 6a85e11

Browse files
committed
new django example code
1 parent 37b15c4 commit 6a85e11

14 files changed

+1561
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
title: django.contrib.staticfiles.finders BaseFinder Example Code
2+
category: page
3+
slug: django-contrib-staticfiles-finders-basefinder-examples
4+
sortorder: 500011067
5+
toc: False
6+
sidebartitle: django.contrib.staticfiles.finders BaseFinder
7+
meta: Python example code for the BaseFinder class from the django.contrib.staticfiles.finders module of the Django project.
8+
9+
10+
BaseFinder is a class within the django.contrib.staticfiles.finders module of the Django project.
11+
12+
13+
## Example 1 from django-pipeline
14+
[django-pipeline](https://github.com/jazzband/django-pipeline)
15+
([project documentation](https://django-pipeline.readthedocs.io/en/latest/)
16+
and
17+
[PyPI package information](https://pypi.org/project/django-pipeline/))
18+
is a code library for handling and compressing
19+
[static content assets](/static-content.html) when handling requests in
20+
[Django](/django.html) web applications.
21+
22+
The django-pipeline project is open sourced under the
23+
[MIT License](https://github.com/jazzband/django-pipeline/blob/master/LICENSE.txt)
24+
and it is maintained by the developer community group
25+
[Jazzband](https://jazzband.co/).
26+
27+
[**django-pipeline / pipeline / finders.py**](https://github.com/jazzband/django-pipeline/blob/master/pipeline/./finders.py)
28+
29+
```python
30+
# finders.py
31+
from itertools import chain
32+
33+
from django.contrib.staticfiles.storage import staticfiles_storage
34+
~~from django.contrib.staticfiles.finders import BaseFinder, BaseStorageFinder, find, \
35+
AppDirectoriesFinder as DjangoAppDirectoriesFinder, FileSystemFinder as DjangoFileSystemFinder
36+
from django.utils._os import safe_join
37+
from os.path import normpath
38+
39+
from pipeline.conf import settings
40+
41+
42+
class PipelineFinder(BaseStorageFinder):
43+
storage = staticfiles_storage
44+
45+
def find(self, path, all=False):
46+
if not settings.PIPELINE_ENABLED:
47+
return super(PipelineFinder, self).find(path, all)
48+
else:
49+
return []
50+
51+
def list(self, ignore_patterns):
52+
return []
53+
54+
55+
~~class ManifestFinder(BaseFinder):
56+
def find(self, path, all=False):
57+
matches = []
58+
for elem in chain(settings.STYLESHEETS.values(), settings.JAVASCRIPT.values()):
59+
if normpath(elem['output_filename']) == normpath(path):
60+
match = safe_join(settings.PIPELINE_ROOT, path)
61+
if not all:
62+
return match
63+
matches.append(match)
64+
return matches
65+
66+
def list(self, *args):
67+
return []
68+
69+
70+
~~class CachedFileFinder(BaseFinder):
71+
def find(self, path, all=False):
72+
try:
73+
start, _, extn = path.rsplit('.', 2)
74+
except ValueError:
75+
return []
76+
path = '.'.join((start, extn))
77+
return find(path, all=all) or []
78+
79+
def list(self, *args):
80+
return []
81+
82+
83+
class PatternFilterMixin(object):
84+
ignore_patterns = []
85+
86+
def get_ignored_patterns(self):
87+
return list(set(self.ignore_patterns))
88+
89+
def list(self, ignore_patterns):
90+
if ignore_patterns:
91+
ignore_patterns = ignore_patterns + self.get_ignored_patterns()
92+
return super(PatternFilterMixin, self).list(ignore_patterns)
93+
94+
95+
96+
97+
## ... source file continues with no further BaseFinder examples...
98+
99+
```
100+
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
title: django.contrib.staticfiles.finders BaseStorageFinder Example Code
2+
category: page
3+
slug: django-contrib-staticfiles-finders-basestoragefinder-examples
4+
sortorder: 500011068
5+
toc: False
6+
sidebartitle: django.contrib.staticfiles.finders BaseStorageFinder
7+
meta: Python example code for the BaseStorageFinder class from the django.contrib.staticfiles.finders module of the Django project.
8+
9+
10+
BaseStorageFinder is a class within the django.contrib.staticfiles.finders module of the Django project.
11+
12+
13+
## Example 1 from django-pipeline
14+
[django-pipeline](https://github.com/jazzband/django-pipeline)
15+
([project documentation](https://django-pipeline.readthedocs.io/en/latest/)
16+
and
17+
[PyPI package information](https://pypi.org/project/django-pipeline/))
18+
is a code library for handling and compressing
19+
[static content assets](/static-content.html) when handling requests in
20+
[Django](/django.html) web applications.
21+
22+
The django-pipeline project is open sourced under the
23+
[MIT License](https://github.com/jazzband/django-pipeline/blob/master/LICENSE.txt)
24+
and it is maintained by the developer community group
25+
[Jazzband](https://jazzband.co/).
26+
27+
[**django-pipeline / pipeline / finders.py**](https://github.com/jazzband/django-pipeline/blob/master/pipeline/./finders.py)
28+
29+
```python
30+
# finders.py
31+
from itertools import chain
32+
33+
from django.contrib.staticfiles.storage import staticfiles_storage
34+
~~from django.contrib.staticfiles.finders import BaseFinder, BaseStorageFinder, find, \
35+
AppDirectoriesFinder as DjangoAppDirectoriesFinder, FileSystemFinder as DjangoFileSystemFinder
36+
from django.utils._os import safe_join
37+
from os.path import normpath
38+
39+
from pipeline.conf import settings
40+
41+
42+
~~class PipelineFinder(BaseStorageFinder):
43+
storage = staticfiles_storage
44+
45+
def find(self, path, all=False):
46+
if not settings.PIPELINE_ENABLED:
47+
return super(PipelineFinder, self).find(path, all)
48+
else:
49+
return []
50+
51+
def list(self, ignore_patterns):
52+
return []
53+
54+
55+
class ManifestFinder(BaseFinder):
56+
def find(self, path, all=False):
57+
matches = []
58+
for elem in chain(settings.STYLESHEETS.values(), settings.JAVASCRIPT.values()):
59+
if normpath(elem['output_filename']) == normpath(path):
60+
match = safe_join(settings.PIPELINE_ROOT, path)
61+
if not all:
62+
return match
63+
matches.append(match)
64+
return matches
65+
66+
def list(self, *args):
67+
68+
69+
## ... source file continues with no further BaseStorageFinder examples...
70+
71+
```
72+
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
title: django.contrib.staticfiles.finders find Example Code
2+
category: page
3+
slug: django-contrib-staticfiles-finders-find-examples
4+
sortorder: 500011069
5+
toc: False
6+
sidebartitle: django.contrib.staticfiles.finders find
7+
meta: Python example code for the find callable from the django.contrib.staticfiles.finders module of the Django project.
8+
9+
10+
find is a callable within the django.contrib.staticfiles.finders module of the Django project.
11+
12+
13+
## Example 1 from django-pipeline
14+
[django-pipeline](https://github.com/jazzband/django-pipeline)
15+
([project documentation](https://django-pipeline.readthedocs.io/en/latest/)
16+
and
17+
[PyPI package information](https://pypi.org/project/django-pipeline/))
18+
is a code library for handling and compressing
19+
[static content assets](/static-content.html) when handling requests in
20+
[Django](/django.html) web applications.
21+
22+
The django-pipeline project is open sourced under the
23+
[MIT License](https://github.com/jazzband/django-pipeline/blob/master/LICENSE.txt)
24+
and it is maintained by the developer community group
25+
[Jazzband](https://jazzband.co/).
26+
27+
[**django-pipeline / pipeline / finders.py**](https://github.com/jazzband/django-pipeline/blob/master/pipeline/./finders.py)
28+
29+
```python
30+
# finders.py
31+
from itertools import chain
32+
33+
from django.contrib.staticfiles.storage import staticfiles_storage
34+
~~from django.contrib.staticfiles.finders import BaseFinder, BaseStorageFinder, find, \
35+
AppDirectoriesFinder as DjangoAppDirectoriesFinder, FileSystemFinder as DjangoFileSystemFinder
36+
from django.utils._os import safe_join
37+
from os.path import normpath
38+
39+
from pipeline.conf import settings
40+
41+
42+
class PipelineFinder(BaseStorageFinder):
43+
storage = staticfiles_storage
44+
45+
~~ def find(self, path, all=False):
46+
if not settings.PIPELINE_ENABLED:
47+
return super(PipelineFinder, self).find(path, all)
48+
else:
49+
return []
50+
51+
def list(self, ignore_patterns):
52+
return []
53+
54+
55+
class ManifestFinder(BaseFinder):
56+
~~ def find(self, path, all=False):
57+
matches = []
58+
for elem in chain(settings.STYLESHEETS.values(), settings.JAVASCRIPT.values()):
59+
if normpath(elem['output_filename']) == normpath(path):
60+
match = safe_join(settings.PIPELINE_ROOT, path)
61+
if not all:
62+
return match
63+
matches.append(match)
64+
return matches
65+
66+
def list(self, *args):
67+
return []
68+
69+
70+
class CachedFileFinder(BaseFinder):
71+
~~ def find(self, path, all=False):
72+
try:
73+
start, _, extn = path.rsplit('.', 2)
74+
except ValueError:
75+
return []
76+
path = '.'.join((start, extn))
77+
~~ return find(path, all=all) or []
78+
79+
def list(self, *args):
80+
return []
81+
82+
83+
class PatternFilterMixin(object):
84+
ignore_patterns = []
85+
86+
def get_ignored_patterns(self):
87+
return list(set(self.ignore_patterns))
88+
89+
def list(self, ignore_patterns):
90+
if ignore_patterns:
91+
ignore_patterns = ignore_patterns + self.get_ignored_patterns()
92+
return super(PatternFilterMixin, self).list(ignore_patterns)
93+
94+
95+
class AppDirectoriesFinder(PatternFilterMixin, DjangoAppDirectoriesFinder):
96+
ignore_patterns = [
97+
'*.js',
98+
'*.css',
99+
'*.less',
100+
'*.scss',
101+
'*.styl',
102+
103+
104+
## ... source file continues with no further find examples...
105+
106+
```
107+
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
title: django.contrib.staticfiles.finders get_finders Example Code
2+
category: page
3+
slug: django-contrib-staticfiles-finders-get-finders-examples
4+
sortorder: 500011070
5+
toc: False
6+
sidebartitle: django.contrib.staticfiles.finders get_finders
7+
meta: Python example code for the get_finders callable from the django.contrib.staticfiles.finders module of the Django project.
8+
9+
10+
get_finders is a callable within the django.contrib.staticfiles.finders module of the Django project.
11+
12+
13+
## Example 1 from django-pipeline
14+
[django-pipeline](https://github.com/jazzband/django-pipeline)
15+
([project documentation](https://django-pipeline.readthedocs.io/en/latest/)
16+
and
17+
[PyPI package information](https://pypi.org/project/django-pipeline/))
18+
is a code library for handling and compressing
19+
[static content assets](/static-content.html) when handling requests in
20+
[Django](/django.html) web applications.
21+
22+
The django-pipeline project is open sourced under the
23+
[MIT License](https://github.com/jazzband/django-pipeline/blob/master/LICENSE.txt)
24+
and it is maintained by the developer community group
25+
[Jazzband](https://jazzband.co/).
26+
27+
[**django-pipeline / pipeline / manifest.py**](https://github.com/jazzband/django-pipeline/blob/master/pipeline/./manifest.py)
28+
29+
```python
30+
# manifest.py
31+
import os
32+
33+
from django.conf.settings import settings as django_settings
34+
~~from django.contrib.staticfiles.finders import get_finders
35+
from django.contrib.staticfiles.storage import staticfiles_storage
36+
37+
from pipeline.conf import settings
38+
39+
from manifesto import Manifest
40+
41+
from pipeline.packager import Packager
42+
43+
44+
class PipelineManifest(Manifest):
45+
def __init__(self):
46+
self.packager = Packager()
47+
self.packages = self.collect_packages()
48+
~~ self.finders = get_finders()
49+
self.package_files = []
50+
51+
def collect_packages(self):
52+
packages = []
53+
for package_name in self.packager.packages['css']:
54+
package = self.packager.package_for('css', package_name)
55+
if package.manifest:
56+
packages.append(package)
57+
for package_name in self.packager.packages['js']:
58+
package = self.packager.package_for('js', package_name)
59+
if package.manifest:
60+
packages.append(package)
61+
return packages
62+
63+
def cache(self):
64+
65+
if settings.PIPELINE_ENABLED:
66+
for package in self.packages:
67+
path = package.output_filename
68+
self.package_files.append(path)
69+
yield staticfiles_storage.url(path)
70+
else:
71+
for package in self.packages:
72+
for path in self.packager.compile(package.paths):
73+
74+
75+
## ... source file continues with no further get_finders examples...
76+
77+
```
78+

0 commit comments

Comments
 (0)