def cliCreate(self,required=False,inList=False): # Object ######## if self.getType() == 'object': result = {} properties = sorted(self['properties'].iteritems(),key=lambda k:k[1]['order'] if 'order' in k[1].keys() else 0) for item in properties: status = '' if 'conditions' in self.keys(): conditions = [cond for cond in self['conditions'] if cond['then_prop'] == item[0]] for cond in conditions: if ( ( cond['if_prop'] in result.keys() and result[cond['if_prop']] in cond['if_val']) or ( cond['if_prop'] not in result.keys() and None in cond['if_val'] ) ): status = cond['then_status'] break if status != 'disabled': item_required = (item[0] in self['required']) if 'required' in self.keys() else False pre_result = item[1].cliCreate(required=item_required) if pre_result is not None: result[item[0]] = pre_result return result # Array ####### elif self.getType() == 'array': result = [] while True: reponse = self['items'].cliCreate(inList=True) if self['items'].getType() in SIMPLE_TYPES: if reponse is not None: result.append(reponse) else: return result elif self['items'].getType() == 'object': if not all(val is None for val in reponse.values()): result.append(reponse) if not Prompt.promptYN('Another {0}?'.format(self['title']),default='n'): return result else: return result # Field ####### elif self.getType() in SIMPLE_TYPES: default = self['default'] if 'default' in self.keys() else None if self.getType() == 'choices': matchObj = re.match(r'^#/choices/(\w+)$',self['$def'],re.M) choices = self['choices'][matchObj.group(1)] default = [item[0] for item in enumerate(self['choices']) if items[1] == default] if default is not None else None else: choices = {} warning = '' while True: description = self['placeholder'] if 'placeholder' in self.keys() else self['description'] if 'description' in self.keys() else self['title'] if inList: description += ' [keep blank to finish]' if self.getType() == 'boolean': result = Prompt.promptYN(self['title'],default=default) else: result = Prompt.promptSingle( description, choix=choices.values(), password=(self.getType() == 'password'), mandatory=required, default=default, warning=warning ) if result == "" or result is None: return default if self.getType() == 'choices': result = choices.keys()[result] try: result = self._convert(result) if result is None: if required: raise Exception else: self.validate(result) return result except: warning='Incorrect answer, {0} expected'.format(self.getType()) elif self.getType() == 'hidden': return self['default'] else: raise Exception
def cliChange(self,json): self.validate(json) # Object ######## if self.getType() == 'object': choices = [] width = len(max([i['title'] for i in self['properties'].values()], key=len)) properties = sorted(self['properties'].iteritems(),key=lambda k:k[1]['order'] if 'order' in k[1].keys() else 0) for key,item in properties: if item.getType() in SIMPLE_TYPES: line = item.display(json[key],width=width,ident='') elif item.getType() == 'array': item_count = unicode(len(json[key])) if key in json.keys() else "0" value = '{0} managed'.format(item_count) line = ("{0:" + unicode(width)+"} - {1}").format(item['title'],value) elif item.getType() == 'hidden': continue else: value = 'Managed' line = ("{0:" + unicode(width)+"} - {1}").format(item['title'],value) choices.append(line) reponse = Prompt.promptChoice(unicode(self['title']),choices,warning='',selected=[],default = None,mandatory=True,multi=False) changed_item = properties[reponse][1] result = changed_item.cliChange(json[properties[reponse][0]]) json.update({properties[reponse][0]:result}) return json # array ######## elif self.getType() == 'array': lines = self.display(json) warning = '\n'.join(lines) choices = ['Add','Delete','Reset all'] reponse = Prompt.promptChoice('** Managed {0}'.format(self['title']),choices,warning=warning,selected=[],default = None,mandatory=True,multi=False) if reponse == 0: # Add element = self['items'].cliCreate() if element is not None: json.append(element) return json elif reponse == 1: # Delete result = Prompt.promptSingle( 'Which {0} must be deleted?'.format(self['items']['title']), choix=[self['items'].display(item) for item in json], password=False, mandatory=True, default=None, warning='' ) del json[result] return json elif reponse == 2: # Reset all del json return [] # Field ######## elif self.getType() in SIMPLE_TYPES: result = self.cliCreate() Prompt.print_question(self['title'] + ' changed!') return result # Hidden ######### elif self.getType() == 'hidden': return self['default']