Skip to content

Commit

Permalink
Fixed date format handling in SQL + fix fetchall items
Browse files Browse the repository at this point in the history
  • Loading branch information
npalacioescat committed Jun 4, 2024
1 parent 2c71d61 commit 4d8d5e8
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
12 changes: 7 additions & 5 deletions cache_manager/_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
'Cache',
]

ATTR_TYPES = ['varchar', 'int', 'date', 'float']
ATTR_TYPES = ['varchar', 'int', 'datetime', 'float']


class Cache:
Expand Down Expand Up @@ -109,8 +109,10 @@ def _create_schema(self):

@staticmethod
def _quotes(string: str, typ: str = 'VARCHAR') -> str:

return f'"{string}"' if typ.startswith('VARCHAR') else string
return f'"{string}"' if (
typ.startswith('VARCHAR') or
typ.startswith('DATETIME')
) else string


@staticmethod
Expand Down Expand Up @@ -190,7 +192,7 @@ def search(

_log(f'Fetching results from attr_{actual_typ}')

for row in self.con.fetchall():
for row in self.cur.fetchall():

key = row['version_id']

Expand Down Expand Up @@ -301,7 +303,7 @@ def create(
{new.status},
{self._quotes(new.filename)},
{self._quotes(new.label)},
{new.date},
{self._quotes(new.date)},
{self._quotes(new.ext)}
)
''')
Expand Down
2 changes: 1 addition & 1 deletion cache_manager/_data/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"status": "INT",
"file_name": "VARCHAR",
"label": "VARCHAR",
"date": "DATE",
"date": "DATETIME",
"ext": "VARCHAR"
}
12 changes: 7 additions & 5 deletions cache_manager/_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __init__(
def new(
cls,
uri,
params,
params: dict | None = None,
version: int = 0,
status: int = 0,
date: str = None,
Expand All @@ -58,13 +58,15 @@ def new(
Creates a new item.
"""

params = params or {}
key = cls.serialize(uri, params)
params['_uri'] = uri
args = {
k: v for k, v in locals().items()
if k not in ['uri', 'params', 'cls']
}

return cls(key, **args)
return cls(**args)

@classmethod
def serialize(cls, uri, attrs: dict | None = None):
Expand All @@ -90,7 +92,7 @@ def _setup(self):
"""
Setting default values
"""

self.filename = self.filename or os.path.basename(self.uri)
self.ext = os.path.splitext(self.filename)[-1]
# TODO: Fix URI/filename
self.filename = self.filename# or os.path.basename(self.params['_uri'])
#self.ext = os.path.splitext(self.filename)[-1]
self.date = self.date or _utils.parse_time()
4 changes: 2 additions & 2 deletions cache_manager/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def hash(value: Any) -> str:
return hashlib.md5(value).hexdigest()


def parse_time(value: str | datetime.datetime) -> str:
def parse_time(value: str | datetime.datetime | None = None) -> str:
"""
Formats a date and time value.
"""
Expand All @@ -62,4 +62,4 @@ def parse_time(value: str | datetime.datetime) -> str:

value = datetime.datetime.now()

return datetime.strftime(value, '%Y-%m-%d %H:%M:%S')
return value.strftime('%Y-%m-%d %H:%M:%S')

0 comments on commit 4d8d5e8

Please sign in to comment.