"""
Usage:
> python visitor.py code startdir [string]

codes:
1 - list directories and files in startdir
2 - search string in files
3 - 1 + 2

Example:
> python visitor.py 2 C:\Test findme
"""

import os
import sys


class FileVisitor:
    def __init__(self, context=None, trace=2):
        self.fcount = 0
        self.dcount = 0
        self.context = context
        self.trace = trace

    def run(self, startdir=os.curdir, reset=True):
        if reset:
            self.reset()
        for (thisdir, subshere, fileshere) in os.walk(startdir):
            self.visitdir(thisdir)
            for fname in fileshere:
                filepath = os.path.join(thisdir, fname)
                self.visitfile(filepath)

    def reset(self):
        self.fcount = self.dcount = 0

    def visitdir(self, dirpath):
        self.dcount += 1
        if self.trace > 0:
            print(dirpath, '...')

    def visitfile(self, filepath):
        self.fcount += 1
        if self.trace > 1:
            print(self.fcount, '=>', filepath)


class SearchVisitor(FileVisitor):
    skipexts = []
    testexts = ['.py', '.txt']
    #skipexts = ['.doc', '.jpg']

    def __init__(self, searchkey, trace=2):
        FileVisitor.__init__(self, searchkey, trace)
        self.scount = 0

    def reset(self):
        self.scount = 0

    def candidate(self, fname):
        ext = os.path.splitext(fname)[1]
        if self.testexts:
            return ext in self.testexts
        else:
            return ext not in self.skipexts

    def visitfile(self, fname):
        FileVisitor.visitfile(self, fname)
        if not self.candidate(fname):
            if self.trace > 0:
                print('Skipping', fname)
        else:
            text = open(fname).read()
            if self.context in text:
                self.visitmatch(fname, text)
                self.scount += 1

    def visitmatch(self, fname, text):
        print('{0} has {1}'.format(fname, self.context))


if __name__ == '__main__':
    dolist = 1
    dosearch = 2
    donext = 4

    def selftest(testmask):
        if testmask & dolist:
            visitor = FileVisitor(trace=2)
            visitor.run(sys.argv[2])
            print('Visited {0} files and {1} dirs'.format(visitor.fcount, visitor.dcount))
        if testmask & dosearch:
            visitor = SearchVisitor(sys.argv[3], trace=0)
            visitor.run(sys.argv[2])
            print('Found in {0} files, visited {1}'.format(visitor.scount, visitor.fcount))

    selftest(int(sys.argv[1]))

d162b3c14d42d3a6dd9db812ebd9322d39463084
<mask token>


class FileVisitor:
    <mask token>
    <mask token>
    <mask token>
    <mask token>
    <mask token>


class SearchVisitor(FileVisitor):
    skipexts = []
    testexts = ['.py', '.txt']

    def __init__(self, searchkey, trace=2):
        FileVisitor.__init__(self, searchkey, trace)
        self.scount = 0

    def reset(self):
        self.scount = 0

    def candidate(self, fname):
        ext = os.path.splitext(fname)[1]
        if self.testexts:
            return ext in self.testexts
        else:
            return ext not in self.skipexts

    def visitfile(self, fname):
        FileVisitor.visitfile(self, fname)
        if not self.candidate(fname):
            if self.trace > 0:
                print('Skipping', fname)
        else:
            text = open(fname).read()
            if self.context in text:
                self.visitmatch(fname, text)
                self.scount += 1

    def visitmatch(self, fname, text):
        print('{0} has {1}'.format(fname, self.context))


<mask token>

<mask token>


class FileVisitor:

    def __init__(self, context=None, trace=2):
        self.fcount = 0
        self.dcount = 0
        self.context = context
        self.trace = trace

    def run(self, startdir=os.curdir, reset=True):
        if reset:
            self.reset()
        for thisdir, subshere, fileshere in os.walk(startdir):
            self.visitdir(thisdir)
            for fname in fileshere:
                filepath = os.path.join(thisdir, fname)
                self.visitfile(filepath)

    def reset(self):
        self.fcount = self.dcount = 0
    <mask token>
    <mask token>


class SearchVisitor(FileVisitor):
    skipexts = []
    testexts = ['.py', '.txt']

    def __init__(self, searchkey, trace=2):
        FileVisitor.__init__(self, searchkey, trace)
        self.scount = 0

    def reset(self):
        self.scount = 0

    def candidate(self, fname):
        ext = os.path.splitext(fname)[1]
        if self.testexts:
            return ext in self.testexts
        else:
            return ext not in self.skipexts

    def visitfile(self, fname):
        FileVisitor.visitfile(self, fname)
        if not self.candidate(fname):
            if self.trace > 0:
                print('Skipping', fname)
        else:
            text = open(fname).read()
            if self.context in text:
                self.visitmatch(fname, text)
                self.scount += 1

    def visitmatch(self, fname, text):
        print('{0} has {1}'.format(fname, self.context))


<mask token>

<mask token>


class FileVisitor:

    def __init__(self, context=None, trace=2):
        self.fcount = 0
        self.dcount = 0
        self.context = context
        self.trace = trace

    def run(self, startdir=os.curdir, reset=True):
        if reset:
            self.reset()
        for thisdir, subshere, fileshere in os.walk(startdir):
            self.visitdir(thisdir)
            for fname in fileshere:
                filepath = os.path.join(thisdir, fname)
                self.visitfile(filepath)

    def reset(self):
        self.fcount = self.dcount = 0
    <mask token>

    def visitfile(self, filepath):
        self.fcount += 1
        if self.trace > 1:
            print(self.fcount, '=>', filepath)


class SearchVisitor(FileVisitor):
    skipexts = []
    testexts = ['.py', '.txt']

    def __init__(self, searchkey, trace=2):
        FileVisitor.__init__(self, searchkey, trace)
        self.scount = 0

    def reset(self):
        self.scount = 0

    def candidate(self, fname):
        ext = os.path.splitext(fname)[1]
        if self.testexts:
            return ext in self.testexts
        else:
            return ext not in self.skipexts

    def visitfile(self, fname):
        FileVisitor.visitfile(self, fname)
        if not self.candidate(fname):
            if self.trace > 0:
                print('Skipping', fname)
        else:
            text = open(fname).read()
            if self.context in text:
                self.visitmatch(fname, text)
                self.scount += 1

    def visitmatch(self, fname, text):
        print('{0} has {1}'.format(fname, self.context))


<mask token>

<mask token>
import os
import sys


class FileVisitor:

    def __init__(self, context=None, trace=2):
        self.fcount = 0
        self.dcount = 0
        self.context = context
        self.trace = trace

    def run(self, startdir=os.curdir, reset=True):
        if reset:
            self.reset()
        for thisdir, subshere, fileshere in os.walk(startdir):
            self.visitdir(thisdir)
            for fname in fileshere:
                filepath = os.path.join(thisdir, fname)
                self.visitfile(filepath)

    def reset(self):
        self.fcount = self.dcount = 0

    def visitdir(self, dirpath):
        self.dcount += 1
        if self.trace > 0:
            print(dirpath, '...')

    def visitfile(self, filepath):
        self.fcount += 1
        if self.trace > 1:
            print(self.fcount, '=>', filepath)


class SearchVisitor(FileVisitor):
    skipexts = []
    testexts = ['.py', '.txt']

    def __init__(self, searchkey, trace=2):
        FileVisitor.__init__(self, searchkey, trace)
        self.scount = 0

    def reset(self):
        self.scount = 0

    def candidate(self, fname):
        ext = os.path.splitext(fname)[1]
        if self.testexts:
            return ext in self.testexts
        else:
            return ext not in self.skipexts

    def visitfile(self, fname):
        FileVisitor.visitfile(self, fname)
        if not self.candidate(fname):
            if self.trace > 0:
                print('Skipping', fname)
        else:
            text = open(fname).read()
            if self.context in text:
                self.visitmatch(fname, text)
                self.scount += 1

    def visitmatch(self, fname, text):
        print('{0} has {1}'.format(fname, self.context))


if __name__ == '__main__':
    dolist = 1
    dosearch = 2
    donext = 4

    def selftest(testmask):
        if testmask & dolist:
            visitor = FileVisitor(trace=2)
            visitor.run(sys.argv[2])
            print('Visited {0} files and {1} dirs'.format(visitor.fcount,
                visitor.dcount))
        if testmask & dosearch:
            visitor = SearchVisitor(sys.argv[3], trace=0)
            visitor.run(sys.argv[2])
            print('Found in {0} files, visited {1}'.format(visitor.scount,
                visitor.fcount))
    selftest(int(sys.argv[1]))

"""
Usage:
> python visitor.py code startdir [string]

codes:
1 - list directories and files in startdir
2 - search string in files
3 - 1 + 2

Example:
> python visitor.py 2 C:\Test findme
"""

import os
import sys


class FileVisitor:
    def __init__(self, context=None, trace=2):
        self.fcount = 0
        self.dcount = 0
        self.context = context
        self.trace = trace

    def run(self, startdir=os.curdir, reset=True):
        if reset:
            self.reset()
        for (thisdir, subshere, fileshere) in os.walk(startdir):
            self.visitdir(thisdir)
            for fname in fileshere:
                filepath = os.path.join(thisdir, fname)
                self.visitfile(filepath)

    def reset(self):
        self.fcount = self.dcount = 0

    def visitdir(self, dirpath):
        self.dcount += 1
        if self.trace > 0:
            print(dirpath, '...')

    def visitfile(self, filepath):
        self.fcount += 1
        if self.trace > 1:
            print(self.fcount, '=>', filepath)


class SearchVisitor(FileVisitor):
    skipexts = []
    testexts = ['.py', '.txt']
    #skipexts = ['.doc', '.jpg']

    def __init__(self, searchkey, trace=2):
        FileVisitor.__init__(self, searchkey, trace)
        self.scount = 0

    def reset(self):
        self.scount = 0

    def candidate(self, fname):
        ext = os.path.splitext(fname)[1]
        if self.testexts:
            return ext in self.testexts
        else:
            return ext not in self.skipexts

    def visitfile(self, fname):
        FileVisitor.visitfile(self, fname)
        if not self.candidate(fname):
            if self.trace > 0:
                print('Skipping', fname)
        else:
            text = open(fname).read()
            if self.context in text:
                self.visitmatch(fname, text)
                self.scount += 1

    def visitmatch(self, fname, text):
        print('{0} has {1}'.format(fname, self.context))


if __name__ == '__main__':
    dolist = 1
    dosearch = 2
    donext = 4

    def selftest(testmask):
        if testmask & dolist:
            visitor = FileVisitor(trace=2)
            visitor.run(sys.argv[2])
            print('Visited {0} files and {1} dirs'.format(visitor.fcount, visitor.dcount))
        if testmask & dosearch:
            visitor = SearchVisitor(sys.argv[3], trace=0)
            visitor.run(sys.argv[2])
            print('Found in {0} files, visited {1}'.format(visitor.scount, visitor.fcount))

    selftest(int(sys.argv[1]))
