Permalink
Browse files

Merge pull request #487 from itofzo/dev_minor_naming_tweaks

Minor naming tweaks
  • Loading branch information...
2 parents 37ad245 + f6d5562 commit aa938f8a2131c94cf6dff3f0f563460ef9b90448 @midgetspy midgetspy committed Oct 1, 2012
Showing with 56 additions and 45 deletions.
  1. +41 −39 sickbeard/postProcessor.py
  2. +15 −6 sickbeard/tv.py
View
@@ -692,52 +692,52 @@ def _is_priority(self, ep_obj, new_ep_quality):
return True
return False
-
+
def process(self):
"""
Post-process a given file
"""
-
- self._log(u"Processing "+self.file_path+" ("+str(self.nzb_name)+")")
-
- if os.path.isdir( self.file_path ):
- self._log(u"File "+self.file_path+" seems to be a directory")
+
+ self._log(u"Processing " + self.file_path + " (" + str(self.nzb_name) + ")")
+
+ if os.path.isdir(self.file_path):
+ self._log(u"File " + self.file_path + " seems to be a directory")
return False
for ignore_file in self.IGNORED_FILESTRINGS:
if ignore_file in self.file_path:
- self._log(u"File "+self.file_path+" is ignored type, skipping" )
+ self._log(u"File " + self.file_path + " is ignored type, skipping")
return False
# reset per-file stuff
self.in_history = False
-
+
# try to find the file info
(tvdb_id, season, episodes) = self._find_info()
-
+
# if we don't have it then give up
if not tvdb_id or season == None or not episodes:
return False
-
+
# retrieve/create the corresponding TVEpisode objects
ep_obj = self._get_ep_obj(tvdb_id, season, episodes)
-
+
# get the quality of the episode we're processing
new_ep_quality = self._get_quality(ep_obj)
- logger.log(u"Quality of the episode we're processing: "+str(new_ep_quality), logger.DEBUG)
-
+ logger.log(u"Quality of the episode we're processing: " + str(new_ep_quality), logger.DEBUG)
+
# see if this is a priority download (is it snatched, in history, or PROPER)
- priority_download = self._is_priority(ep_obj, new_ep_quality)
- self._log(u"Is ep a priority download: "+str(priority_download), logger.DEBUG)
-
+ priority_download = self._is_priority(ep_obj, new_ep_quality)
+ self._log(u"Is ep a priority download: " + str(priority_download), logger.DEBUG)
+
# set the status of the episodes
for curEp in [ep_obj] + ep_obj.relatedEps:
curEp.status = common.Quality.compositeStatus(common.SNATCHED, new_ep_quality)
-
+
# check for an existing file
existing_file_status = self._checkForExistingFile(ep_obj.location)
# if it's not priority then we don't want to replace smaller files in case it was a mistake
if not priority_download:
-
+
# if there's an existing file that we don't want to replace stop here
if existing_file_status in (PostProcessor.EXISTS_LARGER, PostProcessor.EXISTS_SAME):
self._log(u"File exists and we are not going to replace it because it's not smaller, quitting post-processing", logger.DEBUG)
@@ -747,41 +747,43 @@ def process(self):
elif existing_file_status != PostProcessor.DOESNT_EXIST:
self._log(u"Unknown existing file status. This should never happen, please log this as a bug.", logger.ERROR)
return False
-
+
# if the file is priority then we're going to replace it even if it exists
else:
self._log(u"This download is marked a priority download so I'm going to replace an existing file if I find one", logger.DEBUG)
-
+
# delete the existing file (and company)
for cur_ep in [ep_obj] + ep_obj.relatedEps:
try:
self._delete(cur_ep.location, associated_files=True)
# clean up any left over folders
if cur_ep.location:
helpers.delete_empty_folders(ek.ek(os.path.dirname, cur_ep.location), keep_dir=ep_obj.show._location)
- except OSError, IOError:
+ except (OSError, IOError):
raise exceptions.PostProcessingFailed("Unable to delete the existing files")
# if the show directory doesn't exist then make it if allowed
if not ek.ek(os.path.isdir, ep_obj.show._location) and sickbeard.CREATE_MISSING_SHOW_DIRS:
self._log(u"Show directory doesn't exist, creating it", logger.DEBUG)
try:
ek.ek(os.mkdir, ep_obj.show._location)
-
- except OSError, IOError:
+
+ except (OSError, IOError):
raise exceptions.PostProcessingFailed("Unable to create the show directory: " + ep_obj.show._location)
-
+
# get metadata for the show (but not episode because it hasn't been fully processed)
ep_obj.show.writeMetadata(True)
-
+
# update the ep info before we rename so the quality & release name go into the name properly
for cur_ep in [ep_obj] + ep_obj.relatedEps:
with cur_ep.lock:
cur_release_name = None
-
+
# use the best possible representation of the release name
if self.good_results[self.NZB_NAME]:
cur_release_name = self.nzb_name
+ if cur_release_name.lower().endswith('.nzb'):
+ cur_release_name = cur_release_name.rpartition('.')[0]
elif self.good_results[self.FOLDER_NAME]:
cur_release_name = self.folder_name
elif self.good_results[self.FILE_NAME]:
@@ -791,25 +793,25 @@ def process(self):
cur_release_name = self.file_name.rpartition('.')[0]
if cur_release_name:
- self._log("Found release name "+cur_release_name, logger.DEBUG)
+ self._log("Found release name " + cur_release_name, logger.DEBUG)
cur_ep.release_name = cur_release_name
else:
- logger.log("good results: "+repr(self.good_results), logger.DEBUG)
+ logger.log("good results: " + repr(self.good_results), logger.DEBUG)
cur_ep.status = common.Quality.compositeStatus(common.DOWNLOADED, new_ep_quality)
-
+
cur_ep.saveToDB()
# find the destination folder
try:
proper_path = ep_obj.proper_path()
proper_absolute_path = ek.ek(os.path.join, ep_obj.show.location, proper_path)
-
+
dest_path = ek.ek(os.path.dirname, proper_absolute_path)
except exceptions.ShowDirNotFoundException:
raise exceptions.PostProcessingFailed(u"Unable to post-process an episode if the show dir doesn't exist, quitting")
-
- self._log(u"Destination folder for this episode: "+dest_path, logger.DEBUG)
+
+ self._log(u"Destination folder for this episode: " + dest_path, logger.DEBUG)
# create any folders we need
helpers.make_dirs(dest_path)
@@ -823,23 +825,23 @@ def process(self):
else:
# if we're not renaming then there's no new base name, we'll just use the existing name
new_base_name = None
- new_file_name = self.file_name
-
+ new_file_name = self.file_name
+
try:
# move the episode and associated files to the show dir
if sickbeard.KEEP_PROCESSED_DIR:
self._copy(self.file_path, dest_path, new_base_name, sickbeard.MOVE_ASSOCIATED_FILES)
else:
self._move(self.file_path, dest_path, new_base_name, sickbeard.MOVE_ASSOCIATED_FILES)
- except OSError, IOError:
+ except (OSError, IOError):
raise exceptions.PostProcessingFailed("Unable to move the files to their new home")
-
+
# put the new location in the database
for cur_ep in [ep_obj] + ep_obj.relatedEps:
with cur_ep.lock:
cur_ep.location = ek.ek(os.path.join, dest_path, new_file_name)
cur_ep.saveToDB()
-
+
# log it to history
history.logDownload(ep_obj, self.file_path, new_ep_quality, self.release_group)
@@ -861,10 +863,10 @@ def process(self):
# do the library update for trakt
notifiers.trakt_notifier.update_library(ep_obj)
-
+
# do the library update for pyTivo
notifiers.pytivo_notifier.update_library(ep_obj)
-
+
self._run_extra_scripts(ep_obj)
return True
View
@@ -1446,9 +1446,18 @@ def dot(name):
def us(name):
return re.sub('[ -]','_', name)
-
+
+ def release_name(name):
+ if name and name.lower().endswith('.nzb'):
+ name = name.rpartition('.')[0]
+ return name
+
def release_group(name):
+ if not name:
+ return ''
+
np = NameParser(name)
+
try:
parse_result = np.parse(name)
except InvalidNameException, e:
@@ -1475,7 +1484,7 @@ def release_group(name):
'%0S': '%02d' % self.season,
'%E': str(self.episode),
'%0E': '%02d' % self.episode,
- '%RN': self.release_name,
+ '%RN': release_name(self.release_name),
'%RG': release_group(self.release_name),
'%AD': str(self.airdate).replace('-', ' '),
'%A.D': str(self.airdate).replace('-', '.'),
@@ -1497,11 +1506,11 @@ def _format_string(self, pattern, replace_map):
# do the replacements
for cur_replacement in sorted(replace_map.keys(), reverse=True):
- result_name = result_name.replace(cur_replacement, helpers.sanitizeFileName(replace_map[cur_replacement]))
- result_name = result_name.replace(cur_replacement.lower(), helpers.sanitizeFileName(replace_map[cur_replacement].lower()))
+ result_name = result_name.replace(cur_replacement, replace_map[cur_replacement])
+ result_name = result_name.replace(cur_replacement.lower(), replace_map[cur_replacement].lower())
+
+ return helpers.sanitizeFileName(result_name)
- return result_name
-
def _format_pattern(self, pattern=None, multi=None):
"""
Manipulates an episode naming pattern and then fills the template in

0 comments on commit aa938f8

Please sign in to comment.