Skip to content

Commit

Permalink
Fix ruff warnings, reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
littleK0i committed Nov 23, 2023
1 parent b0633c9 commit f036b3b
Show file tree
Hide file tree
Showing 14 changed files with 153 additions and 103 deletions.
2 changes: 1 addition & 1 deletion snowddl/app/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from snowflake.connector import connect
from traceback import TracebackException

from snowddl.blueprint import ObjectType
from snowddl.blueprint import Ident, ObjectType
from snowddl.config import SnowDDLConfig
from snowddl.engine import SnowDDLEngine
from snowddl.parser import default_parser_sequence, PlaceholderParser
Expand Down
20 changes: 16 additions & 4 deletions snowddl/parser/external_access_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,31 @@

class ExternalAccessIntegrationParser(AbstractParser):
def load_blueprints(self):
self.parse_single_file(self.base_path / "external_access_integration.yaml", external_access_integration_json_schema, self.process_external_access_integration)
self.parse_single_file(
self.base_path / "external_access_integration.yaml",
external_access_integration_json_schema,
self.process_external_access_integration,
)

def process_external_access_integration(self, file: ParsedFile):
for integration_name, integration in file.params.items():
allowed_network_rules = [SchemaObjectIdent(self.env_prefix, *network_rule.split(".")) for network_rule in integration.get("allowed_network_rules")]
allowed_network_rules = [
SchemaObjectIdent(self.env_prefix, *network_rule.split("."))
for network_rule in integration.get("allowed_network_rules")
]
allowed_api_authentication_integrations = None
allowed_authentication_secrets = None

if integration.get("allowed_api_authentication_integrations"):
allowed_api_authentication_integrations = [Ident(api_integration) for api_integration in integration.get("allowed_api_authentication_integrations")]
allowed_api_authentication_integrations = [
Ident(api_integration) for api_integration in integration.get("allowed_api_authentication_integrations")
]

if integration.get("allowed_authentication_secrets"):
allowed_authentication_secrets = [SchemaObjectIdent(self.env_prefix, *secret_name.split(".")) for secret_name in integration.get("allowed_authentication_secrets")]
allowed_authentication_secrets = [
SchemaObjectIdent(self.env_prefix, *secret_name.split("."))
for secret_name in integration.get("allowed_authentication_secrets")
]

bp = ExternalAccessIntegrationBlueprint(
full_name=AccountObjectIdent(self.env_prefix, integration_name),
Expand Down
4 changes: 3 additions & 1 deletion snowddl/parser/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ def get_external_access_integrations(self, f: ParsedFile):

def get_secrets(self, f: ParsedFile):
if f.params.get("secrets"):
return {k: build_schema_object_ident(self.env_prefix, v, f.database, f.schema) for k, v in f.params.get("secrets").items()}
return {
k: build_schema_object_ident(self.env_prefix, v, f.database, f.schema) for k, v in f.params.get("secrets").items()
}

return None
2 changes: 1 addition & 1 deletion snowddl/parser/network_rule.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from snowddl.blueprint import NetworkRuleBlueprint, SchemaObjectIdent, Ident
from snowddl.blueprint import NetworkRuleBlueprint, SchemaObjectIdent
from snowddl.parser.abc_parser import AbstractParser, ParsedFile


Expand Down
2 changes: 1 addition & 1 deletion snowddl/resolver/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def compare_dynamic_param_value(bp_value: Union[bool, int, float, str], existing


def dtypes_from_arguments(arguments: str) -> str:
arguments = arguments.translate(str.maketrans('', '', '[] '))
arguments = arguments.translate(str.maketrans("", "", "[] "))

start_dtypes_idx = arguments.index("(")
finish_dtypes_idx = arguments.index(")")
Expand Down
20 changes: 10 additions & 10 deletions snowddl/resolver/external_access_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def get_existing_objects(self):
"SHOW EXTERNAL ACCESS INTEGRATIONS LIKE {env_prefix:ls}",
{
"env_prefix": self.config.env_prefix,
}
},
)

for r in cur:
Expand All @@ -36,7 +36,7 @@ def get_owner_from_grant(self, name):
"SHOW GRANTS ON INTEGRATION {name:i}",
{
"name": name,
}
},
)

for r in cur:
Expand All @@ -55,7 +55,7 @@ def create_object(self, bp: ExternalAccessIntegrationBlueprint):
"CREATE EXTERNAL ACCESS INTEGRATION {name:i}",
{
"name": bp.full_name,
}
},
)

create_query.append_nl(common_query)
Expand All @@ -81,7 +81,7 @@ def compare_object(self, bp: ExternalAccessIntegrationBlueprint, row: dict):
"CREATE OR REPLACE EXTERNAL ACCESS INTEGRATION {name:i}",
{
"name": bp.full_name,
}
},
)

replace_query.append_nl(common_query)
Expand All @@ -92,7 +92,7 @@ def compare_object(self, bp: ExternalAccessIntegrationBlueprint, row: dict):
{
"name": bp.full_name,
"comment": common_query.add_short_hash(bp.comment),
}
},
)

return ResolveResult.ALTER
Expand All @@ -116,38 +116,38 @@ def _build_common_external_access_integration_sql(self, bp: ExternalAccessIntegr
"ALLOWED_NETWORK_RULES = ({allowed_network_rules:i})",
{
"allowed_network_rules": bp.allowed_network_rules,
}
},
)

if bp.allowed_api_authentication_integrations:
query.append_nl(
"ALLOWED_API_AUTHENTICATION_INTEGRATIONS = ({allowed_api_authentication_integrations:i})",
{
"allowed_api_authentication_integrations": bp.allowed_api_authentication_integrations,
}
},
)

if bp.allowed_authentication_secrets:
query.append_nl(
"ALLOWED_AUTHENTICATION_SECRETS = ({allowed_authentication_secrets:i})",
{
"allowed_authentication_secrets": bp.allowed_authentication_secrets,
}
},
)

query.append_nl(
"ENABLED = {enabled:b}",
{
"enabled": bp.enabled,
}
},
)

if bp.comment:
query.append_nl(
"COMMENT = {comment}",
{
"comment": bp.comment,
}
},
)

return query
22 changes: 14 additions & 8 deletions snowddl/resolver/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,19 +189,25 @@ def _build_create_function(self, bp: FunctionBlueprint):
if bp.external_access_integrations:
# Snowflake bug: EXTERNAL_ACCESS_INTEGRATIONS must be identifiers
# It does not accept identifiers in double-quotes
query.append_nl("EXTERNAL_ACCESS_INTEGRATIONS = ({external_access_integrations:r})", {
"external_access_integrations": bp.external_access_integrations,
})
query.append_nl(
"EXTERNAL_ACCESS_INTEGRATIONS = ({external_access_integrations:r})",
{
"external_access_integrations": bp.external_access_integrations,
},
)

if bp.secrets:
query.append_nl("SECRETS = (")

for idx, (var_name, secret_name) in enumerate(bp.secrets.items()):
query.append("{comma:r}{var_name} = {secret_name:i}", {
"comma": "" if idx == 0 else ", ",
"var_name": var_name,
"secret_name": secret_name,
})
query.append(
"{comma:r}{var_name} = {secret_name:i}",
{
"comma": "" if idx == 0 else ", ",
"var_name": var_name,
"secret_name": secret_name,
},
)

query.append(")")

Expand Down
2 changes: 1 addition & 1 deletion snowddl/resolver/network_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def get_owner_from_grant(self, name):
"SHOW GRANTS ON NETWORK POLICY {name:i}",
{
"name": name,
}
},
)

for r in cur:
Expand Down
23 changes: 14 additions & 9 deletions snowddl/resolver/network_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def get_existing_objects_in_schema(self, schema: dict):
{
"database": schema["database"],
"schema": schema["schema"],
}
},
)

for r in cur:
Expand All @@ -43,7 +43,7 @@ def create_object(self, bp: NetworkRuleBlueprint):
"CREATE NETWORK RULE {full_name:i}",
{
"full_name": bp.full_name,
}
},
)

create_query.append(common_query)
Expand All @@ -62,7 +62,12 @@ def compare_object(self, bp: NetworkRuleBlueprint, row: dict):
desc = cur.fetchone()
is_replace_required = False

if bp.type != row["type"] or bp.mode != row["mode"] or bp.value_list != desc["value_list"].split(",") or bp.comment != row["comment"]:
if (
bp.type != row["type"]
or bp.mode != row["mode"]
or bp.value_list != desc["value_list"].split(",")
or bp.comment != row["comment"]
):
is_replace_required = True

if not is_replace_required:
Expand All @@ -75,7 +80,7 @@ def compare_object(self, bp: NetworkRuleBlueprint, row: dict):
"CREATE OR REPLACE NETWORK RULE {full_name:i}",
{
"full_name": bp.full_name,
}
},
)

replace_query.append(common_query)
Expand All @@ -90,7 +95,7 @@ def drop_object(self, row: dict):
"database": row["database"],
"schema": row["schema"],
"name": row["name"],
}
},
)

return ResolveResult.DROP
Expand All @@ -102,29 +107,29 @@ def _build_common_network_rule_sql(self, bp: NetworkRuleBlueprint):
"TYPE = {type}",
{
"type": bp.type,
}
},
)

query.append_nl(
"VALUE_LIST = ({value_list})",
{
"value_list": bp.value_list,
}
},
)

query.append_nl(
"MODE = {mode}",
{
"mode": bp.mode,
}
},
)

if bp.comment:
query.append_nl(
"COMMENT = {comment}",
{
"comment": bp.comment,
}
},
)

return query
22 changes: 14 additions & 8 deletions snowddl/resolver/procedure.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,19 +170,25 @@ def _build_create_procedure(self, bp: ProcedureBlueprint):
if bp.external_access_integrations:
# Snowflake bug: EXTERNAL_ACCESS_INTEGRATIONS must be identifiers
# It does not accept identifiers in double-quotes
query.append_nl("EXTERNAL_ACCESS_INTEGRATIONS = ({external_access_integrations:r})", {
"external_access_integrations": bp.external_access_integrations,
})
query.append_nl(
"EXTERNAL_ACCESS_INTEGRATIONS = ({external_access_integrations:r})",
{
"external_access_integrations": bp.external_access_integrations,
},
)

if bp.secrets:
query.append_nl("SECRETS = (")

for idx, (var_name, secret_name) in enumerate(bp.secrets.items()):
query.append("{comma:r}{var_name} = {secret_name:i}", {
"comma": "" if idx == 0 else ", ",
"var_name": var_name,
"secret_name": secret_name,
})
query.append(
"{comma:r}{var_name} = {secret_name:i}",
{
"comma": "" if idx == 0 else ", ",
"var_name": var_name,
"secret_name": secret_name,
},
)

query.append(")")

Expand Down
Loading

0 comments on commit f036b3b

Please sign in to comment.