def test_convert_bytes(self): from pulsar.utils.system import convert_bytes self.assertEqual(convert_bytes(None), '#NA') self.assertEqual(convert_bytes(4), '4B') self.assertEqual(convert_bytes(1024), '1.0KB') self.assertEqual(convert_bytes(1024**2), '1.0MB') self.assertEqual(convert_bytes(1024**3), '1.0GB') self.assertEqual(convert_bytes(1024**4), '1.0TB') self.assertEqual(convert_bytes(1024**5), '1.0PB') self.assertEqual(convert_bytes(1024**6), '1.0EB') self.assertEqual(convert_bytes(1024**7), '1.0ZB') self.assertEqual(convert_bytes(1024**8), '1.0YB')
def run(self, options, dump=True, **params): target = options.cssfile app = self.app name = app.meta.name self.theme = options.theme or name if not target and not options.variables: target = self.theme mdir = os.path.join(self.app.meta.path, 'media', name) if os.path.isdir(mdir): target = os.path.join(mdir, target) data = self.render(self.theme, options.variables) if dump: if target: targets = ['%s.css' % target] if options.minify: targets.append('%s.min.css' % target) for minify, target in enumerate(targets): if minify: data = self.minify(options, data) with open(target, 'w') as f: f.write(data) b = convert_bytes(len(data)) self.write('Created %s file. Size %s.' % (target, b)) return targets else: self.write(data) return data
def run(self, argv, dump=True, **params): if 'base' not in self.app.extensions: raise RuntimeError('"ui" requires the "base" extension.') options = self.options(argv) target = options.file app = self.app name = app.meta.name self.theme = options.theme or self.app.config['THEME'] or name if not target and not options.variables: target = self.theme mdir = os.path.join(self.app.meta.path, 'media', name) if os.path.isdir(mdir): target = os.path.join(mdir, target) data = self.render(self.theme, options.variables) if dump: if target: targets = ['%s.css' % target] if options.minify: targets.append('%s.min.css' % target) for minify, target in enumerate(targets): if minify: data = self.minify(options, data) with open(target, 'w') as f: f.write(data) b = convert_bytes(len(data)) self.write('Created %s file. Size %s.' % (target, b)) else: self.write(data) return data
def minify(self, options, data): b = convert_bytes(len(data)) self.write('Minimise %s css file via http://cssminifier.com' % b) http = HttpClient(loop=new_event_loop()) response = http.post('http://cssminifier.com/raw', data={'input': data}) if response.status_code == 200: return native_str(response.get_content()) else: response.raise_for_status()
def dump_theme(theme=None, dump_variables=False, minify=False): target = Stream() with cssv.theme(theme) as t: if dump_variables: data = cssv.tojson() target.write(json.dumps(data, indent=4)) data = target.getvalue() else: data = css.render_all() target.write(data) if minify: target = csscompress(target) data = target.getvalue() b = convert_bytes(len(data)) LOGGER.info('Dumped css of %s in size.'% b) return data
def raise_large_body_error(limit): raise HttpException( "Request content length too large. Limit is %s" % convert_bytes(limit), status=LARGE_BODY_CODE )
def raise_large_body_error(limit): raise HttpException("Request content length too large. Limit is %s" % convert_bytes(limit), status=LARGE_BODY_CODE)