From a19a216bc60160c162e616145ef091dd18ce4e61 Mon Sep 17 00:00:00 2001 From: Skip Montanaro Date: Tue, 16 Feb 2021 14:40:46 -0600 Subject: Python 0.9.1 as posted in alt.sources --- lib/macglob.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 lib/macglob.py (limited to 'lib/macglob.py') diff --git a/lib/macglob.py b/lib/macglob.py new file mode 100644 index 0000000..cad2c79 --- /dev/null +++ b/lib/macglob.py @@ -0,0 +1,46 @@ +# Module 'macglob' -- version of 'glob' for the Macintosh. + +# XXX At least one bug is left: a pattern like '*:' is treated +# XXX as a relative pathname (and returns as if it was ':*:'). + +import mac +import macpath +import fnmatch + +def glob(pathname): + if not has_magic(pathname): return [pathname] + dirname, basename = macpath.split(pathname) + if has_magic(dirname): + if dirname[-1:] = ':': dirname = dirname[:-1] + list = glob(dirname) + else: + list = [dirname] + if not has_magic(basename): + result = [] + for dirname in list: + if basename or macpath.isdir(dirname): + name = macpath.cat(dirname, basename) + if macpath.exists(name): + result.append(name) + else: + result = [] + for dirname in list: + sublist = glob1(dirname, basename) + for name in sublist: + result.append(macpath.cat(dirname, name)) + return result + +def glob1(dirname, pattern): + if not dirname: dirname = ':' + try: + names = mac.listdir(dirname) + except mac.error: + return [] + result = [] + for name in names: + if name[0] <> '.' or pattern[0] = '.': + if fnmatch.fnmatch(name, pattern): result.append(name) + return result + +def has_magic(s): + return '*' in s or '?' in s or '[' in s -- cgit v1.2.3