9c303f971279a568bf1aafdc88da893b43756ed1
4 # Copyright 2017 RIFT.IO Inc
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
20 class ManoProjectError(Exception):
24 class ManoProjNameSetErr(ManoProjectError
):
28 class ManoProjXpathNoProjErr(ManoProjectError
):
32 class ManoProjXpathKeyErr(ManoProjectError
):
36 class ManoProject(object):
37 '''Class to handle the project name'''
40 XPATH
= '/{}:project'.format(NS
)
41 XPATH_LEN
= len(XPATH
)
45 NS_NAME
= '{}:{}'.format(NS
, NAME
)
48 def create_from_xpath(cls
, xpath
, log
):
49 name
= cls
.from_xpath(xpath
, log
)
53 proj
= ManoProject(log
, name
=name
)
57 def from_xpath(cls
, xpath
, log
):
58 log
.debug("Get project name from {}".format(xpath
));
60 if cls
.XPATH
in xpath
:
61 idx
= xpath
.find(cls
.XPATH
) + cls
.XPATH_LEN
63 msg
= "Project not found in XPATH: {}".format(xpath
)
65 raise ManoProjXpathNoProjErr(msg
)
69 if (len(sub
) < cls
.NAME_LEN
) or (sub
[0] != '['):
70 msg
= "Project name not found in XPath: {}".format(xpath
)
72 raise ManoProjXpathKeyErr(msg
)
75 idx
= sub
.find(cls
.NS_NAME
)
77 idx
= sub
.find(cls
.NAME
)
79 msg
= "Project name not found in XPath: {}".format(xpath
)
81 raise ManoProjXpathKeyErr(msg
)
85 msg
= "XPath is invalid: {}".format(xpath
)
87 raise ManoProjXpathKeyErr(msg
)
89 sub
= sub
[:idx
-1].strip()
91 k
, n
= sub
.split("=", 2)
94 msg
= "Project name is empty in XPath".format(xpath
)
96 raise ManoProjXpathKeyErr (msg
)
98 log
.debug("Found project name {} from XPath {}".
102 except ValueError as e
:
103 msg
= "Project name not found in XPath: {}, exception: {}" \
106 raise ManoProjXpathKeyErr(msg
)
108 def __init__(self
, log
, name
=None):
117 def name(self
, value
):
118 if self
._name
is None:
121 msg
= "Project name already set to {}".format(self
._name
)
123 raise ManoProjNameSetErr(msg
)
125 def set_from_xpath(self
, xpath
):
126 self
.name
= ManoProject
.get_from_xpath(xpath
, self
._log
)