|
1 | 1 | #-*- coding: utf-8 -*- |
2 | 2 |
|
3 | | -""" |
4 | | -""" |
5 | | - |
6 | | -#example of a merger dictionary for a django project: |
7 | | -# |
8 | | -#config key is optional but is necessary for django template files |
9 | | -#if config is defined then path is mandatory. Can be blank otherwise. |
10 | | -# |
11 | | -merger = { |
12 | | - 'config': "myapp.settings", |
13 | | - 'path': ("/home/username/apps_wsgi/myapp", |
14 | | - #"more paths....", |
15 | | - ), |
16 | | - 'blocks': {'my js': {'static': ('static/js/code1.js', |
17 | | - 'static/js/code2.js', |
18 | | - ), |
19 | | - 'template': ('tasks/templates/tasks/code3template.js', |
20 | | - 'tasks/templates/tasks/codentemplate.js', |
21 | | - 'message/templates/message/moretemplatecode.js', |
22 | | - ), |
23 | | - #all static and rendered template JS will be merged, minified and saved to: |
24 | | - 'jsmin': 'static/js/deploycode.min.js' |
25 | | - }, |
26 | | - |
27 | | - 'my css': {'static': ("tasks/static/tasks/css/fixtypeahead.css", |
28 | | - ), |
29 | | - 'less': ("message/static/message/css/message.less", |
30 | | - "tasks/static/tasks/css/tasks.less" |
31 | | - ), |
32 | | - #the static css and the css compiled from less files are merged, minified and saved to: |
33 | | - 'cssmin': 'static/css/mydeploycss.min.css' |
34 | | - }, |
35 | | - |
36 | | - 'compact': {'less': ("tasks/static/tasks/css/compactform.less", |
37 | | - ), |
38 | | - #the single less is compiled to css and saved as is to: |
39 | | - 'full': 'tasks/static/tasks/css/compactform.css' |
40 | | - }, |
41 | | - }, |
42 | | - } |
43 | | - |
44 | 3 | #------------------------------------------------------------------------- |
45 | 4 | import urllib2, urllib |
46 | 5 | import os |
|
49 | 8 | import random |
50 | 9 | import string |
51 | 10 | import commands |
| 11 | +import yaml |
52 | 12 |
|
53 | 13 |
|
54 | 14 | __author__ = 'Gustavo Vargas <xgvargas@gmail.com>' |
| 15 | +__version_info__ = ('0', '3', '0') |
| 16 | +__version__ = '.'.join(__version_info__) |
55 | 17 | __all__ = [ |
56 | 18 | 'jsMin', |
57 | 19 | 'cssMin', |
58 | 20 | 'jpgMin', |
59 | | - 'pngMin' |
| 21 | + 'pngMin', |
| 22 | + 'process' |
60 | 23 | ] |
61 | 24 |
|
62 | | - |
63 | | -if merger.get('config'): #only imports django if we have a config file defined |
64 | | - import re |
65 | | - |
66 | | - for p in merger['path']: sys.path.append(p) |
67 | | - os.environ.setdefault("DJANGO_SETTINGS_MODULE", merger['config']) |
68 | | - |
69 | | - try: |
70 | | - from django.template.loader import get_template_from_string |
71 | | - from django.template.base import Context |
72 | | - from django.utils.encoding import smart_str |
73 | | - from django.conf import settings |
74 | | - except: |
75 | | - print 'Do you really have django well installed?' |
76 | | - sys.exit(1) |
77 | | - |
78 | | - |
79 | 25 | def _read(file, mode='r'): |
80 | 26 | """ |
81 | 27 | Read entire file content. |
@@ -112,23 +58,47 @@ def merge(obj): |
112 | 58 | print 'Merging: {}'. format(f) |
113 | 59 | merge += _read(f) |
114 | 60 |
|
115 | | - if merger.get('config'): #only process templates if we have a config |
| 61 | + def doless(f): |
| 62 | + print 'Compiling LESS: {}'.format(f) |
| 63 | + ret, tmp = commands.getstatusoutput('lesscpy '+f) |
| 64 | + if ret == 0: |
| 65 | + return tmp |
| 66 | + else: |
| 67 | + print 'LESS to CSS failed for: {} (Do you have lesscpy installed?)'.format(f) |
| 68 | + return '' |
| 69 | + |
| 70 | + if merger.get('config'): #only imports django if we have a config file defined |
| 71 | + import re |
| 72 | + |
| 73 | + for p in merger['path']: sys.path.append(p) |
| 74 | + os.environ.setdefault("DJANGO_SETTINGS_MODULE", merger['config']) |
| 75 | + |
| 76 | + try: |
| 77 | + from django.template.loader import get_template_from_string |
| 78 | + from django.template.base import Context |
| 79 | + from django.utils.encoding import smart_str |
| 80 | + from django.conf import settings |
| 81 | + except: |
| 82 | + print 'Do you really have django well installed?' |
| 83 | + sys.exit(1) |
| 84 | + |
116 | 85 | for f in obj.get('template', []): |
117 | 86 | print 'Merging django template: {}'. format(f) |
118 | 87 | t = _read(f) |
119 | 88 |
|
120 | 89 | if settings.FORCE_SCRIPT_NAME: |
121 | 90 | t = re.sub(r'\{%\s+url\b', settings.FORCE_SCRIPT_NAME+'{% url ', t) |
122 | 91 |
|
123 | | - merge += smart_str(get_template_from_string(t).render(Context({}))) |
| 92 | + tmp = smart_str(get_template_from_string(t).render(Context({}))) |
| 93 | + |
| 94 | + if f.endswith('.less'): |
| 95 | + pass |
| 96 | + #TODO compilar tmp para css |
124 | 97 |
|
125 | | - for f in obj.get('less', []): |
126 | | - print 'Compiling LESS: {}'.format(f) |
127 | | - ret, tmp = commands.getstatusoutput('lesscpy '+f) |
128 | | - if ret == 0: |
129 | 98 | merge += tmp |
130 | | - else: |
131 | | - print 'LESS to CSS failed for: {} (Do you have lesscpy installed?)'.format(f) |
| 99 | + |
| 100 | + for f in obj.get('less', []): |
| 101 | + merge += doless(f) |
132 | 102 |
|
133 | 103 | return merge |
134 | 104 |
|
@@ -333,30 +303,3 @@ def process(obj): |
333 | 303 | #minify css and save to file |
334 | 304 | if obj.get('cssmin'): |
335 | 305 | cssMin(merged, obj['cssmin']) |
336 | | - |
337 | | - |
338 | | -if __name__ == '__main__': |
339 | | - if len(sys.argv) > 2: |
340 | | - print """Error! Use jscssmin.py [--images|--images-full]""" |
341 | | - exit(1) |
342 | | - |
343 | | - img, imgfull = False, False |
344 | | - if len(sys.argv) == 2: |
345 | | - if sys.argv[1] == '--images': img = True |
346 | | - elif sys.argv[1] == '--images-full': img, imgfull = True, True |
347 | | - else: |
348 | | - print """Error! Use jscssmin.py [--images|--images-full]""" |
349 | | - exit(1) |
350 | | - |
351 | | - for k, j in merger['blocks'].items(): |
352 | | - print '\nProcessing block: {}'.format(k) |
353 | | - process(j) |
354 | | - |
355 | | - if img: |
356 | | - print '\nProcessing images' |
357 | | - for root, dirs, files in os.walk(os.getcwd()): |
358 | | - for f in files: |
359 | | - if f.endswith('.jpg'): |
360 | | - jpgMin(os.path.join(root, f), imgfull) |
361 | | - elif f.endswith('.png'): |
362 | | - pngMin(os.path.join(root, f), imgfull) |
0 commit comments