# 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})


import os
import re
import os.path as opath
import sys
    import base64
    import time
from xml2dict import XML2Dict
from cross_platform import *
    from httplib2 import Http
    if itemrev:
        rev[2] = str(itemrev)
    if bossrev:
        rev[3] = str(bossrev)
    if cborev:
        rev[4] = str(cborev)
    rev_str = rev_str.replace(r, ','.join(rev))
    open(opath.join(getPATH0, 'db/revision.txt'), 'w').write(rev_str)

def check_revision(loc, rev_tuple):
    rev = get_revision(loc) + [0, 0]#legacy db support
    if rev:
        return rev_tuple[0] > float(rev[0]), rev_tuple[1] > float(rev[1]), rev_tuple[2] > float(rev[2]), rev_tuple[3] > float(rev[3])
    else:
        return False, False, False, False

def update_master(loc, need_update, poster):
    replace_AND = re.compile('&(?!#)')#no CDATA, sad
    #card, item, boss, combo
    new_rev = [None, None, None, None]
    for s in poster.ht.connections:#cleanup socket pool
        poster.ht.connections[s].close()
    poster.ht.connections = {}
    poster.set_timeout(240)
    if loc == 'jp':
        postdata = ''
    else:
        postdata = '%s&revision=0' % poster.cookie
    if need_update[0]:
        a, b = poster.post('masterdata/card/update', postdata = postdata)

# 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: maclient_update.py
import os
import re
import os.path as opath
import sys
    import base64
    import time
from xml2dict import XML2Dict
from cross_platform import *
    from httplib2 import Http

    if itemrev:
        rev[2] = str(itemrev)
    if bossrev:
        rev[3] = str(bossrev)
    if cborev:
        rev[4] = str(cborev)
    rev_str = rev_str.replace(r, ','.join(rev))
    open(opath.join(getPATH0, 'db/revision.txt'), 'w').write(rev_str)

def check_revision(loc, rev_tuple):
    rev = get_revision(loc) + [0, 0]#legacy db support
    if rev:
        return rev_tuple[0] > float(rev[0]), rev_tuple[1] > float(rev[1]), rev_tuple[2] > float(rev[2]), rev_tuple[3] > float(rev[3])
    else:
        return False, False, False, False

def update_master(loc, need_update, poster):
    replace_AND = re.compile('&(?!#)')#no CDATA, sad
    #card, item, boss, combo
    new_rev = [None, None, None, None]
    for s in poster.ht.connections:#cleanup socket pool
        poster.ht.connections[s].close()
    poster.ht.connections = {}
    poster.set_timeout(240)
    if loc == 'jp':
        postdata = ''
    else:
        postdata = '%s&revision=0' % poster.cookie
    if need_update[0]:
        a, b = poster.post('masterdata/card/update', postdata = postdata)

        resp = XML2Dict().fromstring(replace_AND.sub('&amp;', b)).response  # 不替换会解析出错摔