def load_data(self, file): """ This method load data from file :param file: object file. """ data = self.get_file_module(config.AppConfiguration().get_file_type()) return data.deserialization(file)
def test_yaml_format(self): """ This function tests yaml file. :return: nothing. """ import yaml # test load file = io.StringIO(yaml.dump(self.test_data)) config.AppConfiguration().set_file_type("yaml") self.assertEqual(self.test_data, storage.Storage().load_data(file)) # test save config.AppConfiguration().set_file_type("yaml") self.assertEqual( self.test_data, storage.Storage().save_data(io.StringIO(), self.test_data))
def test_json_format(self): """ This function tests json file. :return: nothing """ import json # test load file = io.StringIO(json.dumps(self.test_data)) config.AppConfiguration().set_file_type("json") self.assertEqual(self.test_data, storage.Storage().load_data(file)) # test save config.AppConfiguration().set_file_type("json") self.assertEqual( self.test_data, storage.Storage().save_data(io.StringIO(), self.test_data))
def save_data(self, file, data): """ This method save data to file. :param file: object file. :param data: data """ f = self.get_file_module(config.AppConfiguration().get_file_type()) return f.serialization(file, data)
def test_pickle_format(self): """ This function tests pickle file. :return: nothing. """ import pickle # test load config.AppConfiguration().set_file_type("pickle") self.assertEqual( self.test_data, storage.Storage().load_data( io.BytesIO(pickle.dumps(self.test_data)))) # test save config.AppConfiguration().set_file_type("pickle") self.assertEqual( self.test_data, storage.Storage().save_data(io.BytesIO(), self.test_data))
def get_data_from_file(self): """ This function loads data from file, which you choose. :return: nothing. """ i = 0 while True: i = self.user_input(lambda: input("1 - Load from *.pickle \n" "2 - Load from *.yaml \n" "3 - Load from *.json \n")) if i == 1: config.AppConfiguration().set_file_type("pickle") return storage.Storage().load_data( open("database." + config.AppConfiguration().get_file_type(), 'rb')) elif i == 2: config.AppConfiguration().set_file_type("yaml") return storage.Storage().load_data( open("database." + config.AppConfiguration().get_file_type(), 'rb')) elif i == 3: config.AppConfiguration().set_file_type("json") return storage.Storage().load_data( open("database." + config.AppConfiguration().get_file_type(), 'r'))
def save_data_into_file(data): """ This function save data into file, which format you choose. :param data: data. :return: nothing. """ if config.AppConfiguration().get_file_type() == "pickle": storage.Storage().save_data( open("database." + config.AppConfiguration().get_file_type(), 'wb'), data) elif config.AppConfiguration().get_file_type() == "yaml": storage.Storage().save_data( open("database." + config.AppConfiguration().get_file_type(), 'w'), data) elif config.AppConfiguration().get_file_type() == "json": storage.Storage().save_data( open("database." + config.AppConfiguration().get_file_type(), 'w'), data)
tunnel.stop() del host_logging, tunnel @contextmanager def pool_context(*args, **kwargs): pool = Pool(*args, **kwargs) yield pool pool.terminate() signal.signal(signal.SIGTERM, handle_sig_term) signal.signal(signal.SIGINT, handle_sig_term) appConfiguration = rb_conf.AppConfiguration( '/etc/pyRsyncBackup/pyRsyncBackup.conf') if not os.path.isdir(appConfiguration.log['dir']): try: os.mkdir(appConfiguration.log['dir']) except OSError as e: print('Ошибка создания директории лог файлов') print(e) sys.exit(1) appLogging = rb_log.Log( __program__, os.path.join(appConfiguration.log['dir'], 'pyRsyncBackup.log'), appConfiguration.log['level'], appConfiguration.log['count'], appConfiguration.log['size']) appLogging.info('Запуск приложения {program} {version}. PID:{pid}'.format(