Ejemplo n.º 1
0
	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']