本文整理汇总了Python中slimmer.css_slimmer函数的典型用法代码示例。如果您正苦于以下问题:Python css_slimmer函数的具体用法?Python css_slimmer怎么用?Python css_slimmer使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了css_slimmer函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_registerCSSFiles__one_slimmed_and_gzipped
def test_registerCSSFiles__one_slimmed_and_gzipped(self):
""" test the registerCSSFiles() with slim_if_possible=True but one
of the files shouldn't be slimmed because its filename indicates
that it's already been slimmed/packed/minified.
In this test, the big challange is when two js files are combined
and one of them should be slimmed, the other one not slimmed.
"""
if css_slimmer is None:
return
class MyProduct:
pass
files = ["test-min.css", "test.css"]
files.append(tuple(files))
registerCSSFiles(
MyProduct, files, rel_path="tests", set_expiry_header=True, slim_if_possible=True, gzip_if_possible=True
)
REQUEST = self.app.REQUEST
RESPONSE = REQUEST.RESPONSE
instance = MyProduct()
for filename in files:
if isinstance(filename, tuple):
filename = ",".join(filename)
static = getattr(instance, filename)
self.assertTrue(isinstance(static, BetterImageFile))
# if you just call static.__str__() you're not calling it
# with a REQUEST that accepts gzip encoding
REQUEST.set("HTTP_ACCEPT_ENCODING", "gzip")
bin_rendered = static.index_html(REQUEST, RESPONSE)
# expect this to be slimmed
if len(filename.split(",")) > 1:
content_parts = []
for filename in filename.split(","):
content = open(right_here(filename)).read()
if filename.find("min") > -1:
content_parts.append(content)
else:
content_parts.append(css_slimmer(content))
expected_content = "\n".join(content_parts)
expected_content = expected_content.strip()
else:
if filename.find("min") > -1:
expected_content = open(right_here(filename)).read()
else:
expected_content = css_slimmer(open(right_here(filename)).read())
expected_content = expected_content.strip()
rendered = _gzip2ascii(bin_rendered)
self.assertEqual(rendered.strip(), expected_content)
示例2: test_registering_with_slimming_and_images_in_css
def test_registering_with_slimming_and_images_in_css(self):
try:
from slimmer import js_slimmer, css_slimmer
except ImportError:
# not possible to test this
return
class MyProduct:
pass
instance = MyProduct()
# it will only fix images that have otherwise been registered
registerImage(MyProduct, right_here("image.jpg"), rel_path="tests")
registerCSSFile(
MyProduct,
"containsimages.css",
rel_path="tests",
set_expiry_header=True,
slim_if_possible=True,
replace_images_with_aliases=True,
)
static = getattr(instance, "containsimages.css")
# Not the same...
self.assertNotEqual(str(static), css_slimmer(open(right_here("containsimages.css")).read()))
# unless you remove all '.\d+.'
self.assertEqual(
re.sub("\.\d{10,11}\.", ".", str(static)), css_slimmer(open(right_here("containsimages.css")).read())
)
# because we haven't registered large.jpg it won't be aliased
self.assertTrue("large.jpg" in str(static))
self.assertTrue("image.jpg" not in str(static))
self.assertEqual(
sorted(["containsimages.css-slimmed.css-aliased.css", "containsimages.css-slimmed.css"]),
sorted(os.listdir(_get_autogenerated_dir())),
)
# if you don it again it should just overwrite the old one
registerCSSFile(
MyProduct,
"containsimages.css",
rel_path="tests",
set_expiry_header=True,
slim_if_possible=True,
replace_images_with_aliases=True,
)
self.assertEqual(
sorted(["containsimages.css-slimmed.css-aliased.css", "containsimages.css-slimmed.css"]),
sorted(os.listdir(_get_autogenerated_dir())),
)
示例3: test_registerCSSFiles__both_slimmed_and_gzipped
def test_registerCSSFiles__both_slimmed_and_gzipped(self):
""" test the registerCSSFiles() with slim_if_possible=True """
if css_slimmer is None:
return
class MyProduct:
pass
# test setting a bunch of files
files = ["large.css", "test.css"]
files.append(tuple(files))
registerCSSFiles(
MyProduct, files, rel_path="tests", set_expiry_header=True, slim_if_possible=True, gzip_if_possible=True
)
instance = MyProduct()
REQUEST = self.app.REQUEST
RESPONSE = REQUEST.RESPONSE
for filename in files:
if isinstance(filename, tuple):
filename = ",".join(filename)
static = getattr(instance, filename)
self.assertTrue(isinstance(static, BetterImageFile))
# if you just call static.__str__() you're not calling it
# with a REQUEST that accepts gzip encoding
REQUEST.set("HTTP_ACCEPT_ENCODING", "gzip")
bin_rendered = static.index_html(REQUEST, RESPONSE)
rendered = _gzip2ascii(bin_rendered).strip()
# expect this to be slimmed
if len(filename.split(",")) > 1:
content_parts = [open(right_here(x)).read() for x in filename.split(",")]
if css_slimmer is None:
expected_content = "\n".join(content_parts)
else:
expected_content = "\n".join([css_slimmer(x) for x in content_parts])
else:
content = open(right_here(filename)).read()
if css_slimmer is None:
expected_content = content
else:
expected_content = css_slimmer(content)
self.assertEqual(rendered.strip(), expected_content.strip())
示例4: test_registerCSSFiles__one_slimmed
def test_registerCSSFiles__one_slimmed(self):
""" test the registerCSSFiles() with slim_if_possible=True but one
of the files shouldn't be slimmed because its filename indicates
that it's already been slimmed/packed/minified.
In this test, the big challange is when two js files are combined
and one of them should be slimmed, the other one not slimmed.
"""
if css_slimmer is None:
return
class MyProduct:
pass
files = ["test-min.css", "test.css"]
files.append(tuple(files))
registerCSSFiles(
MyProduct, files, rel_path="tests", set_expiry_header=True, slim_if_possible=True, gzip_if_possible=False
)
instance = MyProduct()
for filename in files:
if isinstance(filename, tuple):
filename = ",".join(filename)
static = getattr(instance, filename)
self.assertTrue(isinstance(static, BetterImageFile))
rendered = str(static).strip()
# expect this to be slimmed partially
if len(filename.split(",")) > 1:
content_parts = []
for filename in filename.split(","):
content = open(right_here(filename)).read()
if filename.find("min") > -1:
content_parts.append(content)
else:
content_parts.append(css_slimmer(content))
expected_content = "\n".join(content_parts)
expected_content = expected_content.strip()
else:
if filename.find("min") > -1:
expected_content = open(right_here(filename)).read()
else:
expected_content = css_slimmer(open(right_here(filename)).read())
expected_content = expected_content.strip()
self.assertEqual(rendered.strip(), expected_content)
示例5: therapy
def therapy(self):
css = []
for fn in self.absolute_paths:
with open(fn, 'r') as fp:
css.append(fp.read())
css = css_slimmer(''.join(css))
storage.save(self.node.destination, ContentFile(css))
示例6: _get_code
def _get_code(self):
import slimmer
f = open(self.path)
code = f.read()
f.close()
if self.filetype == '.css':
return slimmer.css_slimmer(code)
if self.filetype == '.js':
return slimmer.js_slimmer(code)
if self.filetype == '.html':
return slimmer.xhtml_slimmer(code)
return code
示例7: get_code
def get_code(self):
import slimmer
f = codecs.open(self.path, 'r', 'utf-8')
code = f.read()
f.close()
if self.filetype == '.css':
return slimmer.css_slimmer(code)
if self.filetype == '.js':
return slimmer.js_slimmer(code)
if self.filetype == '.html':
return slimmer.xhtml_slimmer(code)
return code
示例8: test_registering_with_slimming_and_gzip_and_images_in_css
def test_registering_with_slimming_and_gzip_and_images_in_css(self):
try:
from slimmer import js_slimmer, css_slimmer
except ImportError:
# not possible to test this
return
class MyProduct:
pass
instance = MyProduct()
registerJSFile(MyProduct, "test.js", rel_path="tests", set_expiry_header=True, slim_if_possible=True)
static = getattr(instance, "test.js")
self.assertEqual(str(static), js_slimmer(open(right_here("test.js")).read()))
registerCSSFile(
MyProduct,
"test.css",
rel_path="tests",
set_expiry_header=True,
slim_if_possible=True,
gzip_if_possible=True,
)
static = getattr(instance, "test.css")
self.assertEqual(str(static), css_slimmer(open(right_here("test.css")).read()))
# if you don it again it should just overwrite the old one
registerCSSFile(
MyProduct,
"test.css",
rel_path="tests",
set_expiry_header=True,
slim_if_possible=True,
gzip_if_possible=True,
)
static = getattr(instance, "test.css")
self.assertEqual(str(static), css_slimmer(open(right_here("test.css")).read()))
示例9: optimize
def optimize(content, type_):
if type_ == CSS:
if getattr(settings, 'DJANGO_STATIC_YUI_COMPRESSOR', None):
return _run_yui_compressor(content, type_)
return css_slimmer(content)
elif type_ == JS:
if getattr(settings, 'DJANGO_STATIC_CLOSURE_COMPILER', None):
return _run_closure_compiler(content)
if getattr(settings, 'DJANGO_STATIC_YUI_COMPRESSOR', None):
return _run_yui_compressor(content, type_)
return js_slimmer(content)
else:
raise ValueError("Invalid type %r" % type_)
示例10: _registerCSS
def _registerCSS(product, filename, path='css', slim_if_possible=True):
objectid = filename
setattr(product,
objectid,
BetterImageFile(os.path.join(path, filename), globals())
)
obj = getattr(product, objectid)
if css_slimmer is not None and OPTIMIZE:
if slim_if_possible:
slimmed = css_slimmer(open(obj.path,'rb').read())
new_path = obj.path + '-slimmed.css'
new_path = _get_autogenerated_file_path(new_path)
open(new_path, 'wb').write(slimmed)
setattr(obj, 'path', new_path)
示例11: test_registerCSSFiles__both_slimmed
def test_registerCSSFiles__both_slimmed(self):
""" test the registerCSSFiles() with slim_if_possible=True """
if css_slimmer is None:
return
try:
class MyProduct:
pass
# test setting a bunch of files
files = ["large.css", "test.css"]
files.append(tuple(files))
registerCSSFiles(
MyProduct,
files,
rel_path="tests",
set_expiry_header=True,
slim_if_possible=True,
gzip_if_possible=False,
)
instance = MyProduct()
for filename in files:
if isinstance(filename, tuple):
filename = ",".join(filename)
static = getattr(instance, filename)
self.assertTrue(isinstance(static, BetterImageFile))
rendered = str(static)
# expect this to be slimmed
if len(filename.split(",")) > 1:
content_parts = [open(right_here(x)).read() for x in filename.split(",")]
expected_content = "\n".join([css_slimmer(x) for x in content_parts])
else:
expected_content = css_slimmer(open(right_here(filename)).read())
self.assertEqual(rendered.strip(), expected_content.strip())
except ImportError:
pass
示例12: render
def render(self, context):
code = self.nodelist.render(context)
if self.format == 'css':
return css_slimmer(code)
elif self.format in ('js', 'javascript'):
return js_slimmer(code)
elif self.format == 'html':
return html_slimmer(code)
else:
format = guessSyntax(code)
if format:
self.format = format
return self.render(context)
return code
示例13: optimize
def optimize(content, type_):
if type_ == CSS:
if cssmin is not None:
return _run_cssmin(content)
elif getattr(settings, "DJANGO_STATIC_YUI_COMPRESSOR", None):
return _run_yui_compressor(content, type_)
return slimmer.css_slimmer(content)
elif type_ == JS:
if getattr(settings, "DJANGO_STATIC_CLOSURE_COMPILER", None):
return _run_closure_compiler(content)
if getattr(settings, "DJANGO_STATIC_YUI_COMPRESSOR", None):
return _run_yui_compressor(content, type_)
if getattr(settings, "DJANGO_STATIC_JSMIN", None):
return _run_jsmin(content)
return slimmer.js_slimmer(content)
else:
raise ValueError("Invalid type %r" % type_)
示例14: test_registering_with_slimming_basic
def test_registering_with_slimming_basic(self):
try:
from slimmer import js_slimmer, css_slimmer
except ImportError:
# not possible to test this
return
class MyProduct:
pass
instance = MyProduct()
registerJSFile(MyProduct, "test.js", rel_path="tests", set_expiry_header=True, slim_if_possible=True)
static = getattr(instance, "test.js")
self.assertEqual(str(static), js_slimmer(open(right_here("test.js")).read()))
# this will have created a file called 'test.js-slimmed.js' whose content
# is the same as str(static)
copy_test_js = os.path.join(_get_autogenerated_dir(), "test.js-slimmed.js")
self.assertTrue(os.path.isfile(copy_test_js))
self.assertEqual(open(copy_test_js).read(), str(static))
# and it that directory there should not be any other files
for f in os.listdir(os.path.dirname(copy_test_js)):
self.assertEqual(f, os.path.basename(copy_test_js))
registerCSSFile(MyProduct, "test.css", rel_path="tests", set_expiry_header=True, slim_if_possible=True)
static = getattr(instance, "test.css")
self.assertEqual(str(static), css_slimmer(open(right_here("test.css")).read()))
# this will have created a file called 'test.css-slimmed.css' whose content
# is the same as str(static)
copy_test_css = os.path.join(_get_autogenerated_dir(), "test.css-slimmed.css")
self.assertTrue(os.path.isfile(copy_test_css))
self.assertEqual(open(copy_test_css).read(), str(static))
# and it that directory there should not be any other files other
# than the one we made before called test.js-slimmed.js
for f in os.listdir(os.path.dirname(copy_test_css)):
self.assertTrue(f == os.path.basename(copy_test_js) or f == os.path.basename(copy_test_css))
# if you don it again it should just overwrite the old one
registerCSSFile(MyProduct, "test.css", rel_path="tests", set_expiry_header=True, slim_if_possible=True)
static = getattr(instance, "test.css")
# there should still only be two files in the autogenerated directory
self.assertEqual(len(os.listdir(_get_autogenerated_dir())), 2)
示例15: registerCSS
def registerCSS(filename, path='css', slim_if_possible=True):
product = OFS.misc_.misc_.IssueTrackerMassContainer
objectid = filename
setattr(product,
objectid,
BetterImageFile(os.path.join(path, filename), globals())
)
obj = getattr(product, objectid)
if css_slimmer is not None and OPTIMIZE:
if slim_if_possible:
slimmed = css_slimmer(open(obj.path,'rb').read())
new_path = obj.path + '-slimmed'
open(new_path, 'wb').write(slimmed)
setattr(obj, 'path', new_path)