Skip to content

Commit

Permalink
Replace ConfigParser with ConfigObj
Browse files Browse the repository at this point in the history
  • Loading branch information
pl4nty committed Mar 13, 2023
1 parent 0bfde30 commit 4009908
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
6 changes: 3 additions & 3 deletions ctfcli/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import configparser
import configobj
import importlib
import os
import subprocess
Expand Down Expand Up @@ -51,10 +51,10 @@ def init(self, directory=None, no_config=False, no_git=False):
(path / ".ctf").mkdir()

# Create initial .ctf/config file
config = configparser.ConfigParser()
config = configobj.ConfigObj()
config["config"] = {"url": ctf_url, "access_token": ctf_token}
config["challenges"] = {}
with (path / ".ctf" / "config").open(mode="a+") as f:
with (path / ".ctf" / "config").open(mode="a+b") as f:
config.write(f)

# Create a git repo in the event folder
Expand Down
25 changes: 8 additions & 17 deletions ctfcli/utils/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import configparser
import configobj
import json
import os

Expand Down Expand Up @@ -35,25 +35,15 @@ def get_project_path():

def load_config():
path = get_config_path()
parser = configparser.ConfigParser()
parser = configobj.ConfigObj(path)

# Preserve case in configparser
parser.optionxform = str

parser.read(path)
return parser


def preview_config(as_string=False):
config = load_config()

d = {}
for section in config.sections():
d[section] = {}
for k, v in config.items(section):
d[section][k] = v

preview = json.dumps(d, sort_keys=True, indent=4)
preview = json.dumps(config, sort_keys=True, indent=4)

if as_string is True:
return preview
Expand All @@ -71,7 +61,10 @@ def generate_session():
# Handle SSL verification disabling
try:
# Get an ssl_verify config. Default to True if it doesn't exist
ssl_verify = config["config"].getboolean("ssl_verify", True)
if config["config"].get("ssl_verify"):
ssl_verify = config["config"].as_bool("ssl_verify")
else:
ssl_verify = True
except ValueError:
# If we didn't a proper boolean value we should load it as a string
# https://requests.kennethreitz.org/en/master/user/advanced/#ssl-cert-verification
Expand All @@ -80,8 +73,6 @@ def generate_session():
s = APISession(prefix_url=url)
s.verify = ssl_verify
s.headers.update({"Authorization": f"Token {access_token}"})

if 'cookies' in config:
s.cookies.update(config["cookies"])
s.cookies.update(config["config"].get("cookies", {}))

return s

0 comments on commit 4009908

Please sign in to comment.