Skip to content

Commit

Permalink
[hop] Add sql_adapter module if missing.
Browse files Browse the repository at this point in the history
  • Loading branch information
joel-m committed Nov 22, 2024
1 parent 188e06b commit e643ba8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
11 changes: 8 additions & 3 deletions half_orm/packager/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def read_template(file_name):
WARNING_TEMPLATE = read_template('warning')
BASE_TEST = read_template('base_test')
TEST = read_template('relation_test')
SQL_ADAPTER_TEMPLATE = read_template('sql_adapter')
SKIP = re.compile('[A-Z]')

MODULE_FORMAT = (
Expand Down Expand Up @@ -301,9 +302,12 @@ def generate(repo):
package_dir = os.path.join(repo.base_dir, package_name)
files_list = []
try:
sql_adapter_module = importlib.import_module('sql_adapter', package_dir)
sql_adapter_module = importlib.import_module('.sql_adapter', package_name)
SQL_ADAPTER.update(sql_adapter_module.SQL_ADAPTER)
except ModuleNotFoundError as exc:
os.makedirs(package_dir)
with open(os.path.join(package_dir, 'sql_adapter.py'), "w") as file_:
file_.write(SQL_ADAPTER_TEMPLATE)
sys.stderr.write(f"{exc}\n")
except AttributeError as exc:
sys.stderr.write(f"{exc}\n")
Expand Down Expand Up @@ -335,6 +339,7 @@ def generate(repo):

if len(NO_APAPTER):
print("MISSING ADAPTER FOR SQL TYPE")
for key, value in NO_APAPTER.items():
print(f"- {key}: {value}")
print(f"Add the following items to __SQL_ADAPTER in {os.path.join(package_dir, 'sql_adapter.py')}")
for key in NO_APAPTER.keys():
print(f" '{key}': typing.Any,")
__update_init_files(package_dir, files_list, warning)
9 changes: 9 additions & 0 deletions half_orm/packager/templates/sql_adapter
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from half_orm.sql_adapter import SQL_ADAPTER
import typing

__SQL_ADAPTER = {

}

__SQL_ADAPTER.update({f'_{key}': value for key, value in __SQL_ADAPTER.items()})
SQL_ADAPTER = __SQL_ADAPTER
6 changes: 5 additions & 1 deletion half_orm/sql_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import datetime
import uuid

SQL_ADAPTER = {
__SQL_ADAPTER = {
'string': str,
'bigint': int,
'int8': int,
Expand All @@ -12,6 +12,7 @@
'bool': bool,
'character': str,
'char': str,
'bpchar': str,
'character varying': str,
'varchar': str,
'cidr': typing.Any,
Expand Down Expand Up @@ -55,3 +56,6 @@
'uuid': uuid.UUID,
'xml': typing.Any,
}

__SQL_ADAPTER.update({f'_{key}': value for key, value in __SQL_ADAPTER.items()})
SQL_ADAPTER = __SQL_ADAPTER

0 comments on commit e643ba8

Please sign in to comment.