Ejemplo n.º 1
0
 def test_matches(self):
     result = base_utils.args_to_dict(
         ['aBc:DeF', 'SyS=DEf', 'XY_Z:', 'F__o0O=', 'B8r:=:=', '_bAZ_=:=:'])
     self.assertEqual(
         result, {
             'abc': 'DeF',
             'sys': 'DEf',
             'xy_z': '',
             'f__o0o': '',
             'b8r': '=:=',
             '_baz_': ':=:'
         })
Ejemplo n.º 2
0
    def test_unmatches(self):
        # Temporarily shut warning messages from args_to_dict() when an argument
        # doesn't match its pattern.
        logger = logging.getLogger()
        saved_level = logger.level
        logger.setLevel(logging.ERROR)

        try:
            result = base_utils.args_to_dict(['ab-c:DeF', '--SyS=DEf', 'a*=b', 'a*b',
                                         ':VAL', '=VVV', 'WORD'])
            self.assertEqual({}, result)
        finally:
            # Restore level.
            logger.setLevel(saved_level)
Ejemplo n.º 3
0
    def test_unmatches(self):
        # Temporarily shut warning messages from args_to_dict() when an argument
        # doesn't match its pattern.
        logger = logging.getLogger()
        saved_level = logger.level
        logger.setLevel(logging.ERROR)

        try:
            result = base_utils.args_to_dict([
                'ab-c:DeF', '--SyS=DEf', 'a*=b', 'a*b', ':VAL', '=VVV', 'WORD'
            ])
            self.assertEqual({}, result)
        finally:
            # Restore level.
            logger.setLevel(saved_level)
Ejemplo n.º 4
0
def args_to_storage_dict(args):
    """Map args into storage dictionaries.

    This function is to be used (likely) in control files to obtain a storage
    dictionary from command line arguments.

    @param args: a list of arguments as passed to control file.

    @return a tuple (storage_dict, rest_of_args) where storage_dict is a
            dictionary for storage filtering and rest_of_args is a dictionary
            of keys which do not match storage dict keys.
    """
    args_dict = base_utils.args_to_dict(args)
    storage_dict = {}

    # A list of all allowed keys and their type.
    key_list = ('device', 'bus', 'model', 'size', 'fs_uuid', 'fstype',
                'is_mounted', 'mountpoint')

    def set_if_exists(src, dst, key):
        """If |src| has |key| copies its value to |dst|.

        @return True if |key| exists in |src|, False otherwise.
        """
        if key in src:
            dst[key] = src[key]
            return True
        else:
            return False

    for key in key_list:
        if set_if_exists(args_dict, storage_dict, key):
            del args_dict[key]

    # Return the storage dict and the leftovers of the args to be evaluated
    # later.
    return storage_dict, args_dict
Ejemplo n.º 5
0
 def test_matches(self):
     result = base_utils.args_to_dict(['aBc:DeF', 'SyS=DEf', 'XY_Z:',
                                  'F__o0O=', 'B8r:=:=', '_bAZ_=:=:'])
     self.assertEqual(result, {'abc':'DeF', 'sys':'DEf', 'xy_z':'',
                               'f__o0o':'', 'b8r':'=:=', '_baz_':':=:'})
Ejemplo n.º 6
0
 def test_no_args(self):
     result = base_utils.args_to_dict([])
     self.assertEqual({}, result)
Ejemplo n.º 7
0
 def test_no_args(self):
     result = base_utils.args_to_dict([])
     self.assertEqual({}, result)