Skip to content

Commit

Permalink
Support for multiple sections in the config file, allows for user to …
Browse files Browse the repository at this point in the history
…have multiple profiles stored
  • Loading branch information
mike-jt79 committed Feb 1, 2021
1 parent 535450e commit 6a0acee
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 38 deletions.
24 changes: 16 additions & 8 deletions epiccli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ def format_localised_currency(data):

@click.group()
@click.pass_context
@click.option("--config", help="Configuration file to load (default is ~/.epic/config)")
def main(ctx, config):
@click.option('-c', "--config", help="Configuration file to load (default is ~/.epic/config)")
@click.option('-p', "--profile", help="Load the named profile from the configuration file", default="default", show_default=True)
def main(ctx, config, profile):
"""CLI for communicating with the EPIC"""
click.echo(pyfiglet.Figlet().renderText("EPIC by Zenotech"))

Expand All @@ -73,7 +74,7 @@ def main(ctx, config):
try:
click.echo("Loading config from %s" % config_file)

config = EpicConfig(config_file=config_file)
config = EpicConfig(config_file=config_file, config_section=profile)

# V2 API Client
epic = EPICClient(
Expand All @@ -97,12 +98,12 @@ def configure(ctx):
config_file = os.path.join(Path.home(), '.epic', 'config')
if os.path.isfile(config_file):
try:
click.echo("loading")
config = EpicConfig(config_file=config_file)
default_url = config.EPIC_API_URL
default_token = config.EPIC_TOKEN
except ConfigurationException as e:
pass

epic_url = click.prompt(
"Please enter the EPIC Url to connect to", default=default_url
)
Expand All @@ -111,11 +112,18 @@ def configure(ctx):
"Please enter your EPIC API token", default=default_token
)

config = configparser.RawConfigParser()
config.add_section("epic")
config.set("epic", "url", epic_url)
config.set("epic", "token", token)
profile = click.prompt(
"Name of profile to store", default='default'
)

config = configparser.ConfigParser()
config_file = os.path.expanduser(config_file)
if os.path.isfile(config_file):
config.read(config_file)
if not config.has_section(profile):
config.add_section(profile)
config.set(profile, "url", epic_url)
config.set(profile, "token", token)
try:
os.makedirs(os.path.dirname(config_file))
except OSError as e:
Expand Down
43 changes: 13 additions & 30 deletions epiccli/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,27 @@

import os
import errno
from configparser import SafeConfigParser, RawConfigParser
from configparser import ConfigParser

from .exceptions import ConfigurationException, CommandError, ResponseError


class EpicConfig(object):
""" Class for loading and checking CLI configuration """

def __init__(self, epic_url=None, epic_token=None, config_file=None):
def __init__(self, epic_url=None, epic_token=None, config_file=None, config_section="default"):
super(EpicConfig, self).__init__()
self._load_config(epic_url, epic_token, config_file)
self._load_config(epic_url, epic_token, config_file, config_section)
self._check_config()

def _load_config(self, epic_url=None, epic_token=None, config_file=None):
def _load_config(self, epic_url=None, epic_token=None, config_file=None, config_section='default'):
"""
Load client config, order of precedence = args > env > config_file
"""
self.EPIC_API_URL = None
self.EPIC_TOKEN = None
if config_file is not None:
self._load_config_file(config_file)
self._load_config_file(config_file, config_section)
self._config_file = config_file
self.EPIC_API_URL = os.environ.get("EPIC_API_ENDPOINT", self.EPIC_API_URL)
self.EPIC_TOKEN = os.environ.get("EPIC_TOKEN", self.EPIC_TOKEN)
Expand All @@ -71,31 +71,14 @@ def _check_config(self):
"Missing EPIC URL, set EPIC_TOKEN or supply a configuration file"
)

def _load_config_file(self, file):
parser = SafeConfigParser(allow_no_value=True)
def _load_config_file(self, file, config_section):
parser = ConfigParser(allow_no_value=True)
if os.path.isfile(file):
parser.read(file)
if parser.has_section("epic"):
self.EPIC_API_URL = parser.get("epic", "url")
self.EPIC_TOKEN = parser.get("epic", "token")
if parser.has_section(config_section):
self.EPIC_API_URL = parser.get(config_section, "url")
self.EPIC_TOKEN = parser.get(config_section, "token")
else:
raise ConfigurationException(f"Invalid EPIC configuration, cannot find section {config_section}")
else:
raise ConfigurationException("Invalid EPIC configuration file %s" % file)

def write_config_file(self, file=None):
output_file = file if file is not None else self._config_file
if output_file is None:
raise ConfigurationException(
"Valid config file not specified for write_config_file"
)
config = RawConfigParser()
config.add_section("epic")
config.set("epic", "url", self.EPIC_API_URL)
config.set("epic", "token", self.EPIC_TOKEN)
config_file = os.path.expanduser(output_file)
try:
os.makedirs(os.path.dirname(config_file))
except OSError as e:
if e.errno != errno.EEXIST:
raise
with open(config_file, "w") as configfile:
config.write(configfile)
raise ConfigurationException(f"Invalid EPIC configuration file {file}")

0 comments on commit 6a0acee

Please sign in to comment.