Wrap functions in a class.

This commit is contained in:
Andrew Williams
2010-01-26 12:47:31 +00:00
parent de71407c19
commit 3e9de23e1a

View File

@@ -14,12 +14,14 @@ import shutil
import ConfigParser
import logging
def getLogger():
class TvOrganiser():
def _getLogger(self):
lgr = logging.getLogger('tvorganise')
lgr.addHandler(logging.StreamHandler())
return lgr
def getConfig(file):
def _getConfig(self, file):
config = {}
@@ -46,10 +48,11 @@ def getConfig(file):
config['regex'] = regex
self._config = config
return config
def findFiles(args):
def _findFiles(self, args):
"""
Takes a list of files/folders, grabs files inside them. Does not recurse
more than one level (if a folder is supplied, it will list files within)
@@ -65,7 +68,7 @@ def findFiles(args):
allfiles.append(cfile)
return allfiles
def processNames(names, verbose=False):
def processNames(self, names, verbose=False):
"""
Takes list of names, runs them though the regexs
"""
@@ -109,28 +112,12 @@ def processNames(names, verbose=False):
return allEps
def make_path(path):
try:
os.makedirs(path)
except OSError:
#print "Couldn't make path"
pass
def does_file_exist(path):
try:
os.stat(path)
except OSError:
file_exists = False
else:
file_exists = True
return file_exists
def same_partition(f1, f2):
def _same_partition(f1, f2):
return os.stat(f1).st_dev == os.stat(f2).st_dev
###########################
###########################
def main():
def main(self):
parser = OptionParser(usage="%prog [options] <file or directories>")
parser.add_option("-a", "--always", dest = "always",
action="store_true", default = False,
@@ -170,15 +157,14 @@ def main():
print "Old path:", oldfile
print "New path:", newfile
ans= "always"
if ans == "always": opts.always = True
if ans or opts.always:
make_path(newpath)
file_exists = does_file_exist(newfile)
if file_exists:
if opts.always:
if not os.path.exists(newpath):
os.mkdirs(newpath)
if os.path.exists(newfile):
print "[!] File already exists, not copying"
else:
if same_partition(oldfile, newpath):
if self._same_partition(oldfile, newpath):
print "[*] Moving file"
try:
os.rename(oldfile, newfile)
@@ -196,4 +182,6 @@ def main():
print "Skipping file"
if __name__ == '__main__':
main()
t = TvOrganiser()
t.main()