Skip to content

Commit

Permalink
Add default values to functions
Browse files Browse the repository at this point in the history
Aimed to be a fix for #66 (concat incompatible type errors under certain conditions)
  • Loading branch information
hughrun committed Feb 12, 2021
1 parent 1038bf6 commit 039a142
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
8 changes: 4 additions & 4 deletions ephemetoot/ephemetoot.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,9 @@ def print_rate_limit_message(mastodon):
)


def retry_on_error(options, mastodon, toot, attempts):
def retry_on_error(options, mastodon, toot, attempts=0):

if attempts < 6:
if attempts < 6:
try:
console_print(
"Attempt " + str(attempts) + " at " + datestamp_now(), options, False
Expand All @@ -396,7 +396,7 @@ def retry_on_error(options, mastodon, toot, attempts):
raise TimeoutError("Gave up after 5 attempts")


def process_toot(config, options, mastodon, deleted_count, toot):
def process_toot(config, options, mastodon, toot, deleted_count=0):

keep_pinned = "keep_pinned" in config and config["keep_pinned"]
toots_to_keep = config["toots_to_keep"] if "toots_to_keep" in config else []
Expand Down Expand Up @@ -531,7 +531,7 @@ def check_batch(config, options, mastodon, user_id, timeline, deleted_count=0):
try:
for toot in timeline:
# process_toot returns the value of the deleted_count so we can keep track here
deleted_count = process_toot(config, options, mastodon, deleted_count, toot)
deleted_count = process_toot(config, options, mastodon, toot, deleted_count)

# the account_statuses call is paginated with a 40-toot limit
# get the id of the last toot to include as 'max_id' in the next API call.
Expand Down
24 changes: 11 additions & 13 deletions tests/test_ephemetoot.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def test_check_batch(capfd, monkeypatch):
# this simulates what would happen if the toot was being deleted
monkeypatch.setattr(
"ephemetoot.ephemetoot.process_toot",
lambda config, options, mastodon, deleted_count, toot: deleted_count + 1,
lambda config, options, mastodon, toot, deleted_count: deleted_count + 1,
)
# run check_batch
ephemetoot.check_batch(config, options, mastodon, user_id, timeline, 0)
Expand All @@ -249,7 +249,7 @@ def test_check_batch_quiet(capfd, monkeypatch):
timeline = mastodon.account_statuses(user_id=user_id, limit=2, max_id=0)
monkeypatch.setattr(
"ephemetoot.ephemetoot.process_toot",
lambda config, options, mastodon, deleted_count, toot: deleted_count + 1,
lambda config, options, mastodon, toot, deleted_count: deleted_count + 1,
)
ephemetoot.check_batch(config, options, mastodon, user_id, timeline, 0)
# deleted_count should be 10
Expand All @@ -265,7 +265,7 @@ def test_check_batch_quiet_no_toots(capfd, monkeypatch):
timeline = mastodon.account_statuses(user_id=user_id, limit=2, max_id=10)
monkeypatch.setattr(
"ephemetoot.ephemetoot.process_toot",
lambda config, options, mastodon, deleted_count, toot: deleted_count + 1,
lambda config, options, mastodon, toot, deleted_count: deleted_count + 1,
)
# run check_batch
ephemetoot.check_batch(config, options, mastodon, user_id, timeline, 0)
Expand All @@ -281,7 +281,7 @@ def test_check_batch_qq(capfd, monkeypatch):
timeline = mastodon.account_statuses(user_id=user_id, limit=2, max_id=0)
monkeypatch.setattr(
"ephemetoot.ephemetoot.process_toot",
lambda config, options, mastodon, deleted_count, toot: deleted_count + 1,
lambda config, options, mastodon, toot, deleted_count: deleted_count + 1,
)
ephemetoot.check_batch(config, options, mastodon, user_id, timeline, 0)
# deleted_count should be 10 and message printed since there was a delete
Expand All @@ -297,7 +297,7 @@ def test_check_batch_qq_no_deletes(capfd, monkeypatch):
# simulate no deletes occuring
monkeypatch.setattr(
"ephemetoot.ephemetoot.process_toot",
lambda config, options, mastodon, deleted_count, toot: 0,
lambda config, options, mastodon, toot, deleted_count: 0,
)
# run check_batch
ephemetoot.check_batch(config, options, mastodon, user_id, timeline, 0)
Expand All @@ -313,7 +313,7 @@ def test_check_batch_qqq(capfd, monkeypatch):
timeline = mastodon.account_statuses(user_id=user_id, limit=2, max_id=0)
monkeypatch.setattr(
"ephemetoot.ephemetoot.process_toot",
lambda config, options, mastodon, deleted_count, toot: deleted_count + 1,
lambda config, options, mastodon, toot, deleted_count: deleted_count + 1,
)
# run check_batch
ephemetoot.check_batch(config, options, mastodon, user_id, timeline, 0)
Expand Down Expand Up @@ -377,7 +377,6 @@ def test_init_archive_path(tmpdir):
os.path.join(good_path, "/bad/path/"), "Archive path", None
)
ok = ephemetoot.sanitise_input(good_path, "Archive path", None)
also_ok = ephemetoot.sanitise_input("~/Desktop", "Archive path", None)

assert ok == "ok"
assert wrong == "That directory does not exist, please try again"
Expand Down Expand Up @@ -423,7 +422,6 @@ def test_init_sanitise_tag_list():
def test_init_sanitise_url():
tags = ("\033[96m", "\033[2m", "\033[0m")
wrong = ephemetoot.sanitise_input("http://example.social", "Base URL", tags)
also_wrong = ephemetoot.sanitise_input("http://example.social", "Base URL", tags)
ok = ephemetoot.sanitise_input("example.social", "Base URL", tags)

assert (
Expand Down Expand Up @@ -476,7 +474,7 @@ def test_process_toot(capfd, tmpdir, monkeypatch):
toot_dict["visibility"] = "public"
toot_dict["reblog"] = False
toot = dict2obj(toot_dict)
ephemetoot.process_toot(config_file, options, mastodon, 0, toot)
ephemetoot.process_toot(config_file, options, mastodon, toot, 0)
assert (
capfd.readouterr().out
== "❌ deleting toot 104136090490756999 tooted 09 May 2020\n"
Expand All @@ -492,7 +490,7 @@ def test_process_toot_pinned(capfd, tmpdir):
mastodon = Mocktodon()
toot_dict["pinned"] = True
toot = dict2obj(toot_dict)
ephemetoot.process_toot(config_file, options, mastodon, 0, toot)
ephemetoot.process_toot(config_file, options, mastodon, toot, 0)
assert capfd.readouterr().out == "📌 skipping pinned toot - 104136090490756999\n"


Expand All @@ -506,7 +504,7 @@ def test_process_toot_saved(capfd, tmpdir):
mastodon = Mocktodon()
toot_dict["pinned"] = False
toot = dict2obj(toot_dict)
ephemetoot.process_toot(config_file, options, mastodon, 0, toot)
ephemetoot.process_toot(config_file, options, mastodon, toot, 0)
assert capfd.readouterr().out == "💾 skipping saved toot - 104136090490756999\n"


Expand All @@ -522,7 +520,7 @@ def test_process_toot_visibility(capfd, tmpdir):
toot_dict["pinned"] = False # is true above so make false
toot_dict["visibility"] = "testing"
toot = dict2obj(toot_dict)
ephemetoot.process_toot(config_file, options, mastodon, 0, toot)
ephemetoot.process_toot(config_file, options, mastodon, toot, 0)
assert capfd.readouterr().out == "👀 skipping testing toot - 104136090490756999\n"


Expand All @@ -540,7 +538,7 @@ def test_process_toot_hashtag(capfd, tmpdir, monkeypatch):
toot_dict["reblog"] = True
toot = dict2obj(toot_dict)

ephemetoot.process_toot(config_file, options, mastodon, 0, toot)
ephemetoot.process_toot(config_file, options, mastodon, toot, 0)
assert (
capfd.readouterr().out
== "👎 unboosting toot 104136090490756999 boosted 09 May 2020\n"
Expand Down

0 comments on commit 039a142

Please sign in to comment.