mirror of
				https://github.com/clearml/clearml
				synced 2025-06-26 18:16:07 +00:00 
			
		
		
		
	Moved dict parsing in/out (hocon) into utilities.config
This commit is contained in:
		
							parent
							
								
									87088a8c0f
								
							
						
					
					
						commit
						e4801542f5
					
				| @ -263,7 +263,7 @@ class Artifacts(object): | ||||
| 
 | ||||
|     def __init__(self, task): | ||||
|         self._task = task | ||||
|         # notice the double link, this important since the Artifact | ||||
|         # notice the double link, this is important since the Artifact | ||||
|         # dictionary needs to signal the Artifacts base on changes | ||||
|         self._artifacts_container = self._ProxyDictWrite(self) | ||||
|         self._last_artifacts_upload = {} | ||||
|  | ||||
| @ -4,7 +4,6 @@ import tarfile | ||||
| import zipfile | ||||
| from tempfile import mkdtemp, mkstemp | ||||
| 
 | ||||
| import pyparsing | ||||
| import six | ||||
| from typing import List, Dict, Union, Optional, Mapping, TYPE_CHECKING, Sequence | ||||
| 
 | ||||
| @ -12,7 +11,7 @@ from .backend_api import Session | ||||
| from .backend_api.services import models | ||||
| from pathlib2 import Path | ||||
| 
 | ||||
| from .utilities.pyhocon import ConfigFactory, HOCONConverter | ||||
| from .utilities.config import config_dict_to_text, text_to_config_dict | ||||
| 
 | ||||
| from .backend_interface.util import validate_dict, get_single_result, mutually_exclusive | ||||
| from .debugging.log import get_logger | ||||
| @ -338,36 +337,15 @@ class BaseModel(object): | ||||
| 
 | ||||
|     @staticmethod | ||||
|     def _config_dict_to_text(config): | ||||
|         # if already string return as is | ||||
|         if isinstance(config, six.string_types): | ||||
|             return config | ||||
|         if not isinstance(config, dict): | ||||
|             raise ValueError("Model configuration only supports dictionary objects") | ||||
|         try: | ||||
|             try: | ||||
|                 text = HOCONConverter.to_hocon(ConfigFactory.from_dict(config)) | ||||
|             except Exception: | ||||
|                 # fallback json+pyhocon | ||||
|                 # hack, pyhocon is not very good with dict conversion so we pass through json | ||||
|                 import json | ||||
|                 text = json.dumps(config) | ||||
|                 text = HOCONConverter.to_hocon(ConfigFactory.parse_string(text)) | ||||
| 
 | ||||
|         except Exception: | ||||
|             raise ValueError("Could not serialize configuration dictionary:\n", config) | ||||
|         return text | ||||
|         if not isinstance(config, six.string_types) and not isinstance(config, dict): | ||||
|             raise ValueError("Model configuration only supports dictionary or string objects") | ||||
|         return config_dict_to_text | ||||
| 
 | ||||
|     @staticmethod | ||||
|     def _text_to_config_dict(text): | ||||
|         if not isinstance(text, six.string_types): | ||||
|             raise ValueError("Model configuration parsing only supports string") | ||||
|         try: | ||||
|             return ConfigFactory.parse_string(text).as_plain_ordered_dict() | ||||
|         except pyparsing.ParseBaseException as ex: | ||||
|             pos = "at char {}, line:{}, col:{}".format(ex.loc, ex.lineno, ex.column) | ||||
|             six.raise_from(ValueError("Could not parse configuration text ({}):\n{}".format(pos, text)), None) | ||||
|         except Exception: | ||||
|             six.raise_from(ValueError("Could not parse configuration text:\n{}".format(text)), None) | ||||
|         return text_to_config_dict(text) | ||||
| 
 | ||||
|     @staticmethod | ||||
|     def _resolve_config(config_text=None, config_dict=None): | ||||
|  | ||||
| @ -2,6 +2,8 @@ from __future__ import division | ||||
| 
 | ||||
| import six | ||||
| import humanfriendly | ||||
| import pyparsing | ||||
| from .pyhocon import ConfigFactory, HOCONConverter | ||||
| 
 | ||||
| 
 | ||||
| def parse_human_size(value): | ||||
| @ -44,3 +46,38 @@ def get_human_size_default(config, key, default=None): | ||||
|         return default | ||||
| 
 | ||||
|     return parse_human_size(raw_value) | ||||
| 
 | ||||
| 
 | ||||
| def config_dict_to_text(config): | ||||
|     # if already string return as is | ||||
|     if isinstance(config, six.string_types): | ||||
|         return config | ||||
|     if not isinstance(config, dict): | ||||
|         raise ValueError("Configuration only supports dictionary objects") | ||||
|     try: | ||||
|         # noinspection PyBroadException | ||||
|         try: | ||||
|             text = HOCONConverter.to_hocon(ConfigFactory.from_dict(config)) | ||||
|         except Exception: | ||||
|             # fallback json+pyhocon | ||||
|             # hack, pyhocon is not very good with dict conversion so we pass through json | ||||
|             import json | ||||
|             text = json.dumps(config) | ||||
|             text = HOCONConverter.to_hocon(ConfigFactory.parse_string(text)) | ||||
| 
 | ||||
|     except Exception: | ||||
|         raise ValueError("Could not serialize configuration dictionary:\n", config) | ||||
|     return text | ||||
| 
 | ||||
| 
 | ||||
| def text_to_config_dict(text): | ||||
|     if not isinstance(text, six.string_types): | ||||
|         raise ValueError("Configuration parsing only supports string") | ||||
|     # noinspection PyBroadException | ||||
|     try: | ||||
|         return ConfigFactory.parse_string(text).as_plain_ordered_dict() | ||||
|     except pyparsing.ParseBaseException as ex: | ||||
|         pos = "at char {}, line:{}, col:{}".format(ex.loc, ex.lineno, ex.column) | ||||
|         six.raise_from(ValueError("Could not parse configuration text ({}):\n{}".format(pos, text)), None) | ||||
|     except Exception: | ||||
|         six.raise_from(ValueError("Could not parse configuration text:\n{}".format(text)), None) | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 allegroai
						allegroai