def remove_command(args, syspurposestore): """ Uses the syspurposestore to remove one or more values from a particular property. :param args: The parsed args from argparse, expected attributes are: prop_name: the string name of the property to add to values: A list of the values to remove from the given property (could be anything json-serializable) :param syspurposestore: An SyspurposeStore object to manipulate :return: None """ for value in args.values: if syspurposestore.remove(args.prop_name, value): print( _("Removed \"{val}\" from {name}.").format( val=make_utf8(value), name=make_utf8(args.prop_name))) else: print( _("Not removing value \"{val}\" from {name}; it was not there." ).format(val=make_utf8(value), name=make_utf8(args.prop_name))) return success_msg = _("{attr} updated.").format(attr=make_utf8(args.prop_name)) to_remove = "".join(args.values) command = "syspurpose remove-{name} ".format( name=args.prop_name) + to_remove check_result(syspurposestore, expectation=lambda res: all(x not in res.get('addons', []) for x in args.values), success_msg=success_msg, command=command, attr=args.prop_name)
def set(self, key, value): """ Set a key (syspurpose parameter) to value :param key: The parameter of the syspurpose file to set :type key: str :param value: The value to set that parameter to :return: Whether any change was made """ value = make_utf8(value) key = make_utf8(key) current_value = make_utf8(self.local_contents.get(key, None)) self.local_contents[key] = value if current_value != value or current_value is None: self._check_key_value_validity(key, value) self.changed = True log.debug('Setting value \'%s\' to key \'%s\'.' % (value, key)) else: log.debug('NOT Setting value \'%s\' to key \'%s\'.') self.changed = current_value != value or current_value is None # Write changes to the syspurpose.json file if self.changed is True: self._write_local() return self.changed
def add(self, key, value): """ Add a value to a list of values specified by key. If the current value specified by the key is scalar/non-list, it is not overridden, but maintained in the list, along with the new value. :param key: The name of the list :param value: The value to append to the list :return: None """ value = make_utf8(value) key = make_utf8(key) try: current_value = self.local_contents[key] if current_value is not None and not isinstance( current_value, list): self.local_contents[key] = [current_value] if self.local_contents[key] is None: self.local_contents[key] = [] if value not in self.local_contents[key]: self.local_contents[key].append(value) else: log.debug('Will not add value \'%s\' to key \'%s\'.' % (value, key)) return False except (AttributeError, KeyError): self.local_contents[key] = [value] self._check_key_value_validity(key, value) self.changed = True log.debug('Adding value \'%s\' to key \'%s\'.' % (value, key)) return True
def add(self, key, value): """ Add a value to a list of values specified by key. If the current value specified by the key is scalar/non-list, it is not overridden, but maintained in the list, along with the new value. :param key: The name of the list :param value: The value to append to the list :return: None """ value = make_utf8(value) key = make_utf8(key) try: current_value = self.contents[key] if current_value is not None and not isinstance( current_value, list): self.contents[key] = [current_value] if self.contents[key] is None: self.contents[key] = [] if value not in self.contents[key]: self.contents[key].append(value) else: return False except (AttributeError, KeyError): self.contents[key] = [value] return True
def remove_command(args, syspurposestore): """ Uses the syspurposestore to remove one or more values from a particular property. :param args: The parsed args from argparse, expected attributes are: prop_name: the string name of the property to add to values: A list of the values to remove from the given property (could be anything json-serializable) :param syspurposestore: An SyspurposeStore object to manipulate :return: None """ for value in args.values: if syspurposestore.remove(args.prop_name, value): print(_("Removed {} from {}.").format(make_utf8(value), make_utf8(args.prop_name))) else: print(_("Not removing value {} from {}; it was not there.").format(make_utf8(value), make_utf8(args.prop_name))) return success_msg = _("{attr} updated.").format(attr=make_utf8(args.prop_name)) to_remove = "".join(args.values) command = "syspurpose remove-{name} ".format(name=args.prop_name) + to_remove check_result( syspurposestore, expectation=lambda res: all(x not in res.get('addons', []) for x in args.values), success_msg=success_msg, command=command, attr=args.prop_name )
def remove(self, key, value): """ Remove a value from a list specified by key. If the current value specified by the key is not a list, unset the value. :param key: The name of the list parameter to manipulate :param value: The value to attempt to remove :return: True if the value was in the list, False if it was not """ value = make_utf8(value) key = make_utf8(key) try: current_value = self.local_contents[key] if current_value is not None and not isinstance( current_value, list) and current_value == value: return self.unset(key) if value in current_value: self.local_contents[key].remove(value) else: return False self.changed = True log.debug('Removing value \'%s\' from key \'%s\'.' % (value, key)) return True except (AttributeError, KeyError, ValueError): log.debug('Will not remove value \'%s\' from key \'%s\'.' % (value, key)) return False
def remove(self, key, value): """ Remove a value from a list specified by key. If the current value specified by the key is not a list, unset the value. :param key: The name of the list parameter to manipulate :param value: The value to attempt to remove :return: True if the value was in the list, False if it was not """ value = make_utf8(value) key = make_utf8(key) try: current_value = self.local_contents[key] if current_value is not None and not isinstance(current_value, list) and current_value == value: return self.unset(key) if value in current_value: self.local_contents[key].remove(value) else: return False self.changed = True log.debug('Removing value \'%s\' from key \'%s\'.' % (value, key)) return True except (AttributeError, KeyError, ValueError): log.debug('Will not remove value \'%s\' from key \'%s\'.' % (value, key)) return False
def add_command(args, syspurposestore): """ Uses the syspurposestore to add one or more values to a particular property. :param args: The parsed args from argparse, expected attributes are: prop_name: the string name of the property to add to values: A list of the values to add to the given property (could be anything json-serializable) :param syspurposestore: An SyspurposeStore object to manipulate :return: None """ any_prop_added = False for value in args.values: if syspurposestore.add(args.prop_name, value): any_prop_added = True print( _("Added {value} to {prop_name}.").format( value=make_utf8(value), prop_name=make_utf8(args.prop_name))) else: print( _("Not adding value {value} to {prop_name}; it already exists." ).format(value=make_utf8(value), prop_name=make_utf8(args.prop_name))) if any_prop_added is False: return success_msg = _("{attr} updated.").format(attr=make_utf8(args.prop_name)) to_add = "".join(args.values) command = "syspurpose add-{name} ".format(name=args.prop_name) + to_add check_result(syspurposestore, expectation=lambda res: all(x in res.get(args.prop_name, []) for x in args.values), success_msg=success_msg, command=command, attr=args.prop_name)
def add(self, key, value): """ Add a value to a list of values specified by key. If the current value specified by the key is scalar/non-list, it is not overridden, but maintained in the list, along with the new value. :param key: The name of the list :param value: The value to append to the list :return: None """ value = make_utf8(value) key = make_utf8(key) try: current_value = self.local_contents[key] if current_value is not None and not isinstance(current_value, list): self.local_contents[key] = [current_value] if self.local_contents[key] is None: self.local_contents[key] = [] if value not in self.local_contents[key]: self.local_contents[key].append(value) else: log.debug('Will not add value \'%s\' to key \'%s\'.' % (value, key)) return False except (AttributeError, KeyError): self.local_contents[key] = [value] self.changed = True log.debug('Adding value \'%s\' to key \'%s\'.' % (value, key)) return True
def set_command(args, syspurposestore): """ Uses the syspurposestore to set the prop_name to value. :param args: The parsed args from argparse, expected attributes are: prop_name: the string name of the property to set value: An object to set the property to (could be anything json-serializable) :param syspurposestore: An SyspurposeStore object to manipulate :return: None """ syspurposestore.set(args.prop_name, args.value) print( _("{} set to {}").format(make_utf8(args.prop_name), make_utf8(args.value)))
def set(self, key, value): """ Set a key (syspurpose parameter) to value :param key: The parameter of the syspurpose file to set :type key: str :param value: The value to set that parameter to :return: Whether any change was made """ value = make_utf8(value) key = make_utf8(key) org = make_utf8(self.contents.get(key, None)) self.contents[key] = value return org != value or org is None
def unset(self, key): """ Unsets a key :param key: The key to unset :return: boolean """ key = make_utf8(key) # Special handling is required for the SLA, since it deviates from the typical CP # empty => null semantics if key == 'service_level_agreement': value = self.local_contents.get(key, None) self.local_contents[key] = '' elif key == 'addons': value = self.local_contents.get(key, None) self.local_contents[key] = [] else: value = self.local_contents.pop(key, None) self.changed = True log.debug('Unsetting value \'%s\' of key \'%s\'.' % (value, key)) self.changed = value is not None # Write changes to the syspurpose.json file if self.changed is True: self._write_local() return self.changed
def add_command(args, syspurposestore): """ Uses the syspurposestore to add one or more values to a particular property. :param args: The parsed args from argparse, expected attributes are: prop_name: the string name of the property to add to values: A list of the values to add to the given property (could be anything json-serializable) :param syspurposestore: An SyspurposeStore object to manipulate :return: None """ for value in args.values: if syspurposestore.add(args.prop_name, value): print( _("Added {} to {}.").format(make_utf8(value), make_utf8(args.prop_name))) else: print( _("Not adding value {} to {}; it already exists.").format( make_utf8(value), make_utf8(args.prop_name)))
def remove_command(args, syspurposestore): """ Uses the syspurposestore to remove one or more values from a particular property. :param args: The parsed args from argparse, expected attributes are: prop_name: the string name of the property to add to values: A list of the values to remove from the given property (could be anything json-serializable) :param syspurposestore: An SyspurposeStore object to manipulate :return: None """ for value in args.values: if syspurposestore.remove(args.prop_name, value): print( _("Removed {} from {}.").format(make_utf8(value), make_utf8(args.prop_name))) else: print( _("Not removing value {} from {}; it was not there.").format( make_utf8(value), make_utf8(args.prop_name)))
def add(self, key, value): """ Add a value to a list of values specified by key. If the current value specified by the key is scalar/non-list, it is not overridden, but maintained in the list, along with the new value. :param key: The name of the list :param value: The value to append to the list :return: None """ value = make_utf8(value) key = make_utf8(key) try: # When existing value was set using set() method, then the # existing valus is not list, but simple value. We have to convert # it first current_value = self.local_contents[key] if current_value is not None and not isinstance( current_value, list): self.local_contents[key] = [current_value] # When existing value is None, then first covert to empty list to be # able to call append method. It is very theoretical case. if self.local_contents[key] is None: self.local_contents[key] = [] if value not in self.local_contents[key]: self.local_contents[key].append(value) else: log.debug('Will not add value \'%s\' to key \'%s\'.' % (value, key)) self.changed = False return self.changed except (AttributeError, KeyError): self.local_contents[key] = [value] self._check_key_value_validity(key, value) self.changed = True log.debug('Adding value \'%s\' to key \'%s\'.' % (value, key)) # Write changes to the syspurpose.json file if self.changed is True: self._write_local() return self.changed
def set(self, key, value): """ Set a key (syspurpose parameter) to value :param key: The parameter of the syspurpose file to set :type key: str :param value: The value to set that parameter to :return: Whether any change was made """ value = make_utf8(value) key = make_utf8(key) org = make_utf8(self.local_contents.get(key, None)) self.local_contents[key] = value if org != value or org is None: self.changed = True log.debug('Setting value \'%s\' to key \'%s\'.' % (value, key)) return org != value or org is None
def set_command(args, syspurposestore): """ Uses the syspurposestore to set the prop_name to value. :param args: The parsed args from argparse, expected attributes are: prop_name: the string name of the property to set value: An object to set the property to (could be anything json-serializable) :param syspurposestore: An SyspurposeStore object to manipulate :return: None """ syspurposestore.set(args.prop_name, args.value) success_msg = _("{attr} set to \"{val}\".").format( attr=make_utf8(args.prop_name), val=make_utf8(args.value)) check_result(syspurposestore, expectation=lambda res: res.get(args.prop_name) == args.value, success_msg=success_msg, command="syspurpose set {name} \"{val}\"".format( name=args.prop_name, val=args.value), attr=args.prop_name)
def unset_command(args, syspurposestore): """ Uses the syspurposestore to unset (clear entirely) the prop_name. :param args: The parsed args from argparse, expected attributes are: prop_name: the string name of the property to unset (clear) :param syspurposestore: An SyspurposeStore object to manipulate :return: None """ syspurposestore.unset(args.prop_name) print(_("{} unset").format(make_utf8(args.prop_name)))
def unset(self, key): """ Unsets a key :param key: The key to unset :return: boolean """ key = make_utf8(key) org = self.contents.get(key, None) if org is not None: self.contents[key] = None return org is not None
def remove(self, key, value): """ Remove a value from a list specified by key. If the current value specified by the key is not a list, unset the value. :param key: The name of the list parameter to manipulate :param value: The value to attempt to remove :return: True if the value was in the list, False if it was not """ value = make_utf8(value) key = make_utf8(key) try: current_value = self.contents[key] if current_value is not None and not isinstance(current_value, list) and current_value == value: self.contents[key] = None return True if value in self.contents[key]: self.contents[key].remove(value) else: return False return True except (AttributeError, KeyError, ValueError): return False
def remove(self, key, value): """ Remove a value from a list specified by key. If the current value specified by the key is not a list, unset the value. :param key: The name of the list parameter to manipulate :param value: The value to attempt to remove :return: True if the value was in the list, False if it was not """ value = make_utf8(value) key = make_utf8(key) try: current_value = self.contents[key] if current_value is not None and not isinstance( current_value, list) and current_value == value: self.contents[key] = None return True if value in self.contents[key]: self.contents[key].remove(value) else: return False return True except (AttributeError, KeyError, ValueError): return False
def unset(self, key): """ Unsets a key :param key: The key to unset :return: boolean """ key = make_utf8(key) # Special handling is required for the SLA, since it deviates from the typical CP # empty => null semantics if key == 'service_level_agreement': value = self.contents.get(key, None) self.contents[key] = '' else: value = self.contents.pop(key, None) return value is not None
def unset_command(args, syspurposestore): """ Uses the syspurposestore to unset (clear entirely) the prop_name. :param args: The parsed args from argparse, expected attributes are: prop_name: the string name of the property to unset (clear) :param syspurposestore: An SyspurposeStore object to manipulate :return: None """ syspurposestore.unset(args.prop_name) success_msg = _("{attr} unset.").format(attr=make_utf8(args.prop_name)) check_result( syspurposestore, expectation=lambda res: res.get(args.prop_name) in ["", None, []], success_msg=success_msg, command="syspurpose unset {name}".format(name=args.prop_name), attr=args.prop_name)
def unset_command(args, syspurposestore): """ Uses the syspurposestore to unset (clear entirely) the prop_name. :param args: The parsed args from argparse, expected attributes are: prop_name: the string name of the property to unset (clear) :param syspurposestore: An SyspurposeStore object to manipulate :return: None """ syspurposestore.unset(args.prop_name) success_msg = _("{attr} unset.").format(attr=make_utf8(args.prop_name)) check_result( syspurposestore, expectation=lambda res: res.get(args.prop_name) in ["", None, []], success_msg=success_msg, command="syspurpose unset {name}".format(args.prop_name), attr=args.prop_name )
def _read_file(self, file_contents=None, expected_contents=None): """ Utility method for logic common to the *read_file* tests. :param file_contents: :param expected_contents: :return: None """ temp_dir = os.path.join(self._mktmp(), 'syspurpose_file.json') with io.open(temp_dir, 'w', encoding='utf-8') as f: if file_contents and not isinstance(file_contents, str): utils.write_to_file_utf8(f, file_contents) else: f.write(utils.make_utf8(file_contents or '')) f.flush() self.assertTrue(os.path.exists(temp_dir), "Unable to create test file in temp dir") # Actually do the test syspurpose_store = self.assertRaisesNothing(files.SyspurposeStore, temp_dir) self.assertRaisesNothing(syspurpose_store.read_file) self.assertEqual(syspurpose_store.contents, expected_contents)
def set_command(args, syspurposestore): """ Uses the syspurposestore to set the prop_name to value. :param args: The parsed args from argparse, expected attributes are: prop_name: the string name of the property to set value: An object to set the property to (could be anything json-serializable) :param syspurposestore: An SyspurposeStore object to manipulate :return: None """ syspurposestore.set(args.prop_name, args.value) success_msg = _("{attr} set to \"{val}\".").format(attr=make_utf8(args.prop_name), val=make_utf8(args.value)) check_result( syspurposestore, expectation=lambda res: res.get(args.prop_name) == args.value, success_msg=success_msg, command="syspurpose set {name} {val}".format(name=args.prop_name, val=args.value), attr=args.prop_name )
def unset(self, key): """ Unsets a key :param key: The key to unset :return: boolean """ key = make_utf8(key) # Special handling is required for the SLA, since it deviates from the typical CP # empty => null semantics if key == 'service_level_agreement': value = self.local_contents.get(key, None) self.local_contents[key] = '' elif key == 'addons': value = self.local_contents.get(key, None) self.local_contents[key] = [] else: value = self.local_contents.pop(key, None) self.changed = True log.debug('Unsetting value \'%s\' of key \'%s\'.' % (value, key)) return value is not None