44
44
import json
45
45
import uuid
46
46
47
+ import yaml
48
+
47
49
from picard .config import get_config
48
50
from picard .const import (
49
51
DEFAULT_FILE_NAMING_FORMAT ,
@@ -131,6 +133,17 @@ def __init__(self, *args):
131
133
super ().__init__ (* args )
132
134
133
135
136
+ class ScriptLiteral (str ):
137
+ @staticmethod
138
+ def yaml_presenter (dumper , data ):
139
+ if data :
140
+ data = data .rstrip () + '\n '
141
+ return dumper .represent_scalar ("tag:yaml.org,2002:str" , data , style = "|" )
142
+
143
+
144
+ yaml .add_representer (ScriptLiteral , ScriptLiteral .yaml_presenter )
145
+
146
+
134
147
class PicardScript ():
135
148
"""Base class for Picard script objects.
136
149
"""
@@ -188,6 +201,14 @@ def __getitem__(self, setting):
188
201
return value
189
202
return None
190
203
204
+ @property
205
+ def script (self ):
206
+ return self ._script
207
+
208
+ @script .setter
209
+ def script (self , value ):
210
+ self ._script = ScriptLiteral (value )
211
+
191
212
@staticmethod
192
213
def make_last_updated ():
193
214
"""Provide consistently formatted last updated string.
@@ -242,17 +263,15 @@ def copy(self):
242
263
new_object ._set_new_id ()
243
264
return new_object
244
265
245
- # TODO: Enable once PyYAML requirement resolved with Python 3.8
246
- #
247
- # def to_yaml(self):
248
- # """Converts the properties of the script object to a YAML formatted string. Note that only property
249
- # names listed in `OUTPUT_FIELDS` will be included in the output.
266
+ def to_yaml (self ):
267
+ """Converts the properties of the script object to a YAML formatted string. Note that only property
268
+ names listed in `OUTPUT_FIELDS` will be included in the output.
250
269
251
- # Returns:
252
- # str: The properties of the script object formatted as a YAML string.
253
- # """
254
- # items = {key: getattr(self, key) for key in dir(self) if key in self.OUTPUT_FIELDS}
255
- # return yaml.dump(items)
270
+ Returns:
271
+ str: The properties of the script object formatted as a YAML string.
272
+ """
273
+ items = {key : getattr (self , key ) for key in dir (self ) if key in self .OUTPUT_FIELDS }
274
+ return yaml .dump (items )
256
275
257
276
def to_json (self , indent = None ):
258
277
"""Converts the properties of the script object to a JSON formatted string. Note that only property
@@ -267,29 +286,27 @@ def to_json(self, indent=None):
267
286
items = {key : getattr (self , key ) for key in dir (self ) if key in self .OUTPUT_FIELDS }
268
287
return json .dumps (items , indent = indent , sort_keys = True )
269
288
270
- # TODO: Enable once PyYAML requirement resolved with Python 3.8
271
- #
272
- # @classmethod
273
- # def create_from_yaml(cls, yaml_string, create_new_id=True):
274
- # """Creates an instance based on the contents of the YAML string provided.
275
- # Properties in the YAML string that are not found in the script object are ignored.
276
-
277
- # Args:
278
- # yaml_string (str): YAML string containing the property settings.
279
-
280
- # Returns:
281
- # object: An instance of the class, populated from the property settings in the YAML string.
282
- # """
283
- # new_object = cls()
284
- # yaml_dict = yaml.safe_load(yaml_string)
285
- # if not isinstance(yaml_dict, dict):
286
- # raise ScriptImportError(N_("File content not a dictionary"))
287
- # if 'title' not in yaml_dict or 'script' not in yaml_dict:
288
- # raise ScriptImportError(N_('Invalid script package'))
289
- # new_object._update_from_dict(yaml_dict)
290
- # if create_new_id or not new_object['id']:
291
- # new_object._set_new_id()
292
- # return new_object
289
+ @classmethod
290
+ def create_from_yaml (cls , yaml_string , create_new_id = True ):
291
+ """Creates an instance based on the contents of the YAML string provided.
292
+ Properties in the YAML string that are not found in the script object are ignored.
293
+
294
+ Args:
295
+ yaml_string (str): YAML string containing the property settings.
296
+
297
+ Returns:
298
+ object: An instance of the class, populated from the property settings in the YAML string.
299
+ """
300
+ new_object = cls ()
301
+ yaml_dict = yaml .safe_load (yaml_string )
302
+ if not isinstance (yaml_dict , dict ):
303
+ raise ScriptImportError (N_ ("File content not a dictionary" ))
304
+ if 'title' not in yaml_dict or 'script' not in yaml_dict :
305
+ raise ScriptImportError (N_ ('Invalid script package' ))
306
+ new_object ._update_from_dict (yaml_dict )
307
+ if create_new_id or not new_object ['id' ]:
308
+ new_object ._set_new_id ()
309
+ return new_object
293
310
294
311
@classmethod
295
312
def create_from_json (cls , json_string , create_new_id = True ):
0 commit comments