def clean(self): cleaned_data = self.cleaned_data edited_answer = cleaned_data.get("edited_answer") or "" needs_further_review = cleaned_data.get("needs_further_review") hidden = cleaned_data.get("hidden") if not edited_answer.strip() or hidden: # hidden self.instance.checked = True self.instance.hidden = True elif normalize_newlines(self.instance.original_answer) == normalize_newlines(edited_answer): # simply approved self.instance.checked = True else: # reviewed self.instance.checked = True self.instance.reviewed_answer = edited_answer if needs_further_review: self.instance.checked = False self.instance.hidden = False else: self.checked = True return cleaned_data
def test_normalize_newlines(self): self.assertEqual(text.normalize_newlines("abc\ndef\rghi\r\n"), "abc\ndef\nghi\n") self.assertEqual(text.normalize_newlines("\n\r\r\n\r"), "\n\n\n\n") self.assertEqual(text.normalize_newlines("abcdefghi"), "abcdefghi") self.assertEqual(text.normalize_newlines(""), "") self.assertEqual(text.normalize_newlines(lazystr("abc\ndef\rghi\r\n")), "abc\ndef\nghi\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 clean(self): reviewed_answer = self.cleaned_data.get("reviewed_answer") or "" needs_further_review = self.cleaned_data.get("needs_further_review") # if the answer was not edited, don't store the original answer again. if normalize_newlines(self.instance.original_answer) == normalize_newlines(reviewed_answer): self.cleaned_data["reviewed_answer"] = "" if not reviewed_answer.strip(): self.cleaned_data["hidden"] = True self.instance.checked = not needs_further_review return self.cleaned_data
def include_files(self): files = {} for form in self.cleaned_data: if form.get("DELETE"): continue files[form["path"]] = normalize_newlines(form["content"]) return files
def handle(self, request, data): try: if(len(data['volume']) > 0): if(data['delete_on_terminate']): delete_on_terminate = 1 else: delete_on_terminate = 0 dev_mapping = {data['device_name']: ("%s::%s" % (data['volume'], delete_on_terminate))} else: dev_mapping = None api.server_create(request, data['name'], data['image_id'], data['flavor'], data.get('keypair'), normalize_newlines(data.get('user_data')), data.get('security_groups'), dev_mapping, instance_count=data.get('count')) messages.success(request, _('Instance "%s" launched.') % data["name"]) except: redirect = reverse("horizon:nova:images_and_snapshots:index") exceptions.handle(request, _('Unable to launch instance: %(exc)s'), redirect=redirect) return shortcuts.redirect('horizon:nova:instances_and_volumes:index')
def handle(self, request, context): custom_script = context.get("customization_script", "") # Determine volume mapping options if context.get("volume_type", None): if context["delete_on_terminate"]: del_on_terminate = 1 else: del_on_terminate = 0 mapping_opts = "%s::%s" % (context["volume_id"], del_on_terminate) dev_mapping = {context["device_name"]: mapping_opts} else: dev_mapping = None try: api.nova.server_create( request, context["name"], context["source_id"], context["flavor"], context["keypair_id"], normalize_newlines(custom_script), context["security_group_ids"], dev_mapping, instance_count=int(context["count"]), ) return True except: exceptions.handle(request) return False
def get_first_title_line(self): """ First line of title field. """ return slimdown( normalize_newlines(self.get_title()).partition("\n")[0] )
def linebreakslink(value, autoescape=True): """ Convert all newlines in a piece of plain text to HTML line breaks (``<br />``). """ autoescape = autoescape and not isinstance(value, SafeData) value = normalize_newlines(value) # if autoescape: # value = escape(value) item_list = [] for item in value.split('\n'): start_index = item.find('link<') if start_index != -1: file_record = item[0:start_index].split(' ')[1] start_file_record = item.find(file_record) + 6 link_str = item[start_index:] close_index = link_str.index('>') link = link_str[5:close_index] replace_string = item[start_file_record:start_index] item = item.replace(link, '') item = item.replace( replace_string, '<a href="/static/%s">%s</a>' % (link, replace_string)).replace('link<', '').replace('>>', '>') item_list.append(item) value = '<br>'.join(item_list) return mark_safe(value.replace('\\n', ''))
def keep_spacing(value, autoescape=None): autoescape = autoescape and not isinstance(value, SafeData) value = normalize_newlines(value) if autoescape: value = escape(value) value = mark_safe(value.replace(' ', ' ')) return mark_safe(value.replace('\n', '<br />' + ' '))
def handle(self, request, data): image_id = data['image_id'] tenant_id = data['tenant_id'] try: image = api.image_get_meta(request, image_id) flavor = api.flavor_get(request, data['flavor']) api.server_create(request, data['name'], image, flavor, data.get('key_name'), normalize_newlines(data.get('user_data')), data.get('security_groups')) msg = _('Instance was successfully launched') LOG.info(msg) messages.success(request, msg) return redirect( 'horizon:nova:instances_and_volumes:instances:index') except api_exceptions.ApiException, e: LOG.exception('ApiException while creating instances of image "%s"' % image_id) messages.error(request, _('Unable to launch instance: %s') % e.message)
def clean_html(text): """ Clean the given HTML. Specifically, do the following: * Convert <b> and <i> to <strong> and <em>. * Encode all ampersands correctly. * Remove all "target" attributes from <a> tags. * Remove extraneous HTML, such as presentational tags that open and immediately close and <br clear="all">. * Convert hard-coded bullets into HTML unordered lists. * Remove stuff like "<p> </p>", but only if it's at the bottom of the text. """ from django.utils.text import normalize_newlines text = normalize_newlines(force_unicode(text)) text = re.sub(r"<(/?)\s*b\s*>", "<\\1strong>", text) text = re.sub(r"<(/?)\s*i\s*>", "<\\1em>", text) text = fix_ampersands(text) # Remove all target="" attributes from <a> tags. text = link_target_attribute_re.sub("\\1", text) # Trim stupid HTML such as <br clear="all">. text = html_gunk_re.sub("", text) # Convert hard-coded bullets into HTML unordered lists. def replace_p_tags(match): s = match.group().replace("</p>", "</li>") for d in DOTS: s = s.replace("<p>%s" % d, "<li>") return "<ul>\n%s\n</ul>" % s text = hard_coded_bullets_re.sub(replace_p_tags, text) # Remove stuff like "<p> </p>", but only if it's at the bottom # of the text. text = trailing_empty_content_re.sub("", text) return text
def handle(self, request, data): try: if len(data["volume"]) > 0: if data["delete_on_terminate"]: delete_on_terminate = 1 else: delete_on_terminate = 0 dev_mapping = {data["device_name"]: ("%s::%s" % (data["volume"], delete_on_terminate))} else: dev_mapping = None api.server_create( request, data["name"], data["image_id"], data["flavor"], data.get("keypair"), normalize_newlines(data.get("user_data")), data.get("security_groups"), dev_mapping, instance_count=data.get("count"), ) messages.success(request, _('Instance "%s" launched.') % data["name"]) except: redirect = reverse("horizon:nova:images_and_snapshots:index") exceptions.handle(request, _("Unable to launch instance: %(exc)s"), redirect=redirect) return shortcuts.redirect("horizon:nova:instances_and_volumes:index")
def keep_spacing(value, autoescape=None): autoescape = autoescape and not isinstance(value, SafeData) value = normalize_newlines(value) if autoescape: value = escape(value) value = mark_safe(value.replace(" ", " ")) return mark_safe(value.replace("\n", "<br />"))
def render_forum_post(value): """ Replaces line breaks in plain text with appropriate HTML; a single newline becomes an HTML line break (``<br />``) and a new line followed by a blank line becomes a paragraph break (``</p>``). Replaces double asterisks surrounding text with bold tags IE: regular **bolded** regular will become regular <b>bolded</b> regular Replaces a single asterisks surrounding text with italicized tags IE: regular *italicized* regular will become regular <i>italicized</i> regular """ # autoescape = autoescape and not isinstance(value, SafeData) # return mark_safe(linebreaks(value, autoescape)) #replace line breaks value = normalize_newlines(force_text(value)) paras = re.split('\n{2,}', value) paras = ['<p>%s</p>' % escape(p).replace('\n', '<br />') for p in paras] value = '\n\n'.join(paras) #replace asterisks value = re.sub(r"\*\*(.*?)\*\*", r"<b>\1</b>", value) value = re.sub(r"\*(.*?)\*", r"<i>\1</i>", value) return mark_safe(value)
def __new__(mcs, name, bases, attrs): if bases: # Only subclasses of `Metric` if not attrs.get("label"): attrs["label"] = name if not attrs.get("slug"): attrs["slug"] = slugify(attrs["label"]) if not attrs.get("sql"): msg = 'Metric "%s" is missing a "sql" attribute or "sql" is empty.' raise ImproperlyConfigured(msg % name) docstring = attrs.get("__doc__") if docstring and docstring.strip(): docstring = normalize_newlines(force_str(docstring)) docstring = "\n".join(line.strip() for line in docstring.split("\n")) paras = re.split("\n{2,}", docstring) paras = [ "<p>%s</p>" % urlize(escape(p).replace("\n", " ").strip()) for p in paras ] attrs["description"] = "\n\n".join(paras) else: attrs["description"] = "" attrs["permission_name"] = "can_view_metric_%s" % attrs[ "slug"].replace("-", "_") attrs["permission_key"] = "postgres_metrics.%s" % attrs[ "permission_name"] return super().__new__(mcs, name, bases, attrs)
def store_statistics(self, form, save=True): raise NotImplementedError("this code was copypasted here, please clean up") if form.cleaned_data["is_json"]: try: data_json = json.loads(form.cleaned_data["data_json"]) except: data_json = {"data": [""], "headers": [""]} try: rows = data_json["data"] headers = data_json["headers"] except: rows = [[""]] # list: list: empty string headers = [""] try: row = rows[0] except IndexError: row = [""] if any(row): name_value = dict(zip(headers, row)) else: data = form.cleaned_data["data"] data = normalize_newlines(data) zipped = [] for row in data.split("\n"): try: name, value = (item.strip() for item in row.split(":")) except: continue zipped.append((name, value)) if any(zipped): name_value = dict(zipped) existing_name["name_value"] = name_value
def _launch(request, zone_id): data = request.POST address = data.get('address', None) if address is not None: try: api.proxy.server_network(self.request, address) except: msg = _("This address was in used.") exceptions.handle(request, msg) try: instance = api.proxy.server_create(request, data['name'], data['image_id'], data['flavors'], zone_id=None, key_name=None, user_data=normalize_newlines(''), security_groups=None, block_device_mapping=None, block_device_mapping_v2=None, nics=None, availability_zone=None, instance_count=int(data.get('count', 1)), #instance_count=1, admin_pass=None, disk_config=None, accessIPv4=None, net_type=int(data['net_type'])) except: LOG.error(traceback.format_exc()) return HttpResponse(status=400) else: return HttpResponse(json.dumps(instance.servers), content_type='application/json')
def _trim_text(text): text = normalize_newlines(text) lines = [l.replace('\t', ' ').strip() for l in text.split('\n')] lines = [re.sub(r' +', r' ', l) for l in lines] lines = [l for l in lines if l] # Ditch any lines beyond the maximum wordcount lines2 = [] running_total = 0 for line in lines: lines2.append(line) running_total += len(line.split(' ')) if running_total > max_words: break # Trim the last line if necessary if running_total > max_words * 1.1: lastwords = lines2.pop().split(' ') running_total = sum([len(l.split(' ')) for l in lines2]) gap = max_words - running_total if gap > 5: lastline = ' '.join(lastwords[:gap]) lines2.append(lastline) return '\n'.join(lines2)
def get_subtitle_lines(self): """ All but first line of the long title field. """ return slimdown( normalize_newlines(self.title).partition("\n")[2] )
def handle(self, request, context): custom_script = context.get('customization_script', '') # Determine volume mapping options if context.get('volume_type', None): if(context['delete_on_terminate']): del_on_terminate = 1 else: del_on_terminate = 0 mapping_opts = ("%s::%s" % (context['volume_id'], del_on_terminate)) dev_mapping = {context['device_name']: mapping_opts} else: dev_mapping = None try: api.nova.server_create(request, context['name'], context['source_id'], context['flavor'], context['keypair_id'], normalize_newlines(custom_script), context['security_group_ids'], dev_mapping, instance_count=int(context['count'])) return True except: exceptions.handle(request) return False
def handle(self, request, data): try: if(len(data['volume']) > 0): if(data['delete_on_terminate']): delete_on_terminate = 1 else: delete_on_terminate = 0 dev_mapping = {data['device_name']: ("%s::%s" % (data['volume'], delete_on_terminate))} else: dev_mapping = None api.server_create(request, data['name'], data['image_id'], data['flavor'], data.get('keypair'), normalize_newlines(data.get('user_data')), data.get('security_groups'), dev_mapping, instance_count=data.get('count')) messages.success(request, _('Instance "%s" launched.') % data["name"]) except: redirect = reverse("horizon:nova:images:index") exceptions.handle(request, _('Unable to launch instance: %(exc)s'), redirect=redirect) return shortcuts.redirect('horizon:nova:instances_and_volumes:index')
def schedule_tsv(self, session): cherrypy.response.headers['Content-Type'] = 'text/tsv' cherrypy.response.headers['Content-Disposition'] = 'attachment;filename=Schedule-{}.tsv'.format(int(localized_now().timestamp())) schedule = defaultdict(list) for event in session.query(Event).order_by('start_time').all(): # strip newlines from event descriptions event.description = normalize_newlines(event.description) event.description = event.description.replace('\n', ' ') # Guidebook wants a date event.date = event.start_time.strftime('%m/%d/%Y') # Guidebook wants an end time, not duration. # also, duration is in half hours. duration=1 means 30 minutes. event.end_time = event.start_time + timedelta(minutes = 30 * event.duration) # now just display the times in these fields, not dates event.end_time = event.end_time.strftime('%I:%M:%S %p') event.start_time = event.start_time.strftime('%I:%M:%S %p') schedule[event.location_label].append(event) return render('schedule/schedule.tsv', { 'schedule': sorted(schedule.items(), key=lambda tup: ORDERED_EVENT_LOCS.index(tup[1][0].location)) })
def clean_html(text): """ Clean the given HTML. Specifically, do the following: * Convert <b> and <i> to <strong> and <em>. * Encode all ampersands correctly. * Remove all "target" attributes from <a> tags. * Remove extraneous HTML, such as presentational tags that open and immediately close and <br clear="all">. * Convert hard-coded bullets into HTML unordered lists. * Remove stuff like "<p> </p>", but only if it's at the bottom of the text. """ # As clean_html is wrapped in allow_lazy, stacklevel 3 is more useful than 2. warnings.warn("The clean_html function is deprecated and will be removed in Django 1.8.", DeprecationWarning, stacklevel=3) text = normalize_newlines(text) text = re.sub(r'<(/?)\s*b\s*>', '<\\1strong>', text) text = re.sub(r'<(/?)\s*i\s*>', '<\\1em>', text) text = fix_ampersands(text) # Remove all target="" attributes from <a> tags. text = link_target_attribute_re.sub('\\1', text) # Trim stupid HTML such as <br clear="all">. text = html_gunk_re.sub('', text) # Convert hard-coded bullets into HTML unordered lists. def replace_p_tags(match): s = match.group().replace('</p>', '</li>') for d in DOTS: s = s.replace('<p>%s' % d, '<li>') return '<ul>\n%s\n</ul>' % s text = hard_coded_bullets_re.sub(replace_p_tags, text) # Remove stuff like "<p> </p>", but only if it's at the bottom # of the text. text = trailing_empty_content_re.sub('', text) return text
def clean_html(text): """ Clean the given HTML. Specifically, do the following: * Convert <b> and <i> to <strong> and <em>. * Encode all ampersands correctly. * Remove all "target" attributes from <a> tags. * Remove extraneous HTML, such as presentational tags that open and immediately close and <br clear="all">. * Convert hard-coded bullets into HTML unordered lists. * Remove stuff like "<p> </p>", but only if it's at the bottom of the text. """ from django.utils.text import normalize_newlines text = normalize_newlines(force_text(text)) text = re.sub(r'<(/?)\s*b\s*>', '<\\1strong>', text) text = re.sub(r'<(/?)\s*i\s*>', '<\\1em>', text) text = fix_ampersands(text) # Remove all target="" attributes from <a> tags. text = link_target_attribute_re.sub('\\1', text) # Trim stupid HTML such as <br clear="all">. text = html_gunk_re.sub('', text) # Convert hard-coded bullets into HTML unordered lists. def replace_p_tags(match): s = match.group().replace('</p>', '</li>') for d in DOTS: s = s.replace('<p>%s' % d, '<li>') return '<ul>\n%s\n</ul>' % s text = hard_coded_bullets_re.sub(replace_p_tags, text) # Remove stuff like "<p> </p>", but only if it's at the bottom # of the text. text = trailing_empty_content_re.sub('', text) return text
def wordify(input): input = strip_tags( input ) input = re.sub(r'&(?:\w+|#\d+);', ' ', force_unicode(input)) # replace entities by a space input = strip_punctuation( input ) input = normalize_newlines( input ) input = force_unicode( re.sub(r'\s+', ' ', input) ) input = input.lower() return input
def dropcap(value): value = normalize_newlines(force_text(value)) paras = re.split('\n{2,}', value) first, paras = paras[0], paras[1:] first = ['<p class="dropcap-first">%s</p>' % first.replace('\n', '<br />')] paras = ['<p>%s</p>' % p.replace('\n', '<br />') for p in paras] paras = first + paras return '\n\n'.join(paras)
def quote_post(self, post): """ Returns a raw post body which quotes the given Post. """ return u'%s wrote:\n\n%s\n\n' % ( escape(post.user.username), quote_post_re.sub('> ', wrap(normalize_newlines(post.body), 80)), )
def remove_newlines(text): """ Removes all newline characters from a block of text. """ # First normalize the newlines using Django's nifty utility normalized_text = normalize_newlines(text) # Then simply remove the newlines like so. return mark_safe(normalized_text.replace('\n', ' '))
def the_candidatesv2(request): lelection = Election.objects.get(name='Marruecos2014') list_candidat = [] lg = translation.get_language() the_candidates = lelection.can_election.candidate_set.all() if lg == 'fr': bc = Background.objects.filter(pk__in=[1, 2, 6]) for OneCandidate in the_candidates: back_dict = [] extras = OneCandidate.backgroundcandidate_set.filter( background__in=bc).order_by('background__id') result = "" for extra in extras: result += normalize_newlines(extra.value).replace('\n', ' ') + u" " back_dict.append(extra.value) candidat_dict = { 'label': OneCandidate.name + u" " + mark_safe(result), 'ville': back_dict[1], 'parti': back_dict[0], 'commission': back_dict[2], 'name': OneCandidate.name, 'value': OneCandidate.slug } list_candidat.append(candidat_dict) else: bc = Background.objects.filter(pk__in=[10, 11, 15, 19]) for OneCandidate in the_candidates: back_dict = [] extras = OneCandidate.backgroundcandidate_set.filter( background__in=bc).order_by('background__id') result = "" for extra in extras: result += normalize_newlines(extra.value).replace('\n', ' ') + u" " back_dict.append(extra.value) candidat_dict = { 'label': back_dict[3] + u" " + mark_safe(result), 'ville': back_dict[1], 'parti': back_dict[0], 'commission': back_dict[2], 'name': back_dict[3], 'value': OneCandidate.slug } list_candidat.append(candidat_dict) return HttpResponse(json.dumps(list_candidat), mimetype="application/json")
def spacify(value, autoescape=None): autoescape = autoescape and not isinstance(value, SafeData) value = normalize_newlines(value) if autoescape: value = escape(value) value = mark_safe(value.replace(' ', ' ')) #return mark_safe(value.replace('\n', '<br />')) return mark_safe(value)
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 data-parnum="%s">%s</p>' % (i, escape(p).replace('\n', '<br />')) for i, p in enumerate(paras)] else: paras = ['<p data-parnum="%s">%s</p>' % (i, p.replace('\n', '<br />')) for i, p in enumerate(paras)] return '\n\n'.join(paras)
def linebreaktex(value, autoescape=True): """ Convert all newlines in a piece of plain text to HTML line breaks """ autoescape = autoescape and not isinstance(value, SafeData) value = normalize_newlines(value) if autoescape: value = escape(value) return mark_safe(value.replace('\n', '\\ \n'))
def clean(self, value): super(MultiEmailField, self).clean(value) addresses = normalize_newlines(value).split('\n') addresses.sort() ret = '' for a in addresses: if a: ret += '''%s\n''' % a return ret
def stdout_colored(self): stdout = self.stdout if stdout: stdout = normalize_newlines(stdout) stdout = stdout.replace('\n', '<br />') stdout = stdout.replace('[32m', '<span style="color:green" >') stdout = stdout.replace('[31m', '<span style="color:red" >') stdout = stdout.replace('[0m', '</span>') return stdout
def code_style(value, autoescape=True): if autoescape: esc = conditional_escape else: esc = lambda x: x value = normalize_newlines(esc(value)) value = value.replace(' ', ' ') value = value.replace('\n', '<br />') return mark_safe(value)
def linebreaks(value, autoescape=False): """Convert newlines into <p> and <br>s.""" value = normalize_newlines(value) paras = re.split('\n{2,}', str(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 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 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 linebreaks(value, autoescape=False): """Convert newlines into <p> and <br>s.""" value = normalize_newlines(value) paras = re.split("\n{2,}", str(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 standardize_report_output_field(field_content): # Normalize newlines replaces \r\n from windows with \n standardized_text = normalize_newlines(field_content) # Replace blank fields with "N/A" if len(standardized_text) == 0: standardized_text = "N/A" return standardized_text
def render_message(template_name, context_dict, remove_newlines=False): """ Shortcut method for rendering message templates, such as the ones used for activation emails. """ message = get_template(template_name).render(Context(context_dict, autoescape=False)) if remove_newlines: message = normalize_newlines(message).replace('\n', '') return message
def handle(self, request, context): custom_script = context.get('customization_script', '') dev_mapping_1 = None dev_mapping_2 = None image_id = '' # Determine volume mapping options source_type = context.get('source_type', None) if source_type in ['image_id', 'instance_snapshot_id']: image_id = context['source_id'] elif source_type in ['volume_id', 'volume_snapshot_id']: dev_mapping_1 = {context['device_name']: '%s::%s' % (context['source_id'], int(bool(context['delete_on_terminate'])))} elif source_type == 'volume_image_id': dev_mapping_2 = [ {'device_name': str(context['device_name']), 'source_type': 'image', 'destination_type': 'volume', 'delete_on_termination': int(bool(context['delete_on_terminate'])), 'uuid': context['source_id'], 'boot_index': '0', 'volume_size': context['volume_size'] } ] netids = context.get('network_id', None) if netids: nics = [{"net-id": netid, "v4-fixed-ip": ""} for netid in netids] else: nics = None avail_zone = context.get('availability_zone', None) try: api.nova.server_create(request, context['name'], image_id, context['flavor'], context['keypair_id'], normalize_newlines(custom_script), context['security_group_ids'], block_device_mapping=dev_mapping_1, block_device_mapping_v2=dev_mapping_2, nics=nics, availability_zone=avail_zone, instance_count=int(context['count']), admin_pass=context['admin_pass']) return True except Exception: exceptions.handle(request) return False
def linebreaksbr(value, autoescape=True): """ Converts all newlines in a piece of plain text to HTML line breaks (``<br />``). """ autoescape = autoescape and not isinstance(value, SafeData) value = normalize_newlines(value) if autoescape: value = escape(value) return mark_safe(value.replace('\n', '<br />'))
def inline_svg_text(text, font_size=None): text = normalize_newlines(text) text_lines = text.split("\n") return { "text_lines": text_lines, "font_size": font_size, "text_y": "{}".format(len(text_lines) / 2), }
def keep_spacing(value, autoescape=True): """ When a given `str` contains multiple spaces it keeps first space and others are replaces by . Newlines converts into HTML's <br>. """ autoescape = autoescape and not isinstance(value, SafeData) value = normalize_newlines(value) if autoescape: value = escape(value) return mark_safe(value.replace(' ', ' ').replace('\n', '<br />'))
def render_message(template_name, context_dict, remove_newlines=False): """ Shortcut method for rendering message templates, such as the ones used for activation emails. """ message = get_template(template_name).render( Context(context_dict, autoescape=False)) if remove_newlines: message = normalize_newlines(message).replace('\n', '') return message
def striptext(text): """ Removes line breaks, leading and trailing spaces, and any extraneous spaces, leaving only single spaces. """ text = normalize_newlines(text).replace('\n', '') text = text.strip() # Using re to remove extraneous spaces text = re.sub(' +', ' ', text) return text
def mailformat(text, width=78): text = normalize_newlines(text.strip()) text = re.sub('\n\n+', '\n\n', text) ps = [] for p in text.split('\n\n'): ps.append(textwrap.fill(p, width=width)) ps.append('') return '\n'.join(ps).strip()
def linebreaksn(value, autoescape=True): """ Convert all newlines in a piece of plain text to jQuery line breaks (`\n`). """ autoescape = autoescape and not isinstance(value, SafeData) value = normalize_newlines(value) if autoescape: value = escape(value) return mark_safe(value.replace('\n', '\\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) 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': e} raise forms.ValidationError(msg) return script else: return None
def render_content(self): def anchor_callback(match): return '<a href="{0}{1}" class="legaltext-anchor" target="_blank">{2}</a>'.format( self.legaltext_version.legaltext.get_absolute_url(), '#{0}'.format(match.group(1)) if match.group(1) else '', match.group(2)) content = ANCHOR_RE.sub( anchor_callback, normalize_newlines(self.content).replace('\n', '<br />')) return render_markdown(content).replace('<p>', '').replace('</p>', '')
def candidate_extra(candidate): try: bc = Background.objects.filter( Q(name__icontains="Parti politique") | Q(name="Circonscription")) extras = candidate.backgroundcandidate_set.filter(background__in=bc) result = "" for extra in extras: result += u" " + normalize_newlines(extra.value).replace('\n', ' ') return mark_safe(result) except Exception, e: return e.message
def linebreaks_wp(pee, autoescape=False): """Straight up port of http://codex.wordpress.org/Function_Reference/wpautop""" if (pee.strip() == ""): return "" pee = normalize_newlines(pee) pee = pee + "\n" pee = re.sub(r'<br />\s*<br />', "\n\n", pee) allblocks = r'(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|option|form|map|area|blockquote|address|math|style|input|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)' pee = re.sub(r'(<' + allblocks + '[^>]*>)', lambda m: "\n"+m.group(1) if m.group(1) else "\n", pee) pee = re.sub(r'(</' + allblocks + '>)', lambda m: m.group(1)+"\n\n" if m.group(1) else "\n\n", pee) #pee = pee.replace("\r\n", "\n") #pee = pee.replace("\r", "\n") #these taken care of by normalize_newlines if (pee.find("<object") != -1): pee = re.sub(r'\s*<param([^>]*)>\s*', lambda m: "<param%s>" % (m.group(1) if m.group(1) else "", ), pee) # no pee inside object/embed pee = re.sub(r'\s*</embed>\s*', '</embed>', pee) pee = re.sub(r"\n\n+", "\n\n", pee) # take care of duplicates pees = re.split(r'\n\s*\n', pee) # since PHP has a PREG_SPLIT_NO_EMPTY, may need to go through and drop any empty strings #pees = [p for p in pees if p] pee = "".join(["<p>%s</p>\n" % tinkle.strip('\n') for tinkle in pees]) pee = re.sub(r'<p>\s*</p>', '', pee) #under certain strange conditions it could create a P of entirely whitespace pee = re.sub(r'<p>([^<]+)</(div|address|form)>', lambda m: "<p>%s</p></%s>" % ((lambda x: x.group(1) if x.group(1) else "")(m), (lambda x: x.group(2) if x.group(2) else "")(m), ), pee) pee = re.sub(r'<p>\s*(</?' + allblocks + r'[^>]*>)\s*</p>', lambda m: m.group(1) if m.group(1) else "", pee) # don't pee all over a tag pee = re.sub(r"<p>(<li.+?)</p>", lambda m: m.group(1) if m.group(1) else "", pee) # problem with nested lists pee = re.sub(r'<p><blockquote([^>]*)>', lambda m: "<blockquote%s><p>" % (m.group(1) if m.group(1) else "",), pee, flags=re.IGNORECASE) pee = pee.replace('</blockquote></p>', '</p></blockquote>') pee = re.sub(r'<p>\s*(</?' + allblocks + r'[^>]*>)', lambda m: m.group(1) if m.group(1) else "", pee) pee = re.sub(r'(</?' + allblocks + '[^>]*>)\s*</p>', lambda m: m.group(1) if m.group(1) else "", pee) def _autop_newline_preservation_helper(matches): return matches.group(0).replace("\n", "<WPPreserveNewline />") pee = re.sub(r'<(script|style).*?</\1>', _autop_newline_preservation_helper, pee, flags=re.DOTALL) pee = re.sub(r'(?<!<br />)\s*\n', "<br />\n", pee) # make line breaks pee = pee.replace('<WPPreserveNewline />', "\n") pee = re.sub(r'(</?' + allblocks + '[^>]*>)\s*<br />', lambda m: m.group(1) if m.group(1) else "", pee) pee = re.sub(r'<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)', lambda m: m.group(1) if m.group(1) else "", pee) if (pee.find('<pre') != -1): def clean_pre(m): if m.group(1) and m.group(2): text = m.group(2) text = text.replace('<br />', '') text = text.replace('<p>', "\n") text = text.replace('</p>', '') text = m.group(1)+escape(text)+"</pre>" else: text = m.group(0) text = text.replace('<br />', '') text = text.replace('<p>', "\n") text = text.replace('</p>', '') return text pee = re.sub('(?is)(<pre[^>]*>)(.*?)</pre>', clean_pre, pee) pee = re.sub( r"\n</p>$", '</p>', pee) return pee