本文整理汇总了Python中lib.drupy.DrupyPHP.scandir方法的典型用法代码示例。如果您正苦于以下问题:Python DrupyPHP.scandir方法的具体用法?Python DrupyPHP.scandir怎么用?Python DrupyPHP.scandir使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lib.drupy.DrupyPHP
的用法示例。
在下文中一共展示了DrupyPHP.scandir方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: scan_directory
# 需要导入模块: from lib.drupy import DrupyPHP [as 别名]
# 或者: from lib.drupy.DrupyPHP import scandir [as 别名]
def scan_directory(dir, mask, nomask = ['.', '..', 'CVS'], \
callback = 0, recurse = True, key = 'filename', min_depth = 0, depth = 0):
"""
Finds all files that match a given mask in a given directory.
Directories and files beginning with a period are excluded; this
prevents hidden files and directories (such as SVN working directories)
from being scanned.
@param dir
The base directory for the scan, without trailing slash.
@param mask
The regular expression of the files to find.
@param nomask
An array of files/directories to ignore.
@param callback
The callback function to call for each match.
@param recurse
When True, the directory scan will recurse the entire tree
starting at the provided directory.
@param key
The key to be used for the returned array of files + Possible
values are "filename", for the path starting with dir,
"basename", for the basename of the file, and "name" for the name
of the file without an extension.
@param min_depth
Minimum depth of directories to return files from.
@param depth
Current depth of recursion + This parameter is only used
internally and should not be passed.
@return
An associative array (keyed on the provided key) of objects with
"path", "basename", and "name" members corresponding to the
matching files.
"""
key = (key if php.in_array(key, \
('filename', 'basename', 'name')) else 'filename')
files = []
if php.is_dir(dir):
dir_files = php.scandir(dir)
for file in dir_files:
if (not php.in_array(file, nomask) and file[0] != '.'):
if (php.is_dir("%s/%s" % (dir, file)) and recurse):
# Give priority to files in this folder by
# merging them in after any subdirectory files.
files = php.array_merge(file_scan_directory("%s/%s" % (dir, file), \
mask, nomask, callback, recurse, key, min_depth, depth + 1), files)
elif (depth >= min_depth and ereg(mask, file)):
# Always use this match over anything already
# set in files with the same $key.
filename = "%s/%s" % (dir, file)
basename_ = php.basename(file)
name = php.substr(basename_, 0, php.strrpos(basename_, '.'))
files[key] = php.stdClass()
files[key].filename = filename
files[key].basename = basename_
files[key].name = name
if (callback):
callback(filename)
return files