Add some helpers to projectconfig_ruamellib
So that it's easier to use in our scripts, add some helper sugar. First, add functions load and dump so that the library can be used similarly to yaml without the need to construct an object first. Then, remove the strip parameter and detect whether we're dumping a list or not. That way people won't forget to pass or not pass strip. Change-Id: I204af8a89c37f36f0480de3a2e669b65354eb73c
This commit is contained in:
parent
e7f77f283f
commit
1ffcd0242e
@ -17,11 +17,10 @@
|
||||
|
||||
import locale
|
||||
import sys
|
||||
import projectconfig_ruamellib
|
||||
import projectconfig_ruamellib as yaml
|
||||
|
||||
|
||||
def main():
|
||||
yaml = projectconfig_ruamellib.YAML(strip=False)
|
||||
locale.setlocale(locale.LC_COLLATE, 'C')
|
||||
|
||||
chandata = yaml.load(open('gerritbot/channels.yaml'))
|
||||
|
@ -15,12 +15,11 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import projectconfig_ruamellib
|
||||
import projectconfig_ruamellib as yaml
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
yaml = projectconfig_ruamellib.YAML()
|
||||
data = yaml.load(open('gerrit/projects.yaml'))
|
||||
|
||||
for project in data:
|
||||
|
@ -21,18 +21,12 @@ def none_representer(dumper, data):
|
||||
|
||||
|
||||
class YAML(object):
|
||||
def __init__(self, strip=True):
|
||||
"""Wrap construction of ruamel yaml object.
|
||||
|
||||
:param bool strip:
|
||||
Whether or not to strip additional leading spaces at the beginning
|
||||
of the line. This is only needed when the root object is a list.
|
||||
"""
|
||||
def __init__(self):
|
||||
"""Wrap construction of ruamel yaml object."""
|
||||
self.yaml = ruamel.yaml.YAML()
|
||||
self.yaml.allow_duplicate_keys = True
|
||||
self.yaml.representer.add_representer(type(None), none_representer)
|
||||
self.yaml.indent(mapping=2, sequence=4, offset=2)
|
||||
self.strip = strip
|
||||
|
||||
def load(self, stream):
|
||||
return self.yaml.load(stream)
|
||||
@ -48,6 +42,17 @@ class YAML(object):
|
||||
return '\n'.join(newlines)
|
||||
|
||||
def dump(self, data, *args, **kwargs):
|
||||
if self.strip:
|
||||
if isinstance(data, list):
|
||||
kwargs['transform'] = self.tr
|
||||
self.yaml.dump(data, *args, **kwargs)
|
||||
|
||||
|
||||
_yaml = YAML()
|
||||
|
||||
|
||||
def load(*args, **kwargs):
|
||||
return _yaml.load(*args, **kwargs)
|
||||
|
||||
|
||||
def dump(*args, **kwargs):
|
||||
return _yaml.dump(*args, **kwargs)
|
||||
|
Loading…
Reference in New Issue
Block a user