예제 #1
0
파일: test_text.py 프로젝트: talwai/petl
def test_totext():

    # exercise function
    table = (("foo", "bar"), ("a", 1), ("b", 2), ("c", 2))
    f = NamedTemporaryFile(delete=False)
    prologue = """{| class="wikitable"
|-
! foo
! bar
"""
    template = """|-
| {foo}
| {bar}
"""
    epilogue = "|}"
    totext(table, f.name, template, prologue, epilogue)

    # check what it did
    with open(f.name, "rb") as o:
        actual = o.read()
        expect = """{| class="wikitable"
|-
! foo
! bar
|-
| a
| 1
|-
| b
| 2
|-
| c
| 2
|}"""
        eq_(expect, actual)
예제 #2
0
파일: test_text.py 프로젝트: larissarmp/TCC
def test_totext():

    # exercise function
    table = (('foo', 'bar'), ('a', 1), ('b', 2), ('c', 2))
    f = NamedTemporaryFile(delete=False)
    f.close()
    prologue = ("{| class='wikitable'\n" "|-\n" "! foo\n" "! bar\n")
    template = ("|-\n" "| {foo}\n" "| {bar}\n")
    epilogue = "|}\n"
    totext(table,
           f.name,
           encoding='ascii',
           template=template,
           prologue=prologue,
           epilogue=epilogue)

    # check what it did
    with io.open(f.name, mode='rt', encoding='ascii', newline='') as o:
        actual = o.read()
        expect = ("{| class='wikitable'\n"
                  "|-\n"
                  "! foo\n"
                  "! bar\n"
                  "|-\n"
                  "| a\n"
                  "| 1\n"
                  "|-\n"
                  "| b\n"
                  "| 2\n"
                  "|-\n"
                  "| c\n"
                  "| 2\n"
                  "|}\n")
        eq_(expect, actual)
예제 #3
0
def test_totext():

    # exercise function
    table = (('foo', 'bar'), ('a', 1), ('b', 2), ('c', 2))
    f = NamedTemporaryFile(delete=False)
    prologue = """{| class="wikitable"
|-
! foo
! bar
"""
    template = """|-
| {foo}
| {bar}
"""
    epilogue = "|}"
    totext(table, f.name, template, prologue, epilogue)

    # check what it did
    with open(f.name, 'rb') as o:
        actual = o.read()
        expect = """{| class="wikitable"
|-
! foo
! bar
|-
| a
| 1
|-
| b
| 2
|-
| c
| 2
|}"""
        eq_(expect, actual)
예제 #4
0
파일: test_text.py 프로젝트: DeanWay/petl
def test_totext_gz():

    # exercise function
    table = (('foo', 'bar'),
             ('a', 1),
             ('b', 2),
             ('c', 2))
    f = NamedTemporaryFile(delete=False)
    f.close()
    fn = f.name + '.gz'
    os.rename(f.name, fn)
    prologue = (
        "{| class='wikitable'\n"
        "|-\n"
        "! foo\n"
        "! bar\n"
    )
    template = (
        "|-\n"
        "| {foo}\n"
        "| {bar}\n"
    )
    epilogue = "|}\n"
    totext(table, fn, encoding='ascii', template=template, prologue=prologue,
           epilogue=epilogue)

    # check what it did
    o = gzip.open(fn, 'rb')
    try:
        actual = o.read()
        expect = (
            b"{| class='wikitable'\n"
            b"|-\n"
            b"! foo\n"
            b"! bar\n"
            b"|-\n"
            b"| a\n"
            b"| 1\n"
            b"|-\n"
            b"| b\n"
            b"| 2\n"
            b"|-\n"
            b"| c\n"
            b"| 2\n"
            b"|}\n"
        )
        eq_(expect, actual)
    finally:
        o.close()
예제 #5
0
def test_totext():

    # exercise function
    tbl = ((u'name', u'id'),
           (u'Արամ Խաչատրյան', 1),
           (u'Johann Strauß', 2),
           (u'Вагиф Сәмәдоғлу', 3),
           (u'章子怡', 4),
           )
    prologue = (
        u"{| class='wikitable'\n"
        u"|-\n"
        u"! name\n"
        u"! id\n"
    )
    template = (
        u"|-\n"
        u"| {name}\n"
        u"| {id}\n"
    )
    epilogue = u"|}\n"
    fn = NamedTemporaryFile().name
    totext(tbl, fn, template=template, prologue=prologue,
           epilogue=epilogue, encoding='utf-8')

    # check what it did
    f = io.open(fn, encoding='utf-8', mode='rt')
    actual = f.read()
    expect = (
        u"{| class='wikitable'\n"
        u"|-\n"
        u"! name\n"
        u"! id\n"
        u"|-\n"
        u"| Արամ Խաչատրյան\n"
        u"| 1\n"
        u"|-\n"
        u"| Johann Strauß\n"
        u"| 2\n"
        u"|-\n"
        u"| Вагиф Сәмәдоғлу\n"
        u"| 3\n"
        u"|-\n"
        u"| 章子怡\n"
        u"| 4\n"
        u"|}\n"
    )
    eq_(expect, actual)
예제 #6
0
파일: test_text.py 프로젝트: DeanWay/petl
def test_totext():

    # exercise function
    table = (('foo', 'bar'),
             ('a', 1),
             ('b', 2),
             ('c', 2))
    f = NamedTemporaryFile(delete=False)
    f.close()
    prologue = (
        "{| class='wikitable'\n"
        "|-\n"
        "! foo\n"
        "! bar\n"
    )
    template = (
        "|-\n"
        "| {foo}\n"
        "| {bar}\n"
    )
    epilogue = "|}\n"
    totext(table, f.name, encoding='ascii', template=template,
           prologue=prologue, epilogue=epilogue)

    # check what it did
    with io.open(f.name, mode='rt', encoding='ascii', newline='') as o:
        actual = o.read()
        expect = (
            "{| class='wikitable'\n"
            "|-\n"
            "! foo\n"
            "! bar\n"
            "|-\n"
            "| a\n"
            "| 1\n"
            "|-\n"
            "| b\n"
            "| 2\n"
            "|-\n"
            "| c\n"
            "| 2\n"
            "|}\n"
        )
        eq_(expect, actual)
예제 #7
0
def test_totext():

    # exercise function
    tbl = (
        (u'name', u'id'),
        (u'Արամ Խաչատրյան', 1),
        (u'Johann Strauß', 2),
        (u'Вагиф Сәмәдоғлу', 3),
        (u'章子怡', 4),
    )
    prologue = (u"{| class='wikitable'\n" u"|-\n" u"! name\n" u"! id\n")
    template = (u"|-\n" u"| {name}\n" u"| {id}\n")
    epilogue = u"|}\n"
    fn = NamedTemporaryFile().name
    totext(tbl,
           fn,
           template=template,
           prologue=prologue,
           epilogue=epilogue,
           encoding='utf-8')

    # check what it did
    f = io.open(fn, encoding='utf-8', mode='rt')
    actual = f.read()
    expect = (u"{| class='wikitable'\n"
              u"|-\n"
              u"! name\n"
              u"! id\n"
              u"|-\n"
              u"| Արամ Խաչատրյան\n"
              u"| 1\n"
              u"|-\n"
              u"| Johann Strauß\n"
              u"| 2\n"
              u"|-\n"
              u"| Вагиф Сәмәдоғлу\n"
              u"| 3\n"
              u"|-\n"
              u"| 章子怡\n"
              u"| 4\n"
              u"|}\n")
    eq_(expect, actual)
예제 #8
0
파일: test_text.py 프로젝트: talwai/petl
def test_totext_gz():

    # exercise function
    table = (("foo", "bar"), ("a", 1), ("b", 2), ("c", 2))
    f = NamedTemporaryFile(delete=False)
    fn = f.name + ".gz"
    f.close()
    os.rename(f.name, fn)
    prologue = """{| class="wikitable"
|-
! foo
! bar
"""
    template = """|-
| {foo}
| {bar}
"""
    epilogue = "|}"
    totext(table, fn, template, prologue, epilogue)

    # check what it did
    o = gzip.open(fn, "rb")
    try:
        actual = o.read()
        expect = """{| class="wikitable"
|-
! foo
! bar
|-
| a
| 1
|-
| b
| 2
|-
| c
| 2
|}"""
        eq_(expect, actual)
    finally:
        o.close()
예제 #9
0
def test_totext_gz():

    # exercise function
    table = (('foo', 'bar'), ('a', 1), ('b', 2), ('c', 2))
    f = NamedTemporaryFile(delete=False)
    fn = f.name + '.gz'
    f.close()
    os.rename(f.name, fn)
    prologue = """{| class="wikitable"
|-
! foo
! bar
"""
    template = """|-
| {foo}
| {bar}
"""
    epilogue = "|}"
    totext(table, fn, template, prologue, epilogue)

    # check what it did
    o = gzip.open(fn, 'rb')
    try:
        actual = o.read()
        expect = """{| class="wikitable"
|-
! foo
! bar
|-
| a
| 1
|-
| b
| 2
|-
| c
| 2
|}"""
        eq_(expect, actual)
    finally:
        o.close()
예제 #10
0
파일: test_text.py 프로젝트: larissarmp/TCC
def test_totext_gz():

    # exercise function
    table = (('foo', 'bar'), ('a', 1), ('b', 2), ('c', 2))
    f = NamedTemporaryFile(delete=False)
    f.close()
    fn = f.name + '.gz'
    os.rename(f.name, fn)
    prologue = ("{| class='wikitable'\n" "|-\n" "! foo\n" "! bar\n")
    template = ("|-\n" "| {foo}\n" "| {bar}\n")
    epilogue = "|}\n"
    totext(table,
           fn,
           encoding='ascii',
           template=template,
           prologue=prologue,
           epilogue=epilogue)

    # check what it did
    o = gzip.open(fn, 'rb')
    try:
        actual = o.read()
        expect = (b"{| class='wikitable'\n"
                  b"|-\n"
                  b"! foo\n"
                  b"! bar\n"
                  b"|-\n"
                  b"| a\n"
                  b"| 1\n"
                  b"|-\n"
                  b"| b\n"
                  b"| 2\n"
                  b"|-\n"
                  b"| c\n"
                  b"| 2\n"
                  b"|}\n")
        eq_(expect, actual)
    finally:
        o.close()
예제 #11
0
def toxml(table,
          target=None,
          root=None,
          head=None,
          rows=None,
          prologue=None,
          epilogue=None,
          style='tag',
          encoding='utf-8'):
    """
    Write the table into a new xml file according to elements defined in the
    function arguments.

    The `root`, `head` and `rows` (string, optional) arguments define the tags
    and the nesting of the xml file. Each one defines xml elements with tags
    separated by slashes (`/`) like in `root/level/tag`. They can have a
    arbitrary number of tags that will reflect in more nesting levels for the
    header or record/row written in the xml file.

    For details on tag naming and nesting rules check xml `specification`_ or
    xml `references`_.

    The `rows` argument define the elements for each row of data to be written
    in the xml file. When specified, it must have at least 2 tags for defining
    the tags for `row/column`. Additional tags will add nesting enclosing all
    records/rows/lines.

    The `head` argument is similar to the rows, but aplies only to one line/row
    of header with fieldnames. When specified, it must have at least 2 tags for
    `fields/name` and the remaining will increase nesting.

    The `root` argument defines the elements enclosing `head` and `rows` and is
    required when using `head` for specifying valid xml documents.

    When none of this arguments are specified, they will default to tags that
    generate output similar to a html table:
    `root='table', head='there/tr/td', rows='tbody/tr/td'`.

    The `prologue` argument (string, optional) could be a snippet of valid xml
    that will be inserted before other elements in the xml. It can optionally
    specify the `XML Prolog` of the file.

    The `epilogue` argument (string, optional) could be a snippet of valid xml
    that will be inserted after all other xml elements except the root closing
    tag. It must specify a closing tag if the `root` argument is not specified. 

    The `style` argument select the format of the elements in the xml file. It
    can be `tag` (default), `name`, `attribute` or a custom string to format
    each row via
    `str.format <http://docs.python.org/library/stdtypes.html#str.format>`_.

    Example usage for writing files::

        >>> import petl as etl
        >>> table1 = [['foo', 'bar'],
        ...           ['a', 1],
        ...           ['b', 2]]
        >>> etl.toxml(table1, 'example.file4.xml')
        >>> # see what we did is similar a html table:
        >>> print(open('example.file4.xml').read())
        <?xml version="1.0" encoding="UTF-8"?>
        <table><thead>
         <tr><th>foo</th><th>bar</th></tr>
        </thead><tbody>
         <tr><td>a</td><td>1</td></tr>
         <tr><td>b</td><td>2</td></tr>
        </tbody></table>
        >>> # define the nesting in xml file:
        >>> etl.toxml(table1, 'example.file5.xml', rows='plan/line/cell')
        >>> print(open('example.file5.xml').read())
        <?xml version="1.0" encoding="UTF-8"?>
        <plan>
         <line><cell>a</cell><cell>1</cell></line>
         <line><cell>b</cell><cell>2</cell></line>
        </plan>
        >>> # choose other style:
        >>> etl.toxml(table1, 'example.file6.xml', rows='row/col', style='attribute')
        >>> print(open('example.file6.xml').read())
        <?xml version="1.0" encoding="UTF-8"?>
        <row>
         <col foo="a" bar="1" />
         <col foo="b" bar="2" />
        </row>
        >>> etl.toxml(table1, 'example.file6.xml', rows='row/col', style='name')
        >>> print(open('example.file6.xml').read())
        <?xml version="1.0" encoding="UTF-8"?>
        <row>
         <col><foo>a</foo><bar>1</bar></col>
         <col><foo>b</foo><bar>2</bar></col>
        </row>

    The `toxml()` function is just a wrapper over :func:`petl.io.text.totext`.
    For advanced cases use a template with `totext()` for generating xml files.

    .. versionadded:: 1.7.0

    .. _specification: https://www.w3.org/TR/xml/
    .. _references: https://www.w3schools.com/xml/xml_syntax.asp

    """
    if not root and not head and not rows:
        root = 'table'
        head = 'thead/tr/th'
        rows = 'tbody/tr/td'

    sample, table2 = iterpeek(table, 2)
    props = fieldnames(sample)

    top = _build_xml_header(style, props, root, head, rows, prologue, encoding)
    template = _build_cols(style, props, rows, True)
    bottom = _build_xml_footer(style, epilogue, rows, root)

    totext(table2,
           source=target,
           encoding=encoding,
           errors='strict',
           template=template,
           prologue=top,
           epilogue=bottom)