Same as 00a989e1b2 but for the cli

This commit is contained in:
Sergi Delgado Segura
2020-01-23 18:31:47 +01:00
parent 00a989e1b2
commit 836048c54d
2 changed files with 25 additions and 75 deletions

View File

@@ -1,33 +1,27 @@
import logging import os
import apps.cli.conf as conf
from common.tools import extend_paths, check_conf_fields, setup_logging
# PISA-SERVER LOG_PREFIX = "cli"
DEFAULT_PISA_API_SERVER = "btc.pisa.watch"
DEFAULT_PISA_API_PORT = 9814
# PISA-CLI # Load config fields
CLIENT_LOG_FILE = "pisa-cli.log" conf_fields = {
APPOINTMENTS_FOLDER_NAME = "appointments" "DEFAULT_PISA_API_SERVER": {"value": conf.DEFAULT_PISA_API_SERVER, "type": str},
"DEFAULT_PISA_API_PORT": {"value": conf.DEFAULT_PISA_API_PORT, "type": int},
"DATA_FOLDER": {"value": conf.DATA_FOLDER, "type": str},
"CLIENT_LOG_FILE": {"value": conf.CLIENT_LOG_FILE, "type": str, "path": True},
"APPOINTMENTS_FOLDER_NAME": {"value": conf.APPOINTMENTS_FOLDER_NAME, "type": str, "path": True},
"CLI_PUBLIC_KEY": {"value": conf.CLI_PUBLIC_KEY, "type": str, "path": True},
"CLI_PRIVATE_KEY": {"value": conf.CLI_PRIVATE_KEY, "type": str, "path": True},
"PISA_PUBLIC_KEY": {"value": conf.PISA_PUBLIC_KEY, "type": str, "path": True},
}
CLI_PUBLIC_KEY = "cli_pk.der" # Expand user (~) if found and check fields are correct
CLI_PRIVATE_KEY = "cli_sk.der" conf_fields["DATA_FOLDER"]["value"] = os.path.expanduser(conf_fields["DATA_FOLDER"]["value"])
PISA_PUBLIC_KEY = "pisa_pk.der" # Extend relative paths
conf_fields = extend_paths(conf_fields["DATA_FOLDER"]["value"], conf_fields)
# Create the file logger # Sanity check fields and build config dictionary
f_logger = logging.getLogger("cli_file_log") config = check_conf_fields(conf_fields)
f_logger.setLevel(logging.INFO)
fh = logging.FileHandler(CLIENT_LOG_FILE) setup_logging(config.get("CLIENT_LOG_FILE"), LOG_PREFIX)
fh.setLevel(logging.INFO)
fh_formatter = logging.Formatter("%(message)s")
fh.setFormatter(fh_formatter)
f_logger.addHandler(fh)
# Create the console logger
c_logger = logging.getLogger("cli_console_log")
c_logger.setLevel(logging.INFO)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
ch_formatter = logging.Formatter("%(asctime)s %(message)s.", "%Y-%m-%d %H:%M:%S")
ch.setFormatter(ch_formatter)
c_logger.addHandler(ch)

View File

@@ -9,24 +9,18 @@ from getopt import getopt, GetoptError
from requests import ConnectTimeout, ConnectionError from requests import ConnectTimeout, ConnectionError
from uuid import uuid4 from uuid import uuid4
from apps.cli import config, LOG_PREFIX
from apps.cli.help import help_add_appointment, help_get_appointment from apps.cli.help import help_add_appointment, help_get_appointment
from apps.cli.blob import Blob from apps.cli.blob import Blob
import apps.cli.conf as conf
from common.logger import Logger from common.logger import Logger
from common.appointment import Appointment from common.appointment import Appointment
from common.cryptographer import Cryptographer from common.cryptographer import Cryptographer
from common.tools import ( from common.tools import check_sha256_hex_format, check_locator_format, compute_locator, setup_data_folder
check_sha256_hex_format,
check_locator_format,
compute_locator,
check_conf_fields,
setup_data_folder,
)
HTTP_OK = 200 HTTP_OK = 200
logger = Logger("Client") logger = Logger(actor="Client", log_name_prefix=LOG_PREFIX)
# FIXME: TESTING ENDPOINT, WON'T BE THERE IN PRODUCTION # FIXME: TESTING ENDPOINT, WON'T BE THERE IN PRODUCTION
@@ -53,42 +47,6 @@ def generate_dummy_appointment():
logger.info("\nData stored in dummy_appointment_data.json") logger.info("\nData stored in dummy_appointment_data.json")
def load_config(config):
"""
Looks through all of the config options to make sure they contain the right type of data and builds a config
dictionary.
Args:
config (:obj:`module`): It takes in a config module object.
Returns:
:obj:`dict` A dictionary containing the config values.
"""
conf_dict = {}
data_folder = config.DATA_FOLDER
if isinstance(data_folder, str):
data_folder = os.path.expanduser(data_folder)
else:
raise ValueError("The provided user folder is invalid.")
conf_fields = {
"DEFAULT_PISA_API_SERVER": {"value": config.DEFAULT_PISA_API_SERVER, "type": str},
"DEFAULT_PISA_API_PORT": {"value": config.DEFAULT_PISA_API_PORT, "type": int},
"DATA_FOLDER": {"value": data_folder, "type": str},
"CLIENT_LOG_FILE": {"value": data_folder + config.CLIENT_LOG_FILE, "type": str},
"APPOINTMENTS_FOLDER_NAME": {"value": data_folder + config.APPOINTMENTS_FOLDER_NAME, "type": str},
"CLI_PUBLIC_KEY": {"value": data_folder + config.CLI_PUBLIC_KEY, "type": str},
"CLI_PRIVATE_KEY": {"value": data_folder + config.CLI_PRIVATE_KEY, "type": str},
"PISA_PUBLIC_KEY": {"value": data_folder + config.PISA_PUBLIC_KEY, "type": str},
}
check_conf_fields(conf_fields, logger)
return conf_dict
# Loads and returns Pisa keys from disk # Loads and returns Pisa keys from disk
def load_key_file_data(file_name): def load_key_file_data(file_name):
try: try:
@@ -380,8 +338,6 @@ def show_usage():
if __name__ == "__main__": if __name__ == "__main__":
config = load_config(conf)
pisa_api_server = config.get("DEFAULT_PISA_API_SERVER") pisa_api_server = config.get("DEFAULT_PISA_API_SERVER")
pisa_api_port = config.get("DEFAULT_PISA_API_PORT") pisa_api_port = config.get("DEFAULT_PISA_API_PORT")
commands = ["add_appointment", "get_appointment", "help"] commands = ["add_appointment", "get_appointment", "help"]