# 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, os.path as opath
import sys
from cross_platform import *
from xml2dict import XML2Dict
def _set_cfg_val(cf, sec, key, val):
    if not cf.has_section(sec):
        cf.add_section(sec)
    cf.set(sec, key, val)

def _write_cfg(cf, fname):
    f = open(fname, "w")
    cf.write(f)
    f.flush()

def maw_importter(plugin_vals):
    def do(*args):
        logger = plugin_vals['logger']
        cf = plugin_vals['cf']
        cfg_file = args[0] if (args[0] and opath.exists(args[0])) else None
        cfg_file = opath.join(getPATH0, 'config.xml') \
                if (opath.exists(opath.join(getPATH0, 'config.xml'))) \
                else cfg_file
        while not cfg_file:
            cfg_file = raw_inputd('输入配置文件的路径 > ')
            if not cfg_file:
                return
            if opath.exists(cfg_file):
                break
            if opath.exists(opath.join(cfg_file, 'config.xml')):
                cfg_file = opath.join(cfg_file, 'config.xml')
                break
            logger.error('输入的路径"%s"不存在配置文件www' % cfg_file)
            cfg_file = None
        try:

# 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/maw_carddeck_importter.py
import os, os.path as opath
import sys
from cross_platform import *
from xml2dict import XML2Dict

def _set_cfg_val(cf, sec, key, val):
    if not cf.has_section(sec):
        cf.add_section(sec)
    cf.set(sec, key, val)

def _write_cfg(cf, fname):
    f = open(fname, "w")
    cf.write(f)
    f.flush()

def maw_importter(plugin_vals):
    def do(*args):
        logger = plugin_vals['logger']
        cf = plugin_vals['cf']
        cfg_file = args[0] if (args[0] and opath.exists(args[0])) else None
        cfg_file = opath.join(getPATH0, 'config.xml') \
                if (opath.exists(opath.join(getPATH0, 'config.xml'))) \
                else cfg_file
        while not cfg_file:
            cfg_file = raw_inputd('输入配置文件的路径 > ')
            if not cfg_file:
                return
            if opath.exists(cfg_file):
                break
            if opath.exists(opath.join(cfg_file, 'config.xml')):
                cfg_file = opath.join(cfg_file, 'config.xml')
                break
            logger.error('输入的路径"%s"不存在配置文件www' % cfg_file)
            cfg_file = None
        try:

            _t = XML2Dict.fromstring(open(cfg_file).read()).config.card