Skip to content

Commit

Permalink
make include path a python module
Browse files Browse the repository at this point in the history
  • Loading branch information
drewbanin committed Mar 25, 2017
1 parent c0e5d36 commit 858d423
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 42 deletions.
1 change: 0 additions & 1 deletion MANIFEST.in

This file was deleted.

2 changes: 1 addition & 1 deletion dbt/compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def recursively_parse_macros_for_node(node, flat_graph, context):
.update(macro_map)

if package_name in (node.get('package_name'),
dbt.loader.GLOBAL_PROJECT_NAME):
dbt.include.GLOBAL_PROJECT_NAME):
context.update(macro_map)

return context
Expand Down
5 changes: 5 additions & 0 deletions dbt/include/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

import os

GLOBAL_DBT_MODULES_PATH = os.path.dirname(__file__)
GLOBAL_PROJECT_NAME = 'dbt'
File renamed without changes.
31 changes: 24 additions & 7 deletions dbt/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import dbt.contracts.project

from dbt.utils import NodeType
from dbt.compat import basestring
from dbt.logger import GLOBAL_LOGGER as logger


Expand Down Expand Up @@ -282,7 +283,7 @@ def parse_schema_tests(tests, root_project, projects):
if configs is None:
continue

if type(configs) not in (list, tuple):
if not isinstance(configs, (list, tuple)):

dbt.utils.compiler_warning(
model_name,
Expand Down Expand Up @@ -310,9 +311,9 @@ def get_nice_schema_test_name(test_type, test_name, args):
for arg_name in sorted(args):
arg_val = args[arg_name]

if type(arg_val) == dict:
if isinstance(arg_val, dict):
parts = arg_val.values()
elif type(arg_val) in (list, tuple):
elif isinstance(arg_val, (list, tuple)):
parts = arg_val
else:
parts = [arg_val]
Expand All @@ -323,6 +324,17 @@ def get_nice_schema_test_name(test_type, test_name, args):
return '{}_{}_{}'.format(test_type, test_name, unique)


def parse_schema_test_arg(test_base, test_config):
if isinstance(test_config, (basestring, int, float, bool)):
return {"arg": test_config}

elif isinstance(test_config, dict):
return {basestring(k): v for (k, v) in test_config.items()}

else:
return None


def parse_schema_test(test_base, model_name, test_config, test_type,
root_project_config, package_project_config,
all_projects):
Expand All @@ -331,10 +343,15 @@ def parse_schema_test(test_base, model_name, test_config, test_type,
'model': model_name
}

if hasattr(test_config, 'items'):
arg_dict.update(test_config)
else:
arg_dict['arg'] = test_config
macro_args = parse_schema_test_arg(test_base, test_config)
if macro_args is None:
dbt.utils.compiler_warning(
test_base.get('path'),
"Invalid option supplied to schema test: {}".format(test_config)
)
return None

arg_dict.update(macro_args)

args = ", ".join([
"{}={}".format(key, arg_dict[key].__repr__())
Expand Down
66 changes: 33 additions & 33 deletions dbt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json

import dbt.project
import dbt.loader
from dbt.include import GLOBAL_DBT_MODULES_PATH

from dbt.compat import basestring
from dbt.logger import GLOBAL_LOGGER as logger
Expand Down Expand Up @@ -43,32 +43,29 @@ def __repr__(self):
return self.schema_table(self.schema, self.table)


def compiler_error(model, msg):
def get_model_name_or_none(model):
if model is None:
name = '<None>'
elif isinstance(model, str):

elif isinstance(model, basestring):
name = model
elif isinstance(model, dict):
name = model.get('name')
else:
name = model.nice_name
return name


def compiler_error(model, msg):
name = get_model_name_or_none(model)
raise RuntimeError(
"! Compilation error while compiling model {}:\n! {}\n"
.format(name, msg)
)


def compiler_warning(model, msg):
if model is None:
name = '<None>'
elif isinstance(model, str):
name = model
elif isinstance(model, dict):
name = model.get('name')
else:
name = model.nice_name

name = get_model_name_or_none(model)
logger.info(
"* Compilation warning while compiling model {}:\n* {}\n"
.format(name, msg)
Expand Down Expand Up @@ -162,31 +159,34 @@ def find_model_by_fqn(models, fqn):
)


def dependency_projects(project, include_global=True):
if include_global:
module_paths = [
dbt.loader.get_include_path(),
project['modules-path']
]
else:
module_paths = [project['modules-path']]
def dependency_projects(project):
module_paths = [
GLOBAL_DBT_MODULES_PATH,
project['modules-path']
]

for module_path in module_paths:
for obj in os.listdir(module_path):
full_obj = os.path.join(module_path, obj)
if os.path.isdir(full_obj):
try:
yield dbt.project.read_project(
os.path.join(full_obj, 'dbt_project.yml'),
project.profiles_dir,
profile_to_load=project.profile_to_load,
args=project.args)
except dbt.project.DbtProjectError as e:
logger.info(
"Error reading dependency project at {}".format(
full_obj)
)
logger.info(str(e))

if not os.path.isdir(full_obj) or obj.startswith('__'):
# exclude non-dirs and dirs that start with __
# the latter could be something like __pycache__
# for the global dbt modules dir
continue

try:
yield dbt.project.read_project(
os.path.join(full_obj, 'dbt_project.yml'),
project.profiles_dir,
profile_to_load=project.profile_to_load,
args=project.args)
except dbt.project.DbtProjectError as e:
logger.info(
"Error reading dependency project at {}".format(
full_obj)
)
logger.info(str(e))


def split_path(path):
Expand Down

0 comments on commit 858d423

Please sign in to comment.