- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
from os import walk
from time import time
from abc import ABCMeta, abstractmethod
class Reporter(object):
__metaclass__=ABCMeta
@abstractmethod
def report(self, catalogs_cntr, files_ctnr, speed):
""" output catalogs_cntr, files_ctnr, speed to somewhere """
#pass
class ConsoleIndicator(Reporter):
def __init__(self, out_splitter):
self.__out_splitter=out_splitter
def report(self, catalogs_cntr, files_ctnr, speed):
if self.__out_splitter and self.__out_splitter.pipes:
_result = "\rcataloges:" + repr(catalogs_cntr).rjust(8)
_result += "\tfiles:" + repr(files_ctnr).rjust(13)
_result += "\tspeed: %12.3f"%speed + " files/s"
self.__out_splitter.write(_result)
self.__out_splitter.flush()
class CatalogsWalker(object):
""" """
def __init__(self, catalogs, reporter=None):
""" """
self.__files_cntr, self.__catalogs_cntr, self.__start_time, self.__speed = 0, 0, 0, 0
self.__reporter = reporter
self.__catalogs = catalogs if hasattr(catalogs, "__iter__") else [catalogs]
def __iter__(self):
self.__files_cntr, self.__catalogs_cntr, self.__start_time, self.__speed = 0, 0, 0, 0
self.__start_time=time()
for catalog in self.__catalogs:
print catalog
for p,d,ns in walk(catalog):
_ = d;
self.__catalogs_cntr+=1
for n in ns:
self.__files_cntr+=1
self.__speed=self.__files_cntr/(time()-self.__start_time)
if self.__reporter:
self.__reporter.report(self.__catalogs_cntr, self.__files_cntr, self.__speed)
yield p,n
@property
def reporter(self):
return self.__reporter
@property
def catalog_counter(self):
return self.__catalogs_cntr
@property
def file_counter(self):
return self.__files_cntr
@property
def start_time(self):
return self.__start_time
@property
def speed(self):
return self.__speed
По мотивам http://govnokod.ru/17181 Учтены замечания некого Анонимус http://govnokod.ru/17181#comment256577. Надеюсь что будут еще замечания, такие же конструктивные и полезные.