fixed static file collection for third party components: now relying on symlinks
[myslice.git] / unfold / static.py
1 import os
2 from django.conf import settings
3 from django.utils.datastructures import SortedDict
4 from django.contrib.staticfiles.finders import BaseFinder, FileSystemFinder
5 from django.core.files.storage import FileSystemStorage
6
7 # DEPRECATED # # http://nicks-liquid-soapbox.blogspot.fr/2011/03/splitting-path-to-list-in-python.html
8 # DEPRECATED # def splitpath(path, maxdepth=20):
9 # DEPRECATED #      ( head, tail ) = os.path.split(path)
10 # DEPRECATED #      return splitpath(head, maxdepth - 1) + [ tail ] \
11 # DEPRECATED #          if maxdepth and head and head != path \
12 # DEPRECATED #          else [ head or tail ]
13
14 # The plugin finder is responsible for collecting JS, CSS and PNG files from
15 # the plugins, which are not declared in the myslice.settings file unlike
16 # applications.
17 class PluginFinder(FileSystemFinder):
18     """
19     A static files finder that looks in the directory of each plugin as
20     specified in the source_dir attribute of the given storage class.
21     """
22     def __init__(self, *args, **kwargs):
23         # The list of plugins that are handled
24         self.locations = []
25         # Mapping of plugin module paths to storage instances
26         self.storages = SortedDict()
27         plugins_dir = self.get_immediate_subdirs(settings.PLUGIN_DIR)
28         for root in plugins_dir:
29             if not os.path.exists(root) or not os.path.isdir(root):
30                 continue
31             if ('', root) not in self.locations:
32                 self.locations.append(('', root))
33         for _, root in self.locations:
34             filesystem_storage = FileSystemStorage(location=root)
35             filesystem_storage.prefix = ''
36             self.storages[root] = filesystem_storage
37
38     def get_immediate_subdirs(self, dir):
39         return [os.path.join(dir, name, 'static') for name in os.listdir(dir) 
40                 if os.path.isdir(os.path.join(dir, name))]
41
42 class ThirdPartyFinder(BaseFinder):
43     """
44     A static files inder that looks in the directory of each third-party
45     resources and tries to preserve the location of each file
46     """
47     # third-party/MODULE/path/to/js
48     extensions = {
49         # PREFIX : EXTENSIONS
50         ''   : ('.html',),
51         'js' : ('.js',),
52         'css': ('.css',),
53         'img': ('.png', '.ico',),
54     }
55
56     def find(self, search_path, all=False):
57         """
58         Given a relative file path this ought to find an
59         absolute file path.
60
61         If the ``all`` parameter is ``False`` (default) only
62         the first found file path will be returned; if set
63         to ``True`` a list of all found files paths is returned.
64         """
65         matches = []
66         #all_extensions = reduce(lambda x,y : x + y, extensions.values())
67
68         for (path, dirs, files) in os.walk(settings.THIRDPARTY_DIR):
69             for file in files:
70                 name, extension = os.path.splitext(file)
71
72                 for type, extensions in self.extensions.items():
73                     if not extension in extensions:
74                         continue
75                     if search_path == os.path.join(type, file):
76                         matched_path = os.path.join(path, file) 
77                         if not all:
78                             return matched_path
79                         matches.append(matched_path)
80         return matches
81
82     def list(self, ignore_patterns):
83         """
84         Given an optional list of paths to ignore, this should return
85         a two item iterable consisting of the relative path and storage
86         instance.
87         """
88
89         for component in os.listdir(settings.THIRDPARTY_DIR):
90             # We are looking forward symlinks only
91             component_path = os.path.join(settings.THIRDPARTY_DIR, component)
92             if not os.path.islink(component_path) or not os.path.isdir(component_path):
93                 continue
94                 
95             for (path, dirs, files) in os.walk(component_path):
96                 for file in files:
97                     name, extension = os.path.splitext(file)
98
99                     for type, extensions in self.extensions.items():
100                         if not extension in extensions:
101                             continue
102                         filesystem_storage = FileSystemStorage(location=path)
103                         filesystem_storage.prefix = type
104                         yield file, filesystem_storage
105             
106 # DEPRECATED #         for (path, dirs, files) in os.walk(settings.THIRDPARTY_DIR):
107 # DEPRECATED #             component_path = path[len(settings.THIRDPARTY_DIR)+1:]
108 # DEPRECATED #             component_name = splitpath(component_path)[0]
109 # DEPRECATED #             print "PATH", path, " -- COMPONENT", component_name
110 # DEPRECATED #             if not os.path.islink(os.path.join(path, component_name)):
111 # DEPRECATED #                 print "IGNORED", component_name
112 # DEPRECATED #                 continue
113 # DEPRECATED #             print "COMPONENT ADDED: ", component_name
114 # DEPRECATED #             print "=="
115 # DEPRECATED # 
116 # DEPRECATED # 
117 # DEPRECATED #             for file in files:
118 # DEPRECATED #                 name, extension = os.path.splitext(file)
119 # DEPRECATED # 
120 # DEPRECATED #                 for type, extensions in self.extensions.items():
121 # DEPRECATED #                     if not extension in extensions:
122 # DEPRECATED #                         continue
123 # DEPRECATED #                     filesystem_storage = FileSystemStorage(location=path)
124 # DEPRECATED #                     filesystem_storage.prefix = type
125 # DEPRECATED #                     yield file, filesystem_storage
126
127 # XXX I commented this since it should be imported from django.contrib.staticfiles.finders -- jordan
128  
129 #class BaseFinder(object):
130 #    """
131 #    A base file finder to be used for custom staticfiles finder classes.
132 #    """
133 #
134 #    def list(self, ignore_patterns):
135 #        """
136 #        Given an optional list of paths to ignore, this should return
137 #        a two item iterable consisting of the relative path and storage
138 #        instance.
139 #        """
140 #        raise NotImplementedError()
141