Python django.utils.text 模块,normalize_newlines() 实例源码
我们从Python开源项目中,提取了以下14个代码示例,用于说明如何使用django.utils.text.normalize_newlines()。
def write_to_disk(self):
"""
Write profile to disk so that openwrt ImageBuilder can pickup and build the image.
"""
to_path = os.path.join(settings.NETWORK_INCLUDES_PATH, self.network.slug, self.name)
if not os.path.exists(to_path):
os.makedirs(to_path)
inc_packages = IncludePackages.from_str(self.include_packages)
inc_packages.dump(open(os.path.join(to_path, "include_packages"), "w"))
files = {}
for fname, content in self.include_files.iteritems():
t = Template(content)
c = Context({"NETWORK_NAME": self.network.name,
"SSH_KEYS": "\n".join([k.key for k in self.ssh_keys.all()])},
autoescape=False)
files[fname] = normalize_newlines(t.render(c))
inc_files = IncludeFiles(files)
inc_files.dump(os.path.join(to_path, "include_files"))
def linebreaks(value, autoescape=False):
"""Converts newlines into <p> and <br />s."""
value = normalize_newlines(value)
paras = re.split('\n{2,}', value)
if autoescape:
paras = ['<p>%s</p>' % escape(p).replace('\n', '<br />') for p in paras]
else:
paras = ['<p>%s</p>' % p.replace('\n', '<br />') for p in paras]
return '\n\n'.join(paras)
def dump(self, to_path):
"""
Write all include files to disk
"""
if os.path.exists(to_path):
shutil.rmtree(to_path)
for abspath, filecontent in self.files.iteritems():
to_dir = os.path.join(to_path, os.path.dirname(abspath[1:]))
filename = os.path.basename(abspath)
if not os.path.exists(to_dir):
os.makedirs(to_dir)
with codecs.open(os.path.join(to_dir, filename), "w", "utf-8") as f:
f.write(normalize_newlines(filecontent))
def angular_templates():
partials_dir = settings.STATICFILES_Dirs[0]
exclude = ('bower_components',)
for (root, dirs, files) in os.walk(partials_dir, topdown=True):
dirs[:] = [d for d in dirs if d not in exclude]
for file_name in files:
if file_name.endswith('.html'):
file_path = os.path.join(root, file_name)
with open(file_path, 'rb') as fh:
file_name = file_path[len(partials_dir) + 1:]
yield (file_name, normalize_newlines(fh.read().decode('utf-8')).replace('\n', ' '))
def clean_uploaded_files(self, prefix, files):
upload_str = prefix + "_upload"
has_upload = upload_str in files
if has_upload:
upload_file = files[upload_str]
log_script_name = upload_file.name
LOG.info('got upload %s', log_script_name)
if upload_file._size > 16 * units.Ki: # 16kb
msg = _('File exceeds maximum size (16kb)')
raise forms.ValidationError(msg)
else:
script = upload_file.read()
if script != "":
try:
normalize_newlines(script)
except Exception as e:
msg = _('There was a problem parsing the'
' %(prefix)s: %(error)s')
msg = msg % {'prefix': prefix,
'error': six.text_type(e)}
raise forms.ValidationError(msg)
return script
else:
return None
def handle(self, request, context):
custom_script = context.get('script_data', '')
image_id = context.get('image_id', None)
netids = context.get('network_id', None)
if netids:
nics = [{"net_id": netid} for netid in netids]
else:
nics = None
avail_zone = context.get('availability_zone', None)
if not avail_zone:
avail_zone = None
try:
mogan.server_create(request,
name=context['name'],
image=image_id,
flavor=context['flavor'],
key_name=context['keypair_id'],
user_data=normalize_newlines(custom_script),
nics=nics,
availability_zone=avail_zone,
server_count=int(context['count']))
return True
except Exception:
exceptions.handle(request)
return False