dotfiles/config/scripts/ovhcli

63 lines
1.9 KiB
Python
Executable file

#!/usr/bin/env python3
import argparse
import json
import logging
import os
import urllib.request
from pprint import pprint
import coloredlogs
import ovh
import xdg.BaseDirectory
coloredlogs.install(level="DEBUG", fmt="%(levelname)s %(message)s")
log = logging.getLogger()
debug = None
class OvhCli:
ROOT = "https://api.ovh.com/1.0?null"
def __init__(self):
self.cacheDir = os.path.join(xdg.BaseDirectory.xdg_cache_home, "ovhcli")
# TODO Corner cases: links, cache dir not done, configurable cache
if not os.path.isdir(self.cacheDir):
assert not os.path.exists(self.cacheDir)
os.makedirs(self.cacheDir)
def updateCache(self):
log.info("Downloading the API description")
rootJsonPath = os.path.join(self.cacheDir, "root.json")
log.debug(f"{self.ROOT} -> {rootJsonPath}")
urllib.request.urlretrieve(self.ROOT, rootJsonPath)
with open(rootJsonPath, "rt") as rootJson:
root = json.load(rootJson)
basePath = root["basePath"]
for apiRoot in root["apis"]:
fmt = "json"
assert fmt in apiRoot["format"]
path = apiRoot["path"]
schema = apiRoot["schema"].format(format=fmt, path=path)
apiJsonPath = os.path.join(self.cacheDir, schema[1:])
apiJsonUrl = basePath + schema
log.debug(f"{apiJsonUrl} -> {apiJsonPath}")
apiJsonPathDir = os.path.dirname(apiJsonPath)
if not os.path.isdir(apiJsonPathDir):
os.makedirs(apiJsonPathDir)
urllib.request.urlretrieve(apiJsonUrl, apiJsonPath)
def createParser(self):
parser = argparse.ArgumentParser(description="Access the OVH API")
return parser
if __name__ == "__main__":
cli = OvhCli()
# cli.updateCache()
parser = cli.createParser()
args = parser.parse_args()
print(args)