Skip to content

Commit

Permalink
refactor database and file management for improved readability and er…
Browse files Browse the repository at this point in the history
…ror handling
  • Loading branch information
Alex Al-Saffar committed Sep 14, 2024
1 parent 9aa7d20 commit 70ea063
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
3 changes: 2 additions & 1 deletion myresources/crocodile/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def close(self, sleep: int = 2):
def _get_table_identifier(self, table: str, sch: Optional[str]):
if sch is None: sch = self.sch
if sch is not None:
return f"""{sch}."{table}" """
return f""" "{sch}"."{table}" """
else: return table

@staticmethod
Expand Down Expand Up @@ -149,6 +149,7 @@ def read_table(self, table: Optional[str] = None, sch: Optional[str] = None, siz
schemas = [a_sch for a_sch in self.schema if a_sch not in ["information_schema", "pg_catalog"]]
if len(schemas) > 1 and "public" in schemas:
schemas.remove("public")
sch = schemas[0]
if table is None:
tables = self.sch_tab[sch]
assert len(tables) > 0, f"No tables found in schema `{sch}`"
Expand Down
7 changes: 3 additions & 4 deletions myresources/crocodile/file_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ def read(path: PLike, **kwargs: Any) -> Any:
@staticmethod
def json(path: PLike, r: bool = False, **kwargs: Any) -> Any: # return could be list or dict etc
import json
try: mydict = json.loads(P(path).read_text(), **kwargs)
try:
mydict = json.loads(P(path).read_text(), **kwargs)
except Exception:
import pyjson5
mydict = pyjson5.loads(P(path).read_text(), **kwargs) # file has C-style comments.
Expand All @@ -136,7 +137,7 @@ def yaml(path: PLike, r: bool = False) -> Any: # return could be list or dict e
return mydict
@staticmethod
def ini(path: PLike, encoding: Optional[str] = None):
if not Path(path).exists() or Path(path).is_dir(): raise FileNotFoundError(f"File not found: {path}")
if not Path(path).exists() or Path(path).is_dir(): raise FileNotFoundError(f"File not found or is a directory: {path}")
import configparser
res = configparser.ConfigParser()
res.read(filenames=[str(path)], encoding=encoding)
Expand All @@ -145,7 +146,6 @@ def ini(path: PLike, encoding: Optional[str] = None):
def toml(path: PLike):
import tomli
return tomli.loads(P(path).read_text())

@staticmethod
def npy(path: PLike, **kwargs: Any):
import numpy as np
Expand Down Expand Up @@ -764,7 +764,6 @@ def share_on_cloud(self, service: Literal['gofile', 'pixeldrain'] = "gofile", ti

else:
raise ValueError("Unsupported service specified.")

def share_on_network(self, username: Optional[str]= None, password: Optional[str] = None):
from crocodile.meta import Terminal
Terminal(stdout=None).run(f"sharing {self} {('--username ' + str(username)) if username else ''} {('--password ' + password) if password else ''}", shell="powershell")
Expand Down

0 comments on commit 70ea063

Please sign in to comment.