Skip to content
This repository was archived by the owner on Sep 12, 2018. It is now read-only.

Commit af4dc4f

Browse files
committed
Merge pull request #340 from dotcloud/config
Config
2 parents a2fd253 + 3050955 commit af4dc4f

File tree

5 files changed

+195
-3
lines changed

5 files changed

+195
-3
lines changed

docker_registry/app.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
from . import toolkit
1414
from .lib import config
1515

16+
from .lib.core.exceptions import ConfigError
17+
1618

1719
VERSION = '0.6.8'
1820
app = flask.Flask('docker-registry')
@@ -46,7 +48,7 @@ def after_request(response):
4648
def init():
4749
# Configure the secret key
4850
if not cfg.secret_key:
49-
raise RuntimeError('Config error: `secret_key\' is not set')
51+
raise ConfigError('`secret_key\' is not set')
5052
app.secret_key = cfg.secret_key
5153
# Configure the email exceptions
5254
info = cfg.email_exceptions

docker_registry/lib/config.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import rsa
44
import yaml
55

6+
from .core.exceptions import ConfigError
7+
from .core.exceptions import FileNotFoundError
8+
69

710
class Config(object):
811

@@ -56,15 +59,35 @@ def load():
5659
if not os.path.isabs(config_path):
5760
config_path = os.path.join(os.path.dirname(__file__), '../../',
5861
'config', config_path)
59-
with open(config_path) as f:
62+
try:
63+
f = open(config_path)
64+
except Exception:
65+
raise FileNotFoundError(
66+
'Heads-up! File is missing: %s' % config_path)
67+
68+
try:
6069
data = yaml.load(f)
70+
except Exception:
71+
raise ConfigError(
72+
'Config file (%s) is not valid yaml' % config_path)
73+
6174
config = data.get('common', {})
6275
flavor = os.environ.get('SETTINGS_FLAVOR', 'dev')
6376
config.update(data.get(flavor, {}))
6477
config['flavor'] = flavor
6578
config = convert_env_vars(config)
6679
if 'privileged_key' in config:
67-
with open(config['privileged_key']) as f:
80+
try:
81+
f = open(config['privileged_key'])
82+
except Exception:
83+
raise FileNotFoundError(
84+
'Heads-up! File is missing: %s' % config['privileged_key'])
85+
86+
try:
6887
config['privileged_key'] = rsa.PublicKey.load_pkcs1(f.read())
88+
except Exception:
89+
raise ConfigError(
90+
'Key at %s is not a valid RSA key' % config['privileged_key'])
91+
6992
_config = Config(config)
7093
return _config

docker_registry/lib/core/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
docker-registry.core
4+
~~~~~~~~~~~~~~~
5+
6+
"""
7+
8+
from __future__ import absolute_import
9+
10+
__all__ = []

docker_registry/lib/core/compat.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
docker-registry.compat
4+
~~~~~~~~~~~~~~~~~~~~~~
5+
6+
This file defines a collection of properties to quickly identify what python
7+
runtime/version we are working with, and handles the import of modules
8+
that changed, hence hiding import gymnastics from other components.
9+
10+
Use imports from here to ensure portability.
11+
12+
Largely stolen from requests (http://docs.python-requests.org/en/latest/)
13+
under Apache2 license
14+
"""
15+
16+
__all__ = []
17+
18+
import logging
19+
logger = logging.getLogger(__name__)
20+
21+
import sys
22+
23+
# -------
24+
# Pythons
25+
# -------
26+
27+
_ver = sys.version_info
28+
29+
is_py2 = (_ver[0] == 2)
30+
is_py3 = (_ver[0] == 3)
31+
32+
is_py25 = (is_py2 and _ver[1] == 5)
33+
is_py26 = (is_py2 and _ver[1] == 6)
34+
# is_py27 = (is_py2 and _ver[1] == 7)
35+
# is_py30 = (is_py3 and _ver[1] == 0)
36+
# is_py31 = (is_py3 and _ver[1] == 1)
37+
# is_py32 = (is_py3 and _ver[1] == 2)
38+
# is_py33 = (is_py3 and _ver[1] == 3)
39+
# is_py34 = (is_py3 and _ver[1] == 4)
40+
41+
# ---------
42+
# Platforms
43+
# ---------
44+
45+
# _ver = sys.version.lower()
46+
47+
# is_pypy = ('pypy' in _ver)
48+
# is_jython = ('jython' in _ver)
49+
# is_ironpython = ('iron' in _ver)
50+
# is_cpython = not any((is_pypy, is_jython, is_ironpython))
51+
52+
# is_windows = 'win32' in str(sys.platform).lower()
53+
# is_linux = ('linux' in str(sys.platform).lower())
54+
# is_osx = ('darwin' in str(sys.platform).lower())
55+
# is_hpux = ('hpux' in str(sys.platform).lower()) # Complete guess.
56+
# is_solaris = ('solar' in str(sys.platform).lower()) # Complete guess.
57+
58+
if is_py25 or is_py26:
59+
logger.debug("Old python! Using simplejson.")
60+
try:
61+
import simplejson as json # noqa
62+
except ImportError:
63+
json = {}
64+
else:
65+
import json # noqa
66+
67+
# ---------
68+
# Specifics
69+
# ---------
70+
71+
if is_py2:
72+
logger.debug("This is python2")
73+
from urllib import quote_plus # noqa
74+
75+
builtin_str = str
76+
bytes = str
77+
str = unicode
78+
basestring = basestring
79+
# numeric_types = (int, long, float)
80+
81+
elif is_py3:
82+
logger.debug("This is python3")
83+
from urllib.parse import quote_plus # noqa
84+
85+
builtin_str = str
86+
str = str
87+
bytes = bytes
88+
basestring = (str, bytes)
89+
# numeric_types = (int, float)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
docker-registry.exceptions
4+
~~~~~~~~~~~~~~~~~~~~~
5+
6+
"""
7+
8+
__all__ = [
9+
"UnspecifiedError",
10+
"UsageError", "NotImplementedError", "FileNotFoundError", "ConfigError"
11+
# "WrongArgumentsError",
12+
# "ConnectionError", "UnreachableError", "MissingError", "BrokenError"
13+
]
14+
15+
16+
class UnspecifiedError(Exception):
17+
18+
"""Base class for all exceptions in docker-registry.
19+
"""
20+
21+
def __init__(self, *args, **kwargs):
22+
self.message = kwargs.pop('message', 'No details')
23+
super(UnspecifiedError, self).__init__(*args, **kwargs)
24+
25+
26+
class UsageError(UnspecifiedError):
27+
28+
"""Exceptions related to use of the library."""
29+
30+
31+
class NotImplementedError(UsageError):
32+
33+
"""Requested feature is not supported / not implemented."""
34+
35+
36+
class FileNotFoundError(UsageError):
37+
38+
"""Requested (config) file not found."""
39+
40+
41+
class ConfigError(UsageError):
42+
43+
"""Something in the configuration file is not ok."""
44+
45+
46+
# class WrongArgumentsError(UsageError):
47+
48+
# """Expected arguments not satisfied."""
49+
50+
51+
# class ConnectionError(UnspecifiedError):
52+
53+
# """Exceptions related to server/client operation."""
54+
55+
56+
# class UnreachableError(ConnectionError):
57+
58+
# """The requested server is not reachable."""
59+
60+
61+
# class MissingError(ConnectionError):
62+
63+
# """The requested ressource is not to be found on the server."""
64+
65+
66+
# class BrokenError(ConnectionError):
67+
68+
# """Something died on our hands, that the server couldn't digest..."""

0 commit comments

Comments
 (0)