Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use config[] not config.get() #8454

Merged
merged 7 commits into from
Oct 30, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ For large features or major changes to codebase, please create **Superset Improv
### Fix Bugs

Look through the GitHub issues. Issues tagged with `#bug` are
open to whoever wants to implement it.
open to whoever wants to implement them.

### Implement Features

Expand Down
30 changes: 14 additions & 16 deletions superset/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from superset.connectors.connector_registry import ConnectorRegistry
from superset.security import SupersetSecurityManager
from superset.utils.core import pessimistic_connection_handling, setup_cache
from superset.utils.log import DBEventLogger, get_event_logger_from_cfg_value
from superset.utils.log import get_event_logger_from_cfg_value

wtforms_json.init()

Expand Down Expand Up @@ -132,19 +132,19 @@ def get_manifest():

app.config["LOGGING_CONFIGURATOR"].configure_logging(app.config, app.debug)

if app.config.get("ENABLE_CORS"):
if app.config["ENABLE_CORS"]:
from flask_cors import CORS

CORS(app, **app.config.get("CORS_OPTIONS"))
CORS(app, **app.config["CORS_OPTIONS"])

if app.config.get("ENABLE_PROXY_FIX"):
if app.config["ENABLE_PROXY_FIX"]:
from werkzeug.middleware.proxy_fix import ProxyFix

app.wsgi_app = ProxyFix( # type: ignore
app.wsgi_app, **app.config.get("PROXY_FIX_CONFIG")
app.wsgi_app, **app.config["PROXY_FIX_CONFIG"]
)

if app.config.get("ENABLE_CHUNK_ENCODING"):
if app.config["ENABLE_CHUNK_ENCODING"]:

class ChunkedEncodingFix(object):
def __init__(self, app):
Expand Down Expand Up @@ -175,7 +175,7 @@ def index(self):
return redirect("/superset/welcome")


custom_sm = app.config.get("CUSTOM_SECURITY_MANAGER") or SupersetSecurityManager
custom_sm = app.config["CUSTOM_SECURITY_MANAGER"] or SupersetSecurityManager
if not issubclass(custom_sm, SupersetSecurityManager):
raise Exception(
"""Your CUSTOM_SECURITY_MANAGER must now extend SupersetSecurityManager,
Expand All @@ -195,21 +195,19 @@ def index(self):

security_manager = appbuilder.sm

results_backend = app.config.get("RESULTS_BACKEND")
results_backend_use_msgpack = app.config.get("RESULTS_BACKEND_USE_MSGPACK")
results_backend = app.config["RESULTS_BACKEND"]
results_backend_use_msgpack = app.config["RESULTS_BACKEND_USE_MSGPACK"]

# Merge user defined feature flags with default feature flags
_feature_flags = app.config.get("DEFAULT_FEATURE_FLAGS") or {}
_feature_flags.update(app.config.get("FEATURE_FLAGS") or {})
_feature_flags = app.config["DEFAULT_FEATURE_FLAGS"]
_feature_flags.update(app.config["FEATURE_FLAGS"])

# Event Logger
event_logger = get_event_logger_from_cfg_value(
app.config.get("EVENT_LOGGER", DBEventLogger())
)
event_logger = get_event_logger_from_cfg_value(app.config["EVENT_LOGGER"])


def get_feature_flags():
GET_FEATURE_FLAGS_FUNC = app.config.get("GET_FEATURE_FLAGS_FUNC")
GET_FEATURE_FLAGS_FUNC = app.config["GET_FEATURE_FLAGS_FUNC"]
if GET_FEATURE_FLAGS_FUNC:
return GET_FEATURE_FLAGS_FUNC(deepcopy(_feature_flags))
return _feature_flags
Expand All @@ -232,7 +230,7 @@ def is_feature_enabled(feature):

# Hook that provides administrators a handle on the Flask APP
# after initialization
flask_app_mutator = app.config.get("FLASK_APP_MUTATOR")
flask_app_mutator = app.config["FLASK_APP_MUTATOR"]
if flask_app_mutator:
flask_app_mutator(app)

Expand Down
10 changes: 4 additions & 6 deletions superset/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def version(verbose):
Fore.YELLOW
+ "Superset "
+ Fore.CYAN
+ "{version}".format(version=config.get("VERSION_STRING"))
+ "{version}".format(version=config["VERSION_STRING"])
)
print(Fore.BLUE + "-=" * 15)
if verbose:
Expand Down Expand Up @@ -372,10 +372,8 @@ def worker(workers):
)
if workers:
celery_app.conf.update(CELERYD_CONCURRENCY=workers)
elif config.get("SUPERSET_CELERY_WORKERS"):
celery_app.conf.update(
CELERYD_CONCURRENCY=config.get("SUPERSET_CELERY_WORKERS")
)
elif config["SUPERSET_CELERY_WORKERS"]:
celery_app.conf.update(CELERYD_CONCURRENCY=config["SUPERSET_CELERY_WORKERS"])

worker = celery_app.Worker(optimization="fair")
worker.start()
Expand Down Expand Up @@ -428,7 +426,7 @@ def load_test_users_run():

Syncs permissions for those users/roles
"""
if config.get("TESTING"):
if config["TESTING"]:

sm = security_manager

Expand Down
4 changes: 2 additions & 2 deletions superset/common/query_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def __init__(
extras: Optional[Dict] = None,
columns: Optional[List[str]] = None,
orderby: Optional[List[List]] = None,
relative_start: str = app.config.get("DEFAULT_RELATIVE_START_TIME", "today"),
relative_end: str = app.config.get("DEFAULT_RELATIVE_END_TIME", "today"),
relative_start: str = app.config["DEFAULT_RELATIVE_START_TIME"],
relative_end: str = app.config["DEFAULT_RELATIVE_END_TIME"],
Comment on lines +69 to +70
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good example of why this change is good; no point in having secondary default values spread across the codebase, as these should only be defaulted in one place.

):
self.granularity = granularity
self.from_dttm, self.to_dttm = utils.get_since_until(
Expand Down
17 changes: 16 additions & 1 deletion superset/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@
from flask_appbuilder.security.manager import AUTH_DB

from superset.stats_logger import DummyStatsLogger
from superset.utils.log import DBEventLogger
from superset.utils.logging_configurator import DefaultLoggingConfigurator

# Realtime stats logger, a StatsD implementation exists
STATS_LOGGER = DummyStatsLogger()
EVENT_LOGGER = DBEventLogger()

SUPERSET_LOG_VIEW = True

BASE_DIR = os.path.abspath(os.path.dirname(__file__))
if "SUPERSET_HOME" in os.environ:
Expand Down Expand Up @@ -109,6 +113,7 @@ def _try_json_readfile(filepath):
# def lookup_password(url):
# return 'secret'
# SQLALCHEMY_CUSTOM_PASSWORD_STORE = lookup_password
SQLALCHEMY_CUSTOM_PASSWORD_STORE = None

# The limit of queries fetched for query search
QUERY_SEARCH_LIMIT = 1000
Expand Down Expand Up @@ -232,6 +237,9 @@ def _try_json_readfile(filepath):
"PRESTO_EXPAND_DATA": False,
}

# This is merely a default.
FEATURE_FLAGS: Dict[str, bool] = {}

# A function that receives a dict of all feature flags
# (DEFAULT_FEATURE_FLAGS merged with FEATURE_FLAGS)
# can alter it, and returns a similar dict. Note the dict of feature
Expand Down Expand Up @@ -371,6 +379,7 @@ def _try_json_readfile(filepath):
# security_manager=None,
# ):
# pass
QUERY_LOGGER = None

# Set this API key to enable Mapbox visualizations
MAPBOX_API_KEY = os.environ.get("MAPBOX_API_KEY", "")
Expand Down Expand Up @@ -444,6 +453,7 @@ class CeleryConfig(object):
# override anything set within the app
DEFAULT_HTTP_HEADERS: Dict[str, Any] = {}
OVERRIDE_HTTP_HEADERS: Dict[str, Any] = {}
HTTP_HEADERS: Dict[str, Any] = {}

# The db id here results in selecting this one as a default in SQL Lab
DEFAULT_DB_ID = None
Expand Down Expand Up @@ -522,13 +532,18 @@ class CeleryConfig(object):
SMTP_MAIL_FROM = "[email protected]"

if not CACHE_DEFAULT_TIMEOUT:
CACHE_DEFAULT_TIMEOUT = CACHE_CONFIG.get("CACHE_DEFAULT_TIMEOUT") # type: ignore
CACHE_DEFAULT_TIMEOUT = CACHE_CONFIG["CACHE_DEFAULT_TIMEOUT"]


ENABLE_CHUNK_ENCODING = False

# Whether to bump the logging level to ERROR on the flask_appbuilder package
# Set to False if/when debugging FAB related issues like
# permission management
SILENCE_FAB = True

FAB_ADD_SECURITY_VIEWS = True

# The link to a page containing common errors and their resolutions
# It will be appended at the bottom of sql_lab errors.
TROUBLESHOOTING_LINK = ""
Expand Down
2 changes: 1 addition & 1 deletion superset/connectors/sqla/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ def mutate_query_from_config(self, sql: str) -> str:
"""Apply config's SQL_QUERY_MUTATOR

Typically adds comments to the query with context"""
SQL_QUERY_MUTATOR = config.get("SQL_QUERY_MUTATOR")
SQL_QUERY_MUTATOR = config["SQL_QUERY_MUTATOR"]
if SQL_QUERY_MUTATOR:
username = utils.get_username()
sql = SQL_QUERY_MUTATOR(sql, username, security_manager, self.database)
Expand Down
6 changes: 3 additions & 3 deletions superset/db_engine_specs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def get_time_grains(cls) -> Tuple[TimeGrain, ...]:
ret_list = []
time_grain_functions = cls.get_time_grain_functions()
time_grains = builtin_time_grains.copy()
time_grains.update(config.get("TIME_GRAIN_ADDONS", {}))
time_grains.update(config["TIME_GRAIN_ADDONS"])
for duration, func in time_grain_functions.items():
if duration in time_grains:
name = time_grains[duration]
Expand All @@ -200,9 +200,9 @@ def get_time_grain_functions(cls) -> Dict[Optional[str], str]:
"""
# TODO: use @memoize decorator or similar to avoid recomputation on every call
time_grain_functions = cls._time_grain_functions.copy()
grain_addon_functions = config.get("TIME_GRAIN_ADDON_FUNCTIONS", {})
grain_addon_functions = config["TIME_GRAIN_ADDON_FUNCTIONS"]
time_grain_functions.update(grain_addon_functions.get(cls.engine, {}))
blacklist: List[str] = config.get("TIME_GRAIN_BLACKLIST", [])
blacklist: List[str] = config["TIME_GRAIN_BLACKLIST"]
for key in blacklist:
time_grain_functions.pop(key)
return time_grain_functions
Expand Down
6 changes: 3 additions & 3 deletions superset/db_engine_specs/hive.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,16 @@ def convert_to_hive_type(col_type):
table_name = form.name.data
schema_name = form.schema.data

if config.get("UPLOADED_CSV_HIVE_NAMESPACE"):
if config["UPLOADED_CSV_HIVE_NAMESPACE"]:
if "." in table_name or schema_name:
raise Exception(
"You can't specify a namespace. "
"All tables will be uploaded to the `{}` namespace".format(
config.get("HIVE_NAMESPACE")
config["HIVE_NAMESPACE"]
)
)
full_table_name = "{}.{}".format(
config.get("UPLOADED_CSV_HIVE_NAMESPACE"), table_name
config["UPLOADED_CSV_HIVE_NAMESPACE"], table_name
)
else:
if "." in table_name and schema_name:
Expand Down
2 changes: 1 addition & 1 deletion superset/db_engine_specs/presto.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ def estimate_statement_cost( # pylint: disable=too-many-locals
parsed_query = ParsedQuery(statement)
sql = parsed_query.stripped()

sql_query_mutator = config.get("SQL_QUERY_MUTATOR")
sql_query_mutator = config["SQL_QUERY_MUTATOR"]
if sql_query_mutator:
sql = sql_query_mutator(sql, user_name, security_manager, database)

Expand Down
2 changes: 1 addition & 1 deletion superset/examples/birth_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def load_birth_names(only_metadata=False, force=False):
"optionName": "metric_11",
}
],
"row_limit": config.get("ROW_LIMIT"),
"row_limit": config["ROW_LIMIT"],
"since": "100 years ago",
"until": "now",
"viz_type": "table",
Expand Down
2 changes: 1 addition & 1 deletion superset/examples/multiformat_time_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def load_multiformat_time_series(only_metadata=False, force=False):
slice_data = {
"metrics": ["count"],
"granularity_sqla": col.column_name,
"row_limit": config.get("ROW_LIMIT"),
"row_limit": config["ROW_LIMIT"],
"since": "2015",
"until": "2016",
"where": "",
Expand Down
2 changes: 1 addition & 1 deletion superset/examples/random_time_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def load_random_time_series_data(only_metadata=False, force=False):

slice_data = {
"granularity_sqla": "day",
"row_limit": config.get("ROW_LIMIT"),
"row_limit": config["ROW_LIMIT"],
"since": "1 year ago",
"until": "now",
"metric": "count",
Expand Down
2 changes: 1 addition & 1 deletion superset/examples/unicode_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def load_unicode_test_data(only_metadata=False, force=False):
"expressionType": "SIMPLE",
"label": "Value",
},
"row_limit": config.get("ROW_LIMIT"),
"row_limit": config["ROW_LIMIT"],
"since": "100 years ago",
"until": "now",
"where": "",
Expand Down
2 changes: 1 addition & 1 deletion superset/examples/world_bank.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def load_world_bank_health_n_pop(only_metadata=False, force=False):
"groupby": [],
"metric": "sum__SP_POP_TOTL",
"metrics": ["sum__SP_POP_TOTL"],
"row_limit": config.get("ROW_LIMIT"),
"row_limit": config["ROW_LIMIT"],
"since": "2014-01-01",
"until": "2014-01-02",
"time_range": "2014-01-01 : 2014-01-02",
Expand Down
2 changes: 1 addition & 1 deletion superset/jinja_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"timedelta": timedelta,
"uuid": uuid,
}
BASE_CONTEXT.update(config.get("JINJA_CONTEXT_ADDONS", {}))
BASE_CONTEXT.update(config["JINJA_CONTEXT_ADDONS"])


def url_param(param: str, default: Optional[str] = None) -> Optional[Any]:
Expand Down
4 changes: 1 addition & 3 deletions superset/migrations/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@
logger = logging.getLogger("alembic.env")


config.set_main_option(
"sqlalchemy.url", current_app.config.get("SQLALCHEMY_DATABASE_URI")
)
config.set_main_option("sqlalchemy.url", current_app.config["SQLALCHEMY_DATABASE_URI"])
target_metadata = Base.metadata # pylint: disable=no-member

# other values from the config, defined by the needs of env.py,
Expand Down
14 changes: 7 additions & 7 deletions superset/models/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@
from superset.viz import viz_types

config = app.config
custom_password_store = config.get("SQLALCHEMY_CUSTOM_PASSWORD_STORE")
stats_logger = config.get("STATS_LOGGER")
log_query = config.get("QUERY_LOGGER")
custom_password_store = config["SQLALCHEMY_CUSTOM_PASSWORD_STORE"]
stats_logger = config["STATS_LOGGER"]
log_query = config["QUERY_LOGGER"]
metadata = Model.metadata # pylint: disable=no-member

PASSWORD_MASK = "X" * 10
Expand All @@ -81,7 +81,7 @@ def set_related_perm(mapper, connection, target):


def copy_dashboard(mapper, connection, target):
dashboard_id = config.get("DASHBOARD_TEMPLATE_ID")
dashboard_id = config["DASHBOARD_TEMPLATE_ID"]
if dashboard_id is None:
return

Expand Down Expand Up @@ -725,7 +725,7 @@ class Database(Model, AuditMixinNullable, ImportMixin):
# short unique name, used in permissions
database_name = Column(String(250), unique=True, nullable=False)
sqlalchemy_uri = Column(String(1024))
password = Column(EncryptedType(String(1024), config.get("SECRET_KEY")))
password = Column(EncryptedType(String(1024), config["SECRET_KEY"]))
cache_timeout = Column(Integer)
select_as_create_table_as = Column(Boolean, default=False)
expose_in_sqllab = Column(Boolean, default=True)
Expand Down Expand Up @@ -906,7 +906,7 @@ def get_sqla_engine(self, schema=None, nullpool=True, user_name=None, source=Non

params.update(self.get_encrypted_extra())

DB_CONNECTION_MUTATOR = config.get("DB_CONNECTION_MUTATOR")
DB_CONNECTION_MUTATOR = config["DB_CONNECTION_MUTATOR"]
if DB_CONNECTION_MUTATOR:
url, params = DB_CONNECTION_MUTATOR(
url, params, effective_username, security_manager, source
Expand Down Expand Up @@ -1262,7 +1262,7 @@ class DatasourceAccessRequest(Model, AuditMixinNullable):
datasource_id = Column(Integer)
datasource_type = Column(String(200))

ROLES_BLACKLIST = set(config.get("ROBOT_PERMISSION_ROLES", []))
ROLES_BLACKLIST = set(config["ROBOT_PERMISSION_ROLES"])

@property
def cls_model(self):
Expand Down
Loading