import win32file import win32con import pywintypes import winerror # Cache used for safe_shell_filter() function _cache = {} _MAXCACHE = 100 FILE_ATTRIBUTE_REPARSE_POINT = 1024 IO_REPARSE_TAG_MOUNT_POINT = 0xA0000003 # Junction IO_REPARSE_TAG_SYMLINK = 0xA000000C from encoding import get_encodings fs_enc = get_encodings()[1] _listdir = os.listdir if sys.platform == "win32": # Add support for long paths (> 260 chars) # and retry ERROR_SHARING_VIOLATION import builtins import winerror import win32api _open = builtins.open def retry_sharing_violation_factory(fn, delay=0.25, maxretries=20): def retry_sharing_violation(*args, **kwargs): retries = 0
#!/usr/bin/env python # -*- coding: utf-8 -*- import codecs import locale import string import sys try: from functools import reduce except ImportError: # Python < 2.6 pass from encoding import get_encodings fs_enc = get_encodings()[1] ascii_printable = "".join([getattr(string, name) for name in "digits", "ascii_letters", "punctuation", "whitespace"]) # Control chars are defined as charcodes in the decimal range 0-31 (inclusive) # except whitespace characters, plus charcode 127 (DEL) control_chars = "".join([chr(i) for i in range(0, 9) + range(14, 32) + [127]]) # Safe character substitution - can be used for filenames # i.e. no \/:*?"<>| will be added through substitution safesubst = {u"\u00a9": u"(C)", # U+00A9 copyright sign u"\u00ae": u"(R)", # U+00AE registered sign u"\u00b1": u"+-", u"\u00b2": u"^2", # U+00B2 superscript two u"\u00b3": u"^3", # U+00B3 superscript three
original_codepage = None if sys.stdout.isatty(): ##if sys.platform == "win32": ##try: ##from win32console import GetConsoleOutputCP, SetConsoleOutputCP ##except ImportError: ##pass ##else: ##original_codepage = GetConsoleOutputCP() ##SetConsoleOutputCP(65001) encodestdio() enc, fs_enc = get_encodings() class SafePrinter(): def __init__(self, pad=False, padchar=" ", sep=" ", end="\n", file_=sys.stdout, fn=None, encoding=None): """ Write safely, avoiding any UnicodeDe-/EncodingErrors on strings and converting all other objects to safe string representations. sprint = SafePrinter(pad=False, padchar=' ', sep=' ', end='\\n', file=sys.stdout, fn=None) sprint(value, ..., pad=False, padchar=' ', sep=' ', end='\\n', file=sys.stdout, fn=None)
# -*- coding: utf-8 -*- import locale import os import sys from encoding import get_encoding, get_encodings from util_str import safe_unicode original_codepage = None enc, fs_enc = get_encodings() _conwidth = None def _get_console_width(): global _conwidth if _conwidth is None: _conwidth = 80 try: if sys.platform == "win32": from ctypes import windll, create_string_buffer import struct # Use stderr handle so that pipes don't affect the reported size stderr_handle = windll.kernel32.GetStdHandle(-12) buf = create_string_buffer(22) consinfo = windll.kernel32.GetConsoleScreenBufferInfo( stderr_handle, buf) if consinfo: _conwidth = struct.unpack("hhhhHhhhhhh", buf.raw)[0]