- root = doc.documentElement
- for attribute,value in root.attributes.items():
- attribute = self._restore_key(attribute)
- if attribute == 'version':
- assert value == self.version, \
- 'Cannot read v%s playlist with a v%s reader' \
- % (value, self.version)
- elif attribute == 'index':
- self._index = int(value)
- else:
- self.info[attribute] = value
- for curve_element in doc.getElementsByTagName('curve'):
- path = curve_element.getAttribute('path')
- info = dict([(self._restore_key(key), value)
- for key,value in curve_element.attributes.items()])
- info.pop('path')
- self.append_curve_by_path(path, info, identify=identify)
- self.jump(self._index) # ensure valid index
-
- def from_string(self, string, identify=True):
- u"""Load a playlist from a string.
-
- Examples
- --------
+ self.set_path(path)
+ dirname = os.path.dirname(self.path) or '.'
+ if makedirs == True and not os.path.isdir(dirname):
+ os.makedirs(dirname)
+ with open(self.path, 'w') as f:
+ f.write(self.flatten())
+ self._digest = self.digest()
+
+
+def from_string(string):
+ u"""Load a playlist from a string.
+
+ Examples
+ --------
+
+ Minimal example.
+
+ >>> string = '''# Hooke playlist version 0.2
+ ... !!python/object/new:hooke.playlist.FilePlaylist
+ ... state:
+ ... version: '0.2'
+ ... listitems:
+ ... - !!python/object:hooke.curve.Curve
+ ... path: curve/one
+ ... - !!python/object:hooke.curve.Curve
+ ... path: curve/two
+ ... '''
+ >>> p = from_string(string)
+ >>> p.set_path('/path/to/playlist')
+ >>> for curve in p:
+ ... print curve.name, curve.path
+ one /path/to/curve/one
+ two /path/to/curve/two
+
+ More complicated example.
+
+ >>> string = '''# Hooke playlist version 0.2
+ ... !!python/object/new:hooke.playlist.FilePlaylist
+ ... listitems:
+ ... - !!python/object:hooke.curve.Curve
+ ... info: {note: The first curve}
+ ... name: one
+ ... path: /path/to/curve/one
+ ... - &id001 !!python/object:hooke.curve.Curve
+ ... command_stack: !!python/object/new:hooke.command_stack.CommandStack
+ ... listitems:
+ ... - !!python/object:hooke.engine.CommandMessage
+ ... arguments: {arg 0: 0, arg 1: X}
+ ... command: command A
+ ... - !!python/object:hooke.engine.CommandMessage
+ ... arguments:
+ ... arg 0: 1
+ ... curve: *id001
+ ... command: command B
+ ... info: {attr with spaces: 'The second curve
+ ...
+ ... with endlines'}
+ ... name: two
+ ... path: /path/to/curve/two
+ ... state:
+ ... _base_path: /path/to
+ ... _index: 1
+ ... info: {note: An example playlist}
+ ... name: playlist.hkp
+ ... path: /path/to/playlist.hkp
+ ... version: '0.2'
+ ... '''
+ >>> p = from_string(string)
+ >>> p.set_path('/path/to/playlist')
+ >>> p._index
+ 1
+ >>> p.info
+ {'note': 'An example playlist'}
+ >>> for curve in p:
+ ... print curve.name, curve.path
+ one /path/to/curve/one
+ two /path/to/curve/two
+ >>> p[-1].info['attr with spaces']
+ 'The second curve\\nwith endlines'
+ >>> type(p[-1].command_stack)
+ <class 'hooke.command_stack.CommandStack'>
+ >>> p[0].command_stack
+ []
+ >>> type(p[0].command_stack)
+ <class 'hooke.command_stack.CommandStack'>
+ >>> p[-1].command_stack # doctest: +NORMALIZE_WHITESPACE
+ [<CommandMessage command A {arg 0: 0, arg 1: X}>,
+ <CommandMessage command B {arg 0: 1, curve: <Curve two>}>]
+ >>> type(p[1].command_stack)
+ <class 'hooke.command_stack.CommandStack'>
+ >>> c2 = p[-1]
+ >>> c2.command_stack[-1].arguments['curve'] == c2
+ True
+ """
+ return yaml.load(string)