#!/usr/bin/env python3
"""make files in given dir into xml bundle string.
"""

__license__ = 'BSD-3'
__docformat__ = 'reStructuredText'

import sys
import os
import xml
import xml.dom as dom


def ft_dir_to_b64(path):
    """
    Convert the given directory to a targz file, then base64 it.

    :param arg1: the directory path
    :type arg1: str
    :returns: the base64-encoded bytes
    :rtype: bytes

    ..note:: Even though we prefer POSIX.1-2001(pax) formatted farfile,
        the whole program will still support GNU tar format.
    ..todo:: any kind of improvement is welcomed.
    """
    import tarfile
    import tempfile
    import base64

    pwd = os.getcwd()
    fileobj = tempfile.SpooledTemporaryFile(mode='w+b')
    tar = tarfile.open(mode="w:gz", format=tarfile.PAX_FORMAT, fileobj=fileobj)
    os.chdir(path)
    tar.add(".")
    os.chdir(pwd)
    tar.close()
    fileobj.seek(0)

    return base64.b64encode(fileobj.read())


def _ft_gen_texbundle_xml(
        b64bytes,
        entryfile="main.tex",
        worker='xelatex',
        timeout=60) -> str:
    """
    Generate xmlbundle from base64 bytes and given metadata as api_1_0.

    :returns: XML-formatted bundle string
    :rtype: str
    """

    impl = dom.getDOMImplementation()
    doc = impl.createDocument(None, "xmlbundle", None)
    reqNode = doc.createElement('request')
    workerNode = doc.createElement('worker')
    workerNode.appendChild(doc.createTextNode(worker))
    reqNode.appendChild(workerNode)
    timeoutNode = doc.createElement('timeout')
    timeoutNode.appendChild(doc.createTextNode(str(timeout)))
    reqNode.appendChild(timeoutNode)
    entryNode = doc.createElement('entryfile')
    entryNode.appendChild(doc.createTextNode(str(entryfile)))
    reqNode.appendChild(entryNode)
    doc.documentElement.appendChild(reqNode)
    bundleNode = doc.createElement('bundle')
    bundleNode.appendChild(doc.createTextNode(b64bytes.decode('UTF-8')))
    doc.documentElement.appendChild(bundleNode)

    return doc.toxml()


if __name__ == "__main__":
    if not len(sys.argv) == 2:
        os.abort()
    mybytes = ft_dir_to_b64(sys.argv[1])
    myxml = _ft_gen_texbundle_xml(mybytes)
    print(myxml)

@@ -185,17 +185,16 @@
 as dom%0A%0A
-%0A
 def ft_d
@@ -207,17 +207,30 @@
 b64(path
-)
+:str) -%3E bytes
 :%0A    %22%22
@@ -998,16 +998,22 @@
 b64bytes
+:bytes
 ,%0A      
@@ -2012,16 +2012,16 @@
 eNode)%0A%0A
-
     retu
@@ -2036,16 +2036,752 @@
 oxml()%0A%0A
+def _ft_gen_texbundle_json_bundled(%0A        b64bytes:bytes,%0A        entryfile='main.tex',%0A        worker='xelatex',%0A        timeout=60) -%3E str:%0A    %22%22%22%0A    Generate json bundle (bundled style) from base64 bytes and given metadata%0A    as api_1_0.%0A%0A    Written to use json.dumps()%0A%0A    :returns: JSON-formatted bundle string%0A    :rtype: str%0A    %22%22%22%0A%0A    import json%0A%0A    return json.dumps(%0A        dict(%0A            request=dict(%0A                worker=worker,%0A                timeout=timeout, %0A                entryfile=entryfile,%0A                ),%0A            type='bundle',%0A            content=dict(%0A                content_type='base64',%0A                content=b64bytes.decode('UTF-8'),%0A                ),%0A            )%0A        )%0A%0A%0A
 %0Aif __na

implement function to gen json submission bundle