| Philip Joseph | 0f5e8c0 | 2017-03-03 01:54:51 +0530 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | # |
| 4 | # Copyright 2017 RIFT.IO Inc |
| 5 | # |
| 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 |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 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. |
| 17 | # |
| 18 | |
| 19 | |
| 20 | class ManoProjectError(Exception): |
| 21 | pass |
| 22 | |
| 23 | |
| 24 | class ManoProjNameSetErr(ManoProjectError): |
| 25 | pass |
| 26 | |
| 27 | |
| 28 | class ManoProjXpathNoProjErr(ManoProjectError): |
| 29 | pass |
| 30 | |
| 31 | |
| 32 | class ManoProjXpathKeyErr(ManoProjectError): |
| 33 | pass |
| 34 | |
| 35 | |
| 36 | class ManoProject(object): |
| 37 | '''Class to handle the project name''' |
| 38 | |
| 39 | NS = 'rw-project' |
| 40 | XPATH = '/{}:project'.format(NS) |
| 41 | XPATH_LEN = len(XPATH) |
| 42 | |
| 43 | NAME = 'name' |
| 44 | NAME_LEN = len(NAME) |
| 45 | NS_NAME = '{}:{}'.format(NS, NAME) |
| 46 | |
| 47 | @classmethod |
| 48 | def create_from_xpath(cls, xpath, log): |
| 49 | name = cls.from_xpath(xpath, log) |
| 50 | if name is None: |
| 51 | return None |
| 52 | |
| 53 | proj = ManoProject(log, name=name) |
| 54 | return proj |
| 55 | |
| 56 | @classmethod |
| 57 | def from_xpath(cls, xpath, log): |
| 58 | log.debug("Get project name from {}".format(xpath)); |
| 59 | |
| 60 | if cls.XPATH in xpath: |
| 61 | idx = xpath.find(cls.XPATH) + cls.XPATH_LEN |
| 62 | if idx == -1: |
| 63 | msg = "Project not found in XPATH: {}".format(xpath) |
| 64 | log.error(msg) |
| 65 | raise ManoProjXpathNoProjErr(msg) |
| 66 | |
| 67 | sub = xpath[idx:] |
| 68 | sub = sub.strip() |
| 69 | if (len(sub) < cls.NAME_LEN) or (sub[0] != '['): |
| 70 | msg = "Project name not found in XPath: {}".format(xpath) |
| 71 | log.error(msg) |
| 72 | raise ManoProjXpathKeyErr(msg) |
| 73 | |
| 74 | sub = sub[1:].strip() |
| 75 | idx = sub.find(cls.NS_NAME) |
| 76 | if idx == -1: |
| 77 | idx = sub.find(cls.NAME) |
| 78 | if idx != 0: |
| 79 | msg = "Project name not found in XPath: {}".format(xpath) |
| 80 | log.error(msg) |
| 81 | raise ManoProjXpathKeyErr(msg) |
| 82 | |
| 83 | idx = sub.find(']') |
| 84 | if idx == -1: |
| 85 | msg = "XPath is invalid: {}".format(xpath) |
| 86 | log.error(msg) |
| 87 | raise ManoProjXpathKeyErr(msg) |
| 88 | |
| 89 | sub = sub[:idx-1].strip() |
| 90 | try: |
| 91 | k, n = sub.split("=", 2) |
| 92 | name = n.strip() |
| 93 | if name is None: |
| 94 | msg = "Project name is empty in XPath".format(xpath) |
| 95 | log.error(msg) |
| 96 | raise ManoProjXpathKeyErr (msg) |
| 97 | |
| 98 | log.debug("Found project name {} from XPath {}". |
| 99 | format(name, xpath)) |
| 100 | return name |
| 101 | |
| 102 | except ValueError as e: |
| 103 | msg = "Project name not found in XPath: {}, exception: {}" \ |
| 104 | .format(xpath, e) |
| 105 | log.exception(msg) |
| 106 | raise ManoProjXpathKeyErr(msg) |
| 107 | |
| 108 | def __init__(self, log, name=None): |
| 109 | self._log = log |
| 110 | self._name = name |
| 111 | |
| 112 | @property |
| 113 | def name(self): |
| 114 | return self._name |
| 115 | |
| 116 | @name.setter |
| 117 | def name(self, value): |
| 118 | if self._name is None: |
| 119 | self._name = value |
| 120 | else: |
| 121 | msg = "Project name already set to {}".format(self._name) |
| 122 | self._log.error(msg) |
| 123 | raise ManoProjNameSetErr(msg) |
| 124 | |
| 125 | def set_from_xpath(self, xpath): |
| 126 | self.name = ManoProject.get_from_xpath(xpath, self._log) |