Skip to content

Commit

Permalink
Yamlet 0.0.2: add load routines to module scope, make opts optional
Browse files Browse the repository at this point in the history
I had apparently not included any tests that didn't specify options to the Loader, so the very most basic example of how to load anything didn't work. Putting myself in the seat of a new user was very revealing. The Loader makes me pass some garbage options class I don't care about. There's no default `load` routine. There's no default `load_file` routine. It's just miserable to get started with this thing. So I fixed that.

Cuts version 0.0.2; will export to Pip.
  • Loading branch information
JoshDreamland committed Nov 8, 2024
1 parent c90b65b commit 25a9ec0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
19 changes: 19 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1245,6 +1245,25 @@ def test_nullification_example_from_the_readme(self):
self.assertEqual(len(y['deleter']), 1)
self.assertEqual(len(y['t3']), 2)

def test_most_basic_goddamn_example_from_the_readme(self):
loader = yamlet.Loader()
t = loader.load_file('yaml-gcl.yaml')
self.assertEqual(t['childtuple']['coolbeans'],
'Hello, world! I say cooool beans!')
self.assertEqual(t['childtuple2']['coolbeans'],
'Hello, world! I say awesome sauce!')

def test_easy_load(self):
hi = yamlet.load('hi')
self.assertEqual(hi, 'hi')

def test_easy_load_file(self):
t = yamlet.load_file('yaml-gcl.yaml')
self.assertEqual(t['childtuple']['coolbeans'],
'Hello, world! I say cooool beans!')
self.assertEqual(t['childtuple2']['coolbeans'],
'Hello, world! I say awesome sauce!')


if __name__ == '__main__':
unittest.main()
32 changes: 27 additions & 5 deletions yamlet.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Copyright (C) 2024 Josh Ventura <joshv10>
# You may use and redistribute this file under the terms of the MIT License.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import ast
import copy
Expand All @@ -14,7 +31,7 @@
import token
import tokenize

VERSION = '0.0.1.A'
VERSION = '0.0.2'
ConstructorError = ruamel.yaml.constructor.ConstructorError
class YamletBaseException(Exception): pass

Expand All @@ -37,10 +54,11 @@ def __init__(self, import_resolver=None, missing_name_value=Error,


class Loader(ruamel.yaml.YAML):
def __init__(self, opts):
def __init__(self, opts=None):
super().__init__()
self.loaded_modules = {}
opts = opts or YamletOptions()
self.yamlet_options = opts
self.loaded_modules = {}
# Set custom dict type for base operations
self.constructor.yaml_base_dict_type = GclDict
self.representer.add_representer(GclDict, self.representer.represent_dict)
Expand Down Expand Up @@ -91,7 +109,7 @@ def Construct(loader, node):
yc.add_constructor("!else", Loader._ConstructElse)
yc.add_constructor("!null", ConstructConstant('null', null))
yc.add_constructor("!external", ConstructConstant('external', external))
for tag, ctor in opts.constructors.items():
for tag, ctor in self.yamlet_options.constructors.items():
yc.add_constructor(tag, UserConstructor(ctor))

def LoadCachedFile(self, fn):
Expand Down Expand Up @@ -168,6 +186,10 @@ def default(x, y): return y if x is None else x
self.traces = default(traces, _DebugOpts.TRACE_VERBOSE)


def load(data): return Loader().load(data)
def load_file(data): return Loader().load_file(data)


'''▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░
░▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒░
░▒▓██▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀██▓▒░
Expand Down

0 comments on commit 25a9ec0

Please sign in to comment.