File manager - Edit - /home/newsbmcs.com/public_html/static/img/logo/text.py.tar
Back
usr/lib/python3/dist-packages/twisted/conch/insults/text.py 0000644 00000012467 15030034372 0020071 0 ustar 00 # -*- test-case-name: twisted.conch.test.test_text -*- # Copyright (c) Twisted Matrix Laboratories. # See LICENSE for details. """ Character attribute manipulation API. This module provides a domain-specific language (using Python syntax) for the creation of text with additional display attributes associated with it. It is intended as an alternative to manually building up strings containing ECMA 48 character attribute control codes. It currently supports foreground and background colors (black, red, green, yellow, blue, magenta, cyan, and white), intensity selection, underlining, blinking and reverse video. Character set selection support is planned. Character attributes are specified by using two Python operations: attribute lookup and indexing. For example, the string \"Hello world\" with red foreground and all other attributes set to their defaults, assuming the name twisted.conch.insults.text.attributes has been imported and bound to the name \"A\" (with the statement C{from twisted.conch.insults.text import attributes as A}, for example) one uses this expression:: A.fg.red[\"Hello world\"] Other foreground colors are set by substituting their name for \"red\". To set both a foreground and a background color, this expression is used:: A.fg.red[A.bg.green[\"Hello world\"]] Note that either A.bg.green can be nested within A.fg.red or vice versa. Also note that multiple items can be nested within a single index operation by separating them with commas:: A.bg.green[A.fg.red[\"Hello\"], " ", A.fg.blue[\"world\"]] Other character attributes are set in a similar fashion. To specify a blinking version of the previous expression:: A.blink[A.bg.green[A.fg.red[\"Hello\"], " ", A.fg.blue[\"world\"]]] C{A.reverseVideo}, C{A.underline}, and C{A.bold} are also valid. A third operation is actually supported: unary negation. This turns off an attribute when an enclosing expression would otherwise have caused it to be on. For example:: A.underline[A.fg.red[\"Hello\", -A.underline[\" world\"]]] A formatting structure can then be serialized into a string containing the necessary VT102 control codes with L{assembleFormattedText}. @see: L{twisted.conch.insults.text._CharacterAttributes} @author: Jp Calderone """ from incremental import Version from twisted.conch.insults import helper, insults from twisted.python import _textattributes from twisted.python.deprecate import deprecatedModuleAttribute flatten = _textattributes.flatten deprecatedModuleAttribute( Version("Twisted", 13, 1, 0), "Use twisted.conch.insults.text.assembleFormattedText instead.", "twisted.conch.insults.text", "flatten", ) _TEXT_COLORS = { "black": helper.BLACK, "red": helper.RED, "green": helper.GREEN, "yellow": helper.YELLOW, "blue": helper.BLUE, "magenta": helper.MAGENTA, "cyan": helper.CYAN, "white": helper.WHITE, } class _CharacterAttributes(_textattributes.CharacterAttributesMixin): """ Factory for character attributes, including foreground and background color and non-color attributes such as bold, reverse video and underline. Character attributes are applied to actual text by using object indexing-syntax (C{obj['abc']}) after accessing a factory attribute, for example:: attributes.bold['Some text'] These can be nested to mix attributes:: attributes.bold[attributes.underline['Some text']] And multiple values can be passed:: attributes.normal[attributes.bold['Some'], ' text'] Non-color attributes can be accessed by attribute name, available attributes are: - bold - blink - reverseVideo - underline Available colors are: 0. black 1. red 2. green 3. yellow 4. blue 5. magenta 6. cyan 7. white @ivar fg: Foreground colors accessed by attribute name, see above for possible names. @ivar bg: Background colors accessed by attribute name, see above for possible names. """ fg = _textattributes._ColorAttribute( _textattributes._ForegroundColorAttr, _TEXT_COLORS ) bg = _textattributes._ColorAttribute( _textattributes._BackgroundColorAttr, _TEXT_COLORS ) attrs = { "bold": insults.BOLD, "blink": insults.BLINK, "underline": insults.UNDERLINE, "reverseVideo": insults.REVERSE_VIDEO, } def assembleFormattedText(formatted): """ Assemble formatted text from structured information. Currently handled formatting includes: bold, blink, reverse, underline and color codes. For example:: from twisted.conch.insults.text import attributes as A assembleFormattedText( A.normal[A.bold['Time: '], A.fg.lightRed['Now!']]) Would produce "Time: " in bold formatting, followed by "Now!" with a foreground color of light red and without any additional formatting. @param formatted: Structured text and attributes. @rtype: L{str} @return: String containing VT102 control sequences that mimic those specified by C{formatted}. @see: L{twisted.conch.insults.text._CharacterAttributes} @since: 13.1 """ return _textattributes.flatten(formatted, helper._FormattingState(), "toVT102") attributes = _CharacterAttributes() __all__ = ["attributes", "flatten"] usr/local/CyberCP/lib/python3.10/site-packages/django/utils/text.py 0000644 00000040037 15030037151 0021006 0 ustar 00 import gzip import re import secrets import unicodedata from gzip import GzipFile from gzip import compress as gzip_compress from io import BytesIO from django.core.exceptions import SuspiciousFileOperation from django.utils.functional import SimpleLazyObject, keep_lazy_text, lazy from django.utils.regex_helper import _lazy_re_compile from django.utils.translation import gettext as _ from django.utils.translation import gettext_lazy, pgettext @keep_lazy_text def capfirst(x): """Capitalize the first letter of a string.""" if not x: return x if not isinstance(x, str): x = str(x) return x[0].upper() + x[1:] # ----- Begin security-related performance workaround ----- # We used to have, below # # re_words = _lazy_re_compile(r"<[^>]+?>|([^<>\s]+)", re.S) # # But it was shown that this regex, in the way we use it here, has some # catastrophic edge-case performance features. Namely, when it is applied to # text with only open brackets "<<<...". The class below provides the services # and correct answers for the use cases, but in these edge cases does it much # faster. re_notag = _lazy_re_compile(r"([^<>\s]+)", re.S) re_prt = _lazy_re_compile(r"<|([^<>\s]+)", re.S) class WordsRegex: @staticmethod def search(text, pos): # Look for "<" or a non-tag word. partial = re_prt.search(text, pos) if partial is None or partial[1] is not None: return partial # "<" was found, look for a closing ">". end = text.find(">", partial.end(0)) if end < 0: # ">" cannot be found, look for a word. return re_notag.search(text, pos + 1) else: # "<" followed by a ">" was found -- fake a match. end += 1 return FakeMatch(text[partial.start(0) : end], end) class FakeMatch: __slots__ = ["_text", "_end"] def end(self, group=0): assert group == 0, "This specific object takes only group=0" return self._end def __getitem__(self, group): if group == 1: return None assert group == 0, "This specific object takes only group in {0,1}" return self._text def __init__(self, text, end): self._text, self._end = text, end # ----- End security-related performance workaround ----- # Set up regular expressions. re_words = WordsRegex re_chars = _lazy_re_compile(r"<[^>]+?>|(.)", re.S) re_tag = _lazy_re_compile(r"<(/)?(\S+?)(?:(\s*/)|\s.*?)?>", re.S) re_newlines = _lazy_re_compile(r"\r\n|\r") # Used in normalize_newlines re_camel_case = _lazy_re_compile(r"(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))") @keep_lazy_text def wrap(text, width): """ A word-wrap function that preserves existing line breaks. Expects that existing line breaks are posix newlines. Preserve all white space except added line breaks consume the space on which they break the line. Don't wrap long words, thus the output text may have lines longer than ``width``. """ def _generator(): for line in text.splitlines(True): # True keeps trailing linebreaks max_width = min((line.endswith("\n") and width + 1 or width), width) while len(line) > max_width: space = line[: max_width + 1].rfind(" ") + 1 if space == 0: space = line.find(" ") + 1 if space == 0: yield line line = "" break yield "%s\n" % line[: space - 1] line = line[space:] max_width = min((line.endswith("\n") and width + 1 or width), width) if line: yield line return "".join(_generator()) class Truncator(SimpleLazyObject): """ An object used to truncate text, either by characters or words. When truncating HTML text (either chars or words), input will be limited to at most `MAX_LENGTH_HTML` characters. """ # 5 million characters are approximately 4000 text pages or 3 web pages. MAX_LENGTH_HTML = 5_000_000 def __init__(self, text): super().__init__(lambda: str(text)) def add_truncation_text(self, text, truncate=None): if truncate is None: truncate = pgettext( "String to return when truncating text", "%(truncated_text)s…" ) if "%(truncated_text)s" in truncate: return truncate % {"truncated_text": text} # The truncation text didn't contain the %(truncated_text)s string # replacement argument so just append it to the text. if text.endswith(truncate): # But don't append the truncation text if the current text already # ends in this. return text return "%s%s" % (text, truncate) def chars(self, num, truncate=None, html=False): """ Return the text truncated to be no longer than the specified number of characters. `truncate` specifies what should be used to notify that the string has been truncated, defaulting to a translatable string of an ellipsis. """ self._setup() length = int(num) text = unicodedata.normalize("NFC", self._wrapped) # Calculate the length to truncate to (max length - end_text length) truncate_len = length for char in self.add_truncation_text("", truncate): if not unicodedata.combining(char): truncate_len -= 1 if truncate_len == 0: break if html: return self._truncate_html(length, truncate, text, truncate_len, False) return self._text_chars(length, truncate, text, truncate_len) def _text_chars(self, length, truncate, text, truncate_len): """Truncate a string after a certain number of chars.""" s_len = 0 end_index = None for i, char in enumerate(text): if unicodedata.combining(char): # Don't consider combining characters # as adding to the string length continue s_len += 1 if end_index is None and s_len > truncate_len: end_index = i if s_len > length: # Return the truncated string return self.add_truncation_text(text[: end_index or 0], truncate) # Return the original string since no truncation was necessary return text def words(self, num, truncate=None, html=False): """ Truncate a string after a certain number of words. `truncate` specifies what should be used to notify that the string has been truncated, defaulting to ellipsis. """ self._setup() length = int(num) if html: return self._truncate_html(length, truncate, self._wrapped, length, True) return self._text_words(length, truncate) def _text_words(self, length, truncate): """ Truncate a string after a certain number of words. Strip newlines in the string. """ words = self._wrapped.split() if len(words) > length: words = words[:length] return self.add_truncation_text(" ".join(words), truncate) return " ".join(words) def _truncate_html(self, length, truncate, text, truncate_len, words): """ Truncate HTML to a certain number of chars (not counting tags and comments), or, if words is True, then to a certain number of words. Close opened tags if they were correctly closed in the given HTML. Preserve newlines in the HTML. """ if words and length <= 0: return "" size_limited = False if len(text) > self.MAX_LENGTH_HTML: text = text[: self.MAX_LENGTH_HTML] size_limited = True html4_singlets = ( "br", "col", "link", "base", "img", "param", "area", "hr", "input", ) # Count non-HTML chars/words and keep note of open tags pos = 0 end_text_pos = 0 current_len = 0 open_tags = [] regex = re_words if words else re_chars while current_len <= length: m = regex.search(text, pos) if not m: # Checked through whole string break pos = m.end(0) if m[1]: # It's an actual non-HTML word or char current_len += 1 if current_len == truncate_len: end_text_pos = pos continue # Check for tag tag = re_tag.match(m[0]) if not tag or current_len >= truncate_len: # Don't worry about non tags or tags after our truncate point continue closing_tag, tagname, self_closing = tag.groups() # Element names are always case-insensitive tagname = tagname.lower() if self_closing or tagname in html4_singlets: pass elif closing_tag: # Check for match in open tags list try: i = open_tags.index(tagname) except ValueError: pass else: # SGML: An end tag closes, back to the matching start tag, # all unclosed intervening start tags with omitted end tags open_tags = open_tags[i + 1 :] else: # Add it to the start of the open tags list open_tags.insert(0, tagname) truncate_text = self.add_truncation_text("", truncate) if current_len <= length: if size_limited and truncate_text: text += truncate_text return text out = text[:end_text_pos] if truncate_text: out += truncate_text # Close any tags still open for tag in open_tags: out += "</%s>" % tag # Return string return out @keep_lazy_text def get_valid_filename(name): """ Return the given string converted to a string that can be used for a clean filename. Remove leading and trailing spaces; convert other spaces to underscores; and remove anything that is not an alphanumeric, dash, underscore, or dot. >>> get_valid_filename("john's portrait in 2004.jpg") 'johns_portrait_in_2004.jpg' """ s = str(name).strip().replace(" ", "_") s = re.sub(r"(?u)[^-\w.]", "", s) if s in {"", ".", ".."}: raise SuspiciousFileOperation("Could not derive file name from '%s'" % name) return s @keep_lazy_text def get_text_list(list_, last_word=gettext_lazy("or")): """ >>> get_text_list(['a', 'b', 'c', 'd']) 'a, b, c or d' >>> get_text_list(['a', 'b', 'c'], 'and') 'a, b and c' >>> get_text_list(['a', 'b'], 'and') 'a and b' >>> get_text_list(['a']) 'a' >>> get_text_list([]) '' """ if not list_: return "" if len(list_) == 1: return str(list_[0]) return "%s %s %s" % ( # Translators: This string is used as a separator between list elements _(", ").join(str(i) for i in list_[:-1]), str(last_word), str(list_[-1]), ) @keep_lazy_text def normalize_newlines(text): """Normalize CRLF and CR newlines to just LF.""" return re_newlines.sub("\n", str(text)) @keep_lazy_text def phone2numeric(phone): """Convert a phone number with letters into its numeric equivalent.""" char2number = { "a": "2", "b": "2", "c": "2", "d": "3", "e": "3", "f": "3", "g": "4", "h": "4", "i": "4", "j": "5", "k": "5", "l": "5", "m": "6", "n": "6", "o": "6", "p": "7", "q": "7", "r": "7", "s": "7", "t": "8", "u": "8", "v": "8", "w": "9", "x": "9", "y": "9", "z": "9", } return "".join(char2number.get(c, c) for c in phone.lower()) def _get_random_filename(max_random_bytes): return b"a" * secrets.randbelow(max_random_bytes) def compress_string(s, *, max_random_bytes=None): compressed_data = gzip_compress(s, compresslevel=6, mtime=0) if not max_random_bytes: return compressed_data compressed_view = memoryview(compressed_data) header = bytearray(compressed_view[:10]) header[3] = gzip.FNAME filename = _get_random_filename(max_random_bytes) + b"\x00" return bytes(header) + filename + compressed_view[10:] class StreamingBuffer(BytesIO): def read(self): ret = self.getvalue() self.seek(0) self.truncate() return ret # Like compress_string, but for iterators of strings. def compress_sequence(sequence, *, max_random_bytes=None): buf = StreamingBuffer() filename = _get_random_filename(max_random_bytes) if max_random_bytes else None with GzipFile( filename=filename, mode="wb", compresslevel=6, fileobj=buf, mtime=0 ) as zfile: # Output headers... yield buf.read() for item in sequence: zfile.write(item) data = buf.read() if data: yield data yield buf.read() # Expression to match some_token and some_token="with spaces" (and similarly # for single-quoted strings). smart_split_re = _lazy_re_compile( r""" ((?: [^\s'"]* (?: (?:"(?:[^"\\]|\\.)*" | '(?:[^'\\]|\\.)*') [^\s'"]* )+ ) | \S+) """, re.VERBOSE, ) def smart_split(text): r""" Generator that splits a string by spaces, leaving quoted phrases together. Supports both single and double quotes, and supports escaping quotes with backslashes. In the output, strings will keep their initial and trailing quote marks and escaped quotes will remain escaped (the results can then be further processed with unescape_string_literal()). >>> list(smart_split(r'This is "a person\'s" test.')) ['This', 'is', '"a person\\\'s"', 'test.'] >>> list(smart_split(r"Another 'person\'s' test.")) ['Another', "'person\\'s'", 'test.'] >>> list(smart_split(r'A "\"funky\" style" test.')) ['A', '"\\"funky\\" style"', 'test.'] """ for bit in smart_split_re.finditer(str(text)): yield bit[0] @keep_lazy_text def unescape_string_literal(s): r""" Convert quoted string literals to unquoted strings with escaped quotes and backslashes unquoted:: >>> unescape_string_literal('"abc"') 'abc' >>> unescape_string_literal("'abc'") 'abc' >>> unescape_string_literal('"a \"bc\""') 'a "bc"' >>> unescape_string_literal("'\'ab\' c'") "'ab' c" """ if not s or s[0] not in "\"'" or s[-1] != s[0]: raise ValueError("Not a string literal: %r" % s) quote = s[0] return s[1:-1].replace(r"\%s" % quote, quote).replace(r"\\", "\\") @keep_lazy_text def slugify(value, allow_unicode=False): """ Convert to ASCII if 'allow_unicode' is False. Convert spaces or repeated dashes to single dashes. Remove characters that aren't alphanumerics, underscores, or hyphens. Convert to lowercase. Also strip leading and trailing whitespace, dashes, and underscores. """ value = str(value) if allow_unicode: value = unicodedata.normalize("NFKC", value) else: value = ( unicodedata.normalize("NFKD", value) .encode("ascii", "ignore") .decode("ascii") ) value = re.sub(r"[^\w\s-]", "", value.lower()) return re.sub(r"[-\s]+", "-", value).strip("-_") def camel_case_to_spaces(value): """ Split CamelCase and convert to lowercase. Strip surrounding whitespace. """ return re_camel_case.sub(r" \1", value).strip().lower() def _format_lazy(format_string, *args, **kwargs): """ Apply str.format() on 'format_string' where format_string, args, and/or kwargs might be lazy. """ return format_string.format(*args, **kwargs) format_lazy = lazy(_format_lazy, str) usr/local/CyberPanel/lib/python3.10/site-packages/django/utils/text.py 0000644 00000040037 15030043505 0021543 0 ustar 00 import gzip import re import secrets import unicodedata from gzip import GzipFile from gzip import compress as gzip_compress from io import BytesIO from django.core.exceptions import SuspiciousFileOperation from django.utils.functional import SimpleLazyObject, keep_lazy_text, lazy from django.utils.regex_helper import _lazy_re_compile from django.utils.translation import gettext as _ from django.utils.translation import gettext_lazy, pgettext @keep_lazy_text def capfirst(x): """Capitalize the first letter of a string.""" if not x: return x if not isinstance(x, str): x = str(x) return x[0].upper() + x[1:] # ----- Begin security-related performance workaround ----- # We used to have, below # # re_words = _lazy_re_compile(r"<[^>]+?>|([^<>\s]+)", re.S) # # But it was shown that this regex, in the way we use it here, has some # catastrophic edge-case performance features. Namely, when it is applied to # text with only open brackets "<<<...". The class below provides the services # and correct answers for the use cases, but in these edge cases does it much # faster. re_notag = _lazy_re_compile(r"([^<>\s]+)", re.S) re_prt = _lazy_re_compile(r"<|([^<>\s]+)", re.S) class WordsRegex: @staticmethod def search(text, pos): # Look for "<" or a non-tag word. partial = re_prt.search(text, pos) if partial is None or partial[1] is not None: return partial # "<" was found, look for a closing ">". end = text.find(">", partial.end(0)) if end < 0: # ">" cannot be found, look for a word. return re_notag.search(text, pos + 1) else: # "<" followed by a ">" was found -- fake a match. end += 1 return FakeMatch(text[partial.start(0) : end], end) class FakeMatch: __slots__ = ["_text", "_end"] def end(self, group=0): assert group == 0, "This specific object takes only group=0" return self._end def __getitem__(self, group): if group == 1: return None assert group == 0, "This specific object takes only group in {0,1}" return self._text def __init__(self, text, end): self._text, self._end = text, end # ----- End security-related performance workaround ----- # Set up regular expressions. re_words = WordsRegex re_chars = _lazy_re_compile(r"<[^>]+?>|(.)", re.S) re_tag = _lazy_re_compile(r"<(/)?(\S+?)(?:(\s*/)|\s.*?)?>", re.S) re_newlines = _lazy_re_compile(r"\r\n|\r") # Used in normalize_newlines re_camel_case = _lazy_re_compile(r"(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))") @keep_lazy_text def wrap(text, width): """ A word-wrap function that preserves existing line breaks. Expects that existing line breaks are posix newlines. Preserve all white space except added line breaks consume the space on which they break the line. Don't wrap long words, thus the output text may have lines longer than ``width``. """ def _generator(): for line in text.splitlines(True): # True keeps trailing linebreaks max_width = min((line.endswith("\n") and width + 1 or width), width) while len(line) > max_width: space = line[: max_width + 1].rfind(" ") + 1 if space == 0: space = line.find(" ") + 1 if space == 0: yield line line = "" break yield "%s\n" % line[: space - 1] line = line[space:] max_width = min((line.endswith("\n") and width + 1 or width), width) if line: yield line return "".join(_generator()) class Truncator(SimpleLazyObject): """ An object used to truncate text, either by characters or words. When truncating HTML text (either chars or words), input will be limited to at most `MAX_LENGTH_HTML` characters. """ # 5 million characters are approximately 4000 text pages or 3 web pages. MAX_LENGTH_HTML = 5_000_000 def __init__(self, text): super().__init__(lambda: str(text)) def add_truncation_text(self, text, truncate=None): if truncate is None: truncate = pgettext( "String to return when truncating text", "%(truncated_text)s…" ) if "%(truncated_text)s" in truncate: return truncate % {"truncated_text": text} # The truncation text didn't contain the %(truncated_text)s string # replacement argument so just append it to the text. if text.endswith(truncate): # But don't append the truncation text if the current text already # ends in this. return text return "%s%s" % (text, truncate) def chars(self, num, truncate=None, html=False): """ Return the text truncated to be no longer than the specified number of characters. `truncate` specifies what should be used to notify that the string has been truncated, defaulting to a translatable string of an ellipsis. """ self._setup() length = int(num) text = unicodedata.normalize("NFC", self._wrapped) # Calculate the length to truncate to (max length - end_text length) truncate_len = length for char in self.add_truncation_text("", truncate): if not unicodedata.combining(char): truncate_len -= 1 if truncate_len == 0: break if html: return self._truncate_html(length, truncate, text, truncate_len, False) return self._text_chars(length, truncate, text, truncate_len) def _text_chars(self, length, truncate, text, truncate_len): """Truncate a string after a certain number of chars.""" s_len = 0 end_index = None for i, char in enumerate(text): if unicodedata.combining(char): # Don't consider combining characters # as adding to the string length continue s_len += 1 if end_index is None and s_len > truncate_len: end_index = i if s_len > length: # Return the truncated string return self.add_truncation_text(text[: end_index or 0], truncate) # Return the original string since no truncation was necessary return text def words(self, num, truncate=None, html=False): """ Truncate a string after a certain number of words. `truncate` specifies what should be used to notify that the string has been truncated, defaulting to ellipsis. """ self._setup() length = int(num) if html: return self._truncate_html(length, truncate, self._wrapped, length, True) return self._text_words(length, truncate) def _text_words(self, length, truncate): """ Truncate a string after a certain number of words. Strip newlines in the string. """ words = self._wrapped.split() if len(words) > length: words = words[:length] return self.add_truncation_text(" ".join(words), truncate) return " ".join(words) def _truncate_html(self, length, truncate, text, truncate_len, words): """ Truncate HTML to a certain number of chars (not counting tags and comments), or, if words is True, then to a certain number of words. Close opened tags if they were correctly closed in the given HTML. Preserve newlines in the HTML. """ if words and length <= 0: return "" size_limited = False if len(text) > self.MAX_LENGTH_HTML: text = text[: self.MAX_LENGTH_HTML] size_limited = True html4_singlets = ( "br", "col", "link", "base", "img", "param", "area", "hr", "input", ) # Count non-HTML chars/words and keep note of open tags pos = 0 end_text_pos = 0 current_len = 0 open_tags = [] regex = re_words if words else re_chars while current_len <= length: m = regex.search(text, pos) if not m: # Checked through whole string break pos = m.end(0) if m[1]: # It's an actual non-HTML word or char current_len += 1 if current_len == truncate_len: end_text_pos = pos continue # Check for tag tag = re_tag.match(m[0]) if not tag or current_len >= truncate_len: # Don't worry about non tags or tags after our truncate point continue closing_tag, tagname, self_closing = tag.groups() # Element names are always case-insensitive tagname = tagname.lower() if self_closing or tagname in html4_singlets: pass elif closing_tag: # Check for match in open tags list try: i = open_tags.index(tagname) except ValueError: pass else: # SGML: An end tag closes, back to the matching start tag, # all unclosed intervening start tags with omitted end tags open_tags = open_tags[i + 1 :] else: # Add it to the start of the open tags list open_tags.insert(0, tagname) truncate_text = self.add_truncation_text("", truncate) if current_len <= length: if size_limited and truncate_text: text += truncate_text return text out = text[:end_text_pos] if truncate_text: out += truncate_text # Close any tags still open for tag in open_tags: out += "</%s>" % tag # Return string return out @keep_lazy_text def get_valid_filename(name): """ Return the given string converted to a string that can be used for a clean filename. Remove leading and trailing spaces; convert other spaces to underscores; and remove anything that is not an alphanumeric, dash, underscore, or dot. >>> get_valid_filename("john's portrait in 2004.jpg") 'johns_portrait_in_2004.jpg' """ s = str(name).strip().replace(" ", "_") s = re.sub(r"(?u)[^-\w.]", "", s) if s in {"", ".", ".."}: raise SuspiciousFileOperation("Could not derive file name from '%s'" % name) return s @keep_lazy_text def get_text_list(list_, last_word=gettext_lazy("or")): """ >>> get_text_list(['a', 'b', 'c', 'd']) 'a, b, c or d' >>> get_text_list(['a', 'b', 'c'], 'and') 'a, b and c' >>> get_text_list(['a', 'b'], 'and') 'a and b' >>> get_text_list(['a']) 'a' >>> get_text_list([]) '' """ if not list_: return "" if len(list_) == 1: return str(list_[0]) return "%s %s %s" % ( # Translators: This string is used as a separator between list elements _(", ").join(str(i) for i in list_[:-1]), str(last_word), str(list_[-1]), ) @keep_lazy_text def normalize_newlines(text): """Normalize CRLF and CR newlines to just LF.""" return re_newlines.sub("\n", str(text)) @keep_lazy_text def phone2numeric(phone): """Convert a phone number with letters into its numeric equivalent.""" char2number = { "a": "2", "b": "2", "c": "2", "d": "3", "e": "3", "f": "3", "g": "4", "h": "4", "i": "4", "j": "5", "k": "5", "l": "5", "m": "6", "n": "6", "o": "6", "p": "7", "q": "7", "r": "7", "s": "7", "t": "8", "u": "8", "v": "8", "w": "9", "x": "9", "y": "9", "z": "9", } return "".join(char2number.get(c, c) for c in phone.lower()) def _get_random_filename(max_random_bytes): return b"a" * secrets.randbelow(max_random_bytes) def compress_string(s, *, max_random_bytes=None): compressed_data = gzip_compress(s, compresslevel=6, mtime=0) if not max_random_bytes: return compressed_data compressed_view = memoryview(compressed_data) header = bytearray(compressed_view[:10]) header[3] = gzip.FNAME filename = _get_random_filename(max_random_bytes) + b"\x00" return bytes(header) + filename + compressed_view[10:] class StreamingBuffer(BytesIO): def read(self): ret = self.getvalue() self.seek(0) self.truncate() return ret # Like compress_string, but for iterators of strings. def compress_sequence(sequence, *, max_random_bytes=None): buf = StreamingBuffer() filename = _get_random_filename(max_random_bytes) if max_random_bytes else None with GzipFile( filename=filename, mode="wb", compresslevel=6, fileobj=buf, mtime=0 ) as zfile: # Output headers... yield buf.read() for item in sequence: zfile.write(item) data = buf.read() if data: yield data yield buf.read() # Expression to match some_token and some_token="with spaces" (and similarly # for single-quoted strings). smart_split_re = _lazy_re_compile( r""" ((?: [^\s'"]* (?: (?:"(?:[^"\\]|\\.)*" | '(?:[^'\\]|\\.)*') [^\s'"]* )+ ) | \S+) """, re.VERBOSE, ) def smart_split(text): r""" Generator that splits a string by spaces, leaving quoted phrases together. Supports both single and double quotes, and supports escaping quotes with backslashes. In the output, strings will keep their initial and trailing quote marks and escaped quotes will remain escaped (the results can then be further processed with unescape_string_literal()). >>> list(smart_split(r'This is "a person\'s" test.')) ['This', 'is', '"a person\\\'s"', 'test.'] >>> list(smart_split(r"Another 'person\'s' test.")) ['Another', "'person\\'s'", 'test.'] >>> list(smart_split(r'A "\"funky\" style" test.')) ['A', '"\\"funky\\" style"', 'test.'] """ for bit in smart_split_re.finditer(str(text)): yield bit[0] @keep_lazy_text def unescape_string_literal(s): r""" Convert quoted string literals to unquoted strings with escaped quotes and backslashes unquoted:: >>> unescape_string_literal('"abc"') 'abc' >>> unescape_string_literal("'abc'") 'abc' >>> unescape_string_literal('"a \"bc\""') 'a "bc"' >>> unescape_string_literal("'\'ab\' c'") "'ab' c" """ if not s or s[0] not in "\"'" or s[-1] != s[0]: raise ValueError("Not a string literal: %r" % s) quote = s[0] return s[1:-1].replace(r"\%s" % quote, quote).replace(r"\\", "\\") @keep_lazy_text def slugify(value, allow_unicode=False): """ Convert to ASCII if 'allow_unicode' is False. Convert spaces or repeated dashes to single dashes. Remove characters that aren't alphanumerics, underscores, or hyphens. Convert to lowercase. Also strip leading and trailing whitespace, dashes, and underscores. """ value = str(value) if allow_unicode: value = unicodedata.normalize("NFKC", value) else: value = ( unicodedata.normalize("NFKD", value) .encode("ascii", "ignore") .decode("ascii") ) value = re.sub(r"[^\w\s-]", "", value.lower()) return re.sub(r"[-\s]+", "-", value).strip("-_") def camel_case_to_spaces(value): """ Split CamelCase and convert to lowercase. Strip surrounding whitespace. """ return re_camel_case.sub(r" \1", value).strip().lower() def _format_lazy(format_string, *args, **kwargs): """ Apply str.format() on 'format_string' where format_string, args, and/or kwargs might be lazy. """ return format_string.format(*args, **kwargs) format_lazy = lazy(_format_lazy, str) usr/lib/python3/dist-packages/apt/progress/text.py 0000644 00000024323 15030043615 0016235 0 ustar 00 # Copyright (c) 2009 Julian Andres Klode <jak@debian.org> # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as # published by the Free Software Foundation; either version 2 of the # License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 # USA """Progress reporting for text interfaces.""" from __future__ import print_function import io import os import signal import sys import types from typing import Callable, Optional, Union import apt_pkg from apt.progress import base __all__ = ['AcquireProgress', 'CdromProgress', 'OpProgress'] def _(msg): # type: (str) -> str """Translate the message, also try apt if translation is missing.""" res = apt_pkg.gettext(msg) if res == msg: res = apt_pkg.gettext(msg, "apt") return res class TextProgress(object): """Internal Base class for text progress classes.""" def __init__(self, outfile=None): # type: (Optional[io.TextIOBase]) -> None self._file = outfile or sys.stdout self._width = 0 def _write(self, msg, newline=True, maximize=False): # type: (str, bool, bool) -> None """Write the message on the terminal, fill remaining space.""" self._file.write("\r") self._file.write(msg) # Fill remaining stuff with whitespace if self._width > len(msg): self._file.write((self._width - len(msg)) * ' ') elif maximize: # Needed for OpProgress. self._width = max(self._width, len(msg)) if newline: self._file.write("\n") else: #self._file.write("\r") self._file.flush() class OpProgress(base.OpProgress, TextProgress): """Operation progress reporting. This closely resembles OpTextProgress in libapt-pkg. """ def __init__(self, outfile=None): # type: (Optional[io.TextIOBase]) -> None TextProgress.__init__(self, outfile) base.OpProgress.__init__(self) self.old_op = "" def update(self, percent=None): # type: (Optional[float]) -> None """Called periodically to update the user interface.""" base.OpProgress.update(self, percent) if self.major_change and self.old_op: self._write(self.old_op) self._write("%s... %i%%\r" % (self.op, self.percent), False, True) self.old_op = self.op def done(self): # type: () -> None """Called once an operation has been completed.""" base.OpProgress.done(self) if self.old_op: self._write(_("%c%s... Done") % ('\r', self.old_op), True, True) self.old_op = "" class AcquireProgress(base.AcquireProgress, TextProgress): """AcquireProgress for the text interface.""" def __init__(self, outfile=None): # type: (Optional[io.TextIOBase]) -> None TextProgress.__init__(self, outfile) base.AcquireProgress.__init__(self) self._signal = None # type: Union[Callable[[int, Optional[types.FrameType]], None], int, signal.Handlers, None] # noqa self._width = 80 self._id = 1 def start(self): # type: () -> None """Start an Acquire progress. In this case, the function sets up a signal handler for SIGWINCH, i.e. window resize signals. And it also sets id to 1. """ base.AcquireProgress.start(self) self._signal = signal.signal(signal.SIGWINCH, self._winch) # Get the window size. self._winch() self._id = 1 def _winch(self, *dummy): # type: (object) -> None """Signal handler for window resize signals.""" if hasattr(self._file, "fileno") and os.isatty(self._file.fileno()): import fcntl import termios import struct buf = fcntl.ioctl(self._file, termios.TIOCGWINSZ, 8 * b' ') # noqa dummy, col, dummy, dummy = struct.unpack('hhhh', buf) self._width = col - 1 # 1 for the cursor def ims_hit(self, item): # type: (apt_pkg.AcquireItemDesc) -> None """Called when an item is update (e.g. not modified on the server).""" base.AcquireProgress.ims_hit(self, item) line = _('Hit ') + item.description if item.owner.filesize: line += ' [%sB]' % apt_pkg.size_to_str(item.owner.filesize) self._write(line) def fail(self, item): # type: (apt_pkg.AcquireItemDesc) -> None """Called when an item is failed.""" base.AcquireProgress.fail(self, item) if item.owner.status == item.owner.STAT_DONE: self._write(_("Ign ") + item.description) else: self._write(_("Err ") + item.description) self._write(" %s" % item.owner.error_text) def fetch(self, item): # type: (apt_pkg.AcquireItemDesc) -> None """Called when some of the item's data is fetched.""" base.AcquireProgress.fetch(self, item) # It's complete already (e.g. Hit) if item.owner.complete: return item.owner.id = self._id self._id += 1 line = _("Get:") + "%s %s" % (item.owner.id, item.description) if item.owner.filesize: line += (" [%sB]" % apt_pkg.size_to_str(item.owner.filesize)) self._write(line) def pulse(self, owner): # type: (apt_pkg.Acquire) -> bool """Periodically invoked while the Acquire process is underway. Return False if the user asked to cancel the whole Acquire process.""" base.AcquireProgress.pulse(self, owner) # only show progress on a tty to not clutter log files etc if (hasattr(self._file, "fileno") and not os.isatty(self._file.fileno())): return True # calculate progress percent = (((self.current_bytes + self.current_items) * 100.0) / float(self.total_bytes + self.total_items)) shown = False tval = '%i%%' % percent end = "" if self.current_cps: eta = int(float(self.total_bytes - self.current_bytes) / self.current_cps) end = " %sB/s %s" % (apt_pkg.size_to_str(self.current_cps), apt_pkg.time_to_str(eta)) for worker in owner.workers: val = '' if not worker.current_item: if worker.status: val = ' [%s]' % worker.status if len(tval) + len(val) + len(end) >= self._width: break tval += val shown = True continue shown = True if worker.current_item.owner.id: val += " [%i %s" % (worker.current_item.owner.id, worker.current_item.shortdesc) else: val += ' [%s' % worker.current_item.description if worker.current_item.owner.active_subprocess: val += ' %s' % worker.current_item.owner.active_subprocess val += ' %sB' % apt_pkg.size_to_str(worker.current_size) # Add the total size and percent if worker.total_size and not worker.current_item.owner.complete: val += "/%sB %i%%" % ( apt_pkg.size_to_str(worker.total_size), worker.current_size * 100.0 / worker.total_size) val += ']' if len(tval) + len(val) + len(end) >= self._width: # Display as many items as screen width break else: tval += val if not shown: tval += _(" [Working]") if self.current_cps: tval += (self._width - len(end) - len(tval)) * ' ' + end self._write(tval, False) return True def media_change(self, medium, drive): # type: (str, str) -> bool """Prompt the user to change the inserted removable media.""" base.AcquireProgress.media_change(self, medium, drive) self._write(_("Media change: please insert the disc labeled\n" " '%s'\n" "in the drive '%s' and press enter\n") % (medium, drive)) return input() not in ('c', 'C') def stop(self): # type: () -> None """Invoked when the Acquire process stops running.""" base.AcquireProgress.stop(self) # Trick for getting a translation from apt self._write((_("Fetched %sB in %s (%sB/s)\n") % ( apt_pkg.size_to_str(self.fetched_bytes), apt_pkg.time_to_str(self.elapsed_time), apt_pkg.size_to_str(self.current_cps))).rstrip("\n")) # Delete the signal again. import signal signal.signal(signal.SIGWINCH, self._signal) class CdromProgress(base.CdromProgress, TextProgress): """Text CD-ROM progress.""" def ask_cdrom_name(self): # type: () -> Optional[str] """Ask the user to provide a name for the disc.""" base.CdromProgress.ask_cdrom_name(self) self._write(_("Please provide a name for this medium, such as " "'Debian 2.1r1 Disk 1'"), False) try: return str(input(":")) except KeyboardInterrupt: return None def update(self, text, current): # type: (str, int) -> None """Set the current progress.""" base.CdromProgress.update(self, text, current) if text: self._write(text, False) def change_cdrom(self): # type: () -> bool """Ask the user to change the CD-ROM.""" base.CdromProgress.change_cdrom(self) self._write(_("Please insert an installation medium and press enter"), False) try: return bool(input() == '') except KeyboardInterrupt: return False usr/lib/python3/dist-packages/twisted/python/text.py 0000644 00000012451 15030063111 0016601 0 ustar 00 # -*- test-case-name: twisted.test.test_text -*- # # Copyright (c) Twisted Matrix Laboratories. # See LICENSE for details. """ Miscellany of text-munging functions. """ def stringyString(object, indentation=""): """ Expansive string formatting for sequence types. C{list.__str__} and C{dict.__str__} use C{repr()} to display their elements. This function also turns these sequence types into strings, but uses C{str()} on their elements instead. Sequence elements are also displayed on separate lines, and nested sequences have nested indentation. """ braces = "" sl = [] if type(object) is dict: braces = "{}" for key, value in object.items(): value = stringyString(value, indentation + " ") if isMultiline(value): if endsInNewline(value): value = value[: -len("\n")] sl.append(f"{indentation} {key}:\n{value}") else: # Oops. Will have to move that indentation. sl.append(f"{indentation} {key}: {value[len(indentation) + 3 :]}") elif type(object) is tuple or type(object) is list: if type(object) is tuple: braces = "()" else: braces = "[]" for element in object: element = stringyString(element, indentation + " ") sl.append(element.rstrip() + ",") else: sl[:] = map(lambda s, i=indentation: i + s, str(object).split("\n")) if not sl: sl.append(indentation) if braces: sl[0] = indentation + braces[0] + sl[0][len(indentation) + 1 :] sl[-1] = sl[-1] + braces[-1] s = "\n".join(sl) if isMultiline(s) and not endsInNewline(s): s = s + "\n" return s def isMultiline(s): """ Returns C{True} if this string has a newline in it. """ return s.find("\n") != -1 def endsInNewline(s): """ Returns C{True} if this string ends in a newline. """ return s[-len("\n") :] == "\n" def greedyWrap(inString, width=80): """ Given a string and a column width, return a list of lines. Caveat: I'm use a stupid greedy word-wrapping algorythm. I won't put two spaces at the end of a sentence. I don't do full justification. And no, I've never even *heard* of hypenation. """ outLines = [] # eww, evil hacks to allow paragraphs delimited by two \ns :( if inString.find("\n\n") >= 0: paragraphs = inString.split("\n\n") for para in paragraphs: outLines.extend(greedyWrap(para, width) + [""]) return outLines inWords = inString.split() column = 0 ptr_line = 0 while inWords: column = column + len(inWords[ptr_line]) ptr_line = ptr_line + 1 if column > width: if ptr_line == 1: # This single word is too long, it will be the whole line. pass else: # We've gone too far, stop the line one word back. ptr_line = ptr_line - 1 (l, inWords) = (inWords[0:ptr_line], inWords[ptr_line:]) outLines.append(" ".join(l)) ptr_line = 0 column = 0 elif not (len(inWords) > ptr_line): # Clean up the last bit. outLines.append(" ".join(inWords)) del inWords[:] else: # Space column = column + 1 # next word return outLines wordWrap = greedyWrap def removeLeadingBlanks(lines): ret = [] for line in lines: if ret or line.strip(): ret.append(line) return ret def removeLeadingTrailingBlanks(s): lines = removeLeadingBlanks(s.split("\n")) lines.reverse() lines = removeLeadingBlanks(lines) lines.reverse() return "\n".join(lines) + "\n" def splitQuoted(s): """ Like a string split, but don't break substrings inside quotes. >>> splitQuoted('the "hairy monkey" likes pie') ['the', 'hairy monkey', 'likes', 'pie'] Another one of those "someone must have a better solution for this" things. This implementation is a VERY DUMB hack done too quickly. """ out = [] quot = None phrase = None for word in s.split(): if phrase is None: if word and (word[0] in ('"', "'")): quot = word[0] word = word[1:] phrase = [] if phrase is None: out.append(word) else: if word and (word[-1] == quot): word = word[:-1] phrase.append(word) out.append(" ".join(phrase)) phrase = None else: phrase.append(word) return out def strFile(p, f, caseSensitive=True): """ Find whether string C{p} occurs in a read()able object C{f}. @rtype: C{bool} """ buf = type(p)() buf_len = max(len(p), 2 ** 2 ** 2 ** 2) if not caseSensitive: p = p.lower() while 1: r = f.read(buf_len - len(p)) if not caseSensitive: r = r.lower() bytes_read = len(r) if bytes_read == 0: return False l = len(buf) + bytes_read - buf_len if l <= 0: buf = buf + r else: buf = buf[l:] + r if buf.find(p) != -1: return True
| ver. 1.4 |
Github
|
.
| PHP 8.2.28 | Generation time: 0.02 |
proxy
|
phpinfo
|
Settings