Compare commits
No commits in common. '5dc7ef76e883bde38b28067b1f4d107c17935b82' and '8119459d2fa263a9ff86830f31ae467580c18271' have entirely different histories.
5dc7ef76e8
...
8119459d2f
@ -1,61 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
import os
|
|
||||||
from os import mkdir
|
|
||||||
from os.path import isdir, isfile, join
|
|
||||||
from typing import Union
|
|
||||||
|
|
||||||
import yaml
|
|
||||||
|
|
||||||
|
|
||||||
class Config:
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
# Make sure we have config
|
|
||||||
self.config_basepath = join(os.environ["HOME"], ".knot")
|
|
||||||
self.config_filename = join(self.config_basepath, "config")
|
|
||||||
if not isdir(self.config_basepath):
|
|
||||||
mkdir(self.config_basepath)
|
|
||||||
|
|
||||||
def get_config(self) -> Union[None, dict]:
|
|
||||||
if not isfile(self.config_filename):
|
|
||||||
return None
|
|
||||||
with open(self.config_filename, "r") as fh:
|
|
||||||
return yaml.safe_load(fh.read())
|
|
||||||
|
|
||||||
def get_config_data(self) -> dict:
|
|
||||||
config_data = self.get_config()
|
|
||||||
config_data.pop("password", None)
|
|
||||||
return config_data
|
|
||||||
|
|
||||||
def get_current(self) -> str:
|
|
||||||
if os.path.islink(self.config_filename):
|
|
||||||
actual_path = os.readlink(self.config_filename)
|
|
||||||
return actual_path.split("-")[-1]
|
|
||||||
else:
|
|
||||||
return "none"
|
|
||||||
|
|
||||||
def set_context(self, context) -> bool:
|
|
||||||
symlink = f"{self.config_filename}-{context}"
|
|
||||||
found = os.path.isfile(symlink)
|
|
||||||
if os.path.islink(self.config_filename):
|
|
||||||
os.remove(self.config_filename)
|
|
||||||
elif os.path.isfile(self.config_filename):
|
|
||||||
os.rename(self.config_filename, symlink)
|
|
||||||
os.symlink(symlink, self.config_filename)
|
|
||||||
self.config_filename = symlink
|
|
||||||
return found
|
|
||||||
|
|
||||||
def set_config(
|
|
||||||
self,
|
|
||||||
baseurl: str,
|
|
||||||
username: str,
|
|
||||||
password: str,
|
|
||||||
):
|
|
||||||
config = {
|
|
||||||
"baseurl": baseurl,
|
|
||||||
"username": username,
|
|
||||||
"password": password
|
|
||||||
}
|
|
||||||
|
|
||||||
with open(self.config_filename, "w") as fh:
|
|
||||||
fh.write(yaml.dump(config))
|
|
@ -1,35 +0,0 @@
|
|||||||
import json
|
|
||||||
|
|
||||||
|
|
||||||
def error(description: str, error: str):
|
|
||||||
response = []
|
|
||||||
reply = {}
|
|
||||||
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/406
|
|
||||||
reply["Code"] = 406
|
|
||||||
reply["Description"] = description
|
|
||||||
reply["Error"] = error
|
|
||||||
response.append(reply)
|
|
||||||
return response
|
|
||||||
|
|
||||||
|
|
||||||
def nested_out(input, tabs="") -> str:
|
|
||||||
string = ""
|
|
||||||
if isinstance(input, str) or isinstance(input, int):
|
|
||||||
string += f"{input}\n"
|
|
||||||
elif isinstance(input, dict):
|
|
||||||
for key, value in input.items():
|
|
||||||
string += f"{tabs}{key}: {nested_out(value, tabs + " ")}"
|
|
||||||
elif isinstance(input, list):
|
|
||||||
for entry in input:
|
|
||||||
string += f"{tabs}\n{nested_out(entry, tabs + ' ')}"
|
|
||||||
return string
|
|
||||||
|
|
||||||
|
|
||||||
def output(response: list[dict], jsonout: bool = False):
|
|
||||||
try:
|
|
||||||
if jsonout:
|
|
||||||
print(json.dumps(response))
|
|
||||||
else:
|
|
||||||
print(nested_out(response))
|
|
||||||
except BrokenPipeError:
|
|
||||||
pass
|
|
Loading…
Reference in new issue