def build_or_pattern(patterns, name=None, escape=False): """ Build a or pattern string from a list of possible patterns :param patterns: :type patterns: :param name: :type name: :param escape: :type escape: :return: :rtype: """ or_pattern = [] for pattern in patterns: if not or_pattern: or_pattern.append('(?') if name: or_pattern.append('P<' + name + '>') else: or_pattern.append(':') else: or_pattern.append('|') or_pattern.append('(?:%s)' % re.escape(pattern) if escape else pattern) or_pattern.append(')') return ''.join(or_pattern)
def build_or_pattern(patterns, escape=False): """Build a or pattern string from a list of possible patterns """ or_pattern = [] for pattern in patterns: if not or_pattern: or_pattern.append('(?:') else: or_pattern.append('|') or_pattern.append('(?:%s)' % re.escape(pattern) if escape else pattern) or_pattern.append(')') return ''.join(or_pattern)
def screen_size(config): """ Builder for rebulk object. :param config: rule configuration :type config: dict :return: Created Rebulk object :rtype: Rebulk """ interlaced = frozenset(config['interlaced']) progressive = frozenset(config['progressive']) frame_rates = [re.escape(rate) for rate in config['frame_rates']] min_ar = config['min_ar'] max_ar = config['max_ar'] rebulk = Rebulk() rebulk = rebulk.string_defaults(ignore_case=True).regex_defaults( flags=re.IGNORECASE) rebulk.defaults( name='screen_size', validator=seps_surround, abbreviations=[dash], disabled=lambda context: is_disabled(context, 'screen_size')) frame_rate_pattern = build_or_pattern(frame_rates, name='frame_rate') interlaced_pattern = build_or_pattern(interlaced, name='height') progressive_pattern = build_or_pattern(progressive, name='height') res_pattern = r'(?:(?P<width>\d{3,4})(?:x|\*))?' rebulk.regex(res_pattern + interlaced_pattern + r'(?P<scan_type>i)' + frame_rate_pattern + '?') rebulk.regex(res_pattern + progressive_pattern + r'(?P<scan_type>p)' + frame_rate_pattern + '?') rebulk.regex(res_pattern + progressive_pattern + r'(?P<scan_type>p)?(?:hd)') rebulk.regex(res_pattern + progressive_pattern + r'(?P<scan_type>p)?x?') rebulk.string('4k', value='2160p') rebulk.regex(r'(?P<width>\d{3,4})-?(?:x|\*)-?(?P<height>\d{3,4})', conflict_solver=lambda match, other: '__default__' if other.name == 'screen_size' else other) rebulk.regex(frame_rate_pattern + '(p|fps)', name='frame_rate', formatter=FrameRate.fromstring, disabled=lambda context: is_disabled(context, 'frame_rate')) rebulk.rules(PostProcessScreenSize(progressive, min_ar, max_ar), ScreenSizeOnlyOne, ResolveScreenSizeConflicts) return rebulk
def screen_size(config): """ Builder for rebulk object. :param config: rule configuration :type config: dict :return: Created Rebulk object :rtype: Rebulk """ interlaced = frozenset({res for res in config['interlaced']}) progressive = frozenset({res for res in config['progressive']}) frame_rates = [re.escape(rate) for rate in config['frame_rates']] min_ar = config['min_ar'] max_ar = config['max_ar'] rebulk = Rebulk() rebulk = rebulk.string_defaults(ignore_case=True).regex_defaults(flags=re.IGNORECASE) rebulk.defaults(name='screen_size', validator=seps_surround, abbreviations=[dash], disabled=lambda context: is_disabled(context, 'screen_size')) frame_rate_pattern = build_or_pattern(frame_rates, name='frame_rate') interlaced_pattern = build_or_pattern(interlaced, name='height') progressive_pattern = build_or_pattern(progressive, name='height') res_pattern = r'(?:(?P<width>\d{3,4})(?:x|\*))?' rebulk.regex(res_pattern + interlaced_pattern + r'(?P<scan_type>i)' + frame_rate_pattern + '?') rebulk.regex(res_pattern + progressive_pattern + r'(?P<scan_type>p)' + frame_rate_pattern + '?') rebulk.regex(res_pattern + progressive_pattern + r'(?P<scan_type>p)?(?:hd)') rebulk.regex(res_pattern + progressive_pattern + r'(?P<scan_type>p)?x?') rebulk.string('4k', value='2160p') rebulk.regex(r'(?P<width>\d{3,4})-?(?:x|\*)-?(?P<height>\d{3,4})', conflict_solver=lambda match, other: '__default__' if other.name == 'screen_size' else other) rebulk.regex(frame_rate_pattern + '(p|fps)', name='frame_rate', formatter=FrameRate.fromstring, disabled=lambda context: is_disabled(context, 'frame_rate')) rebulk.rules(PostProcessScreenSize(progressive, min_ar, max_ar), ScreenSizeOnlyOne, ResolveScreenSizeConflicts) return rebulk
#!/usr/bin/env python # -*- coding: utf-8 -*- """ Common module """ from rebulk.remodule import re seps = r' [](){}+*|=-_~#/\\.,;:' # list of tags/words separators seps_no_groups = seps.replace('[](){}', '') seps_no_fs = seps.replace('/', '').replace('\\', '') title_seps = r'-+/\|' # separators for title dash = (r'-', r'[' + re.escape(seps_no_fs) + ']' ) # abbreviation used by many rebulk objects. alt_dash = (r'@', r'[' + re.escape(seps_no_fs) + ']' ) # abbreviation used by many rebulk objects. def optional(pattern): """ Make a regex pattern optional """ return '(?:' + pattern + ')?'