1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
5 # http://www.apache.org/licenses/LICENSE-2.0
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
13 from toscaparser.common.exception import ExceptionCollector
14 from toscaparser.common.exception import MissingRequiredFieldError
15 from toscaparser.common.exception import UnknownFieldError
16 from toscaparser.common.exception import URLException
17 from toscaparser.utils.gettextutils import _
18 import toscaparser.utils.urlutils
20 SECTIONS = (DESCRIPTION, URL, CREDENTIAL) = \
21 ('description', 'url', 'credential')
24 class Repository(object):
25 def __init__(self, repositories, values):
26 self.name = repositories
28 if isinstance(self.reposit, dict):
29 if 'url' not in self.reposit.keys():
30 ExceptionCollector.appendException(
31 MissingRequiredFieldError(what=_('Repository "%s"')
32 % self.name, required='url'))
33 self.url = self.reposit['url']
34 self.load_and_validate(self.name, self.reposit)
36 def load_and_validate(self, val, reposit_def):
38 if isinstance(reposit_def, dict):
39 for key in reposit_def.keys():
40 if key not in SECTIONS:
41 ExceptionCollector.appendException(
42 UnknownFieldError(what=_('repositories "%s"')
43 % self.keyname, field=key))
45 if URL in reposit_def.keys():
46 reposit_url = reposit_def.get(URL)
47 url_val = toscaparser.utils.urlutils.UrlUtils.\
48 validate_url(reposit_url)
49 if url_val is not True:
50 ExceptionCollector.appendException(
51 URLException(what=_('repsositories "%s" Invalid Url')