Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../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
_sentinel_dict = {}
[54] Fix | Delete
[55] Fix | Delete
class _TemplateMetaclass(type):
[56] Fix | Delete
pattern = r"""
[57] Fix | Delete
%(delim)s(?:
[58] Fix | Delete
(?P<escaped>%(delim)s) | # Escape sequence of two delimiters
[59] Fix | Delete
(?P<named>%(id)s) | # delimiter and a Python identifier
[60] Fix | Delete
{(?P<braced>%(bid)s)} | # delimiter and a braced identifier
[61] Fix | Delete
(?P<invalid>) # Other ill-formed delimiter exprs
[62] Fix | Delete
)
[63] Fix | Delete
"""
[64] Fix | Delete
[65] Fix | Delete
def __init__(cls, name, bases, dct):
[66] Fix | Delete
super(_TemplateMetaclass, cls).__init__(name, bases, dct)
[67] Fix | Delete
if 'pattern' in dct:
[68] Fix | Delete
pattern = cls.pattern
[69] Fix | Delete
else:
[70] Fix | Delete
pattern = _TemplateMetaclass.pattern % {
[71] Fix | Delete
'delim' : _re.escape(cls.delimiter),
[72] Fix | Delete
'id' : cls.idpattern,
[73] Fix | Delete
'bid' : cls.braceidpattern or cls.idpattern,
[74] Fix | Delete
}
[75] Fix | Delete
cls.pattern = _re.compile(pattern, cls.flags | _re.VERBOSE)
[76] Fix | Delete
[77] Fix | Delete
[78] Fix | Delete
class Template(metaclass=_TemplateMetaclass):
[79] Fix | Delete
"""A string class for supporting $-substitutions."""
[80] Fix | Delete
[81] Fix | Delete
delimiter = '$'
[82] Fix | Delete
# r'[a-z]' matches to non-ASCII letters when used with IGNORECASE, but
[83] Fix | Delete
# without the ASCII flag. We can't add re.ASCII to flags because of
[84] Fix | Delete
# backward compatibility. So we use the ?a local flag and [a-z] pattern.
[85] Fix | Delete
# See https://bugs.python.org/issue31672
[86] Fix | Delete
idpattern = r'(?a:[_a-z][_a-z0-9]*)'
[87] Fix | Delete
braceidpattern = None
[88] Fix | Delete
flags = _re.IGNORECASE
[89] Fix | Delete
[90] Fix | Delete
def __init__(self, template):
[91] Fix | Delete
self.template = template
[92] Fix | Delete
[93] Fix | Delete
# Search for $$, $identifier, ${identifier}, and any bare $'s
[94] Fix | Delete
[95] Fix | Delete
def _invalid(self, mo):
[96] Fix | Delete
i = mo.start('invalid')
[97] Fix | Delete
lines = self.template[:i].splitlines(keepends=True)
[98] Fix | Delete
if not lines:
[99] Fix | Delete
colno = 1
[100] Fix | Delete
lineno = 1
[101] Fix | Delete
else:
[102] Fix | Delete
colno = i - len(''.join(lines[:-1]))
[103] Fix | Delete
lineno = len(lines)
[104] Fix | Delete
raise ValueError('Invalid placeholder in string: line %d, col %d' %
[105] Fix | Delete
(lineno, colno))
[106] Fix | Delete
[107] Fix | Delete
def substitute(self, mapping=_sentinel_dict, /, **kws):
[108] Fix | Delete
if mapping is _sentinel_dict:
[109] Fix | Delete
mapping = kws
[110] Fix | Delete
elif kws:
[111] Fix | Delete
mapping = _ChainMap(kws, mapping)
[112] Fix | Delete
# Helper function for .sub()
[113] Fix | Delete
def convert(mo):
[114] Fix | Delete
# Check the most common path first.
[115] Fix | Delete
named = mo.group('named') or mo.group('braced')
[116] Fix | Delete
if named is not None:
[117] Fix | Delete
return str(mapping[named])
[118] Fix | Delete
if mo.group('escaped') is not None:
[119] Fix | Delete
return self.delimiter
[120] Fix | Delete
if mo.group('invalid') is not None:
[121] Fix | Delete
self._invalid(mo)
[122] Fix | Delete
raise ValueError('Unrecognized named group in pattern',
[123] Fix | Delete
self.pattern)
[124] Fix | Delete
return self.pattern.sub(convert, self.template)
[125] Fix | Delete
[126] Fix | Delete
def safe_substitute(self, mapping=_sentinel_dict, /, **kws):
[127] Fix | Delete
if mapping is _sentinel_dict:
[128] Fix | Delete
mapping = kws
[129] Fix | Delete
elif kws:
[130] Fix | Delete
mapping = _ChainMap(kws, mapping)
[131] Fix | Delete
# Helper function for .sub()
[132] Fix | Delete
def convert(mo):
[133] Fix | Delete
named = mo.group('named') or mo.group('braced')
[134] Fix | Delete
if named is not None:
[135] Fix | Delete
try:
[136] Fix | Delete
return str(mapping[named])
[137] Fix | Delete
except KeyError:
[138] Fix | Delete
return mo.group()
[139] Fix | Delete
if mo.group('escaped') is not None:
[140] Fix | Delete
return self.delimiter
[141] Fix | Delete
if mo.group('invalid') is not None:
[142] Fix | Delete
return mo.group()
[143] Fix | Delete
raise ValueError('Unrecognized named group in pattern',
[144] Fix | Delete
self.pattern)
[145] Fix | Delete
return self.pattern.sub(convert, self.template)
[146] Fix | Delete
[147] Fix | Delete
[148] Fix | Delete
[149] Fix | Delete
########################################################################
[150] Fix | Delete
# the Formatter class
[151] Fix | Delete
# see PEP 3101 for details and purpose of this class
[152] Fix | Delete
[153] Fix | Delete
# The hard parts are reused from the C implementation. They're exposed as "_"
[154] Fix | Delete
# prefixed methods of str.
[155] Fix | Delete
[156] Fix | Delete
# The overall parser is implemented in _string.formatter_parser.
[157] Fix | Delete
# The field name parser is implemented in _string.formatter_field_name_split
[158] Fix | Delete
[159] Fix | Delete
class Formatter:
[160] Fix | Delete
def format(self, format_string, /, *args, **kwargs):
[161] Fix | Delete
return self.vformat(format_string, args, kwargs)
[162] Fix | Delete
[163] Fix | Delete
def vformat(self, format_string, args, kwargs):
[164] Fix | Delete
used_args = set()
[165] Fix | Delete
result, _ = self._vformat(format_string, args, kwargs, used_args, 2)
[166] Fix | Delete
self.check_unused_args(used_args, args, kwargs)
[167] Fix | Delete
return result
[168] Fix | Delete
[169] Fix | Delete
def _vformat(self, format_string, args, kwargs, used_args, recursion_depth,
[170] Fix | Delete
auto_arg_index=0):
[171] Fix | Delete
if recursion_depth < 0:
[172] Fix | Delete
raise ValueError('Max string recursion exceeded')
[173] Fix | Delete
result = []
[174] Fix | Delete
for literal_text, field_name, format_spec, conversion in \
[175] Fix | Delete
self.parse(format_string):
[176] Fix | Delete
[177] Fix | Delete
# output the literal text
[178] Fix | Delete
if literal_text:
[179] Fix | Delete
result.append(literal_text)
[180] Fix | Delete
[181] Fix | Delete
# if there's a field, output it
[182] Fix | Delete
if field_name is not None:
[183] Fix | Delete
# this is some markup, find the object and do
[184] Fix | Delete
# the formatting
[185] Fix | Delete
[186] Fix | Delete
# handle arg indexing when empty field_names are given.
[187] Fix | Delete
if field_name == '':
[188] Fix | Delete
if auto_arg_index is False:
[189] Fix | Delete
raise ValueError('cannot switch from manual field '
[190] Fix | Delete
'specification to automatic field '
[191] Fix | Delete
'numbering')
[192] Fix | Delete
field_name = str(auto_arg_index)
[193] Fix | Delete
auto_arg_index += 1
[194] Fix | Delete
elif field_name.isdigit():
[195] Fix | Delete
if auto_arg_index:
[196] Fix | Delete
raise ValueError('cannot switch from manual field '
[197] Fix | Delete
'specification to automatic field '
[198] Fix | Delete
'numbering')
[199] Fix | Delete
# disable auto arg incrementing, if it gets
[200] Fix | Delete
# used later on, then an exception will be raised
[201] Fix | Delete
auto_arg_index = False
[202] Fix | Delete
[203] Fix | Delete
# given the field_name, find the object it references
[204] Fix | Delete
# and the argument it came from
[205] Fix | Delete
obj, arg_used = self.get_field(field_name, args, kwargs)
[206] Fix | Delete
used_args.add(arg_used)
[207] Fix | Delete
[208] Fix | Delete
# do any conversion on the resulting object
[209] Fix | Delete
obj = self.convert_field(obj, conversion)
[210] Fix | Delete
[211] Fix | Delete
# expand the format spec, if needed
[212] Fix | Delete
format_spec, auto_arg_index = self._vformat(
[213] Fix | Delete
format_spec, args, kwargs,
[214] Fix | Delete
used_args, recursion_depth-1,
[215] Fix | Delete
auto_arg_index=auto_arg_index)
[216] Fix | Delete
[217] Fix | Delete
# format the object and append to the result
[218] Fix | Delete
result.append(self.format_field(obj, format_spec))
[219] Fix | Delete
[220] Fix | Delete
return ''.join(result), auto_arg_index
[221] Fix | Delete
[222] Fix | Delete
[223] Fix | Delete
def get_value(self, key, args, kwargs):
[224] Fix | Delete
if isinstance(key, int):
[225] Fix | Delete
return args[key]
[226] Fix | Delete
else:
[227] Fix | Delete
return kwargs[key]
[228] Fix | Delete
[229] Fix | Delete
[230] Fix | Delete
def check_unused_args(self, used_args, args, kwargs):
[231] Fix | Delete
pass
[232] Fix | Delete
[233] Fix | Delete
[234] Fix | Delete
def format_field(self, value, format_spec):
[235] Fix | Delete
return format(value, format_spec)
[236] Fix | Delete
[237] Fix | Delete
[238] Fix | Delete
def convert_field(self, value, conversion):
[239] Fix | Delete
# do any conversion on the resulting object
[240] Fix | Delete
if conversion is None:
[241] Fix | Delete
return value
[242] Fix | Delete
elif conversion == 's':
[243] Fix | Delete
return str(value)
[244] Fix | Delete
elif conversion == 'r':
[245] Fix | Delete
return repr(value)
[246] Fix | Delete
elif conversion == 'a':
[247] Fix | Delete
return ascii(value)
[248] Fix | Delete
raise ValueError("Unknown conversion specifier {0!s}".format(conversion))
[249] Fix | Delete
[250] Fix | Delete
[251] Fix | Delete
# returns an iterable that contains tuples of the form:
[252] Fix | Delete
# (literal_text, field_name, format_spec, conversion)
[253] Fix | Delete
# literal_text can be zero length
[254] Fix | Delete
# field_name can be None, in which case there's no
[255] Fix | Delete
# object to format and output
[256] Fix | Delete
# if field_name is not None, it is looked up, formatted
[257] Fix | Delete
# with format_spec and conversion and then used
[258] Fix | Delete
def parse(self, format_string):
[259] Fix | Delete
return _string.formatter_parser(format_string)
[260] Fix | Delete
[261] Fix | Delete
[262] Fix | Delete
# given a field_name, find the object it references.
[263] Fix | Delete
# field_name: the field being looked up, e.g. "0.name"
[264] Fix | Delete
# or "lookup[3]"
[265] Fix | Delete
# used_args: a set of which args have been used
[266] Fix | Delete
# args, kwargs: as passed in to vformat
[267] Fix | Delete
def get_field(self, field_name, args, kwargs):
[268] Fix | Delete
first, rest = _string.formatter_field_name_split(field_name)
[269] Fix | Delete
[270] Fix | Delete
obj = self.get_value(first, args, kwargs)
[271] Fix | Delete
[272] Fix | Delete
# loop through the rest of the field_name, doing
[273] Fix | Delete
# getattr or getitem as needed
[274] Fix | Delete
for is_attr, i in rest:
[275] Fix | Delete
if is_attr:
[276] Fix | Delete
obj = getattr(obj, i)
[277] Fix | Delete
else:
[278] Fix | Delete
obj = obj[i]
[279] Fix | Delete
[280] Fix | Delete
return obj, first
[281] Fix | Delete
[282] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function