def get_experiment(id): """Get experiments""" console = Console() try: thread = experimentClient.get_experiment_async(id) timeout = time.time() + TIMEOUT with console.status( "[bold green] Fetching Experiment(id = {} )...".format(id)): while not thread.ready(): time.sleep(POLLING_INTERVAL) if time.time() > timeout: console.print("[bold red] Timeout!") return result = thread.get() result = result.result json_data = richJSON.from_data(result) console.print(Panel(json_data, title="Experiment(id = {} )".format(id))) except ApiException as err: if err.body is not None: errbody = json.loads(err.body) click.echo("[Api Error] {}".format(errbody["message"])) else: click.echo("[Api Error] {}".format(err))
def write_json( self, data: Any, indent: int = 2, ) -> None: if self.isatty(): from rich.json import JSON j = JSON.from_data(data, indent=indent) return self.write(j, styled=True) import json return self.write(json.dumps(data))
def delete_serve(model_name: str, model_version: int, wait: bool): """Delete serve""" console = Console() try: thread = serveClient.delete_serve(model_name, model_version, async_req=True) timeout = time.time() + TIMEOUT with console.status( f"[bold green] Deleting Serve with name: {model_name}, version: {model_version}" ): while not thread.ready(): time.sleep(POLLING_INTERVAL) if time.time() > timeout: console.print("[bold red] Timeout!") return result = thread.get() click.echo(result) if wait: if result["status"] == "Deleted": console.print( f"[bold green] Serve: model name:{model_name}, version: {model_version}" ) else: console.print("[bold red] Failed") json_data = richJSON.from_data(result) console.print( Panel( json_data, title= (f"[bold green] Serve Deleted: model name:{model_name}, version:" f" {model_version}"), )) except ApiException as err: if err.body is not None: errbody = json.loads(err.body) click.echo(f"[Api Error] {errbody['message']}") else: click.echo(f"[Api Error] {err}")
def list_config(): """List Submarine CLI Config""" console = Console() _config = loadConfig() json_data = richJSON.from_data({**asdict(_config)}) console.print(Panel(json_data, title="SubmarineCliConfig"))
def print_run_config(obj, run_number, get_config, dotnanorc, html, host, port): dotnanorc = os.path.expanduser(dotnanorc) log = logging.getLogger("getconf") obj.console.print(f"[blue]Loading {dotnanorc}[/blue]") f = open(dotnanorc) dotnanorc = json.load(f) credentials.add_login("rundb", dotnanorc["rundb"]["user"], dotnanorc["rundb"]["password"]) credentials.add_login("runregistrydb", dotnanorc["runregistrydb"]["user"], dotnanorc["runregistrydb"]["password"]) log.info("RunDB socket " + dotnanorc["rundb"]["socket"]) log.info("RunRegistryDB socket " + dotnanorc["runregistrydb"]["socket"]) metadata_query = "runregistry/getRunMetaLast/1" if run_number: metadata_query = "runregistry/getRunMeta/" + str(run_number) r = query(dotnanorc["runregistrydb"]["socket"], metadata_query, dotnanorc["runregistrydb"]["user"], dotnanorc["runregistrydb"]["password"], log) data = json.loads(r.text)[1][0] keys = json.loads(r.text)[0] obj.console.export_html() zip_iterator = zip(keys, data) data_dict = dict(zip_iterator) run_number = data_dict['RUN_NUMBER'] text = f"Considering run number {run_number}" grid = Table(title=f"Run #{run_number}", show_header=False, show_edge=True) grid.add_column() grid.add_column() for key, val in data_dict.items(): grid.add_row(str(key), str(val)) obj.console.print(grid) if not get_config: return obj.console.print() obj.console.print() obj.console.print() obj.console.print(Text("Configuration following...")) obj.console.print() obj.console.print() obj.console.print() metadata_query = "runregistry/getRunBlob/" + str(run_number) data = query(dotnanorc["runregistrydb"]["socket"], metadata_query, dotnanorc["runregistrydb"]["user"], dotnanorc["runregistrydb"]["password"], log) f = tempfile.NamedTemporaryFile(mode="w+b", suffix='.tar.gz', delete=False) f.write(data.content) fname = f.name f.close() # f = open(fname) with tempfile.TemporaryDirectory() as temp_name: tar = tarfile.open(fname, "r:gz") tar.extractall(temp_name) tar.close() for rootn, dirn, filen in os.walk(temp_name): for f in filen: name = os.path.join(rootn, f) print(name) try: # such a sloppy way of doing things. Get a grip man! file_name = name.split(temp_name)[1] file_name = "/".join(file_name.split("/")[2:]) except: file_name = name fi = open(name, "r") grid = Table(title=f"File: {file_name}", show_header=False) grid.add_row(JSON(fi.read())) obj.console.print(grid) if not html: return html_text = obj.console.export_html() class Server: def __init__(self, html_text, host, port): self.host = host if host == "0.0.0.0" or host == "localhost": self.host = socket.gethostname() self.port = port self.html_text = html_text def run(self): app = Flask(__name__) def index(): return self.html_text app.add_url_rule("/", "index", index, methods=["GET"]) app.run(host=self.host, port=self.port, debug=False, use_reloader=False) serv = Server(html_text, host, port) obj.console.print( f"\n\n\n\n\nYou can now navigate to:\nhttp:{serv.host}:{serv.port}\nCtrl-C when you are done!\n\n\n\n\n" ) serv.run()
def test_print_json_data_with_default(): date = datetime.date(2021, 1, 1) json = JSON.from_data({"date": date}, default=lambda d: d.isoformat()) assert str(json.text) == '{\n "date": "2021-01-01"\n}'