From 804e2b720229d95b21d5ce043024254763ff76ad Mon Sep 17 00:00:00 2001 From: kakwa Date: Tue, 12 May 2015 11:34:28 +0200 Subject: [PATCH] adding an exception for failures to open role file --- ldapcherry/exceptions.py | 10 ++++++++++ ldapcherry/roles.py | 7 +++++-- tests/test_Roles.py | 11 ++++++++++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/ldapcherry/exceptions.py b/ldapcherry/exceptions.py index 21a0876..bc8517d 100644 --- a/ldapcherry/exceptions.py +++ b/ldapcherry/exceptions.py @@ -9,6 +9,7 @@ class MissingParameter(Exception): def __init__(self, section, key): self.section = section self.key = key + self.log = "missing parameter <%(key)s> in section <%(section)s>" % { 'key' : key, 'section' : section } class MissingKey(Exception): def __init__(self, key): @@ -17,8 +18,17 @@ class MissingKey(Exception): class DumplicateRoleKey(Exception): def __init__(self, role): self.role = role + self.log = "duplicate role key <%(role)s> in role file" % { 'role' : role} class DumplicateRoleContent(Exception): def __init__(self, role1, role2): self.role1 = role1 self.role2 = role2 + self.log = "role <%(role1)s> and <%(role2)s> are identical" % { 'role1' : role1, 'role2': role2} + +class MissingRolesFile(Exception): + def __init__(self, rolefile): + self.rolefile = rolefile + self.log = "fail to open role file <%(rolefile)s>" % { 'rolefile' : rolefile} + + diff --git a/ldapcherry/roles.py b/ldapcherry/roles.py index 5852718..358316c 100644 --- a/ldapcherry/roles.py +++ b/ldapcherry/roles.py @@ -10,13 +10,16 @@ import sys from ldapcherry.pyyamlwrapper import loadNoDump from ldapcherry.pyyamlwrapper import DumplicatedKey -from ldapcherry.exceptions import DumplicateRoleKey, MissingKey, DumplicateRoleContent +from ldapcherry.exceptions import DumplicateRoleKey, MissingKey, DumplicateRoleContent, MissingRolesFile class Roles: def __init__(self, role_file): - stream = open(role_file, 'r') + try: + stream = open(role_file, 'r') + except: + raise MissingRolesFile(role_file) try: self.roles_raw = loadNoDump(stream) except DumplicatedKey as e: diff --git a/tests/test_Roles.py b/tests/test_Roles.py index b86dfc2..dea4c54 100644 --- a/tests/test_Roles.py +++ b/tests/test_Roles.py @@ -7,7 +7,7 @@ from __future__ import unicode_literals import pytest import sys from ldapcherry.roles import Roles -from ldapcherry.exceptions import DumplicateRoleKey, MissingKey, DumplicateRoleContent +from ldapcherry.exceptions import DumplicateRoleKey, MissingKey, DumplicateRoleContent, MissingRolesFile from ldapcherry.pyyamlwrapper import DumplicatedKey, RelationError @@ -34,6 +34,15 @@ class TestError(object): else: raise AssertionError("expected an exception") + + def testNoFile(self): + try: + inv = Roles('./tests/cfg/dontexist') + except MissingRolesFile: + return + else: + raise AssertionError("expected an exception") + def testRoleContentDuplication(self): try: inv = Roles('./tests/cfg/roles_content_dump.yml')