2019-07-08 07:57:29 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2023-11-23 22:59:09 +01:00
|
|
|
import argparse
|
|
|
|
import json
|
|
|
|
import logging
|
2019-07-08 07:57:29 +02:00
|
|
|
import os
|
|
|
|
import urllib.request
|
|
|
|
from pprint import pprint
|
2023-11-23 22:59:09 +01:00
|
|
|
|
2019-07-08 07:57:29 +02:00
|
|
|
import coloredlogs
|
2023-11-23 22:59:09 +01:00
|
|
|
import ovh
|
|
|
|
import xdg.BaseDirectory
|
2019-07-08 07:57:29 +02:00
|
|
|
|
2021-06-13 11:49:21 +02:00
|
|
|
coloredlogs.install(level="DEBUG", fmt="%(levelname)s %(message)s")
|
2019-07-08 07:57:29 +02:00
|
|
|
log = logging.getLogger()
|
|
|
|
|
|
|
|
debug = None
|
|
|
|
|
2021-06-13 11:49:21 +02:00
|
|
|
|
|
|
|
class OvhCli:
|
2019-07-08 07:57:29 +02:00
|
|
|
ROOT = "https://api.ovh.com/1.0?null"
|
2021-06-13 11:49:21 +02:00
|
|
|
|
2019-07-08 07:57:29 +02:00
|
|
|
def __init__(self):
|
2021-06-13 11:49:21 +02:00
|
|
|
self.cacheDir = os.path.join(xdg.BaseDirectory.xdg_cache_home, "ovhcli")
|
2019-07-08 07:57:29 +02:00
|
|
|
# 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")
|
2021-06-13 11:49:21 +02:00
|
|
|
rootJsonPath = os.path.join(self.cacheDir, "root.json")
|
2019-07-08 07:57:29 +02:00
|
|
|
log.debug(f"{self.ROOT} -> {rootJsonPath}")
|
|
|
|
urllib.request.urlretrieve(self.ROOT, rootJsonPath)
|
2021-06-13 11:49:21 +02:00
|
|
|
with open(rootJsonPath, "rt") as rootJson:
|
2019-07-08 07:57:29 +02:00
|
|
|
root = json.load(rootJson)
|
2021-06-13 11:49:21 +02:00
|
|
|
basePath = root["basePath"]
|
2019-07-08 07:57:29 +02:00
|
|
|
|
2021-06-13 11:49:21 +02:00
|
|
|
for apiRoot in root["apis"]:
|
|
|
|
fmt = "json"
|
|
|
|
assert fmt in apiRoot["format"]
|
|
|
|
path = apiRoot["path"]
|
|
|
|
schema = apiRoot["schema"].format(format=fmt, path=path)
|
2019-07-08 07:57:29 +02:00
|
|
|
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):
|
2021-06-13 11:49:21 +02:00
|
|
|
parser = argparse.ArgumentParser(description="Access the OVH API")
|
2019-07-08 07:57:29 +02:00
|
|
|
return parser
|
|
|
|
|
|
|
|
|
2021-06-13 11:49:21 +02:00
|
|
|
if __name__ == "__main__":
|
2019-07-08 07:57:29 +02:00
|
|
|
cli = OvhCli()
|
|
|
|
# cli.updateCache()
|
|
|
|
parser = cli.createParser()
|
|
|
|
args = parser.parse_args()
|
|
|
|
print(args)
|