From 140644d8b6e68a2a6ed3e0cfc491cbf7ce437819 Mon Sep 17 00:00:00 2001 From: rdb Date: Sun, 20 May 2018 11:50:32 +0200 Subject: [PATCH] stdpy: add Python 3 stuff to glob module, remove re dependency --- direct/src/stdpy/glob.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/direct/src/stdpy/glob.py b/direct/src/stdpy/glob.py index 28eee2cb6e..d1a4946c0e 100755 --- a/direct/src/stdpy/glob.py +++ b/direct/src/stdpy/glob.py @@ -4,7 +4,6 @@ virtual file system. """ import sys import os -import re import fnmatch from direct.stdpy import file @@ -76,7 +75,27 @@ def glob0(dirname, basename): return [] -magic_check = re.compile('[*?[]') - def has_magic(s): - return magic_check.search(s) is not None + if isinstance(s, bytes): + return b'*' in s or b'?' in s or b'[' in s + else: + return '*' in s or '?' in s or '[' in s + +def escape(pathname): + drive, pathname = os.path.splitdrive(pathname) + if sys.version_info >= (3, 0) and isinstance(pathname, bytes): + newpath = bytearray(drive) + for c in pathname: + if c == 42 or c == 63 or c == 91: + newpath += bytes((91, c, 93)) + else: + newpath.append(c) + return bytes(newpath) + else: + newpath = drive + for c in pathname: + if c == '*' or c == '?' or c == '[': + newpath += '[' + c + ']' + else: + newpath += c + return newpath