jhj0517 commited on
Commit
f4628d6
·
1 Parent(s): eec2a6d

Update to `ruamel.yaml`

Browse files
Files changed (1) hide show
  1. modules/utils/files_manager.py +13 -22
modules/utils/files_manager.py CHANGED
@@ -1,39 +1,30 @@
1
  import os
2
  import fnmatch
3
- import yaml
4
  from gradio.utils import NamedString
5
 
6
  from modules.utils.paths import DEFAULT_PARAMETERS_CONFIG_PATH
7
 
8
 
9
- class YAMLDumper(yaml.SafeDumper):
10
- def __init__(self, *args, **kwargs):
11
- super().__init__(*args, **kwargs)
12
- self.add_representer(str, self.str_presenter)
13
-
14
- def write_line_break(self, data=None):
15
- super().write_line_break(data)
16
- if len(self.indents) == 1:
17
- super().write_line_break()
18
-
19
- @staticmethod
20
- def str_presenter(dumper, data):
21
- special_chars = set(' \n\t:-[]{},&*#?|>!%@`"\'')
22
- if any(char in special_chars for char in data) or data == '':
23
- return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='"')
24
- return dumper.represent_scalar('tag:yaml.org,2002:str', data)
25
-
26
-
27
  def load_yaml(path: str = DEFAULT_PARAMETERS_CONFIG_PATH):
 
 
28
  with open(path, 'r', encoding='utf-8') as file:
29
- config = yaml.safe_load(file)
30
  return config
31
 
32
 
33
  def save_yaml(data: dict, path: str = DEFAULT_PARAMETERS_CONFIG_PATH):
34
- data = yaml.dump(data, sort_keys=False, default_flow_style=False, Dumper=YAMLDumper, allow_unicode=True)
 
 
 
 
 
 
 
35
  with open(path, 'w', encoding='utf-8') as file:
36
- file.write(data)
37
  return path
38
 
39
 
 
1
  import os
2
  import fnmatch
3
+ from ruamel.yaml import YAML
4
  from gradio.utils import NamedString
5
 
6
  from modules.utils.paths import DEFAULT_PARAMETERS_CONFIG_PATH
7
 
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  def load_yaml(path: str = DEFAULT_PARAMETERS_CONFIG_PATH):
10
+ yaml = YAML(typ="safe")
11
+ yaml.preserve_quotes = True
12
  with open(path, 'r', encoding='utf-8') as file:
13
+ config = yaml.load(file)
14
  return config
15
 
16
 
17
  def save_yaml(data: dict, path: str = DEFAULT_PARAMETERS_CONFIG_PATH):
18
+ yaml = YAML(typ="safe")
19
+ yaml.map_indent = 2
20
+ yaml.sequence_indent = 4
21
+ yaml.sequence_dash_offset = 2
22
+ yaml.preserve_quotes = True
23
+ yaml.default_flow_style = False
24
+ yaml.sort_base_mapping_type_on_output = False
25
+
26
  with open(path, 'w', encoding='utf-8') as file:
27
+ yaml.dump(data, file)
28
  return path
29
 
30