Skip to content
This repository has been archived by the owner on Mar 22, 2018. It is now read-only.

Commit

Permalink
Moved config function use to commissaire.config.
Browse files Browse the repository at this point in the history
  • Loading branch information
ashcrow authored and mbarnes committed Sep 22, 2016
1 parent 33ccbce commit 2c5ed18
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 69 deletions.
65 changes: 3 additions & 62 deletions src/commissaire_service/storage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,72 +18,13 @@
import json

import commissaire.models as models

from commissaire import constants as C
from commissaire.config import ConfigurationError, read_config_file

from commissaire_service.service import CommissaireService
from commissaire_service.storage.storehandlermanager import (
StoreHandlerManager, ConfigurationError)


# FIXME This should be moved to a place for utilities.
def read_config_file(path=None):
"""
Attempts to parse a configuration file, formatted as a JSON object.
If a config file path is explicitly given, then failure to open the
file will raise an IOError. Otherwise a default path is tried, but
no IOError is raised on failure. If the file can be opened but not
parsed, an exception is always raised.
:param path: Full path to the config file, or None
:type path: str or None
:returns: configuration content as a dictionary
:rtype: dict
:raises: IOError, TypeError, ValueError
"""
json_object = {}
using_default = False

if path is None:
path = '/etc/commissaire/commissaire.conf'
using_default = True

try:
with open(path, 'r') as fp:
json_object = json.load(fp)
if using_default:
print('Using configuration in {0}'.format(path))
except IOError:
if not using_default:
raise

if type(json_object) is not dict:
raise TypeError(
'{0}: File content must be a JSON object'.format(path))

# Special case:
#
# In the configuration file, the "authentication-plugin" member
# can also be specified as a JSON object. The object must have
# at least a 'name' member specifying the plugin module name.
auth_key = 'authentication-plugin'
auth_plugin = json_object.get(auth_key)
if type(auth_plugin) is dict:
if 'name' not in auth_plugin:
raise ValueError(
'{0}: "{1}" is missing a "name" member'.format(
path, auth_key))

# Special case:
#
# In the configuration file, the "storage-handlers" member can
# be specified as a JSON object or a list of JSON objects.
handler_key = 'storage-handlers'
handler_list = json_object.get(handler_key)
if type(handler_list) is dict:
json_object[handler_key] = [handler_list]

return json_object
StoreHandlerManager)


class StorageService(CommissaireService):
Expand Down
8 changes: 1 addition & 7 deletions src/commissaire_service/storage/storehandlermanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,10 @@

from copy import deepcopy

from commissaire.config import ConfigurationError
from commissaire.models import ValidationError


class ConfigurationError(Exception):
"""
Exception class for user configuration errors.
"""
pass


class StoreHandlerManager(object):
"""
Configures StoreHandler instances and routes storage requests to
Expand Down

0 comments on commit 2c5ed18

Please sign in to comment.