Skip to content

Commit

Permalink
# Conflicts:
Browse files Browse the repository at this point in the history
#	annotation/models/models.py
#	annotation/vep_annotation.py
#	snpdb/management/commands/one_off_fix_symbolic_variants.py
  • Loading branch information
TheMadBug committed Jul 25, 2024
1 parent 69d99c7 commit f2b77c5
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 18 deletions.
6 changes: 3 additions & 3 deletions library/common_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
_end = '_end_'


def make_trie(*words):
def make_trie(*words) -> dict:
root = {}
for word in words:
current_dict = root
Expand All @@ -14,7 +14,7 @@ def make_trie(*words):
return root


def get_multi_entries(trie, prefix=''):
def get_multi_entries(trie, prefix='') -> Counter:
counter = Counter()
trie_keys = list(trie.keys())

Expand All @@ -32,7 +32,7 @@ def get_multi_entries(trie, prefix=''):
return counter


def get_common_prefix_dirs(files):
def get_common_prefix_dirs(files) -> list[str]:
trie = make_trie(*tuple(files))
most_common = get_multi_entries(trie).most_common(3)
return [k for k, _ in most_common]
6 changes: 3 additions & 3 deletions library/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ class Git:
def __init__(self, directory=None):
self.directory = directory

def git_cmd(self, params):
def git_cmd(self, params) -> str:
output = subprocess.check_output(["git"] + params, cwd=self.directory)
return output.decode().strip()

@cached_property
def hash(self):
def hash(self) -> str:
return self.git_cmd(["rev-parse", "HEAD"])

@cached_property
Expand All @@ -25,7 +25,7 @@ def last_modified_date(self):
return parse(date_string)

@cached_property
def branch(self):
def branch(self) -> str:
return self.git_cmd(["rev-parse", "--abbrev-ref", "HEAD"])

@cached_property
Expand Down
10 changes: 5 additions & 5 deletions library/guardian_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ def public_group():


@lru_cache()
def _cached_admin_bot():
def _cached_admin_bot() -> User:
return User.objects.get(username='admin_bot')


def admin_bot():
def admin_bot() -> User:
"""
lru_cache is great for performance, but it can break unit tests by caching a User object across sessions (when the DB has been rolled back).
Specifically this object is often referenced by things that want to save the user to the DB.
Expand All @@ -41,7 +41,7 @@ def admin_bot():
return _cached_admin_bot()


def bot_group():
def bot_group() -> Group:
g, _ = Group.objects.get_or_create(name='variantgrid/bot')
return g

Expand Down Expand Up @@ -140,12 +140,12 @@ def clear_permissions(obj, permissions):
remove_perm(permission, group, obj)


def check_can_write(obj, user):
def check_can_write(obj, user: User):
if not obj.can_write(user):
raise PermissionDenied(f"You do not have WRITE permission for {obj.pk}")


def check_can_delete(user, pk, owner):
def check_can_delete(user: User, pk, owner):
can_delete = user and (user.is_superuser or user == owner)
if not can_delete:
raise PermissionDenied(f"You are not allowed to delete {pk}")
2 changes: 1 addition & 1 deletion library/keycloak.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(self,

class Keycloak:

def __init__(self, connector: ServerAuth = None):
def __init__(self, connector: Optional[ServerAuth] = None):
if not connector:
connector = ServerAuth.keycloak_connector()
self.connector = connector
Expand Down
2 changes: 1 addition & 1 deletion library/utils/django_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def render_ajax_view(
return render(request, "snpdb/embedded_ajax.html", {"embedded_content": text, "menubar": menubar})


ModelT = TypeVar("T", bound=Model)
ModelT = TypeVar("ModelT", bound=Model)


def refresh_for_update(obj: ModelT) -> ModelT:
Expand Down
4 changes: 2 additions & 2 deletions library/utils/export_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,14 @@ def passes_filter(export_method) -> bool:
if isinstance(value, (set, tuple, list)):
value_set = set(value)
else:
value_set = set([value])
value_set = {value}

if decorated_value := decorated_values.get(key):
decorated_set: set
if isinstance(decorated_value, (set, tuple, list)):
decorated_set = set(decorated_value)
else:
decorated_set = set([decorated_value])
decorated_set = {decorated_value}
return bool(value_set.intersection(decorated_set))

# handle decorated value being a collection (and matching a single value in that collection)
Expand Down
2 changes: 1 addition & 1 deletion library/utils/hash_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
def string_deterministic_hash(s: str) -> int:
"""
When you want the same string to always hash to the same value (where security
isnt a concern). Hashlib seemed overkill for this purpose.
isn't a concern). Hashlib seemed overkill for this purpose.
"""
val = 0
for c in s:
Expand Down
2 changes: 2 additions & 0 deletions library/utils/json_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,11 @@ def short(self):
def __str__(self):
return f"[{json.dumps(self.key)}]"


@dataclass(frozen=True)
class JsonPathId(JsonPathPart):
key: str

# not a real path, indicates that we're treating a list as
# a dictionary with "id" as the key
@property
Expand Down
22 changes: 20 additions & 2 deletions library/utils/os_utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import logging
import subprocess
from dataclasses import dataclass
from typing import Optional

from library.utils import FormerTuple

def execute_cmd(cmd: list, **kwargs) -> tuple[int, Optional[str], Optional[str]]:

@dataclass(frozen=True)
class CmdOutput(FormerTuple):
return_code: int
std_out: Optional[str]
std_err: Optional[str]

@property
def as_tuple(self) -> tuple:
return self.return_code, self.std_out, self.std_err


def execute_cmd(cmd: list, **kwargs) -> CmdOutput:
if kwargs.pop("shell", False):
command = ' '.join(cmd)
logging.info('About to call %s', command)
Expand All @@ -13,4 +27,8 @@ def execute_cmd(cmd: list, **kwargs) -> tuple[int, Optional[str], Optional[str]]
pipes = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)

std_out, std_err = pipes.communicate()
return pipes.returncode, std_out.decode() if std_out else None, std_err.decode() if std_err else None
return CmdOutput(
return_code=pipes.returncode,
std_out=std_out.decode() if std_out else None,
std_err=std_err.decode() if std_err else None
)

0 comments on commit f2b77c5

Please sign in to comment.