def test_list_associated_files(p, create_structure, monkeypatch):
    """Run the test."""
    # Given
    test_path = create_structure(p['path'], structure=p['structure'])
    path = os.path.join(test_path, os.path.normcase(p['path']))
    file_path = os.path.join(path, p['structure'][0])

    expected_associated_files = p['expected_associated_files']
    subtitles_only = p.get('subtitles_only', False)
    subfolders = p.get('subfolders', False)
    refine = p.get('refine', False)

    monkeypatch.setattr(app, 'ALLOWED_EXTENSIONS', p['allowed_extensions'])
    monkeypatch.setattr(app, 'MOVE_ASSOCIATED_FILES', 1)

    processor = PostProcessor(file_path)
    processor._rar_basename = _rar_basename

    # When
    found_associated_files = processor.list_associated_files(
        file_path,
        subfolders=subfolders,
        subtitles_only=subtitles_only,
        refine=refine)
    associated_files_basenames = [
        os.path.basename(i) for i in found_associated_files
    ]

    # Then
    assert set(associated_files_basenames) == set(expected_associated_files)
def test_list_associated_files(p, create_structure, monkeypatch):
    """Run the test."""
    # Given
    test_path = create_structure(p['path'], structure=p['structure'])
    path = os.path.join(test_path, os.path.normcase(p['path']))
    file_path = os.path.join(path, p['structure'][0])

    expected_associated_files = p['expected_associated_files']
    subtitles_only = p.get('subtitles_only', False)
    subfolders = p.get('subfolders', False)
    refine = p.get('refine', False)

    monkeypatch.setattr(app, 'ALLOWED_EXTENSIONS', p['allowed_extensions'])
    monkeypatch.setattr(app, 'MOVE_ASSOCIATED_FILES', 1)

    processor = PostProcessor(file_path)
    processor._rar_basename = _rar_basename

    # When
    found_associated_files = processor.list_associated_files(file_path, subfolders=subfolders,
                                                             subtitles_only=subtitles_only, refine=refine)
    associated_files_basenames = [os.path.basename(i) for i in found_associated_files]

    # Then
    assert set(associated_files_basenames) == set(expected_associated_files)
Example #3
0
    def test_process(self):
        show = TVShow(1, 3)
        show.name = test.SHOW_NAME
        show.location = test.SHOW_DIR
        show.save_to_db()

        app.showList = [show]
        episode = TVEpisode(show, test.SEASON, test.EPISODE)
        episode.name = "some episode name"
        episode.save_to_db()

        addNameToCache('show name', 3)
        app.PROCESS_METHOD = 'move'

        post_processor = PostProcessor(test.FILE_PATH)
        self.assertTrue(post_processor.process())
Example #4
0
def test_parse_info(p, monkeypatch, parse_method):
    """Run the test."""
    # Given
    monkeypatch.setattr(NameParser, 'parse', parse_method)
    sut = PostProcessor(file_path=p['file_path'], nzb_name=p['nzb_name'])

    # When
    show, season, episodes, quality, version, airdate = sut._parse_info()

    # Then
    assert show is not None
    assert p['expected'] == {
        'show': show.name,
        'season': season,
        'episodes': episodes,
        'quality': quality,
        'version': version,
    }
def test_parse_info(p, monkeypatch, parse_method):
    """Run the test."""
    # Given
    monkeypatch.setattr(NameParser, 'parse', parse_method)
    sut = PostProcessor(file_path=p['file_path'], nzb_name=p['nzb_name'])

    # When
    show, season, episodes, quality, version, airdate = sut._parse_info()

    # Then
    assert show is not None
    assert p['expected'] == {
        'show': show.name,
        'season': season,
        'episodes': episodes,
        'quality': quality,
        'version': version,
    }
Example #6
0
    def subtitleMissedPP(self):
        t = PageTemplate(rh=self, filename='manage_subtitleMissedPP.mako')
        app.RELEASES_IN_PP = []
        for root, _, files in os.walk(app.TV_DOWNLOAD_DIR, topdown=False):
            # Skip folders that are being used for unpacking
            if u'_UNPACK' in root.upper():
                continue
            for filename in sorted(files):
                if not is_media_file(filename):
                    continue

                video_path = os.path.join(root, filename)
                video_date = datetime.datetime.fromtimestamp(os.stat(video_path).st_ctime)
                video_age = datetime.datetime.today() - video_date

                tv_episode = Episode.from_filepath(video_path)

                if not tv_episode:
                    logger.log(u"Filename '{0}' cannot be parsed to an episode".format(filename), logger.DEBUG)
                    continue

                ep_status = tv_episode.status
                if ep_status in (SNATCHED, SNATCHED_PROPER, SNATCHED_BEST):
                    status = 'snatched'
                elif ep_status == DOWNLOADED:
                    status = 'downloaded'
                else:
                    continue

                if not tv_episode.series.subtitles:
                    continue

                related_files = PostProcessor(video_path).list_associated_files(video_path, subtitles_only=True)
                if related_files:
                    continue

                age_hours = divmod(video_age.seconds, 3600)[0]
                age_minutes = divmod(video_age.seconds, 60)[0]
                if video_age.days > 0:
                    age_unit = 'd'
                    age_value = video_age.days
                elif age_hours > 0:
                    age_unit = 'h'
                    age_value = age_hours
                else:
                    age_unit = 'm'
                    age_value = age_minutes

                app.RELEASES_IN_PP.append({'release': video_path, 'seriesid': tv_episode.series.indexerid,
                                           'show_name': tv_episode.series.name, 'season': tv_episode.season,
                                           'episode': tv_episode.episode, 'status': status, 'age': age_value,
                                           'age_unit': age_unit, 'date': video_date,
                                           'indexername': tv_episode.series.indexer_name})

        return t.render(releases_in_pp=app.RELEASES_IN_PP,
                        controller='manage', action='subtitleMissedPP')
Example #7
0
def test__process_postponed(monkeypatch, p, create_structure):
    """Run the test."""
    # Given
    test_path = create_structure(p['path'], structure=p['structure'])
    path = os.path.join(test_path, os.path.normcase(p['path']))
    video_path = os.path.join(path, p['video'])
    processor = PostProcessor(path)
    sut = ProcessResult(path)

    # Overwrite internal method
    sut.subtitles_enabled = lambda path, resource_name: p['subtitles_enabled']

    # When
    result = sut._process_postponed(processor, video_path, p['video'], p['ignore_subs'])

    # Then
    assert p['expected'] == result
Example #8
0
def test_should_process(p):
    """Run the test."""
    # Given
    current_quality = p['cur_quality']
    new_quality = p['new_quality']
    allowed_qualities = p['allowed_qualities']
    preferred_qualities = p['preferred_qualities']
    expected = p['expected']

    # When
    replace, msg = PostProcessor._should_process(current_quality, new_quality, allowed_qualities, preferred_qualities)
    actual = replace

    # Then
    if expected != actual:
        print(msg)
    assert expected == actual
def test_rename_associated_file(p, create_dir, monkeypatch):
    """Test rename_associated_file."""
    # Given
    new_path = p['new_path']
    new_basename = p.get('new_basename')
    filepath = p['filepath']

    monkeypatch.setattr(app, 'NFO_RENAME', p.get('nfo_rename', 1))

    if p.get('subtitles'):
        # Workaround for absolute subtitles directory
        if p['subtitles']['absolute']:
            subs_dir = create_dir(p['subtitles']['dir'])
            monkeypatch.setattr(app, 'SUBTITLES_DIR', subs_dir)
            p['expected'] = os.path.join(subs_dir, p['expected'])
        else:
            monkeypatch.setattr(app, 'SUBTITLES_DIR', p['subtitles']['dir'])

    # When
    result = Sut.rename_associated_file(new_path, new_basename, filepath)

    # Then
    assert os.path.normcase(result) == os.path.normcase(p['expected'])
def test_rename_associated_file(p, create_dir, monkeypatch):
    """Test rename_associated_file."""
    # Given
    new_path = p['new_path']
    new_basename = p.get('new_basename')
    filepath = p['filepath']

    monkeypatch.setattr(app, 'NFO_RENAME', p.get('nfo_rename', 1))

    if p.get('subtitles'):
        # Workaround for absolute subtitles directory
        if p['subtitles']['absolute']:
            subs_dir = create_dir(p['subtitles']['dir'])
            monkeypatch.setattr(app, 'SUBTITLES_DIR', subs_dir)
            p['expected'] = os.path.join(subs_dir, p['expected'])
        else:
            monkeypatch.setattr(app, 'SUBTITLES_DIR', p['subtitles']['dir'])

    # When
    result = Sut.rename_associated_file(new_path, new_basename, filepath)

    # Then
    assert os.path.normcase(result) == os.path.normcase(p['expected'])
Example #11
0
 def setUp(self):
     """Set up tests."""
     self.post_processor = PostProcessor(test.FILE_PATH)