more robust autologout for weird situations
[myslice.git] / unfold / prelude.py
1 from types import StringTypes, ListType
2
3 from django.template.loader import render_to_string
4
5 debug=True
6
7 class Prelude:
8
9     """A class for collecting dependencies on js/css files or fragments"""
10
11     keys=[ 'js_files','css_files','js_chunks', 'css_chunks' ]
12     def __init__ (self, js_files=None, css_files=None, js_chunks=None, css_chunks=None):
13         # it's tempting to use sets but sets are not ordered..
14         self.js_files  = Prelude._normalize(js_files)
15         self.css_files = Prelude._normalize(css_files)
16         self.js_chunks = Prelude._normalize(js_chunks)
17         self.css_chunks= Prelude._normalize(css_chunks)
18
19     @staticmethod
20     def _normalize (input):
21         if not input:                           return []
22         elif isinstance (input, ListType):      return input
23         elif isinstance (input, StringTypes):   return [ input ]
24         else:                                   return list (input)
25
26     def add_js_files (self, x):
27         for i in Prelude._normalize (x):
28             if i not in self.js_files: self.js_files.append(i)
29     def add_css_files (self, x):
30         for i in Prelude._normalize (x):
31             if i not in self.css_files: self.css_files.append(i)
32     def add_js_chunks (self, x):
33         self.js_chunks += Prelude._normalize (x)
34     def add_css_chunks (self, x):
35         self.css_chunks += Prelude._normalize (x)
36
37     def inspect_string (self,msg):
38         result =  'Prelude.inspect %s (%s) with '%(msg,self)
39         result += ",".join( [ "%s->%s"%(k,len(getattr(self,k))) for k in ['js_files','js_chunks','css_files','css_chunks'] ] )
40         return result
41     def inspect (self,msg):
42         print self.inspect_string(msg)
43
44     # first attempt was to use a simple dict like this
45     #    env={}
46     #    env['js_files']=  self.js_files
47     #    env['css_files']= self.css_files
48     #    env['js_chunks']= '\n'.join(self.js_chunks)
49     #    env['css_chunks']='\n'.join(self.css_chunks)
50     #    return env
51     # together with this in prelude.html
52     # {% for js_file in js_files %} {% insert_str prelude js_file %} {% endfor %}
53     # {% for css_file in css_files %} {% insert_str prelude css_file %} {% endfor %}
54     # somehow however this would not work too well, 
55     # probably insert_above is not powerful enough to handle that
56     # 
57     # so a much simpler and safer approach is for us to compute the html header directly
58     # this requires  to filter on full urls
59     #
60     @staticmethod
61     def full_url (input):
62         if input.startswith("http://") or input.startswith("https://"):
63             return input
64         else:
65             from myslice.settings import STATIC_URL
66             return "%s%s"%(STATIC_URL,input)
67         
68     def prelude_env (self): 
69         env={}
70         env['js_urls'] = [ Prelude.full_url (js_file) for js_file in self.js_files ]
71         env['css_urls'] = [ Prelude.full_url (css_file) for css_file in self.css_files ]
72         env['js_chunks']= self.js_chunks
73         env['css_chunks']=self.css_chunks
74         if debug:
75             print "prelude has %d js_files, %d css files, %d js chunks and %d css_chunks"%\
76                 (len(self.js_files),len(self.css_files),len(self.js_chunks),len(self.css_chunks),)
77         # render this with prelude.html and put the result in header_prelude
78         header_prelude = render_to_string ('prelude.html',env)
79         return { 'header_prelude' : header_prelude }