Removed extraneous loop branching, fixed TorrentToMedia for PEP standards

This commit is contained in:
Berkona 2013-02-26 18:33:08 -05:00
commit 054e9bf59c

View file

@ -23,10 +23,6 @@ nzbtomedia_configure_logging(os.path.dirname(sys.argv[0]))
Logger = logging.getLogger(__name__)
def category_search_recurs(inputDirectory, inputName, root, categories):
pass
def category_search(inputDirectory, inputName, inputCategory, root, categories):
categorySearch = [os.path.normpath(inputDirectory), ""] # initializie
notfound = 0
@ -51,6 +47,7 @@ def category_search(inputDirectory, inputName, inputCategory, root, categories):
else:
Logger.error("SEARCH: Could not identify Category of Torrent Name in the directory structure. Please check downloader settings. Exiting")
sys.exit(-1)
if categorySearch2[1] in categories:
Logger.debug("SEARCH: Found Category: %s in directory structure", categorySearch2[1])
if not inputCategory:
@ -123,6 +120,7 @@ def category_search(inputDirectory, inputName, inputCategory, root, categories):
return inputDirectory, inputName, inputCategory, root
def is_sample(filePath, inputName):
# 200 MB in bytes
SIZE_CUTOFF = 200 * 1024 * 1024
@ -132,6 +130,7 @@ def is_sample(filePath, inputName):
else:
return False
def copy_link(source, target, useLink, outputDestination):
# Create destination folder
if not os.path.exists(outputDestination):
@ -158,6 +157,7 @@ def copy_link(source, target, useLink, outputDestination):
shutil.copy(source, target)
return True
def unpack(dirpath, file, destination):
# Using Windows
if os.name == 'nt':
@ -165,12 +165,10 @@ def unpack(dirpath, file, destination):
cmd_7zip = [extractionTool, 'x -y']
ext_7zip = [".rar", ".zip", ".tar.gz", "tgz", ".tar.bz2", ".tbz", ".tar.lzma", ".tlz", ".7z", ".xz"]
EXTRACT_COMMANDS = dict.fromkeys(ext_7zip, cmd_7zip)
# Using linux
elif os.name == 'posix':
Logger.info("EXTRACTOR: We are using *nix")
EXTRACT_COMMANDS = {".rar": ["unrar", "e"], ".zip": ["unzip", ""], ".tar.gz": ["tar", "xzf"], ".tgz": ["tar", "xzf"], ".tar.bz2": ["tar", "xjf"], ".tbz": ["tar", "xjf"], ".tar.lzma": ["tar", "--lzma xf"], ".tlz": ["tar", "--lzma xf"], ".txz": ["tar", "--xz xf"], ".7z": ["7zr", "x"], }
# Need to add a check for which commands that can be utilized in *nix systems..
else:
Logger.error("EXTRACTOR: Unknown OS, exiting")
@ -228,6 +226,7 @@ def unpack(dirpath, file, destination):
os.chdir(pwd) # Go back to our Original Working Directory
return True
def flatten(outputDestination):
Logger.info("FLATTEN: Flattening directory: %s", outputDestination)
for dirpath, dirnames, filenames in os.walk(outputDestination): # Flatten out the directory to make postprocessing easier
@ -242,6 +241,7 @@ def flatten(outputDestination):
Logger.info("FLATTEN: Could not flatten %s", source)
removeEmptyFolders(outputDestination) # Cleanup empty directories
def removeEmptyFolders(path):
Logger.info("REMOVER: Removing empty folders in: %s", path)
if not os.path.isdir(path):
@ -261,6 +261,7 @@ def removeEmptyFolders(path):
Logger.debug("REMOVER: Removing empty folder: %s", path)
os.rmdir(path)
Logger.info("TorrentToMedia %s", VERSION)
config = ConfigParser.ConfigParser()
configFilename = os.path.join(os.path.dirname(sys.argv[0]), "autoProcessMedia.cfg")
@ -323,23 +324,21 @@ else:
sys.exit(-1)
Logger.debug("MAIN: Scanning files in directory: %s", inputDirectory)
numloop = 0
if root == 1:
Logger.debug("MAIN: Looking for %s in filename", inputName)
elif root == 2:
Logger.debug("MAIN: Looking for files with modified/created dates less than 5 minutes old.")
now = datetime.datetime.now()
for dirpath, dirnames, filenames in os.walk(inputDirectory):
for file in filenames:
if root == 1:
if numloop == 0: # only log this once.
Logger.debug("MAIN: Looking for %s in filename", inputName)
numloop = numloop + 1
if (inputName in file) or (os.path.splitext(file)[0] in inputName):
pass # This file does match the Torrent name
Logger.debug("Found file %s that matches Torrent Name %s", file, inputName)
else:
continue # This file does not match the Torrent name, skip it
if root == 2:
if numloop == 0: # only log this once.
Logger.debug("MAIN: Looking for files with modified/created dates less than 5 minutes old.")
numloop = numloop + 1
mtime_lapse = now - datetime.datetime.fromtimestamp(os.path.getmtime(os.path.join(dirpath, file)))
ctime_lapse = now - datetime.datetime.fromtimestamp(os.path.getctime(os.path.join(dirpath, file)))
if (mtime_lapse < datetime.timedelta(minutes=5)) or (ctime_lapse < datetime.timedelta(minutes=5)):
@ -432,5 +431,6 @@ elif inputCategory == tvCategory:
if inputHash and useLink:
Logger.debug("MAIN: We are using hardlinks with uTorrent, calling uTorrent to resume download")
utorrentClass.start(inputHash)
sys.stdout = old_stdout
log_file.close()