def command_template(self, namespace: Namespace): slash_number = 20 enable_stdout = namespace.output[0] == '-' if not enable_stdout and not path.exists(namespace.output[0]): makedirs(namespace.output[0]) for reference in namespace.reference: stream = stdout if enable_stdout else open( path.join(namespace.output[0], f'{reference}.toml'), 'w') if enable_stdout: print(f'Profile of {reference}') print('-' * slash_number) cls = import_reference(reference) if not issubclass(cls, ProfileMixin): raise RuntimeError(f'Class {cls} do NOT supported profile.') if not issubclass(cls, Task): print( f'Class {cls} is NOT a Task, this may cause unpredictable issue.', file=stderr) toml_dump(cls.profile_template(), stream) if not enable_stdout: stream.flush() stream.close() else: print('-' * slash_number)
def convert_old_conf_to_new() -> None: for instance_directory in BubblejailDirectories.\ iter_instances_path(): if (instance_directory / FILE_NAME_SERVICES).is_file(): continue print(f"Converting {instance_directory.stem}") old_conf_path = instance_directory / 'config.toml' with open(old_conf_path) as old_conf_file: old_conf_dict = toml_load(old_conf_file) new_conf: Dict[str, Any] = {} try: services_list = old_conf_dict.pop('services') except KeyError: services_list = [] for service_name in services_list: new_conf[service_name] = {} try: old_service_dict = old_conf_dict.pop('service') except KeyError: old_service_dict = {} for service_name, service_dict in old_service_dict.items(): new_conf[service_name] = service_dict new_conf['common'] = old_conf_dict with open(instance_directory / FILE_NAME_SERVICES, mode='x') as f: toml_dump(new_conf, f)
def create_new_instance( cls, new_name: str, profile_name: Optional[str] = None, create_dot_desktop: bool = False, print_import_tips: bool = False, ) -> BubblejailInstance: instance_directory = next(cls.iter_instances_directories()) / new_name # Exception will be raised if directory already exists instance_directory.mkdir(mode=0o700, parents=True) # Make home directory (instance_directory / 'home').mkdir(mode=0o700) # Profile profile: BubblejailProfile = cls.profile_get( profile_name) if profile_name is not None else BubblejailProfile() # Make config.json with (instance_directory / FILE_NAME_SERVICES).open(mode='x') as instance_conf_file: toml_dump(profile.config.get_service_conf_dict(), instance_conf_file) instance = BubblejailInstance(instance_directory) if create_dot_desktop: if profile.dot_desktop_path is not None: cls.overwrite_desktop_entry_for_profile( instance_name=new_name, profile_object=profile, ) else: cls.generate_empty_desktop_entry(new_name) if profile_name is not None: instance.metadata_creation_profile_name = profile_name if profile_name is not None and print_import_tips: print('Import tips: ', profile.import_tips) return instance
def test_load_configuration_invalid_value_toml(self): old_data = toml_load(open('./tests/data/sample.toml')) sample = {'system': "${SYSTEM:testing"} toml_dump(sample, open('./tests/data/sample.toml', mode='w+')) with pytest.raises(Exception): ConfigLoader("./tests/data/sample.toml") toml_dump(old_data, open('./tests/data/sample.toml', mode='w+')) data = ConfigLoader("./tests/data/sample.toml").get_config() assert data['system'] == 'testing' assert data['testing']['demo'] == 'default' assert data['plain'] == "value" assert data['integer'] == 1 assert data['float'] == 1.0 sample = {'system': "SYSTEM:testing}"} toml_dump(sample, open('./tests/data/sample.toml', mode='w+')) with pytest.raises(Exception): ConfigLoader("./tests/data/sample.toml") toml_dump(old_data, open('./tests/data/sample.toml', mode='w+'))
def save_config(self, config: BubblejailInstanceConfig) -> None: with open(self.path_config_file, mode='w') as conf_file: toml_dump(config.get_service_conf_dict(), conf_file)
def _save_metadata_key(self, key: str, value: Any) -> None: toml_dict = self._get_metadata_dict() toml_dict[key] = value with open(self.path_metadata_file, mode='w') as metadata_file: toml_dump(toml_dict, metadata_file)