mirror of
https://github.com/nikdoof/tvorganise.git
synced 2025-12-13 06:42:16 +00:00
pylint cleanup
This commit is contained in:
@@ -16,10 +16,39 @@ import shutil
|
|||||||
import ConfigParser
|
import ConfigParser
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
def same_partition(path1, path2):
|
||||||
|
"""
|
||||||
|
Checks to see if two paths are on the same device, returns a
|
||||||
|
bool to indicate
|
||||||
|
"""
|
||||||
|
return os.stat(path1).st_dev == os.stat(path2).st_dev
|
||||||
|
|
||||||
|
def find_files(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)
|
||||||
|
"""
|
||||||
|
filelist = []
|
||||||
|
for cfile in args:
|
||||||
|
if os.path.isdir(cfile):
|
||||||
|
for sf in os.listdir(cfile):
|
||||||
|
newpath = os.path.join(cfile, sf)
|
||||||
|
if os.path.isfile(newpath):
|
||||||
|
filelist.append(newpath)
|
||||||
|
elif os.path.isfile(cfile):
|
||||||
|
filelist.append(cfile)
|
||||||
|
return filelist
|
||||||
|
|
||||||
|
|
||||||
class TvOrganiser():
|
class TvOrganiser():
|
||||||
|
"""
|
||||||
|
TvOrganiser takes a list of files and validates them against known filename
|
||||||
|
formats, then moves the files to a specified tree layout.
|
||||||
|
"""
|
||||||
|
|
||||||
_config = {}
|
_config = {}
|
||||||
|
__logger = None
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
pass
|
||||||
@@ -31,12 +60,12 @@ class TvOrganiser():
|
|||||||
self.__logger.addHandler(logging.StreamHandler())
|
self.__logger.addHandler(logging.StreamHandler())
|
||||||
return self.__logger
|
return self.__logger
|
||||||
|
|
||||||
def _getConfig(self, file):
|
def _getConfig(self, cfile):
|
||||||
|
|
||||||
config = {}
|
config = {}
|
||||||
|
|
||||||
configpsr = ConfigParser.RawConfigParser()
|
configpsr = ConfigParser.RawConfigParser()
|
||||||
configpsr.read('tvorganise.cfg')
|
configpsr.read(cfile)
|
||||||
|
|
||||||
if configpsr.has_section('main'):
|
if configpsr.has_section('main'):
|
||||||
for k, v in configpsr.items('main'):
|
for k, v in configpsr.items('main'):
|
||||||
@@ -61,28 +90,11 @@ class TvOrganiser():
|
|||||||
self._config = config
|
self._config = config
|
||||||
return config
|
return config
|
||||||
|
|
||||||
def _findFiles(self, args):
|
def process(self, names):
|
||||||
"""
|
|
||||||
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)
|
|
||||||
"""
|
|
||||||
allfiles = []
|
|
||||||
for cfile in args:
|
|
||||||
if os.path.isdir(cfile):
|
|
||||||
for sf in os.listdir(cfile):
|
|
||||||
newpath = os.path.join(cfile, sf)
|
|
||||||
if os.path.isfile(newpath):
|
|
||||||
allfiles.append(newpath)
|
|
||||||
elif os.path.isfile(cfile):
|
|
||||||
allfiles.append(cfile)
|
|
||||||
return allfiles
|
|
||||||
|
|
||||||
def processNames(self, names, verbose=False):
|
|
||||||
"""
|
"""
|
||||||
Takes list of names, runs them though the regexs
|
Takes list of names, runs them though the regexs
|
||||||
"""
|
"""
|
||||||
allEps = []
|
episodelist = []
|
||||||
for f in names:
|
for f in names:
|
||||||
filepath, filename = os.path.split(f)
|
filepath, filename = os.path.split(f)
|
||||||
filename, ext = os.path.splitext(filename)
|
filename, ext = os.path.splitext(filename)
|
||||||
@@ -90,7 +102,7 @@ class TvOrganiser():
|
|||||||
# Remove leading . from extension
|
# Remove leading . from extension
|
||||||
ext = ext.replace(".", "", 1)
|
ext = ext.replace(".", "", 1)
|
||||||
|
|
||||||
for r in config['regex']:
|
for r in self._config['regex']:
|
||||||
match = r.match(filename)
|
match = r.match(filename)
|
||||||
if match:
|
if match:
|
||||||
showname, seasno, epno, epname = match.groups()
|
showname, seasno, epno, epname = match.groups()
|
||||||
@@ -106,22 +118,18 @@ class TvOrganiser():
|
|||||||
self._logger.debug("Seas:", seasno)
|
self._logger.debug("Seas:", seasno)
|
||||||
self._logger.debug("Ep:", epno)
|
self._logger.debug("Ep:", epno)
|
||||||
|
|
||||||
allEps.append({'file_showname': showname,
|
episodelist.append({'file_showname': showname,
|
||||||
'seasno': seasno,
|
'seasno': seasno,
|
||||||
'epno': epno,
|
'epno': epno,
|
||||||
'filepath': filepath,
|
'filepath': filepath,
|
||||||
'filename': filename,
|
'filename': filename,
|
||||||
|
'epname': epname,
|
||||||
'ext': ext})
|
'ext': ext})
|
||||||
break # Matched - to the next file!
|
break # Matched - to the next file!
|
||||||
else:
|
else:
|
||||||
self._logger.warning("Invalid name: %s" % (f))
|
self._logger.warning("Invalid name: %s" % (f))
|
||||||
|
|
||||||
return allEps
|
return episodelist
|
||||||
|
|
||||||
def _same_partition(f1, f2):
|
|
||||||
return os.stat(f1).st_dev == os.stat(f2).st_dev
|
|
||||||
|
|
||||||
###########################
|
|
||||||
|
|
||||||
def main(self):
|
def main(self):
|
||||||
parser = OptionParser(usage="%prog [options] <file or directories>")
|
parser = OptionParser(usage="%prog [options] <file or directories>")
|
||||||
@@ -146,8 +154,8 @@ class TvOrganiser():
|
|||||||
self._logger.error('Unable to find configuration file!')
|
self._logger.error('Unable to find configuration file!')
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
files = self._findFiles(args)
|
files = find_files(args)
|
||||||
files = self.processNames(files, opts.verbose)
|
files = self.process(files)
|
||||||
|
|
||||||
# Warn if no files are found, then exit
|
# Warn if no files are found, then exit
|
||||||
if len(files) == 0:
|
if len(files) == 0:
|
||||||
@@ -165,21 +173,21 @@ class TvOrganiser():
|
|||||||
|
|
||||||
if opts.always:
|
if opts.always:
|
||||||
if not os.path.exists(newpath):
|
if not os.path.exists(newpath):
|
||||||
os.mkdirs(newpath)
|
os.makedirs(newpath)
|
||||||
if os.path.exists(newfile):
|
if os.path.exists(newfile):
|
||||||
self._logger.warning("[!] File already exists, not copying")
|
self._logger.warning("[!] File already exists, not copying")
|
||||||
else:
|
else:
|
||||||
if self._same_partition(oldfile, newpath):
|
if same_partition(oldfile, newpath):
|
||||||
self._logger.info("[*] Moving file")
|
self._logger.info("[*] Moving file")
|
||||||
try:
|
try:
|
||||||
os.rename(oldfile, newfile)
|
os.rename(oldfile, newfile)
|
||||||
except Exception, errormsg:
|
except OSError, errormsg:
|
||||||
self._logger.error("[!] Error moving file! %s" % (errormsg))
|
self._logger.error("[!] Error moving file! %s" % (errormsg))
|
||||||
else:
|
else:
|
||||||
self._logger.info("[*] Copying file")
|
self._logger.info("[*] Copying file")
|
||||||
try:
|
try:
|
||||||
shutil.copy(oldfile, newfile)
|
shutil.copy(oldfile, newfile)
|
||||||
except Exception, errormsg:
|
except IOError, errormsg:
|
||||||
self._logger.error("[!] Error copying file! %s" % (errormsg))
|
self._logger.error("[!] Error copying file! %s" % (errormsg))
|
||||||
else:
|
else:
|
||||||
self._logger.info("[*] ..done")
|
self._logger.info("[*] ..done")
|
||||||
@@ -188,5 +196,5 @@ class TvOrganiser():
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
||||||
t = TvOrganiser()
|
tvorg = TvOrganiser()
|
||||||
t.main()
|
tvorg.main()
|
||||||
|
|||||||
Reference in New Issue
Block a user