-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added new config helper function * flake8 * added docstrings * flake8 * removed need for config prefix * Add in container (#520) * added contains method to container * formatted and added tests * bumped version * Add ability to login using multiple columns (#521) * add list to auth class * adds ability to set a password column * formatted * fixed space in exception
- Loading branch information
1 parent
7f1adbb
commit 2805d61
Showing
10 changed files
with
177 additions
and
4 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
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
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,103 @@ | ||
"""A Module For Manipulating Code Structures.""" | ||
|
||
import pydoc | ||
|
||
|
||
class Dot: | ||
|
||
def locate(self, search_path, default=''): | ||
"""Locate the object from the given search path | ||
Arguments: | ||
search_path {string} -- A search path to fetch the object from like config.application.debug. | ||
Keyword Arguments: | ||
default {string} -- A default string if the search path is not found (default: {''}) | ||
Returns: | ||
any -- Could be a string, object or anything else that is fetched. | ||
""" | ||
value = self.find(search_path, default) | ||
|
||
if isinstance(value, dict): | ||
return self.dict_dot('.'.join(search_path.split('.')[3:]), value) | ||
|
||
if value is not None: | ||
return value | ||
|
||
def dict_dot(self, search, dictionary): | ||
"""Takes a dot notation representation of a dictionary and fetches it from the dictionary. | ||
This will take something like s3.locations and look into the s3 dictionary and fetch the locations | ||
key. | ||
Arguments: | ||
search {string} -- The string to search for in the dictionary using dot notation. | ||
dictionary {dict} -- The dictionary to search through. | ||
Returns: | ||
string -- The value of the dictionary element. | ||
""" | ||
if "." in search: | ||
key, rest = search.split(".", 1) | ||
try: | ||
return self.dict_dot(dictionary[key], rest) | ||
except (KeyError, TypeError): | ||
pass | ||
else: | ||
try: | ||
return dictionary[search] | ||
except TypeError: | ||
pass | ||
|
||
return self.dict_dot(dictionary, search) | ||
|
||
def find(self, search_path, default=''): | ||
"""Used for finding both the uppercase and specified version. | ||
Arguments: | ||
search_path {string} -- The search path to find the module, dictionary key, object etc. | ||
This is typically in the form of dot notation 'config.application.debug' | ||
Keyword Arguments: | ||
default {string} -- The default value to return if the search path could not be found. (default: {''}) | ||
Returns: | ||
any -- Could be a string, object or anything else that is fetched. | ||
""" | ||
value = pydoc.locate(search_path) | ||
|
||
if value: | ||
return value | ||
|
||
paths = search_path.split('.') | ||
|
||
value = pydoc.locate('.'.join(paths[:-1]) + '.' + paths[-1].upper()) | ||
|
||
if value: | ||
return value | ||
|
||
search_path = -1 | ||
|
||
# Go backwards through the dot notation until a match is found. | ||
while search_path < len(paths): | ||
value = pydoc.locate('.'.join(paths[:search_path]) + '.' + paths[search_path].upper()) | ||
|
||
if value: | ||
break | ||
|
||
value = pydoc.locate('.'.join(paths[:search_path]) + '.' + paths[search_path]) | ||
|
||
if value: | ||
break | ||
|
||
if default: | ||
return default | ||
|
||
search_path -= 1 | ||
|
||
return value | ||
|
||
|
||
def config(path, default=''): | ||
return Dot().locate('config.' + path, default) |
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,3 +1,3 @@ | ||
"""Module for specifying the Masonite version in a central location.""" | ||
|
||
VERSION = '2.1.5' | ||
VERSION = '2.1.6' |
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,33 @@ | ||
import pydoc | ||
|
||
from masonite.helpers import config, Dot | ||
|
||
|
||
class TestConfig: | ||
|
||
def setup_method(self): | ||
self.config = config | ||
|
||
def test_config_can_get_value_from_file(self): | ||
assert self.config('application.DEBUG') == True | ||
|
||
def test_config_can_get_dict_value_lowercase(self): | ||
assert self.config('application.debug') == True | ||
|
||
def test_config_can_get_dict_default(self): | ||
assert self.config('sdff.na', 'default') == 'default' | ||
|
||
def test_dict_dot_returns_value(self): | ||
assert Dot().dict_dot('s3.test', {'s3': {'test': 'value'}}) == 'value' | ||
|
||
def test_config_can_get_dict_value_inside_dict(self): | ||
assert self.config('database.DATABASES.default') == 'sqlite' | ||
|
||
def test_config_can_get_dict_value_inside_dict_with_lowercase(self): | ||
assert self.config('database.databases.default') == 'sqlite' | ||
|
||
def test_config_can_get_dict_inside_dict_inside_dict(self): | ||
assert isinstance(self.config('database.databases.sqlite'), dict) | ||
|
||
def test_config_can_get_dict_inside_dict_inside_another_dict(self): | ||
assert self.config('storage.DRIVERS.s3.test_locations.test') == 'value' |
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