Coretex
project_utils.py
1 from typing import Optional
2 
3 import logging
4 
5 import click
6 
7 from . import ui
8 from ...configuration import UserConfiguration
9 from ...entities import Project, ProjectType, ProjectVisibility
10 from ...networking import NetworkRequestError
11 
12 
13 def selectProjectType() -> ProjectType:
14  availableProjectTypes = {
15  "Computer Vision": ProjectType.computerVision,
16  "Motion Recognition": ProjectType.motionRecognition,
17  "Bioinformatics": ProjectType.bioInformatics,
18  "Other": ProjectType.other
19  }
20 
21  choices = list(availableProjectTypes.keys())
22  selectedChoice = ui.arrowPrompt(choices, "Specify type of project that you wish to create")
23 
24  selectedProjectType = availableProjectTypes[selectedChoice]
25  ui.stdEcho(f"You've chosen: {selectedChoice}")
26  return selectedProjectType
27 
28 
29 def selectProjectVisibility() -> ProjectVisibility:
30  availableProjectVisibilities = {
31  "Private": ProjectVisibility.private,
32  "Public": ProjectVisibility.public,
33  }
34 
35  choices = list(availableProjectVisibilities.keys())
36  selectedChoice = ui.arrowPrompt(choices, "Specify visibility of project:")
37 
38  selectedProjectVisibility = availableProjectVisibilities[selectedChoice]
39  ui.stdEcho(f"You've chosen: {selectedChoice}")
40  return selectedProjectVisibility
41 
42 
43 def promptProjectCreate(message: str, name: str, frontendUrl: str) -> Optional[Project]:
44  if not click.confirm(message, default = True):
45  return None
46 
47  selectedProjectType = selectProjectType()
48 
49  try:
50  project = Project.createProject(name, selectedProjectType)
51  ui.successEcho(f"Project \"{name}\" created successfully.")
52  ui.stdEcho(
53  "A new Project has been created. "
54  f"You can open it by clicking on this URL {ui.outputUrl(frontendUrl, project.entityUrl())}."
55  )
56  return project
57  except NetworkRequestError as ex:
58  logging.getLogger("cli").debug(ex, exc_info = ex)
59  raise click.ClickException(f"Failed to create project \"{name}\".")
60 
61 
62 def promptProjectSelect(userConfig: UserConfiguration) -> Optional[Project]:
63  name = ui.clickPrompt("Specify project name that you wish to select")
64 
65  ui.progressEcho("Validating project...")
66  try:
67  project = Project.fetchOne(name = name)
68  ui.successEcho(f"Project \"{name}\" selected successfully!")
69  userConfig.selectProject(project.id)
70  except ValueError:
71  ui.errorEcho(f"Project \"{name}\" not found.")
72  newProject = promptProjectCreate(
73  "Do you want to create a project with that name?",
74  name,
75  userConfig.frontendUrl
76  )
77  if newProject is None:
78  return None
79 
80  project = newProject
81  userConfig.selectProject(project.id)
82 
83  return project
84 
85 
86 def createProject(frontendUrl: str, name: Optional[str] = None, projectType: Optional[int] = None, description: Optional[str] = None) -> Project:
87  if name is None:
88  name = ui.clickPrompt("Please enter name of the project you want to create", type = str)
89 
90  if projectType is None:
91  projectType = selectProjectType()
92  else:
93  projectType = ProjectType(projectType)
94 
95  if description is None:
96  description = ui.clickPrompt("Please enter your project's description", type = str, default = "", show_default = False)
97 
98  try:
99  project = Project.createProject(name, projectType, description = description)
100  ui.successEcho(f"Project \"{name}\" created successfully.")
101  ui.stdEcho(
102  "A new Project has been created. "
103  f"You can open it by clicking on this URL {ui.outputUrl(frontendUrl, project.entityUrl())}."
104  )
105  return project
106  except NetworkRequestError as ex:
107  logging.getLogger("cli").debug(ex, exc_info = ex)
108  raise click.ClickException(f"Failed to create project \"{name}\".")
109 
110 
111 def getProject(name: Optional[str], userConfig: UserConfiguration) -> Optional[Project]:
112  projectId = userConfig.projectId
113  if name is not None:
114  try:
115  return Project.fetchOne(name = name)
116  except:
117  if projectId is None:
118  return promptProjectCreate(
119  "Project not found. Do you want to create a new Project with that name?",
120  name,
121  userConfig.frontendUrl
122  )
123 
124  return Project.fetchById(projectId)
125 
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)
130 
131  if not click.confirm("Would you like to create a new Project?", default = True):
132  return None
133 
134  return createProject(userConfig.frontendUrl, name)
135 
136  return Project.fetchById(projectId)