def save_credentials(config_obj): """ Function that saves credentials to base path in a JSON format. Obviously not very secure, but temp fix. :param config_obj: Configuration QDialog object (from configuration.py) """ targetFolder = os.path.join(helpers.ROOT_DIR, config_obj.credentialsFolder) helpers.create_folder_if_needed(targetFolder) apiKey = config_obj.binanceApiKey.text() apiSecret = config_obj.binanceApiSecret.text() telegramApiKey = config_obj.telegramApiKey.text() telegramChatId = config_obj.telegramChatID.text() defaultPath = os.path.join(targetFolder, 'default.json') filePath, _ = QFileDialog.getSaveFileName(config_obj, 'Save Credentials', defaultPath, 'JSON (*.json)') filePath = filePath.strip() if filePath: helpers.write_json_file(filePath=filePath, apiKey=apiKey, apiSecret=apiSecret, telegramApiKey=telegramApiKey, chatID=telegramChatId) config_obj.credentialResult.setText( f'Credentials saved successfully to {os.path.basename(filePath)}.') else: config_obj.credentialResult.setText('Credentials could not be saved.')
def create_appropriate_config_folders(self, folder: str) -> str: """ Creates appropriate configuration folders. If a configuration folder doesn't exist, it'll create that. Next, it'll try to check if a type of configuration folder exists (e.g. Live, Simulation, Backtest). If it exists, it'll just return the path to it. If not, it'll create the folder then return the path to it. :param folder: Folder to create inside configuration folder. :return: Absolute path to new folder. """ basePath = os.path.join(helpers.ROOT_DIR, self.configFolder) helpers.create_folder_if_needed(basePath) targetPath = os.path.join(basePath, folder) helpers.create_folder_if_needed(targetPath, basePath=basePath) return targetPath
def load_credentials(config_obj, auto: bool = True): """ Attempts to load credentials automatically from path program regularly stores credentials in if auto is True. :param config_obj: Configuration QDialog object (from configuration.py) :param auto: Boolean regarding whether bot called this function or not. If bot called it, silently try to load credentials. If a user called it, however, open a file dialog to ask for the file path to credentials. """ targetFolder = os.path.join(helpers.ROOT_DIR, config_obj.credentialsFolder) if helpers.create_folder_if_needed(targetFolder): config_obj.credentialResult.setText('No credentials found.') return if not auto: filePath, _ = QFileDialog.getOpenFileName(config_obj, 'Load Credentials', targetFolder, "JSON (*.json)") else: filePath = os.path.join(targetFolder, 'default.json') try: credentials = helpers.load_json_file(jsonfile=filePath) config_obj.binanceApiKey.setText(credentials['apiKey']) config_obj.binanceApiSecret.setText(credentials['apiSecret']) config_obj.telegramApiKey.setText(credentials['telegramApiKey']) config_obj.telegramChatID.setText(credentials['chatID']) config_obj.credentialResult.setText( f'Credentials loaded successfully from {os.path.basename(filePath)}.' ) except FileNotFoundError: config_obj.credentialResult.setText('Could not load credentials.') except Exception as e: config_obj.credentialResult.setText(str(e))
def load_credentials(self, auto: bool = True): """ Attempts to load credentials automatically from path program regularly stores credentials in if auto is True. """ targetFolder = os.path.join(helpers.ROOT_DIR, self.credentialsFolder) if helpers.create_folder_if_needed(targetFolder): self.credentialResult.setText('No credentials found.') return if not auto: filePath, _ = QFileDialog.getOpenFileName(self, 'Load Credentials', targetFolder, "JSON (*.json)") else: filePath = os.path.join(targetFolder, 'default.json') try: credentials = helpers.load_json_file(jsonfile=filePath) self.binanceApiKey.setText(credentials['apiKey']) self.binanceApiSecret.setText(credentials['apiSecret']) self.telegramApiKey.setText(credentials['telegramApiKey']) self.telegramChatID.setText(credentials['chatID']) self.credentialResult.setText(f'Credentials loaded successfully from {os.path.basename(filePath)}.') except FileNotFoundError: self.credentialResult.setText('Could not load credentials.') except Exception as e: self.credentialResult.setText(str(e))