Example #1
0
    def load(self, lines=None):
        """Parses given lines into the config"""
        if not lines:
            return
        if type(lines) is not list:
            lines = lines.split("\n")

        assert (type(lines) is list)
        possible_continuation = False
        last_option = None
        for line in lines:
            if len(line.strip()) == 0:
                last_option = None
                possible_continuation = False
                continue
            # We can continue only if this line starts with space and
            # the prior line ended in a continuation character (\ or ,)
            if possible_continuation and not line[0].isspace():
                possible_continuation = False
                last_option = None
            line = line.split('#')[0].strip()

            # Check for possible continuation to the next line
            if line.endswith('\\'):
                possible_continuation = True
                line = line[:-1].strip()
            elif line.endswith(','):
                possible_continuation = True

            # Add the option to ourself
            if '=' in line:
                option, value = line.split('=', 1)
                option = option.strip()
                if option:
                    last_option = option
                    self.set(option, value.strip())

            # Line continues from previous, just append to prior value
            elif possible_continuation and last_option:
                old_value = self.get(last_option)
                if type(old_value) is list:
                    old_value.extend(to_list(line))
                else:
                    old_value += " " + line
                self.set(last_option, old_value)

            # Import another config file
            elif line[:8] == 'include ':
                filename = line[8:].strip()
                lines = load_file(filename)
                self.load(lines)
                possible_continuation = False
                last_option = None
Example #2
0
    def load(self, lines=None):
        """Parses given lines into the config"""
        if not lines:
            return
        if type(lines) is not list:
            lines = lines.split("\n")

        assert(type(lines) is list)
        possible_continuation = False
        last_option = None
        for line in lines:
            if len(line.strip()) == 0:
                last_option = None
                possible_continuation = False
                continue
            # We can continue only if this line starts with space and
            # the prior line ended in a continuation character (\ or ,)
            if possible_continuation and not line[0].isspace():
                possible_continuation = False
                last_option = None
            line = line.split('#')[0].strip()

            # Check for possible continuation to the next line
            if line.endswith('\\'):
                possible_continuation = True
                line = line[:-1].strip()
            elif line.endswith(','):
                possible_continuation = True

            # Add the option to ourself
            if '=' in line:
                option, value = line.split('=', 1)
                option = option.strip()
                if option:
                    last_option = option
                    self.set(option, value.strip())

            # Line continues from previous, just append to prior value
            elif possible_continuation and last_option:
                old_value = self.get(last_option)
                if type(old_value) is list:
                    old_value.extend(to_list(line))
                else:
                    old_value += " " + line
                self.set(last_option, old_value)

            # Import another config file
            elif line[:8] == 'include ':
                filename = line[8:].strip()
                lines = load_file(filename)
                self.load(lines)
                possible_continuation = False
                last_option = None
Example #3
0
 def _recurse_set(parent_dict, fields, value):
     field = fields[0]
     if len(fields) == 1:
         if type(parent_dict.get(field, None)) is list:
             parent_dict[field] = to_list(value)
         elif type(parent_dict.get(field, None)) is bool:
             parent_dict[field] = to_bool(value)
         else:
             parent_dict[field] = value
         return
     if field not in parent_dict:
         parent_dict[field] = {}
     elif type(parent_dict[field]) is not dict:
         buf = parent_dict[field]
         parent_dict[field] = {'': buf}
     _recurse_set(parent_dict[field], fields[1:], value)
Example #4
0
 def _recurse_set(parent_dict, fields, value):
     field = fields[0]
     if len(fields) == 1:
         if type(parent_dict.get(field,None)) is list:
             parent_dict[field] = to_list(value)
         elif type(parent_dict.get(field,None)) is bool:
             parent_dict[field] = to_bool(value)
         else:
             parent_dict[field] = value
         return
     if field not in parent_dict:
         parent_dict[field] = {}
     elif type(parent_dict[field]) is not dict:
         buf = parent_dict[field]
         parent_dict[field] = {'': buf}
     _recurse_set(parent_dict[field], fields[1:], value)
Example #5
0
    def copy(self, obj):
        """Copies contents of another config object"""
        if obj is None or len(obj.__dict__.keys()) < 1:
            return

        for key, value in obj.__dict__.items():
            if key.startswith('_'):
                continue
            old_value = self.__dict__.get(key, None)
            if old_value is not None and type(old_value) != type(value):
                if type(old_value) is list and type(value) is str:
                    value = to_list(value)
                else:
                    raise InvalidConfig("key %s (type %s) given for a type %s" %(
                            key, type(value), type(old_value)))
            self.__dict__[key] = value
Example #6
0
    def copy(self, obj):
        """Copies contents of another config object"""
        if obj is None or len(obj.__dict__.keys()) < 1:
            return

        for key, value in obj.__dict__.items():
            if key.startswith('_'):
                continue
            old_value = self.__dict__.get(key, None)
            if old_value is not None and type(old_value) != type(value):
                if type(old_value) is list and type(value) is str:
                    value = to_list(value)
                else:
                    raise InvalidConfig(
                        "key %s (type %s) given for a type %s" %
                        (key, type(value), type(old_value)))
            self.__dict__[key] = value
Example #7
0
 def test_too_many_args(self):
     _list = [1, 2, 3, 4, 5, 6]
     with self.assertRaises(TypeError):
         to_list(*_list)
Example #8
0
 def test_too_few_args(self):
     _list = [1, 2, 3, 4]
     with self.assertRaises(TypeError):
         to_list(*_list)
Example #9
0
 def test_5_random_ints(self):
     _list = [random.randint(0, 100) for _ in range(5)]
     self.assertEqual(_list, to_list(*_list))
Example #10
0
 def test_instructions_example(self):
     self.assertEqual(to_list('10H', 'JH', 'QH', 'KH', 'AH'),
                      ['10H', 'JH', 'QH', 'KH', 'AH'])
Example #11
0
def literalize_simple(asker, list):
    import lists
    return ''.join(to_char(asker, c) for c in lists.to_list(asker, list))