Staging
v0.8.1
Revision 7c9bcf82436189e69db28df1b3a7cc8d7dd7b5c4 authored by Moshe Zadka on 31 March 2001, 14:26:54 UTC, committed by Moshe Zadka on 31 March 2001, 14:26:54 UTC
  which can be called from a start tag handler.  When the corresponding end
  tag is read the flag is cleared.  However, it didn't get cleared when
  the start tag was for an empty element of the type <tag .../>.  This
  modification fixes the problem.
1 parent ea96661
Raw File
glob.py
"""Filename globbing utility."""

import os
import fnmatch
import re


def glob(pathname):
	"""Return a list of paths matching a pathname pattern.

	The pattern may contain simple shell-style wildcards a la fnmatch.

	"""
	if not has_magic(pathname):
		if os.path.exists(pathname):
			return [pathname]
		else:
			return []
	dirname, basename = os.path.split(pathname)
	if has_magic(dirname):
		list = glob(dirname)
	else:
		list = [dirname]
	if not has_magic(basename):
		result = []
		for dirname in list:
			if basename or os.path.isdir(dirname):
				name = os.path.join(dirname, basename)
				if os.path.exists(name):
					result.append(name)
	else:
		result = []
		for dirname in list:
			sublist = glob1(dirname, basename)
			for name in sublist:
				result.append(os.path.join(dirname, name))
	return result

def glob1(dirname, pattern):
	if not dirname: dirname = os.curdir
	try:
		names = os.listdir(dirname)
	except os.error:
		return []
	result = []
	for name in names:
		if name[0] != '.' or pattern[0] == '.':
			if fnmatch.fnmatch(name, pattern):
				result.append(name)
	return result


magic_check = re.compile('[*?[]')

def has_magic(s):
	return magic_check.search(s) is not None
back to top