Пример #1
0
    def replace(clazz,
                filename,
                replacements,
                backup=True,
                word_boundary=False,
                word_boundary_chars=None):
        check.check_string(filename)
        check.check_dict(replacements, check.STRING_TYPES, check.STRING_TYPES)
        check.check_bool(backup)
        check.check_bool(word_boundary)
        check.check_set(word_boundary_chars, allow_none=True)

        content = file_util.read(filename, codec='utf-8')
        new_content = text_replace.replace(
            content,
            replacements,
            word_boundary=word_boundary,
            word_boundary_chars=word_boundary_chars)
        if content == new_content:
            return False
        if backup:
            file_util.backup(filename)
        file_util.save(filename,
                       content=new_content.encode('utf-8'),
                       mode=file_util.mode(filename))
        return True
Пример #2
0
    def write_values(clazz, filename, stream_name, values):
        'Write the content of stream_name for filename as values.'
        filename = file_check.check_file(filename)
        stream_name = clazz.check_stream_name(stream_name)
        check.check_dict(values)

        value = pickle.dumps(values)
        clazz.write_stream(filename, stream_name, value)
        assert value == clazz.read_stream(filename, stream_name)
Пример #3
0
    def run(self, options, values):
        'Run the task.'
        check.check_computer_setup_options(options)
        check.check_dict(values)

        assert '_db' in values
        db = values['_db']
        if not 'name' in values:
            raise computer_setup_error('Value not found: "name"')
        db['name'] = values.get('name')
Пример #4
0
    def is_needed(self, options, values):
        'Return True of the task needs to run.'
        check.check_computer_setup_options(options)
        check.check_dict(values)

        assert '_db' in values
        db = values['_db']
        if not 'name' in values:
            raise computer_setup_error('Value not found: "name"')
        actual_name = db.get('name', None)
        expected_name = values.get('name')
        return actual_name != expected_name
Пример #5
0
    def replace(clazz,
                s,
                replacements,
                word_boundary=False,
                word_boundary_chars=None):
        'Replace all instances of dict d in string s.'
        check.check_string(s)
        check.check_dict(replacements, check.STRING_TYPES, check.STRING_TYPES)
        check.check_bool(word_boundary)
        check.check_set(word_boundary_chars, allow_none=True)

        for src_string, dst_string in replacements.items():
            s = clazz.replace_all(s,
                                  src_string,
                                  dst_string,
                                  word_boundary=word_boundary,
                                  word_boundary_chars=word_boundary_chars)
        return s
Пример #6
0
 def _process_main(self, location, niceness_level, timeout):
     #os.nice(20)
     terminated = False
     self.log_i('trash process starts')
     while True:
         if terminated:
             break
         self._delete_one()
         try:
             payload = self._queue.get(timeout=timeout)
             check.check_dict(payload)
             command = payload['command']
             if command == 'terminate':
                 self.log_i('got terminate command')
                 terminated = True
             elif command == 'wakeup':
                 self.log_i('got wakeup command')
                 pass
         except Queue.Empty as ex:
             self.log_d('caught Queue.Empty exception')
             pass
     self.log_i('trash process ends')
     return 0
Пример #7
0
    def __new__(clazz, label, title, attributes):
        check.check_string(label)
        check.check_string(title)
        check.check_dict(attributes, check.STRING_TYPES)

        return clazz.__bases__[0].__new__(clazz, label, title, attributes)
Пример #8
0
 def test_check_dict_and_value(self):
     C.check_dict({'a': 5, 'b': 6}, value_type=compat.INTEGER_TYPES)
     with self.assertRaises(TypeError) as context:
         C.check_dict({'a': 5, 6: 'b'}, value_type=compat.INTEGER_TYPES)
Пример #9
0
 def test_check_dict_and_key(self):
     C.check_dict({'a': 5, 'b': 6}, key_type=compat.STRING_TYPES)
     with self.assertRaises(TypeError) as context:
         C.check_dict({'a': 5, 6: 'b'}, key_type=compat.STRING_TYPES)
Пример #10
0
 def test_check_dict(self):
     C.check_dict({'a': 5})
     with self.assertRaises(TypeError) as context:
         C.check_dict(True)
Пример #11
0
  def __init__(self, values):
    check.check_dict(values)

    self._values = self._cpu_info_values()
    for key, value in values.items():
      setattr(self._values, key, value)