Fixed indents

This commit is contained in:
spla 2020-10-24 11:03:00 +02:00
pare 8659e6c68b
commit 9ab7d02526

207
setup.py
Veure arxiu

@ -9,126 +9,125 @@ import os
import sys import sys
def create_dir(): def create_dir():
if not os.path.exists('secrets'): if not os.path.exists('secrets'):
os.makedirs('secrets') os.makedirs('secrets')
def create_file(): def create_file():
if not os.path.exists('secrets/secrets.txt'): if not os.path.exists('secrets/secrets.txt'):
with open('secrets/secrets.txt', 'w'): pass with open('secrets/secrets.txt', 'w'): pass
print(secrets_filepath + " created!") print(secrets_filepath + " created!")
def create_config(): def create_config():
if not os.path.exists(config_filepath): if not os.path.exists(config_filepath):
print(config_filepath + " created!") print(config_filepath + " created!")
with open('config.txt', 'w'): pass with open('config.txt', 'w'): pass
def write_params(): def write_params():
with open(secrets_filepath, 'a') as the_file: with open(secrets_filepath, 'a') as the_file:
print("Writing secrets parameter names to " + secrets_filepath) print("Writing secrets parameter names to " + secrets_filepath)
the_file.write('uc_client_id: \n'+'uc_client_secret: \n'+'uc_access_token: \n') the_file.write('uc_client_id: \n'+'uc_client_secret: \n'+'uc_access_token: \n')
def write_config(): def write_config():
with open(config_filepath, 'a') as the_file: with open(config_filepath, 'a') as the_file:
the_file.write('mastodon_hostname: \n') the_file.write('mastodon_hostname: \n')
print("adding parameter name 'mastodon_hostname' to "+ config_filepath) print("adding parameter name 'mastodon_hostname' to "+ config_filepath)
def read_client_lines(self): def read_client_lines(self):
client_path = 'app_clientcred.txt' client_path = 'app_clientcred.txt'
with open(client_path) as fp: with open(client_path) as fp:
line = fp.readline() line = fp.readline()
cnt = 1 cnt = 1
while line: while line:
if cnt == 1: if cnt == 1:
print("Writing client id to " + secrets_filepath) print("Writing client id to " + secrets_filepath)
modify_file(secrets_filepath, "uc_client_id: ", value=line.rstrip()) modify_file(secrets_filepath, "uc_client_id: ", value=line.rstrip())
elif cnt == 2: elif cnt == 2:
print("Writing client secret to " + secrets_filepath) print("Writing client secret to " + secrets_filepath)
modify_file(secrets_filepath, "uc_client_secret: ", value=line.rstrip()) modify_file(secrets_filepath, "uc_client_secret: ", value=line.rstrip())
line = fp.readline() line = fp.readline()
cnt += 1 cnt += 1
def read_token_line(self): def read_token_line(self):
token_path = 'app_usercred.txt' token_path = 'app_usercred.txt'
with open(token_path) as fp: with open(token_path) as fp:
line = fp.readline() line = fp.readline()
print("Writing access token to " + secrets_filepath) print("Writing access token to " + secrets_filepath)
modify_file(secrets_filepath, "uc_access_token: ", value=line.rstrip()) modify_file(secrets_filepath, "uc_access_token: ", value=line.rstrip())
def read_config_line(): def read_config_line():
with open(config_filepath) as fp: with open(config_filepath) as fp:
line = fp.readline() line = fp.readline()
modify_file(config_filepath, "mastodon_hostname: ", value=hostname) modify_file(config_filepath, "mastodon_hostname: ", value=hostname)
def log_in(): def log_in():
error = 0 error = 0
try: try:
global hostname global hostname
hostname = input("Enter Mastodon hostname: ") hostname = input("Enter Mastodon hostname: ")
user_name = input("User name, ex. user@" + hostname +"? ") user_name = input("User name, ex. user@" + hostname +"? ")
user_password = getpass.getpass("User password? ") user_password = getpass.getpass("User password? ")
app_name = input("This app name? ") app_name = input("This app name? ")
Mastodon.create_app(app_name, scopes=["read","write"], Mastodon.create_app(app_name, scopes=["read","write"],
to_file="app_clientcred.txt", api_base_url=hostname) to_file="app_clientcred.txt", api_base_url=hostname)
mastodon = Mastodon(client_id = "app_clientcred.txt", api_base_url = hostname) mastodon = Mastodon(client_id = "app_clientcred.txt", api_base_url = hostname)
mastodon.log_in( mastodon.log_in(
user_name, user_name,
user_password, user_password,
scopes = ["read", "write"], scopes = ["read", "write"],
to_file = "app_usercred.txt" to_file = "app_usercred.txt"
) )
except MastodonIllegalArgumentError as i_error: except MastodonIllegalArgumentError as i_error:
error = 1 error = 1
if os.path.exists("secrets/secrets.txt"): if os.path.exists("secrets/secrets.txt"):
print("Removing secrets/secrets.txt file..") print("Removing secrets/secrets.txt file..")
os.remove("secrets/secrets.txt") os.remove("secrets/secrets.txt")
if os.path.exists("app_clientcred.txt"): if os.path.exists("app_clientcred.txt"):
print("Removing app_clientcred.txt file..") print("Removing app_clientcred.txt file..")
os.remove("app_clientcred.txt") os.remove("app_clientcred.txt")
sys.exit(i_error) sys.exit(i_error)
except MastodonNetworkError as n_error: except MastodonNetworkError as n_error:
error = 1 error = 1
if os.path.exists("secrets/secrets.txt"): if os.path.exists("secrets/secrets.txt"):
print("Removing secrets/secrets.txt file..") print("Removing secrets/secrets.txt file..")
os.remove("secrets/secrets.txt") os.remove("secrets/secrets.txt")
if os.path.exists("app_clientcred.txt"): if os.path.exists("app_clientcred.txt"):
print("Removing app_clientcred.txt file..") print("Removing app_clientcred.txt file..")
os.remove("app_clientcred.txt") os.remove("app_clientcred.txt")
sys.exit(n_error) sys.exit(n_error)
except MastodonReadTimeout as r_error: except MastodonReadTimeout as r_error:
error = 1 error = 1
if os.path.exists("secrets/secrets.txt"): if os.path.exists("secrets/secrets.txt"):
print("Removing secrets/secrets.txt file..") print("Removing secrets/secrets.txt file..")
os.remove("secrets/secrets.txt") os.remove("secrets/secrets.txt")
if os.path.exists("app_clientcred.txt"): if os.path.exists("app_clientcred.txt"):
print("Removing app_clientcred.txt file..") print("Removing app_clientcred.txt file..")
os.remove("app_clientcred.txt") os.remove("app_clientcred.txt")
sys.exit(r_error) sys.exit(r_error)
except MastodonAPIError as a_error: except MastodonAPIError as a_error:
error = 1 error = 1
if os.path.exists("secrets/secrets.txt"): if os.path.exists("secrets/secrets.txt"):
print("Removing secrets/secrets.txt file..") print("Removing secrets/secrets.txt file..")
os.remove("secrets/secrets.txt") os.remove("secrets/secrets.txt")
if os.path.exists("app_clientcred.txt"): if os.path.exists("app_clientcred.txt"):
print("Removing app_clientcred.txt file..") print("Removing app_clientcred.txt file..")
os.remove("app_clientcred.txt") os.remove("app_clientcred.txt")
sys.exit(a_error) sys.exit(a_error)
finally: finally:
if error == 0: if error == 0:
create_dir()
create_dir() create_file()
create_file() write_params()
write_params() client_path = 'app_clientcred.txt'
client_path = 'app_clientcred.txt' read_client_lines(client_path)
read_client_lines(client_path) token_path = 'app_usercred.txt'
token_path = 'app_usercred.txt' read_token_line(token_path)
read_token_line(token_path) if os.path.exists("app_clientcred.txt"):
if os.path.exists("app_clientcred.txt"): print("Removing app_clientcred.txt temp file..")
print("Removing app_clientcred.txt temp file..") os.remove("app_clientcred.txt")
os.remove("app_clientcred.txt") if os.path.exists("app_usercred.txt"):
if os.path.exists("app_usercred.txt"): print("Removing app_usercred.txt temp file..")
print("Removing app_usercred.txt temp file..") os.remove("app_usercred.txt")
os.remove("app_usercred.txt") print("Secrets setup done!\n")
print("Secrets setup done!\n")
def modify_file(file_name,pattern,value=""): def modify_file(file_name,pattern,value=""):
fh=fileinput.input(file_name,inplace=True) fh=fileinput.input(file_name,inplace=True)