本文整理汇总了Python中compressor.CssCompressor.output方法的典型用法代码示例。如果您正苦于以下问题:Python CssCompressor.output方法的具体用法?Python CssCompressor.output怎么用?Python CssCompressor.output使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类compressor.CssCompressor
的用法示例。
在下文中一共展示了CssCompressor.output方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: render
# 需要导入模块: from compressor import CssCompressor [as 别名]
# 或者: from compressor.CssCompressor import output [as 别名]
def render(self, context):
content = self.nodelist.render(context)
if not settings.COMPRESS:
soup = BeautifulSoup(content)
if self.kind == 'css':
output = soup.link['href']
elif self.kind == 'js':
output = soup.script['src']
return output
if self.kind == 'css':
compressor = CssCompressor(content)
if self.kind == 'js':
compressor = JsCompressor(content)
in_cache = cache.get(compressor.cachekey)
if in_cache:
return in_cache
else:
output = compressor.output()
soup = BeautifulSoup(output)
if self.kind == 'css':
output = soup.link['href']
elif self.kind == 'js':
output = soup.script['src']
cache.set(compressor.cachekey, output, 86400) # rebuilds the cache once a day if nothign has changed.
return output
示例2: render
# 需要导入模块: from compressor import CssCompressor [as 别名]
# 或者: from compressor.CssCompressor import output [as 别名]
def render(self, context):
content = self.nodelist.render(context)
if 'COMPRESS_URL' in context:
media_url = context['COMPRESS_URL']
else:
media_url = settings.MEDIA_URL
if self.kind == 'css':
compressor = CssCompressor(content, xhtml=self.xhtml, media_url=media_url)
if self.kind == 'js':
compressor = JsCompressor(content, xhtml=self.xhtml, media_url=media_url)
in_cache = cache.get(compressor.cachekey)
if in_cache:
return in_cache
else:
# do this to prevent dog piling
in_progress_key = '%s.django_css.in_progress.%s' % (DOMAIN, compressor.cachekey)
added_to_cache = cache.add(in_progress_key, True, 300)
if added_to_cache:
output = compressor.output()
cache.set(compressor.cachekey, output, 2591000) # rebuilds the cache every 30 days if nothing has changed.
cache.set(in_progress_key, False, 300)
else:
while cache.get(in_progress_key):
sleep(0.1)
output = cache.get(compressor.cachekey)
return output
示例3: render
# 需要导入模块: from compressor import CssCompressor [as 别名]
# 或者: from compressor.CssCompressor import output [as 别名]
def render(self, context):
content = self.nodelist.render(context)
if not settings.COMPRESS or not len(content.strip()):
return content
if self.kind == "css":
compressor = CssCompressor(content)
if self.kind == "js":
compressor = JsCompressor(content)
in_cache = cache.get(compressor.cachekey)
if in_cache:
return in_cache
else:
# do this to prevent dog piling
in_progress_key = "django_compressor.in_progress.%s" % compressor.cachekey
in_progress = cache.get(in_progress_key)
if in_progress:
while cache.get(in_progress_key):
sleep(0.1)
output = cache.get(compressor.cachekey)
else:
cache.set(in_progress_key, True, 300)
try:
output = compressor.output()
cache.set(
compressor.cachekey, output, 2591000
) # rebuilds the cache every 30 days if nothign has changed.
except:
from traceback import format_exc
raise Exception(format_exc())
# finally:
# in_progress_key = False
cache.set(in_progress_key, False, 300)
return output
示例4: render
# 需要导入模块: from compressor import CssCompressor [as 别名]
# 或者: from compressor.CssCompressor import output [as 别名]
def render(self, context):
content = self.nodelist.render(context)
if self.kind == 'css':
compressor = CssCompressor(content, xhtml=self.xhtml)
if self.kind == 'js':
compressor = JsCompressor(content, xhtml=self.xhtml)
in_cache = cache.get(compressor.cachekey)
if in_cache:
return in_cache
else:
# do this to prevent dog piling
in_progress_key = 'django_css.in_progress.%s' % compressor.cachekey
in_progress = cache.get(in_progress_key)
if in_progress:
while cache.get(in_progress_key):
sleep(0.1)
output = cache.get(compressor.cachekey)
else:
cache.set(in_progress_key, True, 300)
try:
output = compressor.output()
cache.set(compressor.cachekey, output, 2591000) # rebuilds the cache every 30 days if nothign has changed.
except:
from traceback import format_exc
raise Exception(format_exc())
cache.set(in_progress_key, False, 300)
return output
示例5: render
# 需要导入模块: from compressor import CssCompressor [as 别名]
# 或者: from compressor.CssCompressor import output [as 别名]
def render(self, context):
content = self.nodelist.render(context)
if self.kind == 'css':
compressor = CssCompressor(content, xhtml=self.xhtml, output_filename=self.output_filename)
if self.kind == 'js':
compressor = JsCompressor(content, xhtml=self.xhtml, output_filename=self.output_filename)
in_cache = cache.get(compressor.cachekey)
if in_cache:
return in_cache
else:
output = compressor.output()
cache.set(compressor.cachekey, output, 86400) # rebuilds the cache once a day if nothing has changed.
return output
示例6: render
# 需要导入模块: from compressor import CssCompressor [as 别名]
# 或者: from compressor.CssCompressor import output [as 别名]
def render(self, context):
content = self.nodelist.render(context)
if not settings.COMPRESS:
return content
if self.kind == 'css':
compressor = CssCompressor(content)
if self.kind == 'js':
compressor = JsCompressor(content)
in_cache = cache.get(compressor.cachekey)
if in_cache:
return in_cache
else:
output = compressor.output()
cache.set(compressor.cachekey, output, 86400) # Rebuilds the cache once a day if nothing has changed.
return output
示例7: render
# 需要导入模块: from compressor import CssCompressor [as 别名]
# 或者: from compressor.CssCompressor import output [as 别名]
def render(self, context):
content = self.nodelist.render(context)
if not settings.COMPRESS or not len(content.strip()):
return content
if self.kind == 'css':
compressor = CssCompressor(content)
if self.kind == 'js':
compressor = JsCompressor(content)
output = self.cache_get(compressor.cachekey)
if output is None:
try:
output = compressor.output()
self.cache_set(compressor.cachekey, output)
except:
from traceback import format_exc
raise Exception(format_exc())
return output
示例8: CssMediaTestCase
# 需要导入模块: from compressor import CssCompressor [as 别名]
# 或者: from compressor.CssCompressor import output [as 别名]
class CssMediaTestCase(TestCase):
def setUp(self):
self.css = """
<link rel="stylesheet" href="/media/css/one.css" type="text/css" media="screen" charset="utf-8">
<style type="text/css" media="print">p { border:5px solid green;}</style>
<link rel="stylesheet" href="/media/css/two.css" type="text/css" charset="utf-8" media="all">
<style type="text/css">h1 { border:5px solid green;}</style>
"""
self.cssNode = CssCompressor(self.css)
def test_css_output(self):
links = BeautifulSoup(self.cssNode.output()).findAll('link')
media = [u'screen', u'print', u'all', None]
self.assertEqual(len(links), 4)
self.assertEqual(media, [l.get('media', None) for l in links])
def test_avoid_reordering_css(self):
css = self.css + '<style type="text/css" media="print">p { border:10px solid red;}</style>'
node = CssCompressor(css)
media = [u'screen', u'print', u'all', None, u'print']
links = BeautifulSoup(node.output()).findAll('link')
self.assertEqual(media, [l.get('media', None) for l in links])
示例9: render
# 需要导入模块: from compressor import CssCompressor [as 别名]
# 或者: from compressor.CssCompressor import output [as 别名]
def render(self, context):
content = self.nodelist.render(context)
if self.kind == 'css':
compressor = CssCompressor(content, xhtml=self.xhtml)
if self.kind == 'js':
compressor = JsCompressor(content, xhtml=self.xhtml)
output = cache.get(compressor.cachekey)
if output is None:
# do this to prevent dog piling
in_progress_key = '%s.django_css.in_progress.%s' % (DOMAIN, compressor.cachekey)
added_to_cache = cache.add(in_progress_key, True, 300)
if added_to_cache:
output = compressor.output()
cache.set(compressor.cachekey, output, 2591000) # rebuilds the cache every 30 days if nothing has changed.
cache.set(in_progress_key, False, 300)
else:
while cache.get(in_progress_key):
sleep(0.1)
output = cache.get(compressor.cachekey)
if settings.COMPRESS:
return render_to_string(compressor.template_name, {'filepath':output, 'xhtml':self.xhtml}, context)
else:
return output
示例10: render
# 需要导入模块: from compressor import CssCompressor [as 别名]
# 或者: from compressor.CssCompressor import output [as 别名]
def render(self, context):
content = self.nodelist.render(context)
if not settings.COMPRESS or not len(content.strip()):
return content
if self.kind == 'css':
compressor = CssCompressor(content)
if self.kind == 'js':
compressor = JsCompressor(content)
if self.kind == 'embeddedcss':
compressor = EmbeddedCssCompressor(content)
if self.kind == 'embeddedjs':
compressor = EmbeddedJsCompressor(content)
output = self.cache_get(compressor.cachekey)
if output is None:
try:
output = compressor.output()
# rebuilds the cache every 30 days if nothing has changed.
self.cache_set(compressor.cachekey, output, 2591000)
except:
from traceback import format_exc
raise Exception(format_exc())
return output
示例11: CompressorTestCase
# 需要导入模块: from compressor import CssCompressor [as 别名]
# 或者: from compressor.CssCompressor import output [as 别名]
class CompressorTestCase(TestCase):
def setUp(self):
settings.COMPRESS = True
self.css = """
<link rel="stylesheet" href="/media/css/one.css" type="text/css" charset="utf-8">
<style type="text/css">p { border:5px solid green;}</style>
<link rel="stylesheet" href="/media/css/two.css" type="text/css" charset="utf-8">
"""
self.cssNode = CssCompressor(self.css)
self.js = """
<script src="/media/js/one.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">obj.value = "value";</script>
"""
self.jsNode = JsCompressor(self.js)
def test_css_split(self):
out = [
('file', os.path.join(settings.MEDIA_ROOT, u'css/one.css'), '<link rel="stylesheet" href="/media/css/one.css" type="text/css" charset="utf-8" />'),
('hunk', u'p { border:5px solid green;}', '<style type="text/css">p { border:5px solid green;}</style>'),
('file', os.path.join(settings.MEDIA_ROOT, u'css/two.css'), '<link rel="stylesheet" href="/media/css/two.css" type="text/css" charset="utf-8" />'),
]
split = self.cssNode.split_contents()
split = [(x[0], x[1], str(x[2])) for x in split]
self.assertEqual(out, split)
def test_css_hunks(self):
out = ['body { background:#990; }', u'p { border:5px solid green;}', 'body { color:#fff; }']
self.assertEqual(out, self.cssNode.hunks)
def test_css_output(self):
out = u'body { background:#990; }\np { border:5px solid green;}\nbody { color:#fff; }'
self.assertEqual(out, self.cssNode.combined)
def test_css_mtimes(self):
is_date = re.compile(r'^\d{10}\.\d$')
for date in self.cssNode.mtimes:
self.assert_(is_date.match(str(date)), "mtimes is returning something that doesn't look like a date")
def test_css_return_if_off(self):
settings.COMPRESS = False
self.assertEqual(self.css, self.cssNode.output())
def test_cachekey(self):
is_cachekey = re.compile(r'django_compressor\.\w{12}')
self.assert_(is_cachekey.match(self.cssNode.cachekey), "cachekey is returning something that doesn't look like r'django_compressor\.\w{12}'")
def test_css_hash(self):
self.assertEqual('f7c661b7a124', self.cssNode.hash)
def test_css_return_if_on(self):
output = u'<link rel="stylesheet" href="/media/CACHE/css/f7c661b7a124.css" type="text/css" media="all" charset="utf-8">'
self.assertEqual(output, self.cssNode.output())
def test_js_split(self):
out = [('file', os.path.join(settings.MEDIA_ROOT, u'js/one.js'), '<script src="/media/js/one.js" type="text/javascript" charset="utf-8"></script>'),
('hunk', u'obj.value = "value";', '<script type="text/javascript" charset="utf-8">obj.value = "value";</script>')
]
split = self.jsNode.split_contents()
split = [(x[0], x[1], str(x[2])) for x in split]
self.assertEqual(out, split)
def test_js_hunks(self):
out = ['obj = {};', u'obj.value = "value";']
self.assertEqual(out, self.jsNode.hunks)
def test_js_concat(self):
out = u'obj = {};\nobj.value = "value";'
self.assertEqual(out, self.jsNode.concat())
def test_js_output(self):
out = u'obj={};obj.value="value";'
self.assertEqual(out, self.jsNode.combined)
def test_js_return_if_off(self):
settings.COMPRESS = False
self.assertEqual(self.js, self.jsNode.output())
def test_js_return_if_on(self):
output = u'<script type="text/javascript" src="/media/CACHE/js/3f33b9146e12.js" charset="utf-8"></script>'
self.assertEqual(output, self.jsNode.output())
示例12: CompressorTestCase
# 需要导入模块: from compressor import CssCompressor [as 别名]
# 或者: from compressor.CssCompressor import output [as 别名]
class CompressorTestCase(BaseTestCase):
def setUp(self):
super(CompressorTestCase, self).setUp()
self.ccssFile = os.path.join(settings.MEDIA_ROOT, u'css/three.css')
self.css = dedent("""
<link rel="stylesheet" href="/media/css/one.css" type="text/css">
<style type="text/css">p { border:5px solid green;}</style>
<link rel="stylesheet" href="/media/css/two.css" type="text/css">
<link rel="stylesheet" href="/media/css/three.ccss" type="text/css">
<style type="text/ccss">
""")+ \
'\n'.join((
"small:\n font-size:10px",
'</style>\n<style type="ccss">',
"h1:\n font-weight:bold",
"</style>"))
self.css_hash = "1ff892c21b66"
self.js_hash = "3f33b9146e12"
self.cssNode = CssCompressor(self.css)
self.js = """
<script src="/media/js/one.js" type="text/javascript"></script>
<script type="text/javascript">obj.value = "value";</script>
"""
self.jsNode = JsCompressor(self.js)
def test_get_filename(self):
settings.COMPRESS_URL = '/static_test/'
settings.COMPRESS_ROOT = os.path.join(os.path.dirname(__file__),'static_test_dir')
path = os.path.join(settings.MEDIA_URL,'something.css')
filename = self.cssNode.get_filename(path)
self.assertEqual(filename,os.path.join(settings.MEDIA_ROOT,'something.css'))
path = "http://something.com/static/something.css"
self.assertRaises(UncompressableFileError, self.cssNode.get_filename,path)
self.assertRaises(UncompressableFileError, self.jsNode.get_filename,path)
def test_css_compiler_exists(self):
settings.COMPILER_FORMATS = {
'.ccss': {
'binary_path': 'python ' + os.path.join(self.TEST_DIR,'clevrcss.py'),
'arguments': '*.ccss'
},
}
self.assertRaises(Exception, self.cssNode.output)
def test_css_split(self):
out = [
('file', os.path.join(settings.MEDIA_ROOT, u'css/one.css'), '<link rel="stylesheet" href="/media/css/one.css" type="text/css" />'),
('hunk', u'p { border:5px solid green;}', '<style type="text/css">p { border:5px solid green;}</style>'),
('file', os.path.join(settings.MEDIA_ROOT, u'css/two.css'), '<link rel="stylesheet" href="/media/css/two.css" type="text/css" />'),
('file', self.ccssFile, '<link rel="stylesheet" href="/media/css/three.css" type="text/css" />'),
('hunk', 'small {\n font-size: 10px;\n}', "<style type='text/css'>\nsmall {\n font-size: 10px;\n}\n</style>"),
('hunk', 'h1 {\n font-weight: bold;\n}', "<style type='text/css'>\nh1 {\n font-weight: bold;\n}\n</style>")
]
split = self.cssNode.split_contents()
split = [(x[0], x[1], str(x[2])) for x in split]
self.assertEqual(out, split)
if os.path.exists(self.ccssFile):
os.remove(self.ccssFile)
def test_css_hunks(self):
out = ['body { background:#990; }', 'p { border:5px solid green;}', 'body { color:#fff; }', 'a {\n color: #5c4032;\n}', 'small {\n font-size: 10px;\n}', 'h1 {\n font-weight: bold;\n}']
self.assertEqual(out, self.cssNode.hunks)
if os.path.exists(self.ccssFile):
os.remove(self.ccssFile)
def test_css_output(self):
out = u'body { background:#990; }\np { border:5px solid green;}\nbody { color:#fff; }\na {\n color: #5c4032;\n}\nsmall {\n font-size: 10px;\n}\nh1 {\n font-weight: bold;\n}'
self.assertEqual(out, self.cssNode.combined)
if os.path.exists(self.ccssFile):
os.remove(self.ccssFile)
def test_css_mtimes(self):
is_date = re.compile(r'^\d{10}\.\d$')
for date in self.cssNode.mtimes:
self.assert_(is_date.match(str(date)), "mtimes is returning something that doesn't look like a date")
if os.path.exists(self.ccssFile):
os.remove(self.ccssFile)
def test_css_return_if_off(self):
settings.COMPRESS = False
css = """
<link rel="stylesheet" href="/media/css/one.css" type="text/css">
<style type="text/css">p { border:5px solid green;}</style>
<link rel="stylesheet" href="/media/css/two.css" type="text/css">
<link rel="stylesheet" href="/media/css/three.css" type="text/css">
<style type='text/css'>\nsmall {\n font-size: 10px;\n}
</style>\n<style type='text/css'>\nh1 {\n font-weight: bold;\n}\n</style>
"""
self.assertEqual(dedent(css).strip(), dedent(self.cssNode.output()).strip())
if os.path.exists(self.ccssFile):
os.remove(self.ccssFile)
def test_css_hash(self):
self.assertEqual(self.css_hash, self.cssNode.hash)
if os.path.exists(self.ccssFile):
os.remove(self.ccssFile)
#.........这里部分代码省略.........
示例13: test_avoid_reordering_css
# 需要导入模块: from compressor import CssCompressor [as 别名]
# 或者: from compressor.CssCompressor import output [as 别名]
def test_avoid_reordering_css(self):
css = self.css + '<style type="text/css" media="print">p { border:10px solid red;}</style>'
node = CssCompressor(css)
media = [u'screen', u'print', u'all', None, u'print']
links = BeautifulSoup(node.output()).findAll('link')
self.assertEqual(media, [l.get('media', None) for l in links])
示例14: CompressorTestCase
# 需要导入模块: from compressor import CssCompressor [as 别名]
# 或者: from compressor.CssCompressor import output [as 别名]
class CompressorTestCase(TestCase):
def setUp(self):
settings.COMPRESS = True
settings.COMPILER_FORMATS = {
'.ccss': {
'binary_path': 'python ' + os.path.join(django_settings.TEST_DIR,'clevercss.py'),
'arguments': '*.ccss'
},
'.xcss': {
'python':'clevercss.convert',
},
}
self.ccssFile = os.path.join(settings.MEDIA_ROOT, u'css/three.css')
self.xcssFile = os.path.join(settings.MEDIA_ROOT, u'css/four.xcss')
self.css = dedent("""
<link rel="stylesheet" href="/media/css/one.css" type="text/css">
<style type="text/css">p { border:5px solid green;}</style>
<link rel="stylesheet" href="/media/css/two.css" type="text/css">
<link rel="stylesheet" href="/media/css/three.ccss" type="text/css">
<link rel="stylesheet" href="/media/css/four.xcss" type="text/css">
<style type="text/xcss">
""")+ \
'\n'.join((
"small:\n font-size:10px",
'</style>\n<style type="xcss">',
"h1:\n font-weight:bold",
"</style>"))
self.cssNode = CssCompressor(self.css)
self.js = """
<script src="/media/js/one.js" type="text/javascript"></script>
<script type="text/javascript">obj.value = "value";</script>
"""
self.jsNode = JsCompressor(self.js)
def test_get_filename(self):
settings.COMPRESS_URL = '/static_test/'
settings.COMPRESS_ROOT = os.path.join(os.path.dirname(__file__),'static_test_dir')
path = os.path.join(settings.MEDIA_URL,'something.css')
filename = self.cssNode.get_filename(path)
self.assertEqual(filename,os.path.join(settings.MEDIA_ROOT,'something.css'))
path = "http://something.com/static/something.css"
self.assertRaises(UncompressableFileError, self.cssNode.get_filename,path)
self.assertRaises(UncompressableFileError, self.jsNode.get_filename,path)
def test_css_compiler_exists(self):
settings.COMPILER_FORMATS = {
'.ccss': {
'binary_path': 'python ' + os.path.join(django_settings.TEST_DIR,'clevrcss.py'),
'arguments': '*.ccss'
},
}
self.assertRaises(Exception, self.cssNode.output)
def test_css_split(self):
out = [
{'elem': '<link rel="stylesheet" href="/media/css/one.css" type="text/css" />',
'filename': os.path.join(settings.MEDIA_ROOT, u'css/one.css') },
{'elem': '<style type="text/css">p { border:5px solid green;}</style>',
'data': u'p { border:5px solid green;}' },
{'elem': '<link rel="stylesheet" href="/media/css/two.css" type="text/css" />',
'filename': os.path.join(settings.MEDIA_ROOT, u'css/two.css') },
{'elem': '<link rel="stylesheet" href="/media/css/three.css" type="text/css" />',
'filename': self.ccssFile, },
{'elem': '<link rel="stylesheet" href="/media/css/four.xcss" type="text/css" />',
'filename': self.xcssFile,
'data': u'p {\n color: black;\n}' },
{'elem': '<style type="text/xcss">\nsmall:\n font-size:10px\n</style>',
'data': u'small {\n font-size: 10px;\n}' },
{'elem': '<style type="xcss">\nh1:\n font-weight:bold\n</style>',
'data': u'h1 {\n font-weight: bold;\n}' }
]
split = self.cssNode.split_contents()
for item in split:
item['elem'] = str(item['elem'])
self.assertEqual(out, split)
if os.path.exists(self.ccssFile):
os.remove(self.ccssFile)
def test_css_hunks(self):
out = ['body { background:#990; }',
'p { border:5px solid green;}',
'body { color:#fff; }',
'a {\n color: #5c4032;\n}',
'p {\n color: black;\n}',
'small {\n font-size: 10px;\n}',
'h1 {\n font-weight: bold;\n}'
]
self.assertEqual(out, self.cssNode.hunks)
if os.path.exists(self.ccssFile):
os.remove(self.ccssFile)
def test_css_output(self):
out = '\n'.join((
'body { background:#990; }',
'p { border:5px solid green;}',
'body { color:#fff; }',
'a {\n color: #5c4032;\n}',
#.........这里部分代码省略.........