def test_include_wrong_file(self): """test wrong include""" include_conf = conf.Configure2Dict('./conf/test.include.wrong.conf') include_dict = include_conf.get_dict(ignore_error=True) ut.assert_in('./conf/test.included.conf', include_dict['$include'].keys()) in_confdict = include_dict['$include']['./conf/test.included.conf'] ut.assert_eq(in_confdict['layer_included']['layer1-option00'], 'layer1-option00') conf.Dict2Configure(include_dict).write_conf( './conf/test.include.write') try: include_dict = include_conf.get_dict(ignore_error=False) ut.assert_eq('should raise IOError', 1) except IOError as _: pass
def test_arrary_special_chars(self): """test arrary""" # test conf arrary list_conf = conf.Configure2Dict('./conf/test.list.conf', False).get_dict() ut.assert_eq(len(list_conf['arrary_test']['a']), 4) ut.assert_eq(len(list_conf['arrary_test']['b']), 2) ut.assert_eq(len(list_conf['arrary_test']['c']), 1) # test special chars ut.assert_eq(list_conf['sepecial_chars']['with_back_slash_d'], r'^/((home(/disk\d+)*)|tmp)/(normandy|work|mapred)/.*') # test disorder of arrays confdict = conf.Configure2Dict('./conf/conf_mock.conf', False).get_dict() ut.assert_eq('123' in confdict['test']['nice']['prefix'], True) ut.assert_eq(len(confdict['test']['nice']['prefix']), 2) newdict = { 'minghao': 'abc', 'liuxuan': [1, 2, 3, 4], 'layer1': { 'layer2': 'abc', 'layer2-array': [{ 'key-1': 1 }, { 'key-1': 2 }], }, 'layer1-array': [{ 'key-1': 1 }, { 'key-1': 2 }], 'zksdfjk': 'abc', 'kehyrj': 1, 'test': 'abcdefg\d' } confdict['layer2']['newtest'] = newdict confobj = conf.Dict2Configure(confdict) confobj.write_conf('./conf/test.conf') test_dict = conf.Configure2Dict('./conf/test.conf').get_dict() print('test_comments:', confdict['test_comments'])
def cleanup_oldlost(self, dump_file=None): """ cleanup old lost devices. :param dump_file: if dump_file is not None, we will store devices info into dump_file Otherwise, we will cleanup the lost devices only. """ self._lock.acquire() log.info('start - empty_lost devices, dump_file:%s' % dump_file) if self._lost_devices is None: log.info('end - does not keep_lost devices, return') self._lock.release() return if dump_file is None: self._lost_devices = {} log.info('end - does not have dump_file, return') self._lock.release() return info_dict = {} info_dict['devices'] = {} if len(self._lost_devices) != 0: info_dict['devices']['lost'] = [] info_dict['devices']['lost_num'] = len(self._lost_devices) else: info_dict['devices']['lost_num'] = 0 for dkey in self._lost_devices.keys(): try: tmp_dict = {} tmp_dict['key'] = dkey tmp_dict['last_healthy'] = self._devices[ dkey].get_last_healthy() del self._lost_devices[dkey] log.info('end - empty_lost devices') info_dict['devices']['lost'].append(tmp_dict) except KeyError as error: log.warn('failed to dump lost_file, error:%s' % str(error)) conf_writer = conf.Dict2Configure(info_dict) conf_writer.write_conf(dump_file) self._lock.release() return
'liuxuan': [1, 2, 3, 4], 'layer1': { 'layer2': 'abc', 'layer2-array': [{ 'key-1': 1 }, { 'key-1': 2 }], }, 'layer1-array': [{ 'key-1': 1 }, { 'key-1': 2 }], 'zksdfjk': 'abc', 'kehyrj': 1 } confdict['layer2']['newtest'] = newdict confobj = conf.Dict2Configure(confdict) confobj.write_conf('./test.conf') newconfdict = {} newconfdict['test'] = '123' newconfdict['arr'] = [1, 2, 3, 4] confobj = conf.Dict2Configure(newconfdict) confobj.write_conf('./test_normal.conf') # vi:set tw=0 ts=4 sw=4 nowrap fdm=indent