def test_remove_extension(self): """ Test remove extension """ test_cases = { None: None, 42: 42, '': '', '.': '.', 'filename': 'filename', '.bashrc': '.bashrc', '.nzb': '.nzb', 'file.nzb': 'file', 'file.name.nzb': 'file.name', '.torrent': '.torrent', 'file.torrent': 'file', 'file.name.torrent': 'file.name', '.avi': '.avi', 'file.avi': 'file', 'file.name.avi': 'file.name', } unicode_test_cases = { '': '', '.': '.', 'filename': 'filename', '.bashrc': '.bashrc', '.nzb': '.nzb', 'file.nzb': 'file', 'file.name.nzb': 'file.name', '.torrent': '.torrent', 'file.torrent': 'file', 'file.name.torrent': 'file.name', '.avi': '.avi', 'file.avi': 'file', 'file.name.avi': 'file.name', } for tests in test_cases, unicode_test_cases: for (extension, result) in six.iteritems(tests): self.assertEqual(remove_extension(extension), result)
def test_remove_extension(self): """ Test remove extension """ test_cases = { None: None, 42: 42, '': '', '.': '.', 'filename': 'filename', '.bashrc': '.bashrc', '.nzb': '.nzb', 'file.nzb': 'file', 'file.name.nzb': 'file.name', '.torrent': '.torrent', 'file.torrent': 'file', 'file.name.torrent': 'file.name', '.avi': '.avi', 'file.avi': 'file', 'file.name.avi': 'file.name', } unicode_test_cases = { '': '', '.': '.', 'filename': 'filename', '.bashrc': '.bashrc', '.nzb': '.nzb', 'file.nzb': 'file', 'file.name.nzb': 'file.name', '.torrent': '.torrent', 'file.torrent': 'file', 'file.name.torrent': 'file.name', '.avi': '.avi', 'file.avi': 'file', 'file.name.avi': 'file.name', } for tests in test_cases, unicode_test_cases: for (extension, result) in six.iteritems(tests): self.assertEqual(remove_extension(extension), result)
def test_remove_extension(self): """ Test remove extension """ test_cases = { None: None, 42: 42, "": "", ".": ".", "filename": "filename", ".bashrc": ".bashrc", ".nzb": ".nzb", "file.nzb": "file", "file.name.nzb": "file.name", ".torrent": ".torrent", "file.torrent": "file", "file.name.torrent": "file.name", ".avi": ".avi", "file.avi": "file", "file.name.avi": "file.name", } unicode_test_cases = { "": "", ".": ".", "filename": "filename", ".bashrc": ".bashrc", ".nzb": ".nzb", "file.nzb": "file", "file.name.nzb": "file.name", ".torrent": ".torrent", "file.torrent": "file", "file.name.torrent": "file.name", ".avi": ".avi", "file.avi": "file", "file.name.avi": "file.name", } for tests in test_cases, unicode_test_cases: for (extension, result) in tests.items(): assert remove_extension(extension) == result
def parse(self, name, cache_result=True, skip_scene_detection=False): name = self._unicodify(name) if self.naming_pattern: cache_result = False cached = name_parser_cache[name] if cached: return cached # break it into parts if there are any (dirname, file name, extension) dir_name, filename = os.path.split(name) if self.filename: base_filename = remove_extension(filename) else: base_filename = filename # set up a result to use final_result = ParseResult(name) # try parsing the file name filename_result = self._parse_string(base_filename, skip_scene_detection) # use only the direct parent dir dir_name = os.path.basename(dir_name) # parse the dirname for extra info if needed dir_name_result = self._parse_string(dir_name, skip_scene_detection) # build the ParseResult object final_result.air_date = self._combine_results(filename_result, dir_name_result, "air_date") # anime absolute numbers final_result.ab_episode_numbers = self._combine_results( filename_result, dir_name_result, "ab_episode_numbers") # season and episode numbers final_result.season_number = self._combine_results( filename_result, dir_name_result, "season_number") final_result.episode_numbers = self._combine_results( filename_result, dir_name_result, "episode_numbers") final_result.scene_season = self._combine_results( filename_result, dir_name_result, "scene_season") # if the dirname has a release group/show name I believe it over the filename final_result.series_name = self._combine_results( dir_name_result, filename_result, "series_name") final_result.extra_info = self._combine_results( dir_name_result, filename_result, "extra_info") final_result.release_group = self._combine_results( dir_name_result, filename_result, "release_group") final_result.version = self._combine_results(dir_name_result, filename_result, "version") final_result.which_regex = [] if final_result == filename_result: final_result.which_regex = filename_result.which_regex final_result.score = filename_result.score elif final_result == dir_name_result: final_result.which_regex = dir_name_result.which_regex final_result.score = dir_name_result.score else: final_result.score = 0 if filename_result: final_result.which_regex += filename_result.which_regex final_result.score += filename_result.score if dir_name_result: final_result.which_regex += dir_name_result.which_regex final_result.score += dir_name_result.score final_result.show = self._combine_results(filename_result, dir_name_result, "show") final_result.quality = self._combine_results(filename_result, dir_name_result, "quality") if not final_result.show: raise InvalidShowException( f"Unable to match {name} to a show in your database. Parser result: {final_result}" ) # if there's no useful info in it then raise an exception if (final_result.season_number is None and not final_result.episode_numbers and final_result.air_date is None and not final_result.ab_episode_numbers and not final_result.series_name): raise InvalidNameException( f"Unable to parse {name} to a valid episode of {final_result.show.name}. Parser result: {final_result}" ) if cache_result: name_parser_cache[name] = final_result logger.debug(f"Parsed {name} into {final_result}") return final_result
def parse(self, name, cache_result=True, skip_scene_detection=False): name = self._unicodify(name) if self.naming_pattern: cache_result = False cached = name_parser_cache.get(name) if cached: return cached # break it into parts if there are any (dirname, file name, extension) dir_name, file_name = ek(os.path.split, name) if self.file_name: base_file_name = remove_extension(file_name) else: base_file_name = file_name # set up a result to use final_result = ParseResult(name) # try parsing the file name file_name_result = self._parse_string(base_file_name, skip_scene_detection) # use only the direct parent dir dir_name = ek(os.path.basename, dir_name) # parse the dirname for extra info if needed dir_name_result = self._parse_string(dir_name, skip_scene_detection) # build the ParseResult object final_result.air_date = self._combine_results(file_name_result, dir_name_result, 'air_date') # anime absolute numbers final_result.ab_episode_numbers = self._combine_results( file_name_result, dir_name_result, 'ab_episode_numbers') # season and episode numbers final_result.season_number = self._combine_results( file_name_result, dir_name_result, 'season_number') final_result.episode_numbers = self._combine_results( file_name_result, dir_name_result, 'episode_numbers') final_result.scene_season = self._combine_results( file_name_result, dir_name_result, 'scene_season') # if the dirname has a release group/show name I believe it over the filename final_result.series_name = self._combine_results( dir_name_result, file_name_result, 'series_name') final_result.extra_info = self._combine_results( dir_name_result, file_name_result, 'extra_info') final_result.release_group = self._combine_results( dir_name_result, file_name_result, 'release_group') final_result.version = self._combine_results(dir_name_result, file_name_result, 'version') final_result.which_regex = [] if final_result == file_name_result: final_result.which_regex = file_name_result.which_regex elif final_result == dir_name_result: final_result.which_regex = dir_name_result.which_regex else: if file_name_result: final_result.which_regex += file_name_result.which_regex if dir_name_result: final_result.which_regex += dir_name_result.which_regex final_result.show = self._combine_results(file_name_result, dir_name_result, 'show') final_result.quality = self._combine_results(file_name_result, dir_name_result, 'quality') if not final_result.show: raise InvalidShowException( "Unable to match {0} to a show in your database. Parser result: {1}" .format(name, six.text_type(final_result))) # if there's no useful info in it then raise an exception if final_result.season_number is None and not final_result.episode_numbers and final_result.air_date is None and not final_result.ab_episode_numbers and not final_result.series_name: raise InvalidNameException( "Unable to parse {0} to a valid episode of {1}. Parser result: {2}" .format(name, final_result.show.name, six.text_type(final_result))) if cache_result: name_parser_cache.add(name, final_result) logger.log("Parsed " + name + " into " + six.text_type(final_result), logger.DEBUG) return final_result
def parse(self, name, cache_result=True, skip_scene_detection=False): name = self._unicodify(name) if self.naming_pattern: cache_result = False cached = name_parser_cache.get(name) if cached: return cached # break it into parts if there are any (dirname, file name, extension) dir_name, file_name = ek(os.path.split, name) if self.file_name: base_file_name = remove_extension(file_name) else: base_file_name = file_name # set up a result to use final_result = ParseResult(name) # try parsing the file name file_name_result = self._parse_string(base_file_name, skip_scene_detection) # use only the direct parent dir dir_name = ek(os.path.basename, dir_name) # parse the dirname for extra info if needed dir_name_result = self._parse_string(dir_name, skip_scene_detection) # build the ParseResult object final_result.air_date = self._combine_results(file_name_result, dir_name_result, 'air_date') # anime absolute numbers final_result.ab_episode_numbers = self._combine_results(file_name_result, dir_name_result, 'ab_episode_numbers') # season and episode numbers final_result.season_number = self._combine_results(file_name_result, dir_name_result, 'season_number') final_result.episode_numbers = self._combine_results(file_name_result, dir_name_result, 'episode_numbers') final_result.scene_season = self._combine_results(file_name_result, dir_name_result, 'scene_season') # if the dirname has a release group/show name I believe it over the filename final_result.series_name = self._combine_results(dir_name_result, file_name_result, 'series_name') final_result.extra_info = self._combine_results(dir_name_result, file_name_result, 'extra_info') final_result.release_group = self._combine_results(dir_name_result, file_name_result, 'release_group') final_result.version = self._combine_results(dir_name_result, file_name_result, 'version') final_result.which_regex = [] if final_result == file_name_result: final_result.which_regex = file_name_result.which_regex elif final_result == dir_name_result: final_result.which_regex = dir_name_result.which_regex else: if file_name_result: final_result.which_regex += file_name_result.which_regex if dir_name_result: final_result.which_regex += dir_name_result.which_regex final_result.show = self._combine_results(file_name_result, dir_name_result, 'show') final_result.quality = self._combine_results(file_name_result, dir_name_result, 'quality') if not final_result.show: raise InvalidShowException("Unable to match {0} to a show in your database. Parser result: {1}".format( name, six.text_type(final_result))) # if there's no useful info in it then raise an exception if final_result.season_number is None and not final_result.episode_numbers and final_result.air_date is None and not final_result.ab_episode_numbers and not final_result.series_name: raise InvalidNameException("Unable to parse {0} to a valid episode of {1}. Parser result: {2}".format( name, final_result.show.name, six.text_type(final_result))) if cache_result: name_parser_cache.add(name, final_result) logger.log("Parsed " + name + " into " + six.text_type(final_result), logger.DEBUG) return final_result