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
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)
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')
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
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
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
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)
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)
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)
def test_check_dict(self): C.check_dict({'a': 5}) with self.assertRaises(TypeError) as context: C.check_dict(True)
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)