Skip to content

Commit 455f3db

Browse files
committed
Resolved Code Duplications and Error Handling Issues
1 parent 13d9a36 commit 455f3db

File tree

1 file changed

+24
-44
lines changed

1 file changed

+24
-44
lines changed

mlc/main.py

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -954,23 +954,21 @@ def find(self, run_args):
954954
else:
955955
repo = run_args.get('repo', run_args.get('item', run_args.get('artifact')))
956956

957-
# Check if repo is None or empty
957+
# Check if repo is None or empty
958958
if not repo:
959-
raise ValueError(
960-
"Please enter a Repo Alias, Repo UID, or Repo URL in one of the following formats:\n"
961-
"- <repo_owner>@<repos_name>\n"
962-
"- <repo_url>\n"
963-
"- <repo_uid>\n"
964-
"- <repo_alias>\n"
965-
"- <repo_alias>,<repo_uid>"
966-
)
959+
return {"return": 1, "error": "Please enter a Repo Alias, Repo UID, or Repo URL in one of the following formats:\n"
960+
"- <repo_owner>@<repos_name>\n"
961+
"- <repo_url>\n"
962+
"- <repo_uid>\n"
963+
"- <repo_alias>\n"
964+
"- <repo_alias>,<repo_uid>"}
967965

968966
# Handle the different repo input formats
969967
repo_name = None
970968
repo_uid = None
971969

972970
# Check if the repo is in the format of a repo UID (alphanumeric string)
973-
if repo.isalnum(): # Assuming repo_uid is alphanumeric
971+
if self.is_uid(repo):
974972
repo_uid = repo
975973
if "," in repo:
976974
repo_split = repo.split(",")
@@ -979,25 +977,18 @@ def find(self, run_args):
979977
repo_uid = repo_split[1]
980978
elif "@" in repo:
981979
repo_name = repo
982-
elif re.match(r"(?:https?://)?(?:www\.)?github\.com/([^/]+)/([^/.]+)(?:\.git)?", repo):
983-
# Convert GitHub URL to user@repo_name format if necessary
984-
pattern = r"(?:https?://)?(?:www\.)?github\.com/([^/]+)/([^/.]+)(?:\.git)?"
985-
match = re.match(pattern, repo)
986-
if match:
987-
user, repo_name = match.groups()
988-
repo_name = f"{user}@{repo_name}"
980+
elif "github.com" in repo:
981+
result = self.github_url_to_user_repo_format(repo)
982+
if result["return"] == 0:
983+
repo_name = result["value"]
989984
else:
990-
raise ValueError(f"Invalid GitHub URL format for: {repo}")
991-
else:
992-
# If URL is malformed, handle it separately
993-
if "github.com" in repo and not re.match(r"(https?://)?(?:www\.)?github\.com/([^/]+)/([^/.]+)(?:\.git)?", repo):
994-
raise ValueError(f"Invalid GitHub URL format for: {repo}")
985+
return result
995986

996987
# Check if repo_name exists in repos.json
997988
matched_repo_path = None
998989
for repo_obj in repos_list:
999990
if repo_name and repo_name == os.path.basename(repo_obj.path) :
1000-
matched_repo_path = repo_obj.path
991+
matched_repo_path = repo_obj
1001992
break
1002993

1003994
# Search through self.repos for matching repos
@@ -1007,16 +998,16 @@ def find(self, run_args):
1007998
lst.append(i)
1008999
elif repo_name == i.meta['alias']:
10091000
lst.append(i)
1010-
elif repo.isalnum() and not any(i.meta['uid'] == repo_uid for i in self.repos):
1011-
raise ValueError(f"No repository with UID: '{repo_uid}' was found")
1012-
elif not matched_repo_path and not any(i.meta['alias'] == repo_name for i in self.repos) and not any(i.meta['uid'] == repo_uid for i in self.repos ):
1013-
raise ValueError(f"No repository with alias: '{repo_name}' was found")
1001+
elif self.is_uid(repo) and not any(i.meta['uid'] == repo_uid for i in self.repos):
1002+
return {"return": 1, "error": f"No repository with UID: '{repo_uid}' was found"}
10141003
elif "," in repo and not matched_repo_path and not any(i.meta['uid'] == repo_uid for i in self.repos) and not any(i.meta['alias'] == repo_name for i in self.repos):
1015-
raise ValueError(f"No repository with alias: '{repo_name}' and UID: '{repo_uid}' was found")
1016-
1017-
1004+
return {"return": 1, "error": f"No repository with alias: '{repo_name}' and UID: '{repo_uid}' was found"}
1005+
elif not matched_repo_path and not any(i.meta['alias'] == repo_name for i in self.repos) and not any(i.meta['uid'] == repo_uid for i in self.repos ):
1006+
return {"return": 1, "error": f"No repository with alias: '{repo_name}' was found"}
1007+
10181008
# Append the matched repo path
1019-
lst.append(matched_repo_path)
1009+
if(len(lst)==0):
1010+
lst.append(matched_repo_path)
10201011

10211012
return {'return': 0, 'list': lst}
10221013
except Exception as e:
@@ -1426,19 +1417,8 @@ def process_console_output(res, target, action, run_args):
14261417
if len(res['list']) == 0:
14271418
logger.warn(f"""No {target} entry found for the specified tags: {run_args['tags']}!""")
14281419
else:
1429-
seen_paths = set() # To avoid duplicate logging
1430-
for item in res["list"]:
1431-
if isinstance(item, str): # If item is a string (repo path)
1432-
if item not in seen_paths:
1433-
logger.info(f"""Item path: {item}""")
1434-
seen_paths.add(item)
1435-
1436-
elif hasattr(item, "path"): # If item has `.path` attribute
1437-
if item.path not in seen_paths:
1438-
logger.info(f"""Item path: {item.path}""")
1439-
seen_paths.add(item.path)
1440-
1441-
1420+
for item in res['list']:
1421+
logger.info(f"""Item path: {item.path}""")
14421422

14431423
# Main CLI function
14441424
def main():

0 commit comments

Comments
 (0)