maloja/maloja/setup.py

81 lines
2.7 KiB
Python
Raw Permalink Normal View History

2022-04-09 22:02:17 +03:00
import os
2022-03-10 07:31:41 +03:00
from importlib import resources
2020-06-05 14:16:02 +03:00
from distutils import dir_util
2022-04-09 22:02:17 +03:00
2020-06-05 14:16:02 +03:00
from doreah.io import col, ask, prompt
2020-08-17 20:04:51 +03:00
from doreah import auth
2020-06-05 14:16:02 +03:00
from .pkg_global.conf import data_dir, dir_settings, malojaconfig
2020-06-05 14:16:02 +03:00
# EXTERNAL API KEYS
2021-12-25 04:22:57 +03:00
ext_apikeys = [
2021-12-24 11:41:41 +03:00
"LASTFM_API_KEY",
"SPOTIFY_API_ID",
"SPOTIFY_API_SECRET",
"AUDIODB_API_KEY"
]
2020-06-05 14:16:02 +03:00
def copy_initial_local_files():
2022-03-10 07:31:41 +03:00
with resources.files("maloja") / 'data_files' as folder:
for cat in dir_settings:
dir_util.copy_tree(os.path.join(folder,cat),dir_settings[cat],update=False)
2020-06-05 14:16:02 +03:00
2020-08-18 05:55:36 +03:00
charset = list(range(10)) + list("abcdefghijklmnopqrstuvwxyz") + list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
2020-08-17 20:04:51 +03:00
def randomstring(length=32):
import random
Refactoring (#83) * Merge isinstance calls * Inline variable that is immediately returned * Replace set() with comprehension * Replace assignment with augmented assignment * Remove unnecessary else after guard condition * Convert for loop into list comprehension * Replace unused for index with underscore * Merge nested if conditions * Convert for loop into list comprehension * Convert for loop into set comprehension * Remove unnecessary else after guard condition * Replace if statements with if expressions * Simplify sequence comparison * Replace multiple comparisons with in operator * Merge isinstance calls * Merge nested if conditions * Add guard clause * Merge duplicate blocks in conditional * Replace unneeded comprehension with generator * Inline variable that is immediately returned * Remove unused imports * Replace unneeded comprehension with generator * Remove unused imports * Remove unused import * Inline variable that is immediately returned * Swap if/else branches and remove unnecessary else * Use str.join() instead of for loop * Multiple refactors - Remove redundant pass statement - Hoist repeated code outside conditional statement - Swap if/else to remove empty if body * Inline variable that is immediately returned * Simplify generator expression * Replace if statement with if expression * Multiple refactoring - Replace range(0, x) with range(x) - Swap if/else branches - Remove unnecessary else after guard condition * Use str.join() instead of for loop * Hoist repeated code outside conditional statement * Use str.join() instead of for loop * Inline variables that are immediately returned * Merge dictionary assignment with declaration * Use items() to directly unpack dictionary values * Extract dup code from methods into a new one
2021-10-19 15:58:24 +03:00
return "".join(str(random.choice(charset)) for _ in range(length))
2020-06-05 14:16:02 +03:00
def setup():
copy_initial_local_files()
2021-12-19 23:10:55 +03:00
SKIP = malojaconfig["SKIP_SETUP"]
2020-06-05 14:16:02 +03:00
print("Various external services can be used to display images. If not enough of them are set up, only local images will be used.")
2021-12-25 04:22:57 +03:00
for k in ext_apikeys:
2021-12-24 11:41:41 +03:00
keyname = malojaconfig.get_setting_info(k)['name']
2021-12-19 23:10:55 +03:00
key = malojaconfig[k]
2021-11-27 22:04:13 +03:00
if key is False:
2022-04-09 18:05:54 +03:00
print(f"\tCurrently not using a {col['red'](keyname)} for image display.")
2021-11-27 22:04:13 +03:00
elif key is None or key == "ASK":
2022-04-09 18:05:54 +03:00
promptmsg = f"\tPlease enter your {col['gold'](keyname)}. If you do not want to use one at this moment, simply leave this empty and press Enter."
key = prompt(promptmsg,types=(str,),default=False,skip=SKIP)
2021-12-19 23:10:55 +03:00
malojaconfig[k] = key
2020-06-05 14:16:02 +03:00
else:
2022-04-09 18:05:54 +03:00
print(f"\t{col['lawngreen'](keyname)} found.")
2020-06-05 14:16:02 +03:00
# OWN API KEY
2022-04-09 22:02:17 +03:00
from .apis import apikeystore
2021-12-25 04:22:57 +03:00
if len(apikeystore) == 0:
2020-06-05 14:16:02 +03:00
answer = ask("Do you want to set up a key to enable scrobbling? Your scrobble extension needs that key so that only you can scrobble tracks to your database.",default=True,skip=SKIP)
if answer:
2021-12-25 04:22:57 +03:00
key = apikeystore.generate_key('default')
2020-06-05 14:16:02 +03:00
print("Your API Key: " + col["yellow"](key))
2020-08-17 20:04:51 +03:00
# PASSWORD
2021-12-19 23:10:55 +03:00
forcepassword = malojaconfig["FORCE_PASSWORD"]
2020-08-17 20:04:51 +03:00
# this is mainly meant for docker, supply password via environment variable
if forcepassword is not None:
# user has specified to force the pw, nothing else matters
auth.defaultuser.setpw(forcepassword)
print("Password has been set.")
elif auth.defaultuser.checkpw("admin"):
# if the actual pw is admin, it means we've never set this up properly (eg first start after update)
2022-03-29 05:33:44 +03:00
while True:
2022-02-13 07:56:36 +03:00
newpw = prompt("Please set a password for web backend access. Leave this empty to generate a random password.",skip=SKIP,secret=True)
if newpw is None:
newpw = randomstring(32)
print("Generated password:",col["yellow"](newpw))
2022-03-29 05:33:44 +03:00
break
2022-02-13 07:56:36 +03:00
else:
newpw_repeat = prompt("Please type again to confirm.",skip=SKIP,secret=True)
if newpw != newpw_repeat: print("Passwords do not match!")
2022-03-29 05:33:44 +03:00
else: break
2022-02-20 07:06:38 +03:00
auth.defaultuser.setpw(newpw)