Skip to content

Commit a1b6ed3

Browse files
committed
yaml configurarion file and distutils
1 parent 9414f2e commit a1b6ed3

File tree

5 files changed

+122
-94
lines changed

5 files changed

+122
-94
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dist
2+
build
3+
*.py[co]
4+
*.egg-info

jscssmin.py

Lines changed: 37 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,5 @@
11
#-*- coding: utf-8 -*-
22

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-
443
#-------------------------------------------------------------------------
454
import urllib2, urllib
465
import os
@@ -49,33 +8,20 @@
498
import random
509
import string
5110
import commands
11+
import yaml
5212

5313

5414
__author__ = 'Gustavo Vargas <xgvargas@gmail.com>'
15+
__version_info__ = ('0', '3', '0')
16+
__version__ = '.'.join(__version_info__)
5517
__all__ = [
5618
'jsMin',
5719
'cssMin',
5820
'jpgMin',
59-
'pngMin'
21+
'pngMin',
22+
'process'
6023
]
6124

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-
7925
def _read(file, mode='r'):
8026
"""
8127
Read entire file content.
@@ -112,23 +58,47 @@ def merge(obj):
11258
print 'Merging: {}'. format(f)
11359
merge += _read(f)
11460

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+
11685
for f in obj.get('template', []):
11786
print 'Merging django template: {}'. format(f)
11887
t = _read(f)
11988

12089
if settings.FORCE_SCRIPT_NAME:
12190
t = re.sub(r'\{%\s+url\b', settings.FORCE_SCRIPT_NAME+'{% url ', t)
12291

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
12497

125-
for f in obj.get('less', []):
126-
print 'Compiling LESS: {}'.format(f)
127-
ret, tmp = commands.getstatusoutput('lesscpy '+f)
128-
if ret == 0:
12998
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)
132102

133103
return merge
134104

@@ -333,30 +303,3 @@ def process(obj):
333303
#minify css and save to file
334304
if obj.get('cssmin'):
335305
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)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pyyaml

scripts/jscssmin

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!python
2+
3+
4+
if len(sys.argv) > 2:
5+
print """Error! Use jscssmin.py [--images|--images-full]"""
6+
exit(1)
7+
8+
img, imgfull = False, False
9+
if len(sys.argv) == 2:
10+
if sys.argv[1] == '--images': img = True
11+
elif sys.argv[1] == '--images-full': img, imgfull = True, True
12+
else:
13+
print """Error! Use jscssmin.py [--images|--images-full]"""
14+
exit(1)
15+
16+
try:
17+
with open('jscssmin.yaml', 'r') as fh:
18+
global merger
19+
merger = yaml.load(fh.read())
20+
except:
21+
print "Oops! File 'jscssmin.yaml' is not present in current directory!"
22+
exit(2)
23+
24+
for k, j in merger['blocks'].items():
25+
print '\nProcessing block: {}'.format(k)
26+
process(j)
27+
28+
if img:
29+
print '\nProcessing images'
30+
for root, dirs, files in os.walk(os.getcwd()):
31+
for f in files:
32+
if f.endswith('.jpg'):
33+
jpgMin(os.path.join(root, f), imgfull)
34+
elif f.endswith('.png'):
35+
pngMin(os.path.join(root, f), imgfull)

setup.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
from setuptools import find_packages, setup
5+
import pkg_resources
6+
import codecs
7+
import jscssmin
8+
9+
10+
with codecs.open('README.md', encoding='utf-8') as f:
11+
long_description = f.read()
12+
13+
with open("requirements.txt", "r") as f:
14+
install_requires = [str(req) for req in pkg_resources.parse_requirements(f)]
15+
16+
setup(
17+
name='jscssmin',
18+
version=jscssmin.__version__,
19+
license="Apache",
20+
description='JS and CSS automated merge and minifier',
21+
long_description=long_description,
22+
author='Gustavo vargas',
23+
author_email='xgvargas@gmail.com',
24+
url='https://github.com/xgvargas/js-css-min-django',
25+
#packages=find_packages(exclude=['*test*']),
26+
py_modules = ['jscssmin'],
27+
package_data={'': ['LICENSE']},
28+
scripts=['scripts/jscssmin'],
29+
install_requires=install_requires,
30+
classifiers=[
31+
'Development Status :: 5 - Production/Stable',
32+
'Environment :: Console',
33+
'Intended Audience :: End Users/Desktop',
34+
'Intended Audience :: Developers',
35+
'Intended Audience :: System Administrators',
36+
'License :: OSI Approved :: Apache Software License',
37+
'Operating System :: OS Independent',
38+
'Programming Language :: Python',
39+
'Programming Language :: Python :: 2.6',
40+
'Programming Language :: Python :: 2.7',
41+
'Topic :: Software Development :: Code Generators',
42+
'Topic :: Software Development :: Pre-processors',
43+
'Framework :: Django',
44+
],
45+
)

0 commit comments

Comments
 (0)