"Fossies" - the Fresh Open Source Software Archive

Member "murano-8.0.0/murano/dsl/contracts/basic.py" (16 Oct 2019, 3876 Bytes) of package /linux/misc/openstack/murano-8.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "basic.py" see the Fossies "Dox" file reference documentation.

    1 #    Copyright (c) 2016 Mirantis, Inc.
    2 #
    3 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    4 #    not use this file except in compliance with the License. You may obtain
    5 #    a copy of the License at
    6 #
    7 #         http://www.apache.org/licenses/LICENSE-2.0
    8 #
    9 #    Unless required by applicable law or agreed to in writing, software
   10 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   11 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   12 #    License for the specific language governing permissions and limitations
   13 #    under the License.
   14 
   15 import six
   16 
   17 from murano.dsl import contracts
   18 from murano.dsl import dsl_types
   19 from murano.dsl import exceptions
   20 from murano.dsl import helpers
   21 
   22 
   23 class String(contracts.ContractMethod):
   24     name = 'string'
   25 
   26     def transform(self):
   27         if self.value is None:
   28             return None
   29         if isinstance(self.value, six.text_type):
   30             return self.value
   31         if isinstance(self.value, six.string_types) or \
   32                 isinstance(self.value, six.integer_types):
   33             return six.text_type(self.value)
   34         if isinstance(self.value, dsl_types.MuranoObject):
   35             return self.value.object_id
   36         if isinstance(self.value, dsl_types.MuranoObjectInterface):
   37             return self.value.id
   38         raise exceptions.ContractViolationException(
   39             'Value {0} violates string() contract'.format(
   40                 helpers.format_scalar(self.value)))
   41 
   42     def validate(self):
   43         if self.value is None or isinstance(self.value, six.string_types):
   44             return self.value
   45         raise exceptions.ContractViolationException()
   46 
   47     def generate_schema(self):
   48         types = 'string'
   49         if '_notNull' not in self.value:
   50             types = [types] + ['null']
   51 
   52         return {
   53             'type': types
   54         }
   55 
   56 
   57 class Bool(contracts.ContractMethod):
   58     name = 'bool'
   59 
   60     def validate(self):
   61         if self.value is None or isinstance(self.value, bool):
   62             return self.value
   63         raise exceptions.ContractViolationException()
   64 
   65     def transform(self):
   66         if self.value is None:
   67             return None
   68         return True if self.value else False
   69 
   70     def generate_schema(self):
   71         types = 'boolean'
   72         if '_notNull' not in self.value:
   73             types = [types] + ['null']
   74 
   75         return {
   76             'type': types
   77         }
   78 
   79 
   80 class Int(contracts.ContractMethod):
   81     name = 'int'
   82 
   83     def validate(self):
   84         if self.value is None or isinstance(
   85                 self.value, int) and not isinstance(self.value, bool):
   86             return self.value
   87         raise exceptions.ContractViolationException()
   88 
   89     def transform(self):
   90         if self.value is None:
   91             return None
   92         try:
   93             return int(self.value)
   94         except Exception:
   95             raise exceptions.ContractViolationException(
   96                 'Value {0} violates int() contract'.format(
   97                     helpers.format_scalar(self.value)))
   98 
   99     def generate_schema(self):
  100         types = 'integer'
  101         if '_notNull' not in self.value:
  102             types = [types] + ['null']
  103 
  104         return {
  105             'type': types
  106         }
  107 
  108 
  109 class NotNull(contracts.ContractMethod):
  110     name = 'not_null'
  111 
  112     def validate(self):
  113         if self.value is None:
  114             raise exceptions.ContractViolationException(
  115                 'null value violates notNull() contract')
  116         return self.value
  117 
  118     def transform(self):
  119         return self.validate()
  120 
  121     def generate_schema(self):
  122         types = self.value.get('type')
  123         if isinstance(types, list) and 'null' in types:
  124             types.remove('null')
  125             if len(types) == 1:
  126                 types = types[0]
  127             self.value['type'] = types
  128         self.value['_notNull'] = True
  129         return self.value