Convert to use logging instead of print, allows for more control.

This commit is contained in:
2010-01-26 13:04:40 +00:00
parent d66811c415
commit 18f8e298fd

View File

@@ -15,11 +15,18 @@ import ConfigParser
import logging import logging
class TvOrganiser(): class TvOrganiser():
def _getLogger(self): _config = {}
lgr = logging.getLogger('tvorganise')
lgr.addHandler(logging.StreamHandler()) def __init__(self):
return lgr pass
@property
def _logger(self):
if not hasattr(self, "__logger"):
self.__logger = logging.getLogger(self.__class__.__name__)
self.__logger.addHandler(logging.StreamHandler())
return self.__logger
def _getConfig(self, file): def _getConfig(self, file):
@@ -90,14 +97,11 @@ class TvOrganiser():
seasno, epno = int(seasno), int(epno) seasno, epno = int(seasno), int(epno)
if verbose: self._logger.debug("File:", filename)
print "*"*20 self._logger.debug("Pattern:", r.pattern)
print "File:", filename self._logger.debug("Showname:", showname)
print "Pattern:", r.pattern self._logger.debug("Seas:", seasno)
print "Showname:", showname self._logger.debug("Ep:", epno)
print "Seas:", seasno
print "Ep:", epno
print "*"*20
allEps.append({ 'file_showname':showname, allEps.append({ 'file_showname':showname,
'seasno':seasno, 'seasno':seasno,
@@ -108,7 +112,7 @@ class TvOrganiser():
}) })
break # Matched - to the next file! break # Matched - to the next file!
else: else:
print "Invalid name: %s" % (f) self._logger.warning("Invalid name: %s" % (f))
return allEps return allEps
@@ -136,17 +140,17 @@ class TvOrganiser():
opts, args = parser.parse_args() opts, args = parser.parse_args()
if os.path.exists(opts.config): if os.path.exists(opts.config):
config = getConfig(opts.config) config = self._getConfig(opts.config)
else: else:
print 'Unable to find configuration file!' self._logger.error('Unable to find configuration file!')
sys.exit(1) sys.exit(1)
files = findFiles(args) files = self._findFiles(args)
files = processNames(files, opts.verbose) files = self.processNames(files, opts.verbose)
# Warn if no files are found, then exit # Warn if no files are found, then exit
if len(files) == 0: if len(files) == 0:
print 'No files found' self._logger.error('No files found')
sys.exit(0) sys.exit(0)
for name in files: for name in files:
@@ -154,32 +158,32 @@ class TvOrganiser():
newpath = config['target_path'] % name newpath = config['target_path'] % name
newfile = os.path.join(newpath, name['filename']) + "." + name['ext'] newfile = os.path.join(newpath, name['filename']) + "." + name['ext']
print "Old path:", oldfile self._logger.info("Old path:", oldfile)
print "New path:", newfile self._logger.info("New path:", newfile)
ans= "always" ans= "always"
if opts.always: if opts.always:
if not os.path.exists(newpath): if not os.path.exists(newpath):
os.mkdirs(newpath) os.mkdirs(newpath)
if os.path.exists(newfile): if os.path.exists(newfile):
print "[!] File already exists, not copying" self._logger.warning("[!] File already exists, not copying")
else: else:
if self._same_partition(oldfile, newpath): if self._same_partition(oldfile, newpath):
print "[*] Moving file" self._logger.info("[*] Moving file")
try: try:
os.rename(oldfile, newfile) os.rename(oldfile, newfile)
except Exception, errormsg: except Exception, errormsg:
print "[!] Error moving file! %s" % (errormsg) self._logger.error("[!] Error moving file! %s" % (errormsg))
else: else:
print "[*] Copying file" self._logger.info("[*] Copying file")
try: try:
shutil.copy(oldfile, newfile) shutil.copy(oldfile, newfile)
except Exception, errormsg: except Exception, errormsg:
print "[!] Error copying file! %s" % (errormsg) self._logger.error("[!] Error copying file! %s" % (errormsg))
else: else:
print "[*] ..done" self._logger.info("[*] ..done")
else: else:
print "Skipping file" self._logger.warning("Skipping file")
if __name__ == '__main__': if __name__ == '__main__':