Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../proc/self/root/lib64/python3....
File: string.py
"""A collection of string constants.
[0] Fix | Delete
[1] Fix | Delete
Public module variables:
[2] Fix | Delete
[3] Fix | Delete
whitespace -- a string containing all ASCII whitespace
[4] Fix | Delete
ascii_lowercase -- a string containing all ASCII lowercase letters
[5] Fix | Delete
ascii_uppercase -- a string containing all ASCII uppercase letters
[6] Fix | Delete
ascii_letters -- a string containing all ASCII letters
[7] Fix | Delete
digits -- a string containing all ASCII decimal digits
[8] Fix | Delete
hexdigits -- a string containing all ASCII hexadecimal digits
[9] Fix | Delete
octdigits -- a string containing all ASCII octal digits
[10] Fix | Delete
punctuation -- a string containing all ASCII punctuation characters
[11] Fix | Delete
printable -- a string containing all ASCII characters considered printable
[12] Fix | Delete
[13] Fix | Delete
"""
[14] Fix | Delete
[15] Fix | Delete
__all__ = ["ascii_letters", "ascii_lowercase", "ascii_uppercase", "capwords",
[16] Fix | Delete
"digits", "hexdigits", "octdigits", "printable", "punctuation",
[17] Fix | Delete
"whitespace", "Formatter", "Template"]
[18] Fix | Delete
[19] Fix | Delete
import _string
[20] Fix | Delete
[21] Fix | Delete
# Some strings for ctype-style character classification
[22] Fix | Delete
whitespace = ' \t\n\r\v\f'
[23] Fix | Delete
ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
[24] Fix | Delete
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
[25] Fix | Delete
ascii_letters = ascii_lowercase + ascii_uppercase
[26] Fix | Delete
digits = '0123456789'
[27] Fix | Delete
hexdigits = digits + 'abcdef' + 'ABCDEF'
[28] Fix | Delete
octdigits = '01234567'
[29] Fix | Delete
punctuation = r"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
[30] Fix | Delete
printable = digits + ascii_letters + punctuation + whitespace
[31] Fix | Delete
[32] Fix | Delete
# Functions which aren't available as string methods.
[33] Fix | Delete
[34] Fix | Delete
# Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def".
[35] Fix | Delete
def capwords(s, sep=None):
[36] Fix | Delete
"""capwords(s [,sep]) -> string
[37] Fix | Delete
[38] Fix | Delete
Split the argument into words using split, capitalize each
[39] Fix | Delete
word using capitalize, and join the capitalized words using
[40] Fix | Delete
join. If the optional second argument sep is absent or None,
[41] Fix | Delete
runs of whitespace characters are replaced by a single space
[42] Fix | Delete
and leading and trailing whitespace are removed, otherwise
[43] Fix | Delete
sep is used to split and join the words.
[44] Fix | Delete
[45] Fix | Delete
"""
[46] Fix | Delete
return (sep or ' ').join(x.capitalize() for x in s.split(sep))
[47] Fix | Delete
[48] Fix | Delete
[49] Fix | Delete
####################################################################
[50] Fix | Delete
import re as _re
[51] Fix | Delete
from collections import ChainMap as _ChainMap
[52] Fix | Delete
[53] Fix | Delete
class _TemplateMetaclass(type):
[54] Fix | Delete
pattern = r"""
[55] Fix | Delete
%(delim)s(?:
[56] Fix | Delete
(?P<escaped>%(delim)s) | # Escape sequence of two delimiters
[57] Fix | Delete
(?P<named>%(id)s) | # delimiter and a Python identifier
[58] Fix | Delete
{(?P<braced>%(id)s)} | # delimiter and a braced identifier
[59] Fix | Delete
(?P<invalid>) # Other ill-formed delimiter exprs
[60] Fix | Delete
)
[61] Fix | Delete
"""
[62] Fix | Delete
[63] Fix | Delete
def __init__(cls, name, bases, dct):
[64] Fix | Delete
super(_TemplateMetaclass, cls).__init__(name, bases, dct)
[65] Fix | Delete
if 'pattern' in dct:
[66] Fix | Delete
pattern = cls.pattern
[67] Fix | Delete
else:
[68] Fix | Delete
pattern = _TemplateMetaclass.pattern % {
[69] Fix | Delete
'delim' : _re.escape(cls.delimiter),
[70] Fix | Delete
'id' : cls.idpattern,
[71] Fix | Delete
}
[72] Fix | Delete
cls.pattern = _re.compile(pattern, cls.flags | _re.VERBOSE)
[73] Fix | Delete
[74] Fix | Delete
[75] Fix | Delete
class Template(metaclass=_TemplateMetaclass):
[76] Fix | Delete
"""A string class for supporting $-substitutions."""
[77] Fix | Delete
[78] Fix | Delete
delimiter = '$'
[79] Fix | Delete
# r'[a-z]' matches to non-ASCII letters when used with IGNORECASE,
[80] Fix | Delete
# but without ASCII flag. We can't add re.ASCII to flags because of
[81] Fix | Delete
# backward compatibility. So we use local -i flag and [a-zA-Z] pattern.
[82] Fix | Delete
# See https://bugs.python.org/issue31672
[83] Fix | Delete
idpattern = r'(?-i:[_a-zA-Z][_a-zA-Z0-9]*)'
[84] Fix | Delete
flags = _re.IGNORECASE
[85] Fix | Delete
[86] Fix | Delete
def __init__(self, template):
[87] Fix | Delete
self.template = template
[88] Fix | Delete
[89] Fix | Delete
# Search for $$, $identifier, ${identifier}, and any bare $'s
[90] Fix | Delete
[91] Fix | Delete
def _invalid(self, mo):
[92] Fix | Delete
i = mo.start('invalid')
[93] Fix | Delete
lines = self.template[:i].splitlines(keepends=True)
[94] Fix | Delete
if not lines:
[95] Fix | Delete
colno = 1
[96] Fix | Delete
lineno = 1
[97] Fix | Delete
else:
[98] Fix | Delete
colno = i - len(''.join(lines[:-1]))
[99] Fix | Delete
lineno = len(lines)
[100] Fix | Delete
raise ValueError('Invalid placeholder in string: line %d, col %d' %
[101] Fix | Delete
(lineno, colno))
[102] Fix | Delete
[103] Fix | Delete
def substitute(*args, **kws):
[104] Fix | Delete
if not args:
[105] Fix | Delete
raise TypeError("descriptor 'substitute' of 'Template' object "
[106] Fix | Delete
"needs an argument")
[107] Fix | Delete
self, *args = args # allow the "self" keyword be passed
[108] Fix | Delete
if len(args) > 1:
[109] Fix | Delete
raise TypeError('Too many positional arguments')
[110] Fix | Delete
if not args:
[111] Fix | Delete
mapping = kws
[112] Fix | Delete
elif kws:
[113] Fix | Delete
mapping = _ChainMap(kws, args[0])
[114] Fix | Delete
else:
[115] Fix | Delete
mapping = args[0]
[116] Fix | Delete
# Helper function for .sub()
[117] Fix | Delete
def convert(mo):
[118] Fix | Delete
# Check the most common path first.
[119] Fix | Delete
named = mo.group('named') or mo.group('braced')
[120] Fix | Delete
if named is not None:
[121] Fix | Delete
return str(mapping[named])
[122] Fix | Delete
if mo.group('escaped') is not None:
[123] Fix | Delete
return self.delimiter
[124] Fix | Delete
if mo.group('invalid') is not None:
[125] Fix | Delete
self._invalid(mo)
[126] Fix | Delete
raise ValueError('Unrecognized named group in pattern',
[127] Fix | Delete
self.pattern)
[128] Fix | Delete
return self.pattern.sub(convert, self.template)
[129] Fix | Delete
[130] Fix | Delete
def safe_substitute(*args, **kws):
[131] Fix | Delete
if not args:
[132] Fix | Delete
raise TypeError("descriptor 'safe_substitute' of 'Template' object "
[133] Fix | Delete
"needs an argument")
[134] Fix | Delete
self, *args = args # allow the "self" keyword be passed
[135] Fix | Delete
if len(args) > 1:
[136] Fix | Delete
raise TypeError('Too many positional arguments')
[137] Fix | Delete
if not args:
[138] Fix | Delete
mapping = kws
[139] Fix | Delete
elif kws:
[140] Fix | Delete
mapping = _ChainMap(kws, args[0])
[141] Fix | Delete
else:
[142] Fix | Delete
mapping = args[0]
[143] Fix | Delete
# Helper function for .sub()
[144] Fix | Delete
def convert(mo):
[145] Fix | Delete
named = mo.group('named') or mo.group('braced')
[146] Fix | Delete
if named is not None:
[147] Fix | Delete
try:
[148] Fix | Delete
return str(mapping[named])
[149] Fix | Delete
except KeyError:
[150] Fix | Delete
return mo.group()
[151] Fix | Delete
if mo.group('escaped') is not None:
[152] Fix | Delete
return self.delimiter
[153] Fix | Delete
if mo.group('invalid') is not None:
[154] Fix | Delete
return mo.group()
[155] Fix | Delete
raise ValueError('Unrecognized named group in pattern',
[156] Fix | Delete
self.pattern)
[157] Fix | Delete
return self.pattern.sub(convert, self.template)
[158] Fix | Delete
[159] Fix | Delete
[160] Fix | Delete
[161] Fix | Delete
########################################################################
[162] Fix | Delete
# the Formatter class
[163] Fix | Delete
# see PEP 3101 for details and purpose of this class
[164] Fix | Delete
[165] Fix | Delete
# The hard parts are reused from the C implementation. They're exposed as "_"
[166] Fix | Delete
# prefixed methods of str.
[167] Fix | Delete
[168] Fix | Delete
# The overall parser is implemented in _string.formatter_parser.
[169] Fix | Delete
# The field name parser is implemented in _string.formatter_field_name_split
[170] Fix | Delete
[171] Fix | Delete
class Formatter:
[172] Fix | Delete
def format(*args, **kwargs):
[173] Fix | Delete
if not args:
[174] Fix | Delete
raise TypeError("descriptor 'format' of 'Formatter' object "
[175] Fix | Delete
"needs an argument")
[176] Fix | Delete
self, *args = args # allow the "self" keyword be passed
[177] Fix | Delete
try:
[178] Fix | Delete
format_string, *args = args # allow the "format_string" keyword be passed
[179] Fix | Delete
except ValueError:
[180] Fix | Delete
if 'format_string' in kwargs:
[181] Fix | Delete
format_string = kwargs.pop('format_string')
[182] Fix | Delete
import warnings
[183] Fix | Delete
warnings.warn("Passing 'format_string' as keyword argument is "
[184] Fix | Delete
"deprecated", DeprecationWarning, stacklevel=2)
[185] Fix | Delete
else:
[186] Fix | Delete
raise TypeError("format() missing 1 required positional "
[187] Fix | Delete
"argument: 'format_string'") from None
[188] Fix | Delete
return self.vformat(format_string, args, kwargs)
[189] Fix | Delete
[190] Fix | Delete
def vformat(self, format_string, args, kwargs):
[191] Fix | Delete
used_args = set()
[192] Fix | Delete
result, _ = self._vformat(format_string, args, kwargs, used_args, 2)
[193] Fix | Delete
self.check_unused_args(used_args, args, kwargs)
[194] Fix | Delete
return result
[195] Fix | Delete
[196] Fix | Delete
def _vformat(self, format_string, args, kwargs, used_args, recursion_depth,
[197] Fix | Delete
auto_arg_index=0):
[198] Fix | Delete
if recursion_depth < 0:
[199] Fix | Delete
raise ValueError('Max string recursion exceeded')
[200] Fix | Delete
result = []
[201] Fix | Delete
for literal_text, field_name, format_spec, conversion in \
[202] Fix | Delete
self.parse(format_string):
[203] Fix | Delete
[204] Fix | Delete
# output the literal text
[205] Fix | Delete
if literal_text:
[206] Fix | Delete
result.append(literal_text)
[207] Fix | Delete
[208] Fix | Delete
# if there's a field, output it
[209] Fix | Delete
if field_name is not None:
[210] Fix | Delete
# this is some markup, find the object and do
[211] Fix | Delete
# the formatting
[212] Fix | Delete
[213] Fix | Delete
# handle arg indexing when empty field_names are given.
[214] Fix | Delete
if field_name == '':
[215] Fix | Delete
if auto_arg_index is False:
[216] Fix | Delete
raise ValueError('cannot switch from manual field '
[217] Fix | Delete
'specification to automatic field '
[218] Fix | Delete
'numbering')
[219] Fix | Delete
field_name = str(auto_arg_index)
[220] Fix | Delete
auto_arg_index += 1
[221] Fix | Delete
elif field_name.isdigit():
[222] Fix | Delete
if auto_arg_index:
[223] Fix | Delete
raise ValueError('cannot switch from manual field '
[224] Fix | Delete
'specification to automatic field '
[225] Fix | Delete
'numbering')
[226] Fix | Delete
# disable auto arg incrementing, if it gets
[227] Fix | Delete
# used later on, then an exception will be raised
[228] Fix | Delete
auto_arg_index = False
[229] Fix | Delete
[230] Fix | Delete
# given the field_name, find the object it references
[231] Fix | Delete
# and the argument it came from
[232] Fix | Delete
obj, arg_used = self.get_field(field_name, args, kwargs)
[233] Fix | Delete
used_args.add(arg_used)
[234] Fix | Delete
[235] Fix | Delete
# do any conversion on the resulting object
[236] Fix | Delete
obj = self.convert_field(obj, conversion)
[237] Fix | Delete
[238] Fix | Delete
# expand the format spec, if needed
[239] Fix | Delete
format_spec, auto_arg_index = self._vformat(
[240] Fix | Delete
format_spec, args, kwargs,
[241] Fix | Delete
used_args, recursion_depth-1,
[242] Fix | Delete
auto_arg_index=auto_arg_index)
[243] Fix | Delete
[244] Fix | Delete
# format the object and append to the result
[245] Fix | Delete
result.append(self.format_field(obj, format_spec))
[246] Fix | Delete
[247] Fix | Delete
return ''.join(result), auto_arg_index
[248] Fix | Delete
[249] Fix | Delete
[250] Fix | Delete
def get_value(self, key, args, kwargs):
[251] Fix | Delete
if isinstance(key, int):
[252] Fix | Delete
return args[key]
[253] Fix | Delete
else:
[254] Fix | Delete
return kwargs[key]
[255] Fix | Delete
[256] Fix | Delete
[257] Fix | Delete
def check_unused_args(self, used_args, args, kwargs):
[258] Fix | Delete
pass
[259] Fix | Delete
[260] Fix | Delete
[261] Fix | Delete
def format_field(self, value, format_spec):
[262] Fix | Delete
return format(value, format_spec)
[263] Fix | Delete
[264] Fix | Delete
[265] Fix | Delete
def convert_field(self, value, conversion):
[266] Fix | Delete
# do any conversion on the resulting object
[267] Fix | Delete
if conversion is None:
[268] Fix | Delete
return value
[269] Fix | Delete
elif conversion == 's':
[270] Fix | Delete
return str(value)
[271] Fix | Delete
elif conversion == 'r':
[272] Fix | Delete
return repr(value)
[273] Fix | Delete
elif conversion == 'a':
[274] Fix | Delete
return ascii(value)
[275] Fix | Delete
raise ValueError("Unknown conversion specifier {0!s}".format(conversion))
[276] Fix | Delete
[277] Fix | Delete
[278] Fix | Delete
# returns an iterable that contains tuples of the form:
[279] Fix | Delete
# (literal_text, field_name, format_spec, conversion)
[280] Fix | Delete
# literal_text can be zero length
[281] Fix | Delete
# field_name can be None, in which case there's no
[282] Fix | Delete
# object to format and output
[283] Fix | Delete
# if field_name is not None, it is looked up, formatted
[284] Fix | Delete
# with format_spec and conversion and then used
[285] Fix | Delete
def parse(self, format_string):
[286] Fix | Delete
return _string.formatter_parser(format_string)
[287] Fix | Delete
[288] Fix | Delete
[289] Fix | Delete
# given a field_name, find the object it references.
[290] Fix | Delete
# field_name: the field being looked up, e.g. "0.name"
[291] Fix | Delete
# or "lookup[3]"
[292] Fix | Delete
# used_args: a set of which args have been used
[293] Fix | Delete
# args, kwargs: as passed in to vformat
[294] Fix | Delete
def get_field(self, field_name, args, kwargs):
[295] Fix | Delete
first, rest = _string.formatter_field_name_split(field_name)
[296] Fix | Delete
[297] Fix | Delete
obj = self.get_value(first, args, kwargs)
[298] Fix | Delete
[299] Fix | Delete
# loop through the rest of the field_name, doing
[300] Fix | Delete
# getattr or getitem as needed
[301] Fix | Delete
for is_attr, i in rest:
[302] Fix | Delete
if is_attr:
[303] Fix | Delete
obj = getattr(obj, i)
[304] Fix | Delete
else:
[305] Fix | Delete
obj = obj[i]
[306] Fix | Delete
[307] Fix | Delete
return obj, first
[308] Fix | Delete
[309] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function