Skip to content

Commit

Permalink
CTX-5430: Code cleanup, removed userdata.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogdan Tintor committed Jul 24, 2024
1 parent 9b28f5e commit e51959e
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 128 deletions.
9 changes: 6 additions & 3 deletions coretex/cli/commands/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import click

from ..modules import project_utils, user, utils, ui
from ..modules import ui
from ..modules.project_utils import getProject
from ..modules.user import initializeUserSession
from ..modules.utils import onBeforeCommandExecute
from ...entities import Model
from ...configuration import UserConfiguration

Expand All @@ -18,7 +21,7 @@ def create(name: str, path: str, project: Optional[str], accuracy: float) -> Non
# If project was provided used that, otherwise get the one from config
# If project that was provided does not exist prompt user to create a new
# one with that name
ctxProject = project_utils.getProject(project, userConfig)
ctxProject = getProject(project, userConfig)
if ctxProject is None:
return

Expand All @@ -32,7 +35,7 @@ def create(name: str, path: str, project: Optional[str], accuracy: float) -> Non


@click.group()
@utils.onBeforeCommandExecute(user.initializeUserSession)
@onBeforeCommandExecute(initializeUserSession)
def model() -> None:
pass

Expand Down
26 changes: 13 additions & 13 deletions coretex/cli/commands/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@
import click

from ...utils import docker
from ..modules import user, ui, utils
from ..modules import ui
from ..modules import node as node_module
from ..modules import update as update_module
from ..modules.node import NodeStatus
from ..modules.user import initializeUserSession
from ..modules.utils import onBeforeCommandExecute, checkEnvironment
from ..modules.update import activateAutoUpdate, getNodeStatus
from ...configuration import UserConfiguration, NodeConfiguration


@click.command()
@click.option("--image", type = str, help = "Docker image url")
@utils.onBeforeCommandExecute(node_module.initializeNodeConfiguration)
@onBeforeCommandExecute(node_module.initializeNodeConfiguration)
def start(image: Optional[str]) -> None:
if node_module.isRunning():
if not ui.clickPrompt(
Expand Down Expand Up @@ -59,7 +61,7 @@ def start(image: Optional[str]) -> None:

node_module.start(dockerImage, userConfig, nodeConfig)
docker.removeDanglingImages(node_module.getRepoFromImageUrl(dockerImage), node_module.getTagFromImageUrl(dockerImage))
update_module.activateAutoUpdate()
activateAutoUpdate()


@click.command()
Expand All @@ -74,7 +76,7 @@ def stop() -> None:
@click.command()
@click.option("-y", "autoAccept", is_flag = True, help = "Accepts all prompts.")
@click.option("-n", "autoDecline", is_flag = True, help = "Declines all prompts.")
@utils.onBeforeCommandExecute(node_module.initializeNodeConfiguration)
@onBeforeCommandExecute(node_module.initializeNodeConfiguration)
def update(autoAccept: bool, autoDecline: bool) -> None:
if autoAccept and autoDecline:
ui.errorEcho("Only one of the flags (\"-y\" or \"-n\") can be used at the same time.")
Expand Down Expand Up @@ -114,7 +116,7 @@ def update(autoAccept: bool, autoDecline: bool) -> None:
ui.stdEcho("Updating node...")
node_module.pull(nodeConfig.image)

if update_module.getNodeStatus() == update_module.NodeStatus.busy and not autoAccept:
if getNodeStatus() == NodeStatus.busy and not autoAccept:
if autoDecline:
return

Expand All @@ -136,8 +138,6 @@ def update(autoAccept: bool, autoDecline: bool) -> None:
node_module.getTagFromImageUrl(nodeConfig.image)
)

update_module.activateAutoUpdate()


@click.command()
@click.option("--verbose", is_flag = True, help = "Configure node settings manually.")
Expand Down Expand Up @@ -171,14 +171,14 @@ def config(verbose: bool) -> None:
ui.previewConfig(userConfig, nodeConfig)

ui.successEcho("Node successfully configured.")
update_module.activateAutoUpdate()
activateAutoUpdate()


@click.group()
@utils.onBeforeCommandExecute(docker.isDockerAvailable)
@utils.onBeforeCommandExecute(user.initializeUserSession)
@utils.onBeforeCommandExecute(node_module.checkResourceLimitations)
@utils.onBeforeCommandExecute(utils.checkEnvironment)
@onBeforeCommandExecute(docker.isDockerAvailable)
@onBeforeCommandExecute(initializeUserSession)
@onBeforeCommandExecute(node_module.checkResourceLimitations)
@onBeforeCommandExecute(checkEnvironment)
def node() -> None:
pass

Expand Down
6 changes: 4 additions & 2 deletions coretex/cli/commands/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

import click

from ..modules import ui, project_utils, utils, user
from ..modules import ui, project_utils
from ..modules.user import initializeUserSession
from ..modules.utils import onBeforeCommandExecute
from ...entities import Project, ProjectVisibility
from ...networking import RequestFailedError
from ...configuration import UserConfiguration
Expand Down Expand Up @@ -90,7 +92,7 @@ def select(name: str) -> None:


@click.group()
@utils.onBeforeCommandExecute(user.initializeUserSession)
@onBeforeCommandExecute(initializeUserSession)
def project() -> None:
pass

Expand Down
9 changes: 5 additions & 4 deletions coretex/cli/commands/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@

import click

from ... import folder_manager
from ..modules import user, utils, project_utils
from ..modules.project_utils import getProject
from ..modules.user import initializeUserSession
from ..modules.utils import onBeforeCommandExecute
from ... import folder_manager
from ...configuration import UserConfiguration
from ...entities import TaskRun, TaskRunStatus
Expand All @@ -33,7 +34,7 @@ class RunException(Exception):


@click.command()
@utils.onBeforeCommandExecute(user.initializeUserSession)
@onBeforeCommandExecute(initializeUserSession)
@click.argument("path", type = click.Path(exists = True, dir_okay = False))
@click.option("--name", type = str, default = None)
@click.option("--description", type = str, default = None)
Expand All @@ -49,7 +50,7 @@ def run(path: str, name: Optional[str], description: Optional[str], snapshot: bo

folder_manager.clearTempFiles()

selectedProject = project_utils.getProject(project, userConfig)
selectedProject = getProject(project, userConfig)
# clearing temporary files in case that node was manually killed before

if selectedProject is None:
Expand Down
4 changes: 1 addition & 3 deletions coretex/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@
from .commands.task import run
from .commands.project import project

from .modules import ui
from .modules import ui, utils
from .modules.intercept import ClickExceptionInterceptor
from .modules import utils

from ..utils.process import CommandException


Expand Down
5 changes: 3 additions & 2 deletions coretex/cli/modules/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@

import click

from . import utils, ui, config_defaults
from . import ui, config_defaults
from .utils import isGPUAvailable
from ...cryptography import rsa
from ...networking import networkManager, NetworkRequestError
from ...utils import CommandException, docker
Expand Down Expand Up @@ -358,7 +359,7 @@ def configureNode(nodeConfig: NodeConfiguration, verbose: bool) -> None:
else:
nodeConfig.image = "coretexai/coretex-node"

if utils.isGPUAvailable():
if isGPUAvailable():
nodeConfig.allowGpu = ui.clickPrompt("Do you want to allow the Node to access your GPU? (Y/n)", type = bool, default = True)
else:
nodeConfig.allowGpu = False
Expand Down
2 changes: 2 additions & 0 deletions coretex/cli/modules/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
from enum import IntEnum
from pathlib import Path

import logging

import requests

from .utils import getExecPath
Expand Down
3 changes: 1 addition & 2 deletions coretex/cli/modules/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@
import venv
import shutil
import logging
import platform

from py3nvml import py3nvml

import click
import requests

from . import ui
import platform

from ...configuration import DEFAULT_VENV_PATH
from ...utils.process import command

Expand Down
22 changes: 11 additions & 11 deletions coretex/networking/network_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from .network_response import NetworkResponse
from .network_manager_base import NetworkManagerBase
from .user_data import UserData
from ..configuration import UserConfiguration


class NetworkManager(NetworkManagerBase):
Expand All @@ -33,27 +33,27 @@ class NetworkManager(NetworkManagerBase):
def __init__(self) -> None:
super().__init__()

self.__userData = UserData()
self.__userConfig = UserConfiguration()

@property
def _apiToken(self) -> Optional[str]:
return self.__userData.apiToken
return self.__userConfig.token

@_apiToken.setter
def _apiToken(self, value: Optional[str]) -> None:
self.__userData.apiToken = value
self.__userConfig.token = value

@property
def _refreshToken(self) -> Optional[str]:
return self.__userData.refreshToken
return self.__userConfig.refreshToken

@_refreshToken.setter
def _refreshToken(self, value: Optional[str]) -> None:
self.__userData.refreshToken = value
self.__userConfig.refreshToken = value

@property
def hasStoredCredentials(self) -> bool:
return self.__userData.hasStoredCredentials
return self.__userConfig.isUserConfigured()

def authenticate(self, username: str, password: str, storeCredentials: bool = True) -> NetworkResponse:
"""
Expand Down Expand Up @@ -82,8 +82,8 @@ def authenticate(self, username: str, password: str, storeCredentials: bool = Tr
"""

if storeCredentials:
self.__userData.username = username
self.__userData.password = password
self.__userConfig.username = username
self.__userConfig.password = password

# authenticate using credentials stored in requests.Session.auth
return super().authenticate(username, password, storeCredentials)
Expand All @@ -101,10 +101,10 @@ def authenticateWithStoredCredentials(self) -> NetworkResponse:
ValueError -> if credentials are not found
"""

if self.__userData.username is None or self.__userData.password is None:
if self.__userConfig.username is None or self.__userConfig.password is None:
raise ValueError(">> [Coretex] Credentials not stored")

return self.authenticate(self.__userData.username, self.__userData.password)
return self.authenticate(self.__userConfig.username, self.__userConfig.password)


networkManager: NetworkManagerBase = NetworkManager()
88 changes: 0 additions & 88 deletions coretex/networking/user_data.py

This file was deleted.

0 comments on commit e51959e

Please sign in to comment.