Advertisement
AbyssWolf

Simple Yaml

Dec 13th, 2022
516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.39 KB | Software | 0 0
  1. import yaml
  2. import os
  3.  
  4. class simple_yml:
  5.     def __init__(self, file_name: str):
  6.         self.file_name = file_name
  7.         self.create_file_if_not_exists(self.file_name)
  8.  
  9.     def create_file_if_not_exists(self, file_name: str):
  10.         if not os.path.exists(file_name):
  11.             open(file_name, 'a').close()
  12.  
  13.     # read Yaml
  14.     def read_yaml(self, file_name: str):
  15.         with open(file_name, 'r') as yaml_file:
  16.             yaml_dict = yaml.safe_load(yaml_file)
  17.         return yaml_dict
  18.  
  19.     # Create key
  20.     def create_key(self, file_name: str, section_name: str, key: str, value):
  21.         data = {}
  22.  
  23.         # Read the existing data from the file
  24.         with open(file_name, 'r') as stream:
  25.             try:
  26.                 data = yaml.safe_load(stream)
  27.             except yaml.YAMLError as exc:
  28.                 print(exc)
  29.  
  30.         # Add the new key-value pair
  31.         if section_name:
  32.             if section_name not in data:
  33.                 data[section_name] = {}
  34.             data[section_name][key] = value
  35.         else:
  36.             data[key] = value
  37.  
  38.         # Write the updated data to the file
  39.         with open(file_name, 'w') as stream:
  40.             yaml.safe_dump(data, stream)
  41.  
  42.     # Overwrite key
  43.     def overwrite_key(self, file_name: str, section_name: str, key: str, value):
  44.         data = {}
  45.  
  46.         # Read the existing data from the file
  47.         with open(file_name, 'r') as stream:
  48.             try:
  49.                 data = yaml.safe_load(stream)
  50.             except yaml.YAMLError as exc:
  51.                 print(exc)
  52.  
  53.         # Overwrite the key-value pair
  54.         if section_name:
  55.             if section_name not in data:
  56.                 data[section_name] = {}
  57.             data[section_name][key] = value
  58.         else:
  59.             data[key] = value
  60.  
  61.         # Write the updated data to the file
  62.         with open(file_name, 'w') as stream:
  63.             yaml.safe_dump(data, stream)
  64.  
  65.     # Read Key
  66.     def read_key(self, file_name: str, key: str, section: str = None):
  67.         # Open the YAML file
  68.         with open(file_name, 'r') as file:
  69.             # Load the contents of the file as a dictionary
  70.             data = yaml.safe_load(file)
  71.  
  72.             # If a section name is provided, get the value for that key in that section
  73.             if section:
  74.                 return data[section][key]
  75.             else:
  76.                 return data[key]
  77.  
Tags: python
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement