示例#1
0
def test_str():
    key0 = 'x.y'
    value0 = 1
    key1 = 'x.z'
    value1 = 2
    p = ParameterSet(key1, value1)
    p.set_value(key0, value0)
    assert_equals(str(p), "%s = %d\n%s = %d" % (key0, value0, key1, value1))
示例#2
0
def test_get_value():
    p = ParameterSet('x.y.z', 1)
    v = p.get_value('x.y.z')
    v = p.get_value('x')
    v = p['x']
    v = p['x.y']
    v = p.x
    v = p.x.y.z
示例#3
0
def test_pop():
    d = {'a.b.c': 0, 'a.b.d': 0, 'a.x': 0}
    p = ParameterSet(**d)
    v = p.pop('a.b')
    assert_equals(p.keys(), ['a.x'])
    assert_equals(v.items(), [
        ('c', 0),
        ('d', 0),
    ])
示例#4
0
def test_merge():
    d1 = {'a': 1, 'b': 2}
    d2 = {'a': 3, 'c': 4}
    dmerged = d1.copy()
    dmerged.update(d2)
    p1 = ParameterSet(d1)
    p2 = ParameterSet(d2)
    pmerged = merge(p1,p2)
    assert_equals(dmerged, pmerged.to_dict())
示例#5
0
def test_in():
    p = ParameterSet('x.y.z', 1)
    assert 'x' in p
    assert 'x.y' in p
    assert 'x.y.z' in p
    assert 'a' not in p
    assert 'x.y.z.a' not in p
示例#6
0
def test_dictconstructor():
    p1 = ParameterSet({'x.y': 1})
    p2 = ParameterSet(**{'x.y': 1})
示例#7
0
    def run(self):
        """
        Implements the directive
        """
        # Get content and options
        file_path = self.arguments[0]
        use_title = 'show-title' in self.options
        use_header = 'show-header' in self.options
        main_key = self.options.get('key', None)
        show_key = 'show-key' in self.options
        if not file_path:
            return [self._report('file_path -option missing')]

        # Transform the path suitable for processing
        file_path = self._get_directive_path(file_path)

        parset = ParameterSet(file_path)
        if main_key:
            parset = parset[main_key]

        title, messages = self.make_title()

        if not parset:
            return [nodes.paragraph(text='')]

        table_data = []
        docparser = Parser()
        # Iterates rows: put the given data in rst elements
        for key in parset.keys():
            the_val = encode(parset[key])
            the_doc = parset.get_doc(key) or ''
            if main_key and show_key:
                key = ".".join([main_key.split(".")[-1], key])
            node1 = nodes.strong(text=key)
            node2 = nodes.literal(text=the_val)
            subdoc = utils.new_document('<>', self.state.document.settings)
            docparser.parse(the_doc, subdoc)
            node3 = subdoc.children
            table_data.append([node1, node2, node3])

        col_widths = self.get_column_widths(3)
        self.check_table_dimensions(table_data, 0, 0)
        header_rows = 0
        if use_header:
            header_rows = 1
            table_data.insert(0, [
                nodes.strong(text="Key"),
                nodes.strong(text="Default"),
                nodes.strong(text="Description"),
            ])

        # Generate the table node from the given list of elements
        table_node = self.build_table_from_list(table_data, col_widths,
                                                header_rows, 0)

        # Optional class parameter
        table_node['classes'] += self.options.get('class', [])

        if use_title and title:
            if main_key:
                ttxt = title.astext()
                title = nodes.title(text="".join([ttxt, ' (', main_key, ')']))
            table_node.insert(0, title)

        return [table_node] + messages
示例#8
0
def test_to_flat_dict():
    din = {'x.y':1, 'x.z':2}
    p = ParameterSet(**din)
    d = p.to_flat_dict()
    assert_equals(d.keys(), din.keys())
示例#9
0
def test_set_value():
    p = ParameterSet()
    p.set_value("x.y", 1)
    p.set_value("x.z", 2)
示例#10
0
def test_items():
    keys = ['x.a', 'x.y.z']
    p = ParameterSet('x.y.z', 1)
    p.set_value('x.a', 2)
    assert_equals(p.items(), [('x.a', 2), ('x.y.z', 1)])
示例#11
0
def test_keys():
    keys = ['a', 'x.y.z']
    p = ParameterSet(keys[1], 1)
    p.set_value(keys[0], 1)
    assert_equals(p.keys(), keys)
示例#12
0
def test_get_past_leafnode_fail():
    p = ParameterSet(a=1)
    v = p['a.b']
示例#13
0
def test_get_by_key_fail():
    p = ParameterSet()
    v = p.get_value('x.y.x')
示例#14
0
def test_decoded():
    p = ParameterSet('x.y.z', '1')
    assert_equals(p.get_value('x.y.z'), 1)
    assert_equals(p['x.y.z'], 1)
    assert_equals(p.x.y.z, 1)
示例#15
0
def test_get_value_default():
    p = ParameterSet('x.y.z', 1)
    assert_equals(p.get_value('x.y.z', 10), 1)
    assert_equals(p.get_value('x.z', 10), 10)
    assert_equals(p.get_value('x.y.z.x', 10), 10)
示例#16
0
def constructor(args):
    if not hasattr(args, "__len__"):
        args = (args,)
    p = ParameterSet(*args)
示例#17
0
def test_failconstructor():
    p = ParameterSet("xxx.parset")
示例#18
0
def test_del():
    d = {'a.b.c': 0, 'a.b.d': 0, 'a.x' : 0}
    p = ParameterSet(**d)
    del p['a.b']
    assert_equals(p.keys(), ['a.x'])
示例#19
0
def test_len():
    p = ParameterSet(a=1,b=2)
    assert_equals(len(p), 2)
示例#20
0
def test_pop():
    d = {'a.b.c': 0, 'a.b.d': 0, 'a.x' : 0}
    p = ParameterSet(**d)
    v = p.pop('a.b')
    assert_equals(p.keys(), ['a.x'])
    assert_equals(v.items(), [('c', 0),('d', 0),])
示例#21
0
def test_to_dict():
    p = ParameterSet(x=1, y=2)
    d = p.to_dict()
    assert d == {'x': 1, 'y': 2}
示例#22
0
def test_constructor():
    p = ParameterSet()
示例#23
0
def test_slice():
    p = ParameterSet('x.y.z', 1)
    assert isinstance(p.x.y, ParameterSet)
示例#24
0
def test_stringconstructor():
    pset = """a.b.c = 1
a.b.d = x"""
    p = ParameterSet(pset)
    assert_equals(p.a.b.c, 1)
示例#25
0
    def run(self):
        """
        Implements the directive
        """
        # Get content and options
        file_path = self.arguments[0]
        use_title = 'show-title' in self.options
        use_header = 'show-header' in self.options
        main_key = self.options.get('key', None)
        show_key = 'show-key' in self.options
        if not file_path:
            return [self._report('file_path -option missing')]

        # Transform the path suitable for processing
        file_path = self._get_directive_path(file_path)

        parset = ParameterSet(file_path)
        if main_key:
            parset = parset[main_key]

        title, messages = self.make_title()

        if not parset:
            return [nodes.paragraph(text='')]

        table_data = []
        docparser = Parser()
        # Iterates rows: put the given data in rst elements
        for key in parset.keys():
            the_val = encode(parset[key])
            the_doc = parset.get_doc(key) or ''
            if main_key and show_key:
                key = ".".join([main_key.split(".")[-1],key])
            node1 = nodes.strong(text=key)
            node2 = nodes.literal(text=the_val)
            subdoc = utils.new_document('<>', self.state.document.settings)
            docparser.parse(the_doc, subdoc)
            node3 = subdoc.children
            table_data.append([node1, node2, node3])


        col_widths = self.get_column_widths(3)
        self.check_table_dimensions(table_data, 0, 0)
        header_rows = 0
        if use_header:
            header_rows = 1
            table_data.insert(0, [nodes.strong(text="Key"),
                                  nodes.strong(text="Default"),
                                  nodes.strong(text="Description"),
                                  ])

        # Generate the table node from the given list of elements
        table_node = self.build_table_from_list(table_data, col_widths, 
                                                header_rows, 0)

        # Optional class parameter
        table_node['classes'] += self.options.get('class', [])

        if use_title and title:
            if main_key:
                ttxt = title.astext()
                title = nodes.title(text="".join([ttxt,' (',main_key,')']))
            table_node.insert(0, title)

        return [table_node] + messages
示例#26
0
def test_fileconstructor():
    testfile = os.path.join(os.path.split(__file__)[0], 'example.parset')
    p = ParameterSet(testfile)
示例#27
0
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA.
#

import sys
import os

from askap.parset import ParameterSet, encode

if len(sys.argv) != 2:
    print """usage:
\t %s <parmeterset_file>
""" % (os.path.basename(sys.argv[0]) )
    sys.exit(1)

p = ParameterSet(sys.argv[1])
buffer = ""
title = "Documentation for "+ os.path.basename(sys.argv[1])+"\n"
title += "="*(len(title)-1)+"\n"

print title
for k in p.keys():
    print "**"+k+"**", "   = :literal:`%s`" % encode(p[k])
    doc = p.get_doc(k) or "**undocumented**"
    for line in doc.split("\n"):
        print " "*2, line
    print

示例#28
0
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA.
#

import sys
import os

from askap.parset import ParameterSet, encode

if len(sys.argv) != 2:
    print """usage:
\t %s <parmeterset_file>
""" % (os.path.basename(sys.argv[0]))
    sys.exit(1)

p = ParameterSet(sys.argv[1])
buffer = ""
title = "Documentation for " + os.path.basename(sys.argv[1]) + "\n"
title += "=" * (len(title) - 1) + "\n"

print title
for k in p.keys():
    print "**" + k + "**", "   = :literal:`%s`" % encode(p[k])
    doc = p.get_doc(k) or "**undocumented**"
    for line in doc.split("\n"):
        print " " * 2, line
    print