Skip to content
This repository has been archived by the owner on Aug 25, 2023. It is now read-only.

feat: use centralized constants #227

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/atc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

from .version import __version__ # noqa: F401

DEBUG = False
__DEBUG__ = False


def dbg(*args, **kwargs):
if DEBUG:
if __DEBUG__:
print(*args, **kwargs)
25 changes: 13 additions & 12 deletions src/atc/configurator/configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import yaml
from deprecated import deprecated

from atc.const import ALIAS, DEBUG, NAME, PATH, RELEASE
from atc.exceptions import NoSuchValueException

# recursive type definition of the details object
Expand Down Expand Up @@ -66,8 +67,8 @@ def clear_all_configurations(self):
############################################

def _set_extras(self):
self.register("ID", {"release": "", "debug": f"__{self._unique_id}"})
self.register("MNT", {"release": "mnt", "debug": "tmp"})
self.register("ID", {RELEASE: "", DEBUG: f"__{self._unique_id}"})
self.register("MNT", {RELEASE: "mnt", DEBUG: "tmp"})

@deprecated(
reason="use .get('ENV') to get literal values.",
Expand Down Expand Up @@ -119,16 +120,16 @@ def _get_item(self, table_id: str) -> TcValue:
return value
else:
# value is a dict
if set(value.keys()) == {"release", "debug"}:
if set(value.keys()) == {RELEASE, DEBUG}:
# Situation like MyForked
if self._is_debug:
value = value["debug"]
value = value[DEBUG]
else:
value = value["release"]
value = value[RELEASE]
continue
elif set(value.keys()) == {"alias"}: # allow alias of alias
elif set(value.keys()) == {ALIAS}: # allow alias of alias
# Situation like MyAlias
new_id = value["alias"]
new_id = value[ALIAS]
if new_id in stack:
raise ValueError(f"Alias loop at key {new_id}")
stack.add(new_id)
Expand Down Expand Up @@ -178,7 +179,7 @@ def _get_item_property(
if property:
composite_key += f"_{property}"
_forbidden_keys.add(composite_key)
if property == "name":
if property == NAME:
_forbidden_keys.add(table_id)
if any(key in _forbidden_keys for key in format_keys):
raise ValueError(
Expand Down Expand Up @@ -214,7 +215,7 @@ def _get_item_property(

# otherwise bare key references are to 'name',
# which _must_ exist in this case
replacements[key] = self._get_item_property(key, "name", _forbidden_keys)
replacements[key] = self._get_item_property(key, NAME, _forbidden_keys)

# we have run through the key names of all replacement keys in the string.
# Any that we could not find were skipped silently above, but that means that
Expand Down Expand Up @@ -337,7 +338,7 @@ def table_name(self, table_id: str):
:param table_id: Table id in the .json or .yaml files.
:return: str: table name
"""
return self.table_property(table_id, "name")
return self.table_property(table_id, NAME)

@deprecated(
reason='Use .get(table_id,"path") instead.',
Expand All @@ -348,7 +349,7 @@ def table_path(self, table_id: str):
:param table_id: Table id in the .json or .yaml files.
:return: str: table path
"""
return self.get(table_id, "path")
return self.get(table_id, PATH)

def get(self, table_id: str, property: str = "") -> str:
return self._get_item_property(table_id, property)
Expand Down Expand Up @@ -378,7 +379,7 @@ def get_all_details(self):
pass

try:
self.table_details[table_id] = self.get(table_id, "name")
self.table_details[table_id] = self.get(table_id, NAME)
except NoSuchValueException:
pass

Expand Down
8 changes: 8 additions & 0 deletions src/atc/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
NAME = "name"
PATH = "path"
PARTITIONING = "partitioning"
FORMAT = "format"
SCHEMA = "schema"
RELEASE = "release"
DEBUG = "debug"
ALIAS = "alias"
5 changes: 3 additions & 2 deletions src/atc/delta/db_handle.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from atc.configurator.configurator import Configurator
from atc.const import FORMAT, PATH
from atc.exceptions import AtcException
from atc.spark import Spark

Expand Down Expand Up @@ -28,8 +29,8 @@ def from_tc(cls, id: str):
tc = Configurator()
return cls(
name=tc.table_name(id),
location=tc.table_property(id, "path", ""),
data_format=tc.table_property(id, "format", "db"),
location=tc.table_property(id, PATH, ""),
data_format=tc.table_property(id, FORMAT, "db"),
)

def _validate(self):
Expand Down
7 changes: 4 additions & 3 deletions src/atc/delta/delta_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pyspark.sql import DataFrame

from atc.configurator.configurator import Configurator
from atc.const import FORMAT, NAME, PATH
from atc.exceptions import AtcException
from atc.functions import get_unique_tempview_name, init_dbutils
from atc.spark import Spark
Expand Down Expand Up @@ -37,9 +38,9 @@ def __init__(self, name: str, location: str = None, data_format: str = "delta"):
def from_tc(cls, id: str) -> "DeltaHandle":
tc = Configurator()
return cls(
name=tc.table_property(id, "name", ""),
location=tc.table_property(id, "path", ""),
data_format=tc.table_property(id, "format", "delta"),
name=tc.table_property(id, NAME, ""),
location=tc.table_property(id, PATH, ""),
data_format=tc.table_property(id, FORMAT, "delta"),
)

def _validate(self):
Expand Down
9 changes: 5 additions & 4 deletions src/atc/eh/EventHubCapture.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from atc import dbg
from atc.configurator.configurator import Configurator
from atc.const import FORMAT, NAME, PARTITIONING, PATH
from atc.eh.eh_exceptions import AtcEhInitException, AtcEhLogicException
from atc.eh.PartitionSpec import PartitionSpec
from atc.functions import init_dbutils
Expand Down Expand Up @@ -38,10 +39,10 @@ class EventHubCapture:
def from_tc(cls, id: str):
tc = Configurator()
return cls(
name=tc.table_property(id, "name"),
path=tc.table_property(id, "path"),
format=tc.table_property(id, "format"),
partitioning=tc.table_property(id, "partitioning"),
name=tc.table_property(id, NAME),
path=tc.table_property(id, PATH),
format=tc.table_property(id, FORMAT),
partitioning=tc.table_property(id, PARTITIONING),
)

def __init__(
Expand Down
7 changes: 4 additions & 3 deletions src/atc/eh/EventHubCaptureExtractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pyspark.sql import functions as f

from atc.configurator.configurator import Configurator
from atc.const import FORMAT, PARTITIONING, PATH
from atc.spark import Spark

utc = datetime.timezone.utc
Expand All @@ -27,10 +28,10 @@ class EventHubCaptureExtractor:
@classmethod
def from_tc(cls, tbl_id: str):
tc = Configurator()
assert tc.table_property(tbl_id, "format") == "avro"
assert tc.table_property(tbl_id, FORMAT) == "avro"
return cls(
path=tc.table_property(tbl_id, "path"),
partitioning=tc.table_property(tbl_id, "partitioning"),
path=tc.table_property(tbl_id, PATH),
partitioning=tc.table_property(tbl_id, PARTITIONING),
)

def __init__(self, path: str, partitioning: str):
Expand Down
3 changes: 2 additions & 1 deletion src/atc/schema_manager/schema_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pyspark.sql.types as T

from atc.configurator import Configurator
from atc.const import SCHEMA
from atc.exceptions import (
FalseSchemaDefinitionException,
NoSuchSchemaException,
Expand Down Expand Up @@ -40,7 +41,7 @@ def get_schema(self, schema_identifier: str) -> T.StructType:

# Otherwise, check if the schema identifier is a table identifier
try:
schema = Configurator().get(table_id=schema_identifier, property="schema")
schema = Configurator().get(table_id=schema_identifier, property=SCHEMA)
except NoSuchValueException:
raise NoSuchSchemaException(schema_identifier)

Expand Down