Skip to content

Commit

Permalink
Tweak docstrings & variables for recursion functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
intgr committed Aug 18, 2014
1 parent f5e97ef commit f8d7c37
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions topy.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ def handle_file(regs, filename):


def walk_dir_tree(dirpath):
"""Walk a directory tree, yielding files exclude hidden directories
(*nix only), files and also binary files"""
"""Walk a directory tree, yielding filenames recursively, except those starting with a dot (.)"""

for root, dirs, files in os.walk(dirpath):
dirs[:] = (d for d in dirs if not d.startswith("."))
Expand All @@ -149,19 +148,21 @@ def walk_dir_tree(dirpath):
yield os.path.join(root, f)


def flatten_files(filelist):
""" Given a list of dirs and filenames, yield a
flattened list of filenames"""
def flatten_files(paths):
"""Given a list of directories and filenames, yield a flattened sequence of filenames."""

for item in filelist:
if os.path.isdir(item):
for filename in walk_dir_tree(item):
for path in paths:
if os.path.isdir(path):
# Once we can drop Python < 3.3 support, this should use 'yield from'
for filename in walk_dir_tree(path):
yield filename
else:
yield item
# Filename, or the path cannot be accessed (privilege errors, file not found, etc)
yield path


parser = OptionParser(usage="%prog [options] FILES OR DIRECTORIES...")
# Argument parsing. Keep this together with main()
parser = OptionParser(usage="%prog [options] FILES/DIRS...")
parser.add_option("-q", "--quiet",
action='store_true', dest='quiet', default=False,
help="silence information messages")
Expand All @@ -173,22 +174,22 @@ def flatten_files(filelist):
def main():
global opts

(opts, files) = parser.parse_args()
(opts, paths) = parser.parse_args()

logging.basicConfig(
level=logging.WARNING if opts.quiet else logging.INFO,
format='%(message)s'
)

if not files:
logging.error("No files specified")
if not paths:
logging.error("No paths specified")
parser.print_help()
exit(1)

path = os.path.join(os.path.dirname(__file__), RETF_FILENAME)
regs = load_rules(path)

for filename in flatten_files(files):
for filename in flatten_files(paths):
handle_file(regs, filename)


Expand Down

0 comments on commit f8d7c37

Please sign in to comment.