a first working mechanism based on Prelude class and requirements()
[unfold.git] / engine / prelude.py
1 from types import StringTypes, ListType
2
3 class Prelude:
4
5     """A class for collecting dependencies on js/css files or fragments"""
6
7     keys=[ 'js_files','css_files','js_chunks', 'css_chunks' ]
8     def __init__ (self):
9         # it's tempting to use sets but sets are not ordered..
10         self.js_files  =[]
11         self.css_files =[]
12         self.js_chunks =[]
13         self.css_chunks=[]
14
15     @staticmethod
16     def _normalize (input):
17         if   isinstance (input, ListType):      return input
18         elif isinstance (input, StringTypes):   return [ input ]
19         else:                                   return list (input)
20
21     def add_js_files (self, x):
22         for i in Prelude._normalize (x):
23             if i not in self.js_files: self.js_files.append(i)
24     def add_css_files (self, x):
25         for i in Prelude._normalize (x):
26             if i not in self.css_files: self.css_files.append(i)
27     def add_js_chunks (self, x):
28         self.js_chunks += Prelude._normalize (x)
29     def add_css_chunks (self, x):
30         self.css_chunks += Prelude._normalize (x)
31
32     def render_env (self): 
33         env={}
34         env['js_files']=  self.js_files
35         env['css_files']= self.css_files
36         env['js_chunks']= '\n'.join(self.js_chunks)
37         env['css_chunks']='\n'.join(self.css_chunks)
38         return env