Skip to content

Commit

Permalink
Refactor credential classes (Variable and Secret)
Browse files Browse the repository at this point in the history
to inherit from BaseCredential class
  • Loading branch information
arash77 committed Dec 3, 2024
1 parent f13b77d commit ff3ee10
Showing 1 changed file with 6 additions and 42 deletions.
48 changes: 6 additions & 42 deletions lib/galaxy/tool_util/deps/requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ def resource_requirements_from_list(requirements: Iterable[Dict[str, Any]]) -> L
return rr


class Secret:
class BaseCredential:
def __init__(
self,
name: str,
Expand All @@ -329,6 +329,8 @@ def to_dict(self) -> Dict[str, Any]:
"description": self.description,
}


class Secret(BaseCredential):
@classmethod
def from_element(cls, elem) -> "Secret":
return cls(
Expand All @@ -338,38 +340,8 @@ def from_element(cls, elem) -> "Secret":
description=elem.get("description", ""),
)

@classmethod
def from_dict(cls, dict: Dict[str, Any]) -> "Secret":
name = dict["name"]
inject_as_env = dict["inject_as_env"]
label = dict.get("label", "")
description = dict.get("description", "")
return cls(name=name, inject_as_env=inject_as_env, label=label, description=description)


class Variable:
def __init__(
self,
name: str,
inject_as_env: str,
label: str = "",
description: str = "",
) -> None:
self.name = name
self.inject_as_env = inject_as_env
self.label = label
self.description = description
if not self.inject_as_env:
raise ValueError("Missing inject_as_env")

def to_dict(self) -> Dict[str, Any]:
return {
"name": self.name,
"inject_as_env": self.inject_as_env,
"label": self.label,
"description": self.description,
}

class Variable(BaseCredential):
@classmethod
def from_element(cls, elem) -> "Variable":
return cls(
Expand All @@ -379,14 +351,6 @@ def from_element(cls, elem) -> "Variable":
description=elem.get("description", ""),
)

@classmethod
def from_dict(cls, dict: Dict[str, Any]) -> "Variable":
name = dict["name"]
inject_as_env = dict["inject_as_env"]
label = dict.get("label", "")
description = dict.get("description", "")
return cls(name=name, inject_as_env=inject_as_env, label=label, description=description)


class CredentialsRequirement:
def __init__(
Expand Down Expand Up @@ -432,8 +396,8 @@ def from_dict(cls, dict: Dict[str, Any]) -> "CredentialsRequirement":
multiple = dict.get("multiple", False)
label = dict.get("label", "")
description = dict.get("description", "")
secrets = [Secret.from_dict(s) for s in dict.get("secrets", [])]
variables = [Variable.from_dict(v) for v in dict.get("variables", [])]
secrets = [Secret.from_element(s) for s in dict.get("secrets", [])]
variables = [Variable.from_element(v) for v in dict.get("variables", [])]
return cls(
name=name,
reference=reference,
Expand Down

0 comments on commit ff3ee10

Please sign in to comment.