#! /usr/bin/env python3.6
from JumpScale9.tools import cmdutils
from js9 import j
from html.parser import HTMLParser
from xml.etree import cElementTree as etree
j.application.start('jsdocs')

parser = cmdutils.ArgumentParser()

commands = ['generate']
parser.add_argument("action", choices=commands, help='Command to perform')


class Parser(HTMLParser):
    def __init__(self):
      HTMLParser.__init__(self)
      self.tb = etree.TreeBuilder()

    def handle_starttag(self, tag, attrs):
        attrs = dict(attrs)
        # if 'id' in attrs and attrs['id'] == 'jumpscale-api':
        self.tb.start(tag, dict(attrs))

    def handle_endtag(self, tag):
        self.tb.end(tag)

    def handle_data(self, data):
        self.tb.data(data)

    def close(self):
        HTMLParser.close(self)
        return self.tb.close()

args = parser.parse_args()
if args.action == 'generate':
    print("UPDATING CORE...")
    corecl = j.clients.git.getClient('/opt/code/github/jumpscale/jumpscale_core/')
    corecl.pull()
    print("UPDATED")

    print("\nUPDATING DOCS...")
    doccl = j.clients.git.getClient('/opt/code/github/jumpscale/jumpscale_docs/')
    doccl.pull()
    print("UPDATED")

    print("\nUPDATING GENERATED DOCS...")
    gencl = j.clients.git.getClient('/opt/code/github/jumpscale/generated_docs/')
    gencl.pull()
    print("UPDATED")

    print("\nConverting Confluence to RST...")
    j.sal.process.execute("""cd /opt/code/github/jumpscale/jumpscale_prototypes/prototypes/confluence2rst; python confluence2rst.py""")
    print("Converted")

    gencl.addRemoveFiles()
    gencl.commit('auto generated docs')
    gencl.push()

j.application.stop()

