-
Notifications
You must be signed in to change notification settings - Fork 512
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
Allow setting defaults through a config file. #279
Open
sunu
wants to merge
7
commits into
scrapinghub:master
Choose a base branch
from
sunu:configfile
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
42b80c0
Allow setting defaults through a config file.
sunu 98d64c8
Update dependencies to include pyyaml.
sunu 7415aa7
A bit of refactoring.
sunu fa5a9c1
Use ConfigParser instead of yaml.
sunu 080813b
Remove yaml dependencies.
sunu 378f22d
Merge branch 'master' of https://github.com/scrapinghub/splash into c…
sunu 8479e7a
Use a global variable instead of __builtin__
sunu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import __builtin__ | ||
import ast | ||
import ConfigParser | ||
import os | ||
|
||
from . import defaults | ||
|
||
|
||
class ConfigError(Exception): | ||
pass | ||
|
||
global CONFIG_PATH | ||
|
||
|
||
class Settings(object): | ||
"""Handles config files and default values of config settings.""" | ||
|
||
NO_CONFIG_FILE_MSG = "Config file doesn't exist at %s" | ||
|
||
def __init__(self): | ||
try: | ||
self.config_path = CONFIG_PATH | ||
except NameError: | ||
# CONFIG_PATH is not defined. User hasn't passed in a config file. | ||
self.config_path = None | ||
self.defaults = {} | ||
for name in dir(defaults): | ||
if name.isupper(): | ||
self.defaults[name] = getattr(defaults, name) | ||
parser = ConfigParser.SafeConfigParser() | ||
# don't convert keys to lowercase. | ||
parser.optionxform = str | ||
if parser.read(self._get_configfile_paths()): | ||
# Safely evaluate configuration values. | ||
self.cfg = {key: ast.literal_eval(val) for (key, val) in parser.items('settings')} | ||
else: | ||
self.cfg = {} | ||
|
||
def _get_configfile_paths(self): | ||
"""Returns a list of config file paths.""" | ||
if self.config_path: | ||
config_dir_path = os.path.abspath(os.path.expanduser(self.config_path)) | ||
configfile_path = os.path.abspath(os.path.join(config_dir_path, 'splash.cfg')) | ||
if not os.path.isfile(configfile_path): | ||
# file doesn't exist | ||
raise ConfigError(self.NO_CONFIG_FILE_MSG % configfile_path) | ||
else: | ||
return configfile_path | ||
else: | ||
xdg_config_home = os.environ.get('XDG_CONFIG_HOME') or \ | ||
os.path.expanduser('~/.config') | ||
return ['/etc/splash.cfg', | ||
'C:\\splash\splash.cfg', | ||
os.path.join(xdg_config_home, 'splash.cfg'), | ||
os.path.expanduser('~/.splash.cfg')] | ||
|
||
def __getattr__(self, item): | ||
val = self.cfg.get(item, None) | ||
if val is None: | ||
val = self.defaults.get(item, None) | ||
if val is None: | ||
raise AttributeError("There is no settings named %s" % item) | ||
return val | ||
|
||
settings = Settings() |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this looks brittle.. It will require changing more code, but maybe we can make this path a Settings constructor parameter and don't create a global
settings
variable at module level? We can create it in server startup code (when the path is known) and pass it down to all components via function/method/constructor arguments instead.