Server IP : 172.67.216.182 / Your IP : 162.158.107.76 Web Server : Apache System : Linux krdc-ubuntu-s-2vcpu-4gb-amd-blr1-01.localdomain 5.15.0-142-generic #152-Ubuntu SMP Mon May 19 10:54:31 UTC 2025 x86_64 User : www ( 1000) PHP Version : 7.4.33 Disable Function : passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /usr/lib/python3/dist-packages/uaclient/files/ |
Upload File : |
import json from enum import Enum from typing import Callable, Dict, Generic, Optional, Type, TypeVar import yaml from uaclient import exceptions from uaclient.data_types import DataObject from uaclient.files.files import UAFile from uaclient.util import DatetimeAwareJSONDecoder class DataObjectFileFormat(Enum): JSON = "json" YAML = "yaml" DOFType = TypeVar("DOFType", bound=DataObject) class DataObjectFile(Generic[DOFType]): def __init__( self, data_object_cls: Type[DOFType], ua_file: UAFile, file_format: DataObjectFileFormat = DataObjectFileFormat.JSON, preprocess_data: Optional[Callable[[Dict], Dict]] = None, ): self.data_object_cls = data_object_cls self.ua_file = ua_file self.file_format = file_format self.preprocess_data = preprocess_data def read(self) -> Optional[DOFType]: raw_data = self.ua_file.read() if raw_data is None: return None parsed_data = None if self.file_format == DataObjectFileFormat.JSON: try: parsed_data = json.loads( raw_data, cls=DatetimeAwareJSONDecoder ) except json.JSONDecodeError: raise exceptions.InvalidFileFormatError( self.ua_file.path, "json" ) elif self.file_format == DataObjectFileFormat.YAML: try: parsed_data = yaml.safe_load(raw_data) except yaml.parser.ParserError: raise exceptions.InvalidFileFormatError( self.ua_file.path, "yaml" ) if parsed_data is None: return None if self.preprocess_data: parsed_data = self.preprocess_data(parsed_data) return self.data_object_cls.from_dict(parsed_data) def write(self, content: DOFType): if self.file_format == DataObjectFileFormat.JSON: str_content = content.to_json() elif self.file_format == DataObjectFileFormat.YAML: data = content.to_dict() str_content = yaml.dump(data, default_flow_style=False) self.ua_file.write(str_content) def delete(self): self.ua_file.delete()