Skip to content

Commit

Permalink
removed FileInternal
Browse files Browse the repository at this point in the history
  • Loading branch information
nevoodoo committed Jan 24, 2024
1 parent c6f7da2 commit 1ce8f9c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 55 deletions.
3 changes: 3 additions & 0 deletions models/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
FamilySimpleInternal,
PedRowInternal,
)
from models.models.file import (
File
)
from models.models.participant import (
NestedParticipant,
NestedParticipantInternal,
Expand Down
75 changes: 30 additions & 45 deletions models/models/file.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,11 @@
import hashlib
from typing import Optional

from cloudpathlib.anypath import AnyPath
from pydantic import BaseModel

from models.base import SMBase


class File(BaseModel):
"""File model for external use"""

id: int
analysis_id: int
path: str
basename: str
dirname: str
nameroot: str
nameext: Optional[str]
checksum: Optional[str]
size: int

def to_internal(self):
"""Convert to internal model"""
return FileInternal(
id=self.id,
analysis_id=self.analysis_id,
path=self.path,
basename=self.basename,
dirname=self.dirname,
nameroot=self.nameroot,
nameext=self.nameext,
checksum=self.checksum,
size=self.size,
)


class FileInternal(SMBase):
class File(SMBase):
"""File model for internal use"""

id: int
Expand All @@ -47,6 +17,35 @@ class FileInternal(SMBase):
nameext: str | None
checksum: str | None
size: int
secondary_files: list[str] = []

@staticmethod
def from_db(**kwargs):
"""
Convert from db keys, mainly converting id to id_
"""
analysis_id = kwargs.get('analysis_id')
path = kwargs.get('path')
basename = kwargs.get('basename')
dirname = kwargs.get('dirname')
nameroot = kwargs.get('nameroot')
nameext = kwargs.get('nameext')
checksum = kwargs.get('checksum')
size = kwargs.get('size')
secondary_files = kwargs.get('secondary_files')

return File(
id=kwargs.pop('id'),
analysis_id=analysis_id,
path=path,
basename=basename,
dirname=dirname,
nameroot=nameroot,
nameext=nameext,
checksum=checksum,
size=size,
secondary_files=secondary_files,
)

@staticmethod
def get_basename(path: str) -> str:
Expand Down Expand Up @@ -83,17 +82,3 @@ def get_size(path: str) -> int:
return AnyPath(path).stat().st_size # pylint: disable=E1101
except FileNotFoundError:
return 0

def to_external(self):
"""Convert to external model"""
return File(
id=self.id,
analysis_id=self.analysis_id,
path=self.path,
basename=self.basename,
dirname=self.dirname,
nameroot=self.nameroot,
nameext=self.nameext,
checksum=self.checksum,
size=self.size,
)
21 changes: 11 additions & 10 deletions scripts/20240124_migrate_output_to_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
import click
from databases import Database

from models.models.file import FileInternal
from models.models.file import File


def _get_connection_string():
from db.python.connect import CredentialedDatabaseConfiguration # pylint: disable=C0415
from db.python.connect import \
CredentialedDatabaseConfiguration # pylint: disable=C0415

config = CredentialedDatabaseConfiguration.dev_config()

Expand Down Expand Up @@ -44,18 +45,18 @@ async def execute_many(connection, query, inserts):
await connection.execute_many(query, inserts)


def get_file_dict(path: str, analysis_id: int) -> Dict:
def get_file_info(path: str, analysis_id: int) -> Dict:
"""Get file dict"""
print('Extracting file dict')
return {
'analysis_id': analysis_id,
'path': path,
'basename': FileInternal.get_basename(path),
'dirname': FileInternal.get_dirname(path),
'nameroot': FileInternal.get_nameroot(path),
'nameext': FileInternal.get_extension(path),
'checksum': FileInternal.get_checksum(path),
'size': FileInternal.get_size(path),
'basename': File.get_basename(path),
'dirname': File.get_dirname(path),
'nameroot': File.get_nameroot(path),
'nameext': File.get_extension(path),
'checksum': File.get_checksum(path),
'size': File.get_size(path),
'secondary_files': '[]',
}

Expand Down Expand Up @@ -92,7 +93,7 @@ async def prepare_files(analyses):
for _, path in path_dict.items():
print(path)
files.append(
get_file_dict(path=path, analysis_id=analysis['id'])
get_file_info(path=path, analysis_id=analysis['id'])
)
print('Extracted and added.')
return files
Expand Down

0 comments on commit 1ce8f9c

Please sign in to comment.