Added setup attribute

This commit is contained in:
spla 2022-08-29 12:34:00 +02:00
pare b2aeb6816f
commit 91adee9fa5

Veure arxiu

@ -1,6 +1,7 @@
import os import os
import os.path import os.path
import requests import requests
import getpass
from collections import OrderedDict from collections import OrderedDict
### ###
@ -23,20 +24,22 @@ class Ejabberd:
name = 'Ejabberd API wrapper' name = 'Ejabberd API wrapper'
def __init__(self, secrets_config_file=None, api_base_url=None, local_vhost=None, admin_account=None, admin_pass=None): def __init__(self, api_base_url=None, local_vhost=None, admin_account=None, admin_pass=None):
self.__secrets_config_file = secrets_config_file self.__ejabberd_config_path = "secrets/ejabberd_secrets.txt"
try: is_setup = self.__check_setup(self)
self.__api_base_url = self.__get_parameter("api_base_url", self.__secrets_config_file) if is_setup:
self.__local_vhost = self.__get_parameter("local_vhost", self.__secrets_config_file)
self.__admin_account = self.__get_parameter("admin_account", self.__secrets_config_file)
self.__admin_pass = self.__get_parameter("admin_pass", self.__secrets_config_file)
except: self.__api_base_url = self.__get_parameter("api_base_url", self.__ejabberd_config_path)
self.__local_vhost = self.__get_parameter("local_vhost", self.__ejabberd_config_path)
self.__admin_account = self.__get_parameter("admin_account", self.__ejabberd_config_path)
self.__admin_pass = self.__get_parameter("admin_pass", self.__ejabberd_config_path)
raise EjabberdAPIConfigError("secrets config file not found! create it and pass it as argument to this class.") else:
self.__api_base_url, self.__local_vhost, self.__admin_account, self.__admin_pass = self.setup(self)
def check_account(self, username, host): def check_account(self, username, host):
@ -253,6 +256,39 @@ class Ejabberd:
return response return response
@staticmethod
def __check_setup(self):
is_setup = False
if not os.path.isfile(self.__ejabberd_config_path):
print(f"File {self.__ejabberd_config_path} not found, running setup.")
else:
is_setup = True
return is_setup
@staticmethod
def setup(self):
if not os.path.exists('secrets'):
os.makedirs('secrets')
self.__api_base_url = input("api_base_url, in ex. 'http://127.0.0.1:5280': ")
self.__local_vhost = input("local_vhost, in ex. 'ejabberd.server': ")
self.__admin_account = input("admin_account, in ex. 'admin@ejabberd.server': ")
self.__admin_pass = getpass.getpass("admin_pass, in ex. 'my_very_hard_secret_pass': ")
if not os.path.exists(self.__ejabberd_config_path):
with open(self.__ejabberd_config_path, 'w'): pass
print(f"{self.__ejabberd_config_path} created!")
with open(self.__ejabberd_config_path, 'a') as the_file:
print("Writing ejabberd secrets parameters to " + self.__ejabberd_config_path)
the_file.write(f'api_base_url: {self.__api_base_url}\n'+f'local_vhost: {self.__local_vhost}\n'+f'admin_account: {self.__admin_account}\n'+f'admin_pass: {self.__admin_pass}\n')
return (self.__api_base_url, self.__local_vhost, self.__admin_account, self.__admin_pass)
@staticmethod @staticmethod
def __get_parameter(parameter, file_path ): def __get_parameter(parameter, file_path ):