Additional clean action to preprocess value before :meth:`to_python`
        method.

        Subclasses may define own clean_value method to allow additional clean
        actions like html cleanup, etc.
554284
def clean_value(self, value):
        '''
        
        '''
        # We have to clean before checking min/max length. It's done in
        # separate method to allow additional clean action in subclasses.
        if self.nontext_replacement is not None:
            value = replace_nontext(value, self.nontext_replacement)
        if self.strip:
            value = value.strip()
        return value
163964
def clean_text(value, topic=False):
    
    for x in PROFANITY_REPLACEMENTS:
        value = value.replace(x[0], x[1])

    for bbset in BBCODE_REPLACEMENTS:
        p = re.compile(bbset[0], re.DOTALL)
        value = p.sub(bbset[1], value)

    bleached = bleach.clean(value, tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRIBUTES, strip=True)

    # We want to retain markdown quotes and we'll be running bleach again in format_post.
    bleached = bleached.replace('&gt;', '>').replace('&amp;', '&')
    return bleached
1056656
def Prepend(self, **kw):
        
        kw = copy_non_reserved_keywords(kw)
        for key, val in kw.items():
            # It would be easier on the eyes to write this using
            # "continue" statements whenever we finish processing an item,
            # but Python 1.5.2 apparently doesn't let you use "continue"
            # within try:-except: blocks, so we have to nest our code.
            try:
                orig = self._dict[key]
            except KeyError:
                # No existing variable in the environment, so just set
                # it to the new value.
                self._dict[key] = val
            else:
                try:
                    # Check if the original looks like a dictionary.
                    # If it is, we can't just try adding the value because
                    # dictionaries don't have __add__() methods, and
                    # things like UserList will incorrectly coerce the
                    # original dict to a list (which we don't want).
                    update_dict = orig.update
                except AttributeError:
                    try:
                        # Most straightforward:  just try to add them
                        # together.  This will work in most cases, when the
                        # original and new values are of compatible types.
                        self._dict[key] = val + orig
                    except (KeyError, TypeError):
                        try:
                            # Check if the added value is a list.
                            add_to_val = val.append
                        except AttributeError:
                            # The added value isn't a list, but the
                            # original is (by process of elimination),
                            # so insert the the new value in the original
                            # (if there's one to insert).
                            if val:
                                orig.insert(0, val)
                        else:
                            # The added value is a list, so append
                            # the original to it (if there's a value
                            # to append).
                            if orig:
                                add_to_val(orig)
                            self._dict[key] = val
                else:
                    # The original looks like a dictionary, so update it
                    # based on what we think the value looks like.
                    if SCons.Util.is_List(val):
                        for v in val:
                            orig[v] = None
                    else:
                        try:
                            update_dict(val)
                        except (AttributeError, TypeError, ValueError):
                            if SCons.Util.is_Dict(val):
                                for k, v in val.items():
                                    orig[k] = v
                            else:
                                orig[val] = None
        self.scanner_map_delete(kw)
642237
def to_python(self, value: Optional[str]) -> Optional[Any]:
        
        # https://docs.djangoproject.com/en/1.8/howto/custom-model-fields/
        # log.debug("to_python: {}, {}", value, type(value))
        if isinstance(value, datetime.datetime):
            return value
        if value is None:
            return value
        if value == '':
            return None
        return iso_string_to_python_datetime(value)
213106
def clean(inputline, strip=True, rem_indents=True, encoding_errors="replace"):
    """"""
    stdout_encoding = get_encoding(sys.stdout)
    inputline = str(inputline)
    if rem_indents:
        inputline = inputline.replace(openindent, "").replace(closeindent, "")
    if strip:
        inputline = inputline.strip()
    return inputline.encode(stdout_encoding, encoding_errors).decode(stdout_encoding)
1268265
def _py_clean(parameter, tab):
    
    if "out" in parameter.direction:
        if parameter.D > 0:
            if ":" in parameter.dimension and ("allocatable" in parameter.modifiers or
                                               "pointer" in parameter.modifiers):
                if parameter.D == 1:
                    clarray = "as_array({0}_o, ({0}_0.value,))".format(parameter.lname)
                else:
                    #We have to reverse the order of the indices because of the way that
                    #fortran orders the array elements. We undo this using a transpose in
                    #the ftypes.as_array() so that the elements are ordered correctly.
                    splice = ', '.join(reversed(["{0}_{1:d}.value".format(parameter.lname, i)
                                                 for i in range(parameter.D)]))       
                    clarray = "as_array({0}_o, ({1}))".format(parameter.lname, splice)

                assign = '{0}result.add("{1}", {2}, {1}_ft)'.format(tab, parameter.lname, clarray)
                fstr = _py_ftype(parameter, tab)
                    
                #For optional out parameters, we won't necessarily have anything to report.
                return _py_check_optional(parameter, tab, [fstr, assign])
            else:
                return '{1}result.add("{0}", {0}_a)'.format(parameter.lname, tab)
        else: #This handles scalar (inout) and (out) variables.
            return '{1}result.add("{0}", {0}_c.value)'.format(parameter.lname, tab)
1324493
def pygmentify(value, **kwargs):
    """"""
    soup = BeautifulSoup(value, 'html.parser')
    for pre in soup.find_all('pre'):

        # Get code
        code = ''.join([to_string(item) for item in pre.contents])
        code = code.replace('&lt;', '<')
        code = code.replace('&gt;', '>')
        code = code.replace('&#39;', "'")
        code = code.replace('&quot;', '"')
        code = code.replace('&amp;', '&')

        # Get lexer by language
        class_list = pre.get('class', [])
        lexers = []
        options = {
            'stripall': True
        }

        # Collect all found lexers
        for c in class_list:
            try:
                lexers.append(get_lexer_by_name(c, **options))
            except ClassNotFound:
                pass

        # Get first lexer match or none
        try:
            lexer = lexers[0]
        except IndexError:
            lexer = None

        # If no lexer, try guessing
        if lexer is None:
            try:
                lexer = guess_lexer(pre.text, **options)
                class_list += [alias for alias in lexer.aliases]
            except ClassNotFound:
                pass

        if lexer is not None:

            # Get formatter
            formatter = HtmlFormatter(**kwargs)

            # Highlight code
            highlighted = highlight(code, lexer, formatter)
            class_string = ' '.join([c for c in class_list])
            highlighted = highlighted.replace(
                '<div class="%s"><pre>' % kwargs['cssclass'],
                '<div class="%s"><pre class="%s">' % (kwargs['cssclass'], class_string)
            )
            pre.replace_with(highlighted)

    return soup.decode(formatter=None).strip()
655524
def to_python(self, value=None):
        

        if value is None:
            return self.default

        if self.resolve:
            return self.resolve(value)
        return value
1207142
def to_python(self, value):
        '''
        
        '''
        try:
            if value is None and self._default is not None:
                return self.default
            self._check_required(value)
            if not isinstance(value, self._python_type):
                value = self._import(value)
            return value
        except ValueError as ex:
            raise ValueError(ex, self._errors['to_python'])
954788
def clean(self):
        
        # Calls handle_starttag, handle_endtag, and handle_data
        self.feed()

        # Clean up any parent tags left open
        if self.current_parent_element['tag'] != '':
            self.cleaned_html += '</{}>'.format(self.current_parent_element['tag'])

        # Remove empty <p> added after lists
        self.cleaned_html = re.sub(r'(</[u|o]l>)<p></p>', r'\g<1>', self.cleaned_html)

        self._remove_pre_formatting()

        return self.cleaned_html
777666
def dom_processing(self, value):
        
        if value == self._defaults['domProcessing'] and 'domProcessing' in self._values:
            del self._values['domProcessing']
        else:
            self._values['domProcessing'] = value
714921
def clean(self, value):
        """"""
        if (
            self.base_type is not None and
            value is not None and
            not isinstance(value, self.base_type)
        ):
            if isinstance(self.base_type, tuple):
                allowed_types = [typ.__name__ for typ in self.base_type]
                allowed_types_text = ' or '.join(allowed_types)
            else:
                allowed_types_text = self.base_type.__name__
            err_msg = 'Value must be of %s type.' % allowed_types_text
            raise ValidationError(err_msg)

        if not self.has_value(value):
            if self.default is not None:
                raise StopValidation(self.default)

            if self.required:
                raise ValidationError('This field is required.')
            else:
                raise StopValidation(self.blank_value)

        return value
90267
def _preprocess_override(meth_types, base_types, meth_argspec, base_argspec):
    
    try:
        base_kw = base_argspec.keywords
        kw = meth_argspec.keywords
    except AttributeError:
        base_kw = base_argspec.varkw
        kw = meth_argspec.varkw
    try:
        kwonly = meth_argspec.kwonlyargs
        base_kwonly = base_argspec.kwonlyargs
    except AttributeError:
        kwonly = None
        base_kwonly = None
    if meth_argspec.varargs is None and base_argspec.varargs is None \
            and kw is None and base_kw is None \
            and kwonly is None and base_kwonly is None \
            and (meth_argspec.defaults is None or \
                len(meth_argspec.args) == len(base_argspec.args)):
        return meth_types, base_types
    arg_types = type_util.get_Tuple_params(meth_types[0])
    base_arg_types = type_util.get_Tuple_params(base_types[0])
    kw_off = len(meth_argspec.args)-1 # -1 for self
    if not meth_argspec.defaults is None and base_argspec.varargs is None:
        kw_off -= len(meth_argspec.defaults)
    base_kw_off = len(base_argspec.args)
    if base_argspec.varargs is None:
        base_kw_off -= 1 # one decrement anyway for self
    arg_types2 = list(arg_types[:kw_off])
    base_arg_types2 = list(base_arg_types[:base_kw_off])
    base_vargtype = None
    vargtype = None
    base_argnames = None
    argnames = None
    if not base_argspec.varargs is None or not base_kw is None:
        base_argnames = util.getargnames(base_argspec)[1:]
        if not base_argspec.varargs is None:
            base_vargtype = base_arg_types[base_argnames.index(base_argspec.varargs)]
            # Should have been already checked:
            assert not meth_argspec.varargs is None
    if not meth_argspec.varargs is None or not kw is None:
        argnames = util.getargnames(meth_argspec)[1:]
        if not meth_argspec.varargs is None:
            vargtype = arg_types[argnames.index(meth_argspec.varargs)]
    if not meth_argspec.defaults is None:
        pos = 0
        if not base_argspec.varargs is None:
            # fill up parent's types with varg type to account for child's defaults
            while len(arg_types2) > len(base_arg_types2):
                base_arg_types2.append(base_vargtype)
            base_arg_types2.append(base_vargtype) # restore one more for the actual vargtype
        else:
            # parent has no vargtype, so fill up child with default-types afap
            while len(arg_types2) < len(base_arg_types2) and \
                    pos < len(meth_argspec.defaults):
                arg_types2.append(arg_types[kw_off+pos])
                pos += 1
    while len(arg_types2) < len(base_arg_types2):
        arg_types2.append(vargtype)
    if not kw is None:
        kw_type = arg_types[argnames.index(kw)]
    if not base_kwonly is None:
        for name in base_kwonly:
            base_arg_types2.append(base_arg_types[base_argnames.index(name)])
            if name in argnames:
                arg_types2.append(arg_types[argnames.index(name)])
            else:
                arg_types2.append(kw_type)
    if not base_kw is None:
        base_arg_types2.append(base_arg_types[base_argnames.index(base_kw)])
        arg_types2.append(kw_type)
    return (typing.Tuple[tuple(arg_types2)], meth_types[1]), \
            (typing.Tuple[tuple(base_arg_types2)], base_types[1])
364513
def apply_pre_filters(instance, html):
    
    # Allow pre processing. Typical use-case is HTML syntax correction.
    for post_func in appsettings.PRE_FILTER_FUNCTIONS:
        html = post_func(instance, html)

    return html
931479
def process_pure_python(self, content):
        
        output = []
        savefig = False # keep up with this to clear figure
        multiline = False # to handle line continuation
        multiline_start = None
        fmtin = self.promptin

        ct = 0

        for lineno, line in enumerate(content):

            line_stripped = line.strip()
            if not len(line):
                output.append(line)
                continue

            # handle decorators
            if line_stripped.startswith('@'):
                output.extend([line])
                if 'savefig' in line:
                    savefig = True # and need to clear figure
                continue

            # handle comments
            if line_stripped.startswith('#'):
                output.extend([line])
                continue

            # deal with lines checking for multiline
            continuation  = u'   %s:'% ''.join(['.']*(len(str(ct))+2))
            if not multiline:
                modified = u"%s %s" % (fmtin % ct, line_stripped)
                output.append(modified)
                ct += 1
                try:
                    ast.parse(line_stripped)
                    output.append(u'')
                except Exception: # on a multiline
                    multiline = True
                    multiline_start = lineno
            else: # still on a multiline
                modified = u'%s %s' % (continuation, line)
                output.append(modified)
                try:
                    mod = ast.parse(
                            '\n'.join(content[multiline_start:lineno+1]))
                    if isinstance(mod.body[0], ast.FunctionDef):
                        # check to see if we have the whole function
                        for element in mod.body[0].body:
                            if isinstance(element, ast.Return):
                                multiline = False
                    else:
                        output.append(u'')
                        multiline = False
                except Exception:
                    pass

            if savefig: # clear figure if plotted
                self.ensure_pyplot()
                self.process_input_line('plt.clf()', store_history=False)
                self.clear_cout()
                savefig = False

        return output
1202877
def clean(self, value):
		

		# If the value is None and it's optional, return as is
		if value is None and self._optional:
			return None

		# If it's an ANY, there is no reasonable expectation that we know what
		#	the value should be, so we return it as is
		if self._type == 'any':
			pass

		# Else if it's a basic string type
		elif self._type in ['base64', 'ip', 'string', 'uuid']:

			# And not already a string
			if not isinstance(value, basestring):
				value = str(value)

		# Else if it's a BOOL just check if the value flags as positive
		elif self._type == 'bool':

			# If it's specifically a string, it needs to match a specific
			#	pattern to be true
			if isinstance(value, basestring):
				value = (value in ('true', 'True', 'TRUE', 't', 'T', 'yes', 'Yes', 'YES', 'y', 'Y', 'x', '1') and True or False);

			# Else
			else:
				value = (value and True or False)

		# Else if it's a date type
		elif self._type == 'date':

			# If it's a python type, use strftime on it
			if isinstance(value, (datetime.date, datetime.datetime)):
				value = value.strftime('%Y-%m-%d')

			# Else if it's already a string
			elif isinstance(value, basestring):
				pass

			# Else convert it to a string
			else:
				value = str(value)

		# Else if it's a datetime type
		elif self._type == 'datetime':

			# If it's a python type, use strftime on it
			if isinstance(value, datetime.datetime):
				value = value.strftime('%Y-%m-%d %H:%M:%S')
			elif isinstance(value, datetime.date):
				value = '%s 00:00:00' % value.strftime('%Y-%m-%d')

			# Else if it's already a string
			elif isinstance(value, basestring):
				pass

			# Else convert it to a string
			else:
				value = str(value)

		# Else if it's a decimal
		elif self._type == 'decimal':

			# If it's not a decimal
			if not isinstance(value, Decimal):
				value = Decimal(value)

			# Convert it to a string
			value = '{0:f}'.format(value)

		# Else if it's a float
		elif self._type == 'float':
			value = float(value)

		# Else if it's an int type
		elif self._type in ['int', 'timestamp', 'uint']:

			# If the value is a string
			if isinstance(value, basestring):

				# If it starts with 0
				if value[0] == '0' and len(value) > 1:

					# If it's followed by X or x, it's hex
					if value[1] in ['x', 'X'] and len(value) > 2:
						value = int(value, 16)

					# Else it's octal
					else:
						value = int(value, 8)

				# Else it's base 10
				else:
					value = int(value)

			# Else if it's not an int already
			elif not isinstance(value, int):
				value = int(value)

		# Else if it's a JSON type
		elif self._type == 'json':

			# If it's already a string
			if isinstance(value, basestring):
				pass

			# Else, encode it
			else:
				value = JSON.encode(value)

		# Else if it's an md5 type
		elif self._type == 'md5':

			# If it's a python type, get the hexadecimal digest
			if isinstance(value, _MD5_TYPE):
				value = value.hexdigest()

			# Else if it's a string
			elif isinstance(value, basestring):
				pass

			# Else, try to convert it to a string
			else:
				value = str(value)

		# Else if it's a price type
		elif self._type == 'price':

			# If it's not already a Decimal
			if not isinstance(value, Decimal):
				value = Decimal(value)

			# Make sure its got 2 decimal places
			value = "{0:f}".format(value.quantize(Decimal('1.00')))

		# Else if it's a time type
		elif self._type == 'time':

			# If it's a python type, use strftime on it
			if isinstance(value, (datetime.time, datetime.datetime)):
				value = value.strftime('%H:%M:%S')

			# Else if it's already a string
			elif isinstance(value, basestring):
				pass

			# Else convert it to a string
			else:
				value = str(value)

		# Else we probably forgot to add a new type
		else:
			raise Exception('%s has not been added to .clean()' % self._type)

		# Return the cleaned value
		return value
1456
def _post_clean(self):
        
        super(NgModelFormMixin, self)._post_clean()
        if self._errors and self.prefix:
            self._errors = ErrorDict((self.add_prefix(name), value) for name, value in self._errors.items())