Server IP : 85.214.239.14 / Your IP : 3.21.44.115 Web Server : Apache/2.4.62 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.18 Disable Function : 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_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /proc/2/root/proc/3/root/usr/lib/python3/dist-packages/ansible_test/_internal/ |
Upload File : |
"""Functions for disk IO.""" from __future__ import annotations import io import json import os import typing as t from .encoding import ( ENCODING, to_bytes, to_text, ) def read_json_file(path: str) -> t.Any: """Parse and return the json content from the specified path.""" return json.loads(read_text_file(path)) def read_text_file(path: str) -> str: """Return the contents of the specified path as text.""" return to_text(read_binary_file(path)) def read_binary_file(path: str) -> bytes: """Return the contents of the specified path as bytes.""" with open_binary_file(path) as file_obj: return file_obj.read() def make_dirs(path: str) -> None: """Create a directory at path, including any necessary parent directories.""" os.makedirs(to_bytes(path), exist_ok=True) def write_json_file( path: str, content: t.Any, create_directories: bool = False, formatted: bool = True, encoder: t.Optional[t.Type[json.JSONEncoder]] = None, ) -> str: """Write the given json content to the specified path, optionally creating missing directories.""" text_content = json.dumps(content, sort_keys=formatted, indent=4 if formatted else None, separators=(', ', ': ') if formatted else (',', ':'), cls=encoder, ) + '\n' write_text_file(path, text_content, create_directories=create_directories) return text_content def write_text_file(path: str, content: str, create_directories: bool = False) -> None: """Write the given text content to the specified path, optionally creating missing directories.""" if create_directories: make_dirs(os.path.dirname(path)) with open_binary_file(path, 'wb') as file_obj: file_obj.write(to_bytes(content)) def open_text_file(path: str, mode: str = 'r') -> t.IO[str]: """Open the given path for text access.""" if 'b' in mode: raise Exception('mode cannot include "b" for text files: %s' % mode) return io.open(to_bytes(path), mode, encoding=ENCODING) # pylint: disable=consider-using-with def open_binary_file(path: str, mode: str = 'rb') -> t.IO[bytes]: """Open the given path for binary access.""" if 'b' not in mode: raise Exception('mode must include "b" for binary files: %s' % mode) return io.open(to_bytes(path), mode) # pylint: disable=consider-using-with class SortedSetEncoder(json.JSONEncoder): """Encode sets as sorted lists.""" def default(self, o: t.Any) -> t.Any: """Return a serialized version of the `o` object.""" if isinstance(o, set): return sorted(o) return json.JSONEncoder.default(self, o)