Python glob.escape() Examples

The following are 19 code examples of glob.escape(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module glob , or try the search function .
Example #1
Source File: format_utils.py    From oj with MIT License 6 votes vote down vote up
def percentparse(s: str, format: str, table: Dict[str, str]) -> Optional[Dict[str, str]]:
    table = {key: '(?P<{}>{})'.format(key, value) for key, value in table.items()}
    used = set()  # type: Set[str]
    pattern = ''
    for token in percentsplit(re.escape(format).replace('\\%', '%')):
        if token.startswith('%'):
            c = token[1]
            if c not in used:
                pattern += table[c]
                used.add(c)
            else:
                pattern += r'(?P={})'.format(c)
        else:
            pattern += token
    m = re.match(pattern, s)
    if not m:
        return None
    return m.groupdict() 
Example #2
Source File: utils.py    From SynThai with MIT License 6 votes vote down vote up
def _load(self):
        """Load text to memory"""

        corpus_directory = glob.escape(self.corpus_directory)
        file_list = sorted(glob.glob(os.path.join(corpus_directory, "*.txt")))

        for path in file_list:
            with open(path, "r", encoding="utf8") as text:
                # Read content from text file
                content = text.read()

                # Preprocessing
                content = self._preprocessing(content)

                # Create text instance
                text = Text(path, os.path.basename(path), content)

                # Add text to corpus
                self.__corpus.append(text) 
Example #3
Source File: format_utils.py    From oj with MIT License 5 votes vote down vote up
def match_with_format(directory: pathlib.Path, format: str, path: pathlib.Path) -> Optional[Match[str]]:
    if os.name == 'nt':
        format = format.replace('/', '\\')
    table = {}
    table['s'] = '(?P<name>.+)'
    table['e'] = '(?P<ext>in|out)'
    pattern = re.compile(re.escape(str(directory.resolve()) + os.path.sep) + percentformat(re.escape(format).replace(re.escape('%'), '%'), table))
    return pattern.match(str(path.resolve())) 
Example #4
Source File: test_glob.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def check_escape(self, arg, expected):
        self.assertEqual(glob.escape(arg), expected)
        self.assertEqual(glob.escape(os.fsencode(arg)), os.fsencode(expected)) 
Example #5
Source File: format_utils.py    From oj with MIT License 5 votes vote down vote up
def glob_with_format(directory: pathlib.Path, format: str) -> List[pathlib.Path]:
    if os.name == 'nt':
        format = format.replace('/', '\\')
    table = {}
    table['s'] = '*'
    table['e'] = '*'
    pattern = (glob.escape(str(directory) + os.path.sep) + percentformat(glob.escape(format).replace(glob.escape('%'), '%'), table))
    paths = list(map(pathlib.Path, glob.glob(pattern)))
    for path in paths:
        log.debug('testcase globbed: %s', path)
    return paths 
Example #6
Source File: _file_sink.py    From loguru with MIT License 5 votes vote down vote up
def _make_glob_patterns(path):
        formatter = string.Formatter()
        tokens = formatter.parse(path)
        escaped = "".join(glob.escape(text) + "*" * (name is not None) for text, name, *_ in tokens)

        root, ext = os.path.splitext(escaped)

        if not ext:
            return [escaped, escaped + ".*"]

        return [escaped, escaped + ".*", root + ".*" + ext, root + ".*" + ext + ".*"] 
Example #7
Source File: setup.py    From antismash with GNU Affero General Public License v3.0 5 votes vote down vote up
def find_data_files():
    """Setuptools package_data globbing is stupid, so make this work ourselves."""
    data_files = []
    for pathname in glob.glob("antismash/**/*", recursive=True):
        if pathname.endswith('.pyc'):
            continue
        if pathname.endswith('.py'):
            continue
        if '__pycache__' in pathname:
            continue
        if pathname[:-1].endswith('.hmm.h3'):
            continue
        if pathname.endswith('bgc_seeds.hmm'):
            continue
        pathname = glob.escape(pathname)
        pathname = pathname[10:]
        data_files.append(pathname)
    if "HARDCODE_ANTISMASH_GIT_VERSION" in os.environ:
        version_file = os.path.join('antismash', 'git_hash')
        with open(version_file, 'wt') as handle:
            try:
                git_version = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD'],
                                                      universal_newlines=True).strip()
                changes = subprocess.check_output(['git', 'status', '--porcelain'],
                                                  universal_newlines=True).splitlines()
                if len(changes) != 0:
                    git_version += "(changed)"
                handle.write(git_version)
            except (OSError, subprocess.CalledProcessError):
                pass
        data_files.append(version_file)
    return data_files 
Example #8
Source File: test_glob.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def check_escape(self, arg, expected):
        self.assertEqual(glob.escape(arg), expected)
        self.assertEqual(glob.escape(os.fsencode(arg)), os.fsencode(expected)) 
Example #9
Source File: utils.py    From SynThai with MIT License 5 votes vote down vote up
def get_token_list(self, index):
        """Get list of (word, tag) pair"""

        if not self.word_delimiter or not self.tag_delimiter:
            return list()

        # Get content by index
        content = self.__corpus[index].content

        # Empty file
        if not content:
            return list()

        # Split each word by word delimiter
        token_list = content.split(self.word_delimiter)

        for idx, token in enumerate(token_list):
            # Empty or Spacebar
            if token == "" or token == constant.SPACEBAR:
                word = constant.SPACEBAR
                tag = constant.PAD_TAG_INDEX

            # Word
            else:
                # Split word and tag by tag delimiter
                datum = token.split(self.tag_delimiter)
                word = datum[0]
                tag = datum[-2]

                # Replace escape character to proper character
                word = word.replace(constant.ESCAPE_WORD_DELIMITER, self.word_delimiter)
                tag = tag.replace(constant.ESCAPE_TAG_DELIMITER, self.tag_delimiter)

            # Replace token with word and tag pair
            token_list[idx] = (word, tag)

        return token_list 
Example #10
Source File: utils.py    From SynThai with MIT License 5 votes vote down vote up
def _preprocessing(self, content):
        """Text preprocessing"""

        # Remove new line
        content = re.sub(r"(\r\n|\r|\n)+", r"", content)

        # Convert one or multiple non-breaking space to space
        content = re.sub(r"(\xa0)+", r"\s", content)

        # Convert multiple spaces to only one space
        content = re.sub(r"\s{2,}", r"\s", content)

        # Trim whitespace from starting and ending of text
        content = content.strip(string.whitespace)

        if self.word_delimiter and self.tag_delimiter:
            # Trim word delimiter from starting and ending of text
            content = content.strip(self.word_delimiter)

            # Convert special characters (word and tag delimiter)
            # in text's content to escape character
            find = "{0}{0}{1}".format(re.escape(self.word_delimiter),
                                      re.escape(self.tag_delimiter))
            replace = "{0}{2}{1}".format(re.escape(self.word_delimiter),
                                         re.escape(self.tag_delimiter),
                                         re.escape(constant.ESCAPE_WORD_DELIMITER))
            content = re.sub(find, replace, content)

            find = "{0}{0}".format(re.escape(self.tag_delimiter))
            replace = "{1}{0}".format(re.escape(self.tag_delimiter),
                                      re.escape(constant.ESCAPE_TAG_DELIMITER))
            content = re.sub(find, replace, content)

        # Replace distinct quotation mark into standard quotation
        content = re.sub(r"\u2018|\u2019", r"\'", content)
        content = re.sub(r"\u201c|\u201d", r"\"", content)

        return content 
Example #11
Source File: vid_utils.py    From telegram-bot-youtube-downloader with MIT License 5 votes vote down vote up
def check_dimension(self):
        if os.path.getsize(self.file_name) > 50 * 1024 * 1023:
            os.system('split -b 49M "{0}" "{1}"'.format(self.file_name, self.file_name))
            os.remove(self.file_name)
        return glob(escape(self.file_name) + '*') 
Example #12
Source File: glob.py    From python2017 with MIT License 5 votes vote down vote up
def glob_escape(pathname):
        """
        Escape all special characters.
        """
        drive, pathname = os.path.splitdrive(pathname)
        pathname = _magic_check.sub(r'[\1]', pathname)
        return drive + pathname 
Example #13
Source File: glob.py    From openhgsenti with Apache License 2.0 5 votes vote down vote up
def glob_escape(pathname):
        """
        Escape all special characters.
        """
        drive, pathname = os.path.splitdrive(pathname)
        pathname = _magic_check.sub(r'[\1]', pathname)
        return drive + pathname 
Example #14
Source File: glob.py    From python with Apache License 2.0 5 votes vote down vote up
def glob_escape(pathname):
        """
        Escape all special characters.
        """
        drive, pathname = os.path.splitdrive(pathname)
        pathname = _magic_check.sub(r'[\1]', pathname)
        return drive + pathname 
Example #15
Source File: rclone.py    From cloudplow with GNU General Public License v3.0 5 votes vote down vote up
def __excludes2string(self):
        return ' '.join(
            "--exclude=%s" % (
                cmd_quote(glob.escape(value) if value.startswith(os.path.sep) else value) if isinstance(value,
                                                                                                        str) else value)
            for value in
            self.config['rclone_excludes']).replace('=None', '').strip() 
Example #16
Source File: rclone.py    From cloudplow with GNU General Public License v3.0 5 votes vote down vote up
def __excludes2string(self):
        if 'rclone_excludes' not in self.config:
            return ''

        return ' '.join(
            "--exclude=%s" % (
                cmd_quote(glob.escape(value) if value.startswith(os.path.sep) else value) if isinstance(value,
                                                                                                        str) else value)
            for value in
            self.config['rclone_excludes']).replace('=None', '').strip() 
Example #17
Source File: test_glob.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def check_escape(self, arg, expected):
        self.assertEqual(glob.escape(arg), expected)
        self.assertEqual(glob.escape(os.fsencode(arg)), os.fsencode(expected)) 
Example #18
Source File: steam.py    From protontricks with GNU General Public License v3.0 4 votes vote down vote up
def get_proton_installations(compat_tool_dir):
    """
    Return a list of custom Proton installations as a list of SteamApp objects
    """

    if not os.path.isdir(compat_tool_dir):
        return []

    comptool_files = glob.glob(
        os.path.join(
            glob.escape(compat_tool_dir), "*", "compatibilitytool.vdf"
        )
    )
    comptool_files += glob.glob(
        os.path.join(glob.escape(compat_tool_dir), "compatibilitytool.vdf")
    )

    custom_proton_apps = []

    for vdf_path in comptool_files:
        with open(vdf_path, "r") as f:
            content = f.read()

        vdf_data = vdf.loads(content)
        internal_name = list(
            vdf_data["compatibilitytools"]["compat_tools"].keys())[0]
        tool_info = vdf_data["compatibilitytools"]["compat_tools"][
            internal_name]

        install_path = tool_info["install_path"]
        from_oslist = tool_info["from_oslist"]
        to_oslist = tool_info["to_oslist"]

        if from_oslist != "windows" or to_oslist != "linux":
            continue

        # Installation path can be relative if the VDF was in
        # 'compatibilitytools.d/'
        # or '.' if the VDF was in 'compatibilitytools.d/TOOL_NAME'
        if install_path == ".":
            install_path = os.path.dirname(vdf_path)
        else:
            install_path = os.path.join(compat_tool_dir, install_path)

        custom_proton_apps.append(
            SteamApp(name=internal_name, install_path=install_path)
        )

    return custom_proton_apps 
Example #19
Source File: steam.py    From protontricks with GNU General Public License v3.0 4 votes vote down vote up
def get_steam_apps(steam_root, steam_path, steam_lib_paths):
    """
    Find all the installed Steam apps and return them as a list of SteamApp
    objects
    """
    steam_apps = []

    for path in steam_lib_paths:
        appmanifest_paths = []
        is_lowercase = os.path.isdir(os.path.join(path, "steamapps"))
        is_mixedcase = os.path.isdir(os.path.join(path, "SteamApps"))

        if is_lowercase:
            appmanifest_paths = glob.glob(
                os.path.join(
                    glob.escape(path), "steamapps", "appmanifest_*.acf"
                )
            )
        elif is_mixedcase:
            appmanifest_paths = glob.glob(
                os.path.join(
                    glob.escape(path), "SteamApps", "appmanifest_*.acf"
                )
            )

        if is_lowercase and is_mixedcase:
            # Log a warning if both 'steamapps' and 'SteamApps' directories
            # exist, as both Protontricks and Steam client have problems
            # dealing with it (see issue #51)
            logger.warning(
                "Both 'steamapps' and 'SteamApps' directories were found at "
                "%s. 'SteamApps' directory should be removed to prevent "
                "issues with app and Proton discovery.",
                path
            )

        for manifest_path in appmanifest_paths:
            steam_app = SteamApp.from_appmanifest(
                manifest_path, steam_lib_paths=steam_lib_paths
            )
            if steam_app:
                steam_apps.append(steam_app)

    # Get the custom Proton installations and non-Steam shortcuts as well
    steam_apps += get_custom_proton_installations(steam_root=steam_root)
    steam_apps += get_custom_windows_shortcuts(steam_path=steam_path)

    # Exclude games that haven't been launched yet
    steam_apps = [
        app for app in steam_apps if app.prefix_path_exists or app.is_proton
    ]

    # Sort the apps by their names
    steam_apps.sort(key=lambda app: app.name)

    return steam_apps