1
0
mirror of https://github.com/hughrun/ephemetoot synced 2025-02-18 12:40:41 +01:00

refactor init()

This moves the input() statements into four separate functions taking values.

The main reason for doing so was to allow for unit testing, however it also makes the init() function a little clearer.
Also, potentially, these input functions can now be used by other functions in future.
This commit is contained in:
Hugh Rundle 2020-08-31 14:28:08 +10:00
parent ffa50dd448
commit 0a01f18e44

View File

@ -19,82 +19,59 @@ import requests
# local
from ephemetoot import plist
def compulsory_input(tags, name, example):
value = ""
while len(value) < 1:
if example:
value = input(tags[0] + name + tags[1] + example + tags[2])
else:
value = input(tags[0] + name + tags[2])
return value
def digit_input(tags, name, example):
value = ""
while value.isdigit() == False:
if example:
value = input(tags[0] + name + tags[1] + example + tags[2])
else:
value = input(tags[0] + name + tags[2])
return value
def yes_no_input(tags, name):
value = ""
while value not in ["y", "n"]:
value = input(
tags[0] + name + tags[1] + "(y or n):" + tags[2]
)
return_val = "true" if value == "y" else "false"
return return_val
def optional_input(tags, name, example):
value = input(tags[0] + name + tags[1] + example + tags[2])
return value
def init():
'''
Creates a config.yaml file in the current directory, based on user input.
'''
try:
init_start = "\033[96m"
init_end = "\033[0m"
init_eg = "\033[2m"
# text colour markers (beginning, example, end)
tags = ("\033[96m", "\033[2m", "\033[0m")
conf_token = ""
while len(conf_token) < 1:
conf_token = input(init_start + "Access token: " + init_end)
conf_user = ""
while len(conf_user) < 1:
conf_user = input(
init_start
+ "Username"
+ init_eg
+ "(without the '@' - e.g. alice):"
+ init_end
)
conf_url = ""
while len(conf_url) < 1:
conf_url = input(
init_start + "Base URL" + init_eg + "(e.g. example.social):" + init_end
)
conf_days = ""
while conf_days.isdigit() == False:
conf_days = input(
init_start + "Days to keep" + init_eg + "(default 365):" + init_end
)
conf_keep_pinned = ""
while conf_keep_pinned not in ["y", "n"]:
conf_keep_pinned = input(
init_start + "Keep pinned toots?" + init_eg + "(y or n):" + init_end
)
conf_pinned = "true" if conf_keep_pinned == "y" else "false"
conf_keep_toots = input(
init_start
+ "Toots to keep"
+ init_eg
+ " (optional list of IDs separated by commas):"
+ init_end
)
conf_keep_hashtags = input(
init_start
+ "Hashtags to keep"
+ init_eg
+ " (optional list separated by commas):"
+ init_end
)
conf_keep_visibility = input(
init_start
+ "Visibility to keep"
+ init_eg
+ " (optional list separated by commas):"
+ init_end
)
conf_archive = input(
init_start
+ "Archive path"
+ init_eg
+ " (optional filepath for archive):"
+ init_end
)
conf_token = compulsory_input(tags, "Access token: ", None)
conf_user = compulsory_input(tags, "Username", "(without the '@' - e.g. alice):")
conf_url = compulsory_input(tags, "Base URL", "(e.g. example.social):")
conf_days = digit_input(tags, "Days to keep", "(default 365):")
conf_pinned = yes_no_input(tags, "Keep pinned toots?")
conf_keep_toots = optional_input(tags, "Toots to keep", "(optional list of IDs separated by commas):")
conf_keep_hashtags = optional_input(tags, "Hashtags to keep", "(optional list separated by commas):")
conf_keep_visibility = optional_input(tags, "Visibility to keep", "(optional list separated by commas):")
conf_archive = optional_input(tags, "Archive path", "(optional filepath for archive):")
# write out the config file
with open("config.yaml", "w") as configfile:
@ -129,6 +106,8 @@ def init():
configfile.close()
except Exception as e:
print(e)
def version(vnum):
'''