Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
working refactor not requiring Flask import from ordbok, doc updates …
…needed
- Loading branch information
1 parent
1f47b0b
commit 76ab8be
Showing
7 changed files
with
116 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from .ordbok import Ordbok | ||
from .config_file import ConfigFile | ||
from .config_env import ConfigEnv | ||
from .config_private import PrivateConfigFile | ||
|
||
|
||
__all__ = ["Ordbok", "ConfigFile", "ConfigEnv", "PrivateConfigFile"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,19 @@ | ||
from .ordbok import Ordbok | ||
|
||
from flask import Flask as BaseFlask, Config as BaseConfig | ||
|
||
|
||
class OrdbokFlaskConfig(BaseConfig, Ordbok): | ||
""" | ||
Extented version of the builtin Flask `Config` that inherits | ||
a `from_yaml` method from Ordbok so that config varibables | ||
can be defined there. | ||
""" | ||
def __init__(self, *args, **kwargs): | ||
self.set_defaults(**kwargs) | ||
return super(OrdbokFlaskConfig, self).__init__(*args, **kwargs) | ||
|
||
@property | ||
def config_cwd(self): | ||
return self.root_path | ||
|
||
|
||
def make_config(self, instance_relative=False): | ||
root_path = self.root_path | ||
if instance_relative: | ||
root_path = self.instance_path | ||
return self.config_class(root_path, self.default_config) | ||
|
||
|
||
def run(self, *args, **kwargs): | ||
if self.config_class is not OrdbokFlaskConfig: | ||
raise Exception( | ||
'Cannot override Flask.run() without using OrdbokFlaskConfig ' | ||
'as Flask.config_class.') | ||
if kwargs.get('use_reloader') is not False and self.debug: | ||
kwargs.setdefault('extra_files', []) | ||
kwargs['extra_files'].extend(self.config.config_file_names) | ||
return super(Flask, self).run(*args, **kwargs) | ||
|
||
|
||
class Flask(BaseFlask): | ||
""" | ||
Extened version of `Flask` that implements the custom config class | ||
defined above. | ||
""" | ||
config_class = OrdbokFlaskConfig | ||
make_config = make_config | ||
run = run | ||
class FlaskOrdbok(Ordbok): | ||
def __init__(self, app=None, **kwargs): | ||
if app: | ||
self.init_app(app) | ||
return super(FlaskOrdbok, self).__init__(**kwargs) | ||
|
||
def init_app(self, app): | ||
self._root_path = app.config.root_path | ||
|
||
def app_run(self, app, *args, **kwargs): | ||
if kwargs.get('use_reloader') is not False and app.debug: | ||
extra_files = kwargs.get('extra_files', []) | ||
extra_files.extend(self.config_file_names) | ||
if extra_files: | ||
kwargs['extra_files'] = extra_files | ||
return app.run(*args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import six | ||
from .config_file import ConfigFile | ||
|
||
|
||
def create_config_file(f): | ||
if isinstance(f, ConfigFile): | ||
return f | ||
elif isinstance(f, six.string_types): | ||
return ConfigFile(f) | ||
else: | ||
raise TypeError( | ||
'Ordbok.config_files can only be derived from ' | ||
'ordbok.ConfigFile or the filename of a config file') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters