Skip to content

Instantly share code, notes, and snippets.

@husio
Created May 23, 2009 15:27
Show Gist options
  • Save husio/116659 to your computer and use it in GitHub Desktop.
Save husio/116659 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8
# Copyright (c) 2009 Piotr Husiatyński. All Rights Reserved.
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
import re
import os
import datetime
_POWERWALK_YEAR = re.compile(r'^\d{4}$')
_POWERWALK_MONTH = re.compile(r'^\d{2}$')
_POWERWALK_DAY = re.compile(r'^\d{2}$')
def powerwalk(top, less_than, year=None, month=None, day=None):
"""
Modification of os.walk function.
powerwalk will walk across the directories tree, and search for path that
starts with /<year>/<month>/<day>/ path, and date `younger` than given
`less_than` datetime object.
Just like os.walk, it yields [root, dirs, files], but only when root matches
given date.
"""
names = os.listdir(top)
dirs, nondirs = [], []
for name in names:
path = os.path.join(top, name)
if os.path.isdir(path):
# check the path date /<year>/<month>/<day>
try:
if day:
# in appropriate path
dirs.append(name)
elif month:
rx = _POWERWALK_DAY.match(name)
path_day = int(rx.group())
if datetime.datetime(year, month, path_day) < less_than:
# in appropriate path, now just go deeper, without
# checking anything
for x in os.walk(path):
yield x
elif year:
rx = _POWERWALK_MONTH.match(name)
path_month = int(rx.group())
if datetime.datetime(year, path_month, 1) <= less_than:
for x in powerwalk(path, less_than, year, path_month, day):
yield x
else:
rx = _POWERWALK_YEAR.match(name)
if not rx:
# check deeper for appropriate path
for x in powerwalk(path, less_than, year, month, day):
yield x
path_year = int(rx.group())
if path_year <= less_than.year:
for x in powerwalk(path, less_than, path_year, month, day):
yield x
except (AttributeError, TypeError, ValueError):
dirs.append(name)
else:
nondirs.append(name)
# show only those paths that matches .*/<year>/<month>/<day>/.*
if day:
for dir in dirs:
for x in os.walk(os.path.join(top, dir)):
yield x
yield top, dirs, nondirs
# -*- coding: utf-8
# Copyright (c) 2009 Piotr Husiatyński. All Rights Reserved.
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
import re
import os
import datetime
_POWERWALK_YEAR = re.compile(r'^\d{4}$')
_POWERWALK_MONTH = re.compile(r'^\d{2}$')
_POWERWALK_DAY = re.compile(r'^\d{2}$')
def powerwalk(top, less_than, more_than, year=None, month=None, day=None):
"""
Modification of os.walk function.
powerwalk will walk across the directories tree, and search for path that
starts with /<year>/<month>/<day>/ path, and date is between `less_than`
and `more_than` datetime objects.
Just like os.walk, it yields [root, dirs, files], but only when root matches
given date.
"""
names = os.listdir(top)
dirs, nondirs = [], []
for name in names:
path = os.path.join(top, name)
if os.path.isdir(path):
# check the path date /<year>/<month>/<day>
try:
if day:
# in appropriate path
dirs.append(name)
elif month:
rx = _POWERWALK_DAY.match(name)
path_day = int(rx.group())
d = datetime.datetime(year, month, path_day)
if d < less_than and d > more_than:
# in appropriate path, now just go deeper, without
# checking anything
for x in os.walk(path):
yield x
elif year:
rx = _POWERWALK_MONTH.match(name)
path_month = int(rx.group())
d = datetime.datetime(year, path_month, 1)
if d <= less_than and d >= more_than:
for x in powerwalk(path, less_than, year, path_month, day):
yield x
else:
rx = _POWERWALK_YEAR.match(name)
if not rx:
# check deeper for appropriate path
for x in powerwalk(path, less_than, year, month, day):
yield x
path_year = int(rx.group())
if path_year <= less_than.year and path_year >= more_than.year:
for x in powerwalk(path, less_than, path_year, month, day):
yield x
except (AttributeError, TypeError, ValueError):
dirs.append(name)
else:
nondirs.append(name)
# show only those paths that matches .*/<year>/<month>/<day>/.*
if day:
for dir in dirs:
for x in os.walk(os.path.join(top, dir)):
yield x
yield top, dirs, nondirs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment