def test_overwrite(mock_exists, mock_samefile, mock_rename, mock_trash): mock_exists.return_value = True mock_samefile.return_value = False rename = Rename(name="{path.stem} Kopie.py", overwrite=True) new_path = rename.run(**ARGS) assert mock_exists.call_count > 0 mock_trash.assert_called_with(os.path.join(USER_DIR, "test Kopie.py")) mock_rename.assert_called_with(Path("~/test Kopie.py").expanduser()) assert new_path is not None
def test_already_exists(mock_exists, mock_samefile, mock_rename, mock_trash): mock_exists.side_effect = [True, False] mock_samefile.return_value = False rename = Rename(name="asd.txt", overwrite=False) new_path = rename.run(**ARGS) assert mock_exists.call_count > 0 mock_trash.assert_not_called() mock_rename.assert_called_with(Path("~/asd 2.txt").expanduser()) assert new_path is not None
def test_already_exists_multiple(mock_exists, mock_samefile, mock_rename, mock_trash): attrs = {"basedir": Path.home(), "path": Path.home() / "test.py"} mock_exists.side_effect = [True, True, True, False] mock_samefile.return_value = False rename = Rename(name="asd.txt", overwrite=False) new_path = rename.run(attrs, False) mock_exists.assert_called() mock_trash.assert_not_called() mock_rename.assert_called_with(Path("~/asd 4.txt").expanduser()) assert new_path is not None
def test_keep_name(mock_exists, mock_samefile, mock_rename, mock_trash): attrs = {"basedir": Path.home(), "path": Path.home() / "test.pdf"} mock_exists.return_value = True mock_samefile.return_value = True rename = Rename(name="{path.stem}.pdf", overwrite=False) new_path = rename.run(attrs, False) mock_exists.assert_called() mock_trash.assert_not_called() mock_rename.assert_not_called() assert new_path is not None
def test_already_exists_multiple_separator(mock_exists, mock_samefile, mock_rename, mock_trash): mock_exists.side_effect = [True, True, True, False] mock_samefile.return_value = False rename = Rename(name="asd.txt", overwrite=False, counter_separator="-") new_path = rename.run(**ARGS) mock_exists.assert_called() mock_trash.assert_not_called() mock_rename.assert_called_with(Path("~/asd-4.txt").expanduser()) assert new_path is not None
def test_tilde_expansion(mock_exists, mock_samefile, mock_rename, mock_trash): mock_exists.return_value = False mock_samefile.return_value = False rename = Rename(name="newname.py", overwrite=False) new_path = rename.run(**ARGS) assert mock_exists.call_count > 0 mock_trash.assert_not_called() expected_path = (Path.home() / "newname.py").expanduser() mock_rename.assert_called_with(expected_path) assert new_path == expected_path
def test_overwrite(mock_exists, mock_samefile, mock_rename, mock_trash): attrs = {"basedir": Path.home(), "path": Path.home() / "test.py"} mock_exists.return_value = True mock_samefile.return_value = False rename = Rename(name="{path.stem} Kopie.py", overwrite=True) new_path = rename.run(attrs, False) mock_exists.assert_called() mock_trash.assert_called_with(os.path.join(USER_DIR, "test Kopie.py")) mock_rename.assert_called_with(Path("~/test Kopie.py").expanduser()) assert new_path is not None
def test_tilde_expansion(mock_exists, mock_samefile, mock_rename, mock_trash): attrs = {"basedir": Path.home(), "path": Path.home() / "test.py"} mock_exists.return_value = False mock_samefile.return_value = False rename = Rename(name="newname.py", overwrite=False) new_path = rename.run(attrs, False) mock_exists.assert_called() mock_trash.assert_not_called() expected_path = (Path.home() / "newname.py").expanduser() mock_rename.assert_called_with(expected_path) assert new_path == expected_path
def test_attrs(mock_exists, mock_samefile, mock_rename, mock_trash): basedir = Path('~') path = Path('~') / 'test.py' mock_exists.return_value = False mock_samefile.return_value = False rename = Rename(name='{nr.upper}-{path.stem} Kopie.py') new_path = rename.run(basedir, path, {'nr': DotDict({'upper': 1})}, False) mock_exists.assert_called() mock_trash.assert_not_called() mock_rename.assert_called_with(Path('~/1-test Kopie.py').expanduser()) assert new_path is not None
def test_already_exists_multiple(mock_exists, mock_samefile, mock_rename, mock_trash): basedir = Path('~') path = Path('~') / 'test.py' mock_exists.side_effect = [True, True, True, False] mock_samefile.return_value = False rename = Rename(name='asd.txt', overwrite=False) new_path = rename.run(basedir, path, {}, False) mock_exists.assert_called() mock_trash.assert_not_called() mock_rename.assert_called_with(Path('~/asd 4.txt').expanduser()) assert new_path is not None
def test_keep_name(mock_exists, mock_samefile, mock_rename, mock_trash): basedir = Path('~') path = Path('~') / 'test.pdf' mock_exists.return_value = True mock_samefile.return_value = True rename = Rename(name='{path.stem}.pdf', overwrite=False) new_path = rename.run(basedir, path, {}, False) mock_exists.assert_called() mock_trash.assert_not_called() mock_rename.assert_not_called() assert new_path is not None
def test_overwrite(mock_exists, mock_samefile, mock_rename, mock_trash): basedir = Path('~') path = Path('~') / 'test.py' mock_exists.return_value = True mock_samefile.return_value = False rename = Rename(name='{path.stem} Kopie.py', overwrite=True) new_path = rename.run(basedir, path, {}, False) mock_exists.assert_called() mock_trash.assert_called_with(os.path.join(USER_DIR, 'test Kopie.py')) mock_rename.assert_called_with(Path('~/test Kopie.py').expanduser()) assert new_path is not None
def test_overwrite_samefile(mock_exists, mock_samefile, mock_rename, mock_trash): attrs = {'basedir': Path.home(), 'path': Path.home() / 'test.PDF'} mock_exists.return_value = True mock_samefile.return_value = True rename = Rename(name='{path.stem}.pdf', overwrite=False) new_path = rename.run(attrs, False) mock_exists.assert_called() mock_trash.assert_not_called() mock_rename.assert_called_with((Path.home() / 'test.pdf').expanduser()) assert new_path is not None
def test_tilde_expansion(mock_exists, mock_samefile, mock_rename, mock_trash): basedir = Path('~') path = Path('~') / 'test.py' mock_exists.return_value = False mock_samefile.return_value = False rename = Rename(name='newname.py', overwrite=False) new_path = rename.run(basedir, path, {}, False) mock_exists.assert_called() mock_trash.assert_not_called() expected_path = (Path('~') / 'newname.py').expanduser() mock_rename.assert_called_with(expected_path) assert new_path == expected_path
def test_already_exists(mock_exists, mock_samefile, mock_rename, mock_trash): attrs = { 'basedir': Path.home(), 'path': Path.home() / 'test.py', } mock_exists.side_effect = [True, False] mock_samefile.return_value = False rename = Rename(name='asd.txt', overwrite=False) new_path = rename.run(attrs, False) mock_exists.assert_called() mock_trash.assert_not_called() mock_rename.assert_called_with(Path('~/asd 2.txt').expanduser()) assert new_path is not None
def test_attrs(mock_exists, mock_samefile, mock_rename, mock_trash): attrs = { "basedir": Path.home(), "path": Path.home() / "test.py", "nr": DotDict({"upper": 1}), } mock_exists.return_value = False mock_samefile.return_value = False rename = Rename(name="{nr.upper}-{path.stem} Kopie.py") new_path = rename.run(attrs, False) mock_exists.assert_called() mock_trash.assert_not_called() mock_rename.assert_called_with(Path("~/1-test Kopie.py").expanduser()) assert new_path is not None
def test_attrs(mock_exists, mock_samefile, mock_rename, mock_trash): attrs = { 'basedir': Path.home(), 'path': Path.home() / 'test.py', 'nr': DotDict({'upper': 1}), } mock_exists.return_value = False mock_samefile.return_value = False rename = Rename(name='{nr.upper}-{path.stem} Kopie.py') new_path = rename.run(attrs, False) mock_exists.assert_called() mock_trash.assert_not_called() mock_rename.assert_called_with(Path('~/1-test Kopie.py').expanduser()) assert new_path is not None
def test_keep_name(mock_exists, mock_samefile, mock_rename, mock_trash): args = { "basedir": Path.home(), "path": Path.home() / "test.pdf", "simulate": False } mock_exists.return_value = True mock_samefile.return_value = True rename = Rename(name="{path.stem}.pdf", overwrite=False) new_path = rename.run(**args) assert mock_exists.call_count > 0 mock_trash.assert_not_called() mock_rename.assert_not_called() assert new_path is not None
def test_overwrite_samefile(mock_exists, mock_samefile, mock_rename, mock_trash): args = { "basedir": Path.home(), "path": Path.home() / "test.PDF", "simulate": False } mock_exists.return_value = True mock_samefile.return_value = True rename = Rename(name="{path.stem}.pdf", overwrite=False) new_path = rename.run(**args) mock_exists.assert_called() mock_trash.assert_not_called() mock_rename.assert_called_with((Path.home() / "test.pdf").expanduser()) assert new_path is not None
def test_args(mock_exists, mock_samefile, mock_rename, mock_trash): args = { "basedir": Path.home(), "path": Path.home() / "test.py", "nr": { "upper": 1 }, "simulate": False, } mock_exists.return_value = False mock_samefile.return_value = False rename = Rename(name="{nr.upper}-{path.stem} Kopie.py") new_path = rename.run(**args) assert mock_exists.call_count > 0 mock_trash.assert_not_called() mock_rename.assert_called_with(Path("~/1-test Kopie.py").expanduser()) assert new_path is not None
def test_flatten_filters_and_actions(): config = """ folder_aliases: Downloads: &downloads ~/Downloads/ Payables_due: &payables_due ~/PayablesDue/ Payables_paid: &payables_paid ~/Accounting/Expenses/ Receivables_due: &receivables_due ~/Receivables/ Receivables_paid: &receivables_paid ~/Accounting/Income/ defaults: filters: &default_filters - extension: pdf - filecontent: '(?P<date>...)' actions: &default_actions - echo: 'Dated: {filecontent.date}' - echo: 'Stem of filename: {filecontent.stem}' post_actions: &default_sorting - rename: '{python.timestamp}-{filecontent.stem}.{extension.lower}' - move: '{path.parent}/{python.quarter}/' rules: - folders: *downloads filters: - *default_filters - filecontent: 'Due Date' # regex to id as payable - filecontent: '(?P<stem>...)' # regex to extract supplier actions: - *default_actions - move: *payables_due - *default_sorting - folders: *downloads filters: - *default_filters - filecontent: 'Account: 000000000' # regex to id as receivables due - filecontent: '(?P<stem>...)' # regex to extract customer actions: - *default_actions - move: *receivables_due - *default_sorting - folders: *downloads filters: - *default_filters - filecontent: 'PAID' # regex to id as receivables paid - filecontent: '(?P<stem>...)' # regex to extract customer - filecontent: '(?P<paid>...)' # regex to extract date paid - filename: startswith: 2020 actions: - *default_actions - move: *receivables_paid - *default_sorting - rename: '{filecontent.paid}_{filecontent.stem}.{extension}' """ conf = Config.from_string(config) assert conf.rules == [ Rule( folders=["~/Downloads/"], filters=[ # default_filters Extension("pdf"), FileContent(expr="(?P<date>...)"), # added filters FileContent(expr="Due Date"), FileContent(expr="(?P<stem>...)"), ], actions=[ # default_actions Echo(msg="Dated: {filecontent.date}"), Echo(msg="Stem of filename: {filecontent.stem}"), # added actions Move(dest="~/PayablesDue/", overwrite=False), # default_sorting Rename( name= "{python.timestamp}-{filecontent.stem}.{extension.lower}", overwrite=False, ), Move(dest="{path.parent}/{python.quarter}/", overwrite=False), ], subfolders=False, system_files=False, ), Rule( folders=["~/Downloads/"], filters=[ # default_filters Extension("pdf"), FileContent(expr="(?P<date>...)"), # added filters FileContent(expr="Account: 000000000"), FileContent(expr="(?P<stem>...)"), ], actions=[ # default_actions Echo(msg="Dated: {filecontent.date}"), Echo(msg="Stem of filename: {filecontent.stem}"), # added actions Move(dest="~/Receivables/", overwrite=False), # default_sorting Rename( name= "{python.timestamp}-{filecontent.stem}.{extension.lower}", overwrite=False, ), Move(dest="{path.parent}/{python.quarter}/", overwrite=False), ], subfolders=False, system_files=False, ), Rule( folders=["~/Downloads/"], filters=[ # default_filters Extension("pdf"), FileContent(expr="(?P<date>...)"), # added filters FileContent(expr="PAID"), FileContent(expr="(?P<stem>...)"), FileContent(expr="(?P<paid>...)"), Filename(startswith="2020"), ], actions=[ # default_actions Echo(msg="Dated: {filecontent.date}"), Echo(msg="Stem of filename: {filecontent.stem}"), # added actions Move(dest="~/Accounting/Income/", overwrite=False), # default_sorting Rename( name= "{python.timestamp}-{filecontent.stem}.{extension.lower}", overwrite=False, ), Move(dest="{path.parent}/{python.quarter}/", overwrite=False), # added actions Rename( name="{filecontent.paid}_{filecontent.stem}.{extension}", overwrite=False, ), ], subfolders=False, system_files=False, ), ]