1 from typing
import Optional
8 from ...configuration
import UserConfiguration
9 from ...entities
import Project, ProjectType, ProjectVisibility
10 from ...networking
import NetworkRequestError
13 def selectProjectType() -> ProjectType:
14 availableProjectTypes = {
15 "Computer Vision": ProjectType.computerVision,
16 "Motion Recognition": ProjectType.motionRecognition,
17 "Bioinformatics": ProjectType.bioInformatics,
18 "Other": ProjectType.other
21 choices = list(availableProjectTypes.keys())
22 selectedChoice = ui.arrowPrompt(choices,
"Specify type of project that you wish to create")
24 selectedProjectType = availableProjectTypes[selectedChoice]
25 ui.stdEcho(f
"You've chosen: {selectedChoice}")
26 return selectedProjectType
29 def selectProjectVisibility() -> ProjectVisibility:
30 availableProjectVisibilities = {
31 "Private": ProjectVisibility.private,
32 "Public": ProjectVisibility.public,
35 choices = list(availableProjectVisibilities.keys())
36 selectedChoice = ui.arrowPrompt(choices,
"Specify visibility of project:")
38 selectedProjectVisibility = availableProjectVisibilities[selectedChoice]
39 ui.stdEcho(f
"You've chosen: {selectedChoice}")
40 return selectedProjectVisibility
43 def promptProjectCreate(message: str, name: str, frontendUrl: str) -> Optional[Project]:
44 if not click.confirm(message, default =
True):
47 selectedProjectType = selectProjectType()
50 project = Project.createProject(name, selectedProjectType)
51 ui.successEcho(f
"Project \"{name}\" created successfully.")
53 "A new Project has been created. "
54 f
"You can open it by clicking on this URL {ui.outputUrl(frontendUrl, project.entityUrl())}."
57 except NetworkRequestError
as ex:
58 logging.getLogger(
"cli").debug(ex, exc_info = ex)
59 raise click.ClickException(f
"Failed to create project \"{name}\".")
62 def promptProjectSelect(userConfig: UserConfiguration) -> Optional[Project]:
63 name = ui.clickPrompt(
"Specify project name that you wish to select")
65 ui.progressEcho(
"Validating project...")
67 project = Project.fetchOne(name = name)
68 ui.successEcho(f
"Project \"{name}\" selected successfully!")
69 userConfig.selectProject(project.id)
71 ui.errorEcho(f
"Project \"{name}\" not found.")
72 newProject = promptProjectCreate(
73 "Do you want to create a project with that name?",
75 userConfig.frontendUrl
77 if newProject
is None:
81 userConfig.selectProject(project.id)
86 def createProject(frontendUrl: str, name: Optional[str] =
None, projectType: Optional[int] =
None, description: Optional[str] =
None) -> Project:
88 name = ui.clickPrompt(
"Please enter name of the project you want to create", type = str)
90 if projectType
is None:
91 projectType = selectProjectType()
93 projectType = ProjectType(projectType)
95 if description
is None:
96 description = ui.clickPrompt(
"Please enter your project's description", type = str, default =
"", show_default =
False)
99 project = Project.createProject(name, projectType, description = description)
100 ui.successEcho(f
"Project \"{name}\" created successfully.")
102 "A new Project has been created. "
103 f
"You can open it by clicking on this URL {ui.outputUrl(frontendUrl, project.entityUrl())}."
106 except NetworkRequestError
as ex:
107 logging.getLogger(
"cli").debug(ex, exc_info = ex)
108 raise click.ClickException(f
"Failed to create project \"{name}\".")
111 def getProject(name: Optional[str], userConfig: UserConfiguration) -> Optional[Project]:
112 projectId = userConfig.projectId
115 return Project.fetchOne(name = name)
117 if projectId
is None:
118 return promptProjectCreate(
119 "Project not found. Do you want to create a new Project with that name?",
121 userConfig.frontendUrl
124 return Project.fetchById(projectId)
126 if projectId
is None:
127 ui.stdEcho(
"To use this command you need to have a Project selected.")
128 if click.confirm(
"Would you like to select an existing Project?", default =
True):
129 return promptProjectSelect(userConfig)
131 if not click.confirm(
"Would you like to create a new Project?", default =
True):
134 return createProject(userConfig.frontendUrl, name)
136 return Project.fetchById(projectId)