# Path: xml2dict.py
# class XML2Dict(object):
#     @classmethod
#     def _parse_node(self, node):
#         node_tree = object_dict()
#         # Save attrs and text, hope there will not be a child with same name
#         if node.text:
#             node_tree.value = node.text
#         for (k, v) in node.attrib.items():
#             k, v = self._namespace_split(k, object_dict({'value':v}))
#             node_tree[k] = v
#         # Save childrens
#         for child in iter(node):
#             tag, tree = self._namespace_split(child.tag, self._parse_node(child))
#             if  tag not in node_tree:  # the first time, so store it in dict
#                 node_tree[tag] = tree
#                 continue
#             old = node_tree[tag]
#             if not isinstance(old, list):
#                 node_tree.pop(tag)
#                 node_tree[tag] = [old]  # multi times, so change old dict to a list
#             node_tree[tag].append(tree)  # add the new one
# 
#         return  node_tree
# 
#     @classmethod
#     def _namespace_split(self, tag, value):
#         """
#            Split the tag  '{http://cs.sfsu.edu/csc867/myscheduler}patients'
#              ns = http://cs.sfsu.edu/csc867/myscheduler
#              name = patients
#         """
#         result = re.compile("\{(.*)\}(.*)").search(tag)
#         if result:
#             # print(tag)
#             value.namespace, tag = result.groups()
#         return (tag, value)
# 
#     @classmethod
#     def parse(self, file):
#         """parse a xml file to a dict"""
#         f = open(file, 'r')
#         return self.fromstring(f.read())
# 
#     @classmethod
#     def fromstring(self, s):
#         """parse a string"""
#         t = ET.fromstring(s)
#         root_tag, root_tree = self._namespace_split(t.tag, self._parse_node(t))
#         return object_dict({root_tag: root_tree})


from _prototype import plugin_prototype
from cross_platform import *
from xml2dict import XML2Dict
    from io import StringIO
    from cStringIO import StringIO
import re
import sys
import time
import os, os.path as opath
import urllib
import gzip
import threading
    import urllib.request as urllib2
    import urllib2
                import maclient
                import maclient_smart

            url = repo + '/raw/' + uri
        try:
            resp = opener.open(urllib2.Request(url, headers=headers), timeout = 15)
            body = resp.read()
            try:
                f = StringIO(body)
                gz = gzip.GzipFile(fileobj = f)
                body = gz.read()
            except:
                pass
        except urllib2.HTTPError as e:
            if not silent:
                print('HTTP Error %s when fetching %s' % (e.code, url))
        except urllib2.URLError as e:
            pass
        else:
            return body

def _check_update(silent = False):
    check_file = opath.join(_get_temp(), '.MAClient.noupdate')
    if opath.exists(check_file):
        if time.time() - os.path.getmtime(check_file) < 10800:#3小时内只检查一次
            return
        os.remove(check_file)
    if not silent:
        print('Retrieving meta info ...')
    body = _http_get('update/meta.xml', silent)
    if not body:
        print('Error fetching meta')
        return

# Path: xml2dict.py
# class XML2Dict(object):
#     @classmethod
#     def _parse_node(self, node):
#         node_tree = object_dict()
#         # Save attrs and text, hope there will not be a child with same name
#         if node.text:
#             node_tree.value = node.text
#         for (k, v) in node.attrib.items():
#             k, v = self._namespace_split(k, object_dict({'value':v}))
#             node_tree[k] = v
#         # Save childrens
#         for child in iter(node):
#             tag, tree = self._namespace_split(child.tag, self._parse_node(child))
#             if  tag not in node_tree:  # the first time, so store it in dict
#                 node_tree[tag] = tree
#                 continue
#             old = node_tree[tag]
#             if not isinstance(old, list):
#                 node_tree.pop(tag)
#                 node_tree[tag] = [old]  # multi times, so change old dict to a list
#             node_tree[tag].append(tree)  # add the new one
# 
#         return  node_tree
# 
#     @classmethod
#     def _namespace_split(self, tag, value):
#         """
#            Split the tag  '{http://cs.sfsu.edu/csc867/myscheduler}patients'
#              ns = http://cs.sfsu.edu/csc867/myscheduler
#              name = patients
#         """
#         result = re.compile("\{(.*)\}(.*)").search(tag)
#         if result:
#             # print(tag)
#             value.namespace, tag = result.groups()
#         return (tag, value)
# 
#     @classmethod
#     def parse(self, file):
#         """parse a xml file to a dict"""
#         f = open(file, 'r')
#         return self.fromstring(f.read())
# 
#     @classmethod
#     def fromstring(self, s):
#         """parse a string"""
#         t = ET.fromstring(s)
#         root_tag, root_tree = self._namespace_split(t.tag, self._parse_node(t))
#         return object_dict({root_tag: root_tree})


# Path: plugins/plugin_update.py
from _prototype import plugin_prototype
from cross_platform import *
from xml2dict import XML2Dict
    from io import StringIO
    from cStringIO import StringIO
import re
import sys
import time
import os, os.path as opath
import urllib
import gzip
import threading
    import urllib.request as urllib2
    import urllib2
                import maclient
                import maclient_smart

            url = repo + '/raw/' + uri
        try:
            resp = opener.open(urllib2.Request(url, headers=headers), timeout = 15)
            body = resp.read()
            try:
                f = StringIO(body)
                gz = gzip.GzipFile(fileobj = f)
                body = gz.read()
            except:
                pass
        except urllib2.HTTPError as e:
            if not silent:
                print('HTTP Error %s when fetching %s' % (e.code, url))
        except urllib2.URLError as e:
            pass
        else:
            return body

def _check_update(silent = False):
    check_file = opath.join(_get_temp(), '.MAClient.noupdate')
    if opath.exists(check_file):
        if time.time() - os.path.getmtime(check_file) < 10800:#3小时内只检查一次
            return
        os.remove(check_file)
    if not silent:
        print('Retrieving meta info ...')
    body = _http_get('update/meta.xml', silent)
    if not body:
        print('Error fetching meta')
        return

    meta = XML2Dict.fromstring(body).maclient
