Skip to content

Commit

Permalink
everything about tracking is hard
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Beck committed Dec 13, 2018
1 parent 808ed75 commit cad67ea
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 60 deletions.
75 changes: 47 additions & 28 deletions dbt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,13 +460,48 @@ def warn_for_unused_resource_config_paths(self, resource_fqns, disabled):
logger.info(dbt.ui.printer.yellow(msg))


class UserConfig(object):
def __init__(self, send_anonymous_usage_stats, use_colors):
self.send_anonymous_usage_stats = send_anonymous_usage_stats
self.use_colors = use_colors

@classmethod
def from_dict(cls, cfg=None):
if cfg is None:
cfg = {}
send_anonymous_usage_stats = cfg.get(
'send_anonymous_usage_stats',
DEFAULT_SEND_ANONYMOUS_USAGE_STATS
)
use_colors = cfg.get(
'use_colors',
DEFAULT_USE_COLORS
)
return cls(send_anonymous_usage_stats, use_colors)

def to_dict(self):
return {
'send_anonymous_usage_stats': self.send_anonymous_usage_stats,
'use_colors': self.use_colors,
}

@classmethod
def from_directory(cls, directory):
user_cfg = None
profile = read_profile(directory)
if profile:
user_cfg = profile.get('config', {})
return cls.from_dict(user_cfg)


class Profile(object):
def __init__(self, profile_name, target_name, send_anonymous_usage_stats,
use_colors, threads, credentials):
def __init__(self, profile_name, target_name, config, threads,
credentials):
self.profile_name = profile_name
self.target_name = target_name
self.send_anonymous_usage_stats = send_anonymous_usage_stats
self.use_colors = use_colors
if isinstance(config, dict):
config = UserConfig.from_dict(config)
self.config = config
self.threads = threads
self.credentials = credentials

Expand All @@ -482,8 +517,7 @@ def to_profile_info(self, serialize_credentials=False):
result = {
'profile_name': self.profile_name,
'target_name': self.target_name,
'send_anonymous_usage_stats': self.send_anonymous_usage_stats,
'use_colors': self.use_colors,
'config': self.config.to_dict(),
'threads': self.threads,
'credentials': self.credentials.incorporate(),
}
Expand Down Expand Up @@ -568,21 +602,11 @@ def from_credentials(cls, credentials, threads, profile_name, target_name,
:raises DbtProfileError: If the profile is invalid.
:returns Profile: The new Profile object.
"""
if user_cfg is None:
user_cfg = {}
send_anonymous_usage_stats = user_cfg.get(
'send_anonymous_usage_stats',
DEFAULT_SEND_ANONYMOUS_USAGE_STATS
)
use_colors = user_cfg.get(
'use_colors',
DEFAULT_USE_COLORS
)
config = UserConfig.from_dict(user_cfg)
profile = cls(
profile_name=profile_name,
target_name=target_name,
send_anonymous_usage_stats=send_anonymous_usage_stats,
use_colors=use_colors,
config=config,
threads=threads,
credentials=credentials
)
Expand Down Expand Up @@ -729,10 +753,8 @@ def from_args(cls, args, project_profile_name=None, cli_vars=None):
cli_vars = dbt.utils.parse_cli_vars(getattr(args, 'vars', '{}'))

threads_override = getattr(args, 'threads', None)
# TODO(jeb): is it even possible for this to not be set?
profiles_dir = getattr(args, 'profiles_dir', PROFILES_DIR)
target_override = getattr(args, 'target', None)
raw_profiles = read_profile(profiles_dir)
raw_profiles = read_profile(args.profiles_dir)
profile_name = cls.pick_profile_name(args.profile,
project_profile_name)

Expand Down Expand Up @@ -781,9 +803,8 @@ def __init__(self, project_name, version, project_root, source_paths,
macro_paths, data_paths, test_paths, analysis_paths,
docs_paths, target_path, clean_targets, log_path,
modules_path, quoting, models, on_run_start, on_run_end,
archive, seeds, profile_name, target_name,
send_anonymous_usage_stats, use_colors, threads, credentials,
packages, args):
archive, seeds, profile_name, target_name, config,
threads, credentials, packages, args):
# 'vars'
self.args = args
self.cli_vars = dbt.utils.parse_cli_vars(getattr(args, 'vars', '{}'))
Expand Down Expand Up @@ -817,8 +838,7 @@ def __init__(self, project_name, version, project_root, source_paths,
self,
profile_name=profile_name,
target_name=target_name,
send_anonymous_usage_stats=send_anonymous_usage_stats,
use_colors=use_colors,
config=config,
threads=threads,
credentials=credentials
)
Expand Down Expand Up @@ -861,8 +881,7 @@ def from_parts(cls, project, profile, args):
packages=project.packages,
profile_name=profile.profile_name,
target_name=profile.target_name,
send_anonymous_usage_stats=profile.send_anonymous_usage_stats,
use_colors=profile.use_colors,
config=profile.config,
threads=profile.threads,
credentials=profile.credentials,
args=args
Expand Down
24 changes: 16 additions & 8 deletions dbt/contracts/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,20 @@ class PackageConfig(APIObject):
SCHEMA = PACKAGE_FILE_CONTRACT


USER_CONFIG_CONTRACT = {
'type': 'object',
'additionalProperties': True,
'properties': {
'send_anonymous_usage_stats': {
'type': 'boolean',
},
'use_colors': {
'type': 'boolean',
},
},
}


PROFILE_INFO_CONTRACT = {
'type': 'object',
'additionalProperties': False,
Expand All @@ -294,12 +308,7 @@ class PackageConfig(APIObject):
'target_name': {
'type': 'string',
},
'send_anonymous_usage_stats': {
'type': 'boolean',
},
'use_colors': {
'type': 'boolean',
},
'config': USER_CONFIG_CONTRACT,
'threads': {
'type': 'number',
},
Expand All @@ -313,8 +322,7 @@ class PackageConfig(APIObject):
},
},
'required': [
'profile_name', 'target_name', 'send_anonymous_usage_stats',
'use_colors', 'threads', 'credentials'
'profile_name', 'target_name', 'config', 'threads', 'credentials'
],
}

Expand Down
10 changes: 5 additions & 5 deletions dbt/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import dbt.profiler

from dbt.utils import ExitCodes
from dbt.config import Project, Profile, RuntimeConfig, PROFILES_DIR, \
from dbt.config import Project, UserConfig, RuntimeConfig, PROFILES_DIR, \
read_profiles
from dbt.exceptions import DbtProfileError, DbtProfileError, RuntimeException

Expand Down Expand Up @@ -118,16 +118,16 @@ def initialize_config_values(parsed):
easy.
"""
try:
profile = Profile.from_args(parsed)
cfg = UserConfig.from_directory(parsed.profiles_dir)
except RuntimeException:
profile = None
cfg = UserConfig.from_dict(None)

if profile is None or profile.send_anonymous_usage_stats:
if cfg.send_anonymous_usage_stats:
dbt.tracking.initialize_tracking(parsed.profiles_dir)
else:
dbt.tracking.do_not_track()

if profile is None or profile.use_colors:
if cfg.use_colors:
dbt.ui.printer.use_colors()


Expand Down
1 change: 1 addition & 0 deletions test/integration/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class TestArgs(object):
def __init__(self, kwargs):
self.which = 'run'
self.single_threaded = False
self.profiles_dir = DBT_CONFIG_DIR
self.__dict__.update(kwargs)


Expand Down
38 changes: 19 additions & 19 deletions test/unit/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ def test_from_raw_profiles(self):
self.assertEqual(profile.profile_name, 'default')
self.assertEqual(profile.target_name, 'postgres')
self.assertEqual(profile.threads, 7)
self.assertTrue(profile.send_anonymous_usage_stats)
self.assertTrue(profile.use_colors)
self.assertTrue(profile.config.send_anonymous_usage_stats)
self.assertTrue(profile.config.use_colors)
self.assertTrue(isinstance(profile.credentials, PostgresCredentials))
self.assertEqual(profile.credentials.type, 'postgres')
self.assertEqual(profile.credentials.host, 'postgres-db-hostname')
Expand All @@ -228,8 +228,8 @@ def test_config_override(self):
profile = self.from_raw_profiles()
self.assertEqual(profile.profile_name, 'default')
self.assertEqual(profile.target_name, 'postgres')
self.assertFalse(profile.send_anonymous_usage_stats)
self.assertFalse(profile.use_colors)
self.assertFalse(profile.config.send_anonymous_usage_stats)
self.assertFalse(profile.config.use_colors)

def test_partial_config_override(self):
self.default_profile_data['config'] = {
Expand All @@ -238,8 +238,8 @@ def test_partial_config_override(self):
profile = self.from_raw_profiles()
self.assertEqual(profile.profile_name, 'default')
self.assertEqual(profile.target_name, 'postgres')
self.assertFalse(profile.send_anonymous_usage_stats)
self.assertTrue(profile.use_colors)
self.assertFalse(profile.config.send_anonymous_usage_stats)
self.assertTrue(profile.config.use_colors)

def test_missing_type(self):
del self.default_profile_data['default']['outputs']['postgres']['type']
Expand Down Expand Up @@ -364,8 +364,8 @@ def test_profile_simple(self):
self.assertEqual(profile.profile_name, 'default')
self.assertEqual(profile.target_name, 'postgres')
self.assertEqual(profile.threads, 7)
self.assertTrue(profile.send_anonymous_usage_stats)
self.assertTrue(profile.use_colors)
self.assertTrue(profile.config.send_anonymous_usage_stats)
self.assertTrue(profile.config.use_colors)
self.assertTrue(isinstance(profile.credentials, PostgresCredentials))
self.assertEqual(profile.credentials.type, 'postgres')
self.assertEqual(profile.credentials.host, 'postgres-db-hostname')
Expand All @@ -389,8 +389,8 @@ def test_profile_override(self):
self.assertEqual(profile.profile_name, 'other')
self.assertEqual(profile.target_name, 'other-postgres')
self.assertEqual(profile.threads, 3)
self.assertTrue(profile.send_anonymous_usage_stats)
self.assertTrue(profile.use_colors)
self.assertTrue(profile.config.send_anonymous_usage_stats)
self.assertTrue(profile.config.use_colors)
self.assertTrue(isinstance(profile.credentials, PostgresCredentials))
self.assertEqual(profile.credentials.type, 'postgres')
self.assertEqual(profile.credentials.host, 'other-postgres-db-hostname')
Expand All @@ -411,8 +411,8 @@ def test_target_override(self):
self.assertEqual(profile.profile_name, 'default')
self.assertEqual(profile.target_name, 'redshift')
self.assertEqual(profile.threads, 1)
self.assertTrue(profile.send_anonymous_usage_stats)
self.assertTrue(profile.use_colors)
self.assertTrue(profile.config.send_anonymous_usage_stats)
self.assertTrue(profile.config.use_colors)
self.assertTrue(isinstance(profile.credentials, RedshiftCredentials))
self.assertEqual(profile.credentials.type, 'redshift')
self.assertEqual(profile.credentials.host, 'redshift-db-hostname')
Expand All @@ -434,8 +434,8 @@ def test_env_vars(self):
self.assertEqual(profile.profile_name, 'default')
self.assertEqual(profile.target_name, 'with-vars')
self.assertEqual(profile.threads, 1)
self.assertTrue(profile.send_anonymous_usage_stats)
self.assertTrue(profile.use_colors)
self.assertTrue(profile.config.send_anonymous_usage_stats)
self.assertTrue(profile.config.use_colors)
self.assertEqual(profile.credentials.type, 'postgres')
self.assertEqual(profile.credentials.host, 'env-postgres-host')
self.assertEqual(profile.credentials.port, 6543)
Expand All @@ -456,8 +456,8 @@ def test_env_vars_env_target(self):
self.assertEqual(profile.profile_name, 'default')
self.assertEqual(profile.target_name, 'with-vars')
self.assertEqual(profile.threads, 1)
self.assertTrue(profile.send_anonymous_usage_stats)
self.assertTrue(profile.use_colors)
self.assertTrue(profile.config.send_anonymous_usage_stats)
self.assertTrue(profile.config.use_colors)
self.assertEqual(profile.credentials.type, 'postgres')
self.assertEqual(profile.credentials.host, 'env-postgres-host')
self.assertEqual(profile.credentials.port, 6543)
Expand Down Expand Up @@ -487,8 +487,8 @@ def test_cli_and_env_vars(self):
self.assertEqual(profile.profile_name, 'default')
self.assertEqual(profile.target_name, 'cli-and-env-vars')
self.assertEqual(profile.threads, 1)
self.assertTrue(profile.send_anonymous_usage_stats)
self.assertTrue(profile.use_colors)
self.assertTrue(profile.config.send_anonymous_usage_stats)
self.assertTrue(profile.config.use_colors)
self.assertEqual(profile.credentials.type, 'postgres')
self.assertEqual(profile.credentials.host, 'cli-postgres-host')
self.assertEqual(profile.credentials.port, 6543)
Expand Down Expand Up @@ -978,7 +978,7 @@ def test_validate_fails(self):
project = self.get_project()
profile = self.get_profile()
# invalid - must be boolean
profile.use_colors = None
profile.config.use_colors = None
with self.assertRaises(dbt.exceptions.DbtProjectError):
dbt.config.RuntimeConfig.from_parts(project, profile, {})

Expand Down

0 comments on commit cad67ea

Please sign in to comment.