def when(self, matches, context): # pylint:disable=inconsistent-return-statements propers = matches.named('other', lambda match: match.value == 'Proper') if propers: raws = {} # Count distinct raw values for proper in propers: raws[raw_cleanup(proper.raw)] = proper value = 0 start = None end = None proper_count_matches = [] for proper in raws.values(): if not start or start > proper.start: start = proper.start if not end or end < proper.end: end = proper.end if proper.children.named('proper_count', 0): value += int( proper.children.named('proper_count', 0).value) elif 'real' in proper.tags: value += 2 else: value += 1 proper_count_match = Match(name='proper_count', start=start, end=end, input_string=matches.input_string) proper_count_match.value = value proper_count_matches.append(proper_count_match) return proper_count_matches
def when(self, matches, context): # pylint:disable=inconsistent-return-statements if matches.named('release_group'): return to_remove = [] to_append = [] for filepart in matches.markers.named('path'): candidate = self.detect(matches, filepart.start, filepart.end, True) if candidate: to_remove.extend(matches.at_match(candidate)) else: candidate = self.detect(matches, filepart.start, filepart.end, False) if candidate: releasegroup = Match(candidate.start, candidate.end, name='release_group', formatter=self.value_formatter, input_string=candidate.input_string) if releasegroup.value: to_append.append(releasegroup) return to_remove, to_append
def then(self, matches, when_response, context): mime = when_response matches.append( Match(len(matches.input_string), len(matches.input_string), name='mimetype', value=mime))
def test_strip_separators_keep_acronyms(): strip_separators = StripSeparators() matches = Matches() m = Match(0, 13, input_string=".S.H.I.E.L.D.") m2 = Match(0, 22, input_string=".Agent.Of.S.H.I.E.L.D.") assert m.raw == '.S.H.I.E.L.D.' matches.append(m) matches.append(m2) returned_matches = strip_separators.when(matches, None) assert returned_matches == matches strip_separators.then(matches, returned_matches, None) assert m.raw == '.S.H.I.E.L.D.' assert m2.raw == 'Agent.Of.S.H.I.E.L.D.'
def when(self, matches, context): to_remove = [] to_append = [] for separator in matches.named(self.property_name + 'Separator'): previous_match = matches.previous( separator, lambda m: m.name == self.property_name, 0) next_match = matches.next(separator, lambda m: m.name == self.property_name, 0) initiator = separator.initiator if previous_match and next_match and separator.value in self.range_separators: to_remove.append(next_match) for episode_number in range(previous_match.value + 1, next_match.value): match = copy.copy(next_match) match.value = episode_number initiator.children.append(match) to_append.append(match) to_append.append(next_match) to_remove.append(separator) previous_match = None for next_match in matches.named(self.property_name): if previous_match: separator = matches.input_string[ previous_match.initiator.end:next_match.initiator.start] if separator not in self.range_separators: separator = strip(separator) if separator in self.range_separators: initiator = previous_match.initiator for episode_number in range(previous_match.value + 1, next_match.value): match = copy.copy(next_match) match.value = episode_number initiator.children.append(match) to_append.append(match) to_append.append( Match(previous_match.end, next_match.start - 1, name=self.property_name + 'Separator', private=True, input_string=matches.input_string)) to_remove.append( next_match ) # Remove and append match to support proper ordering to_append.append(next_match) previous_match = next_match if to_remove or to_append: return to_remove, to_append return False
def _type(matches, value): """ Define type match with given value. :param matches: :param value: :return: """ matches.append( Match(len(matches.input_string), len(matches.input_string), name='type', value=value))
def test_strip_separators(): strip_separators = StripSeparators() matches = Matches() m = Match(3, 11, input_string="pre.ABCDEF.post") assert m.raw == '.ABCDEF.' matches.append(m) returned_matches = strip_separators.when(matches, None) assert returned_matches == matches strip_separators.then(matches, returned_matches, None) assert m.raw == 'ABCDEF'
def when(self, matches, context): to_append = [] for match in matches.named('screen_size'): if not is_disabled(context, 'frame_rate'): for frame_rate in match.children.named('frame_rate'): frame_rate.formatter = FrameRate.fromstring to_append.append(frame_rate) values = match.children.to_dict() if 'height' not in values: continue scan_type = (values.get('scan_type') or 'p').lower() height = values['height'] if 'width' not in values: match.value = '{0}{1}'.format(height, scan_type) continue width = values['width'] calculated_ar = float(width) / float(height) aspect_ratio = Match(match.start, match.end, input_string=match.input_string, name='aspect_ratio', value=round(calculated_ar, 3)) if not is_disabled(context, 'aspect_ratio'): to_append.append(aspect_ratio) if height in self.standard_heights and self.min_ar < calculated_ar < self.max_ar: match.value = '{0}{1}'.format(height, scan_type) else: match.value = '{0}x{1}'.format(width, height) return to_append