kmisc 2.1.117__tar.gz → 2.1.119__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: kmisc
3
- Version: 2.1.117
3
+ Version: 2.1.119
4
4
  Summary: Enginering useful library
5
5
  Home-page: https://github.com/kagepark/kmisc
6
6
  Author: Kage Park
@@ -1954,6 +1954,104 @@ def Keys(src,find=None,start=None,end=None,sym='\n',default=[],word=False,patter
1954
1954
  return rt
1955
1955
  return default
1956
1956
 
1957
+ def XML2Dict(root,path=[],sub=0,ignore_value=['\n']):
1958
+ #path: {a:{
1959
+ # b: {
1960
+ # c: {...},
1961
+ # d: {...},
1962
+ # e: {...},
1963
+ # f: {...},
1964
+ #path=['a','b',('c','d')] # 3rd is c and d only and others are ignore
1965
+ #if anything in the level b, but 3rd is c and d only and others are ignore
1966
+ #path=['a','*',('c','d')]
1967
+ #exclude 'c' and 'e' only at the 3rd level and others are ok in 3rd level
1968
+ #path=['a','*',{'c','e'}]
1969
+ if isinstance(root,str):
1970
+ if os.path.isfile(root):
1971
+ try:
1972
+ tree=ET.parse(root)
1973
+ root=tree.getroot()
1974
+ except:
1975
+ return False
1976
+ def ignore(a,b):
1977
+ if isinstance(b,tuple) and a not in b:
1978
+ return True
1979
+ elif isinstance(b,set) and a in b:
1980
+ return True
1981
+ elif not isinstance(b,(tuple,set)) and b != '*' and a != b:
1982
+ return True
1983
+ return False
1984
+
1985
+ attr_root=root.attrib
1986
+ root_tag=attr_root.get('name',root.tag)
1987
+ #Current Path (root)
1988
+ if path and len(path) > sub:
1989
+ if ignore(root_tag,path[sub]): return {}
1990
+ out={root_tag:{}}
1991
+ for i in attr_root:
1992
+ if path and len(path) > sub+1:
1993
+ if ignore(i,path[sub+1]): continue
1994
+ out[root_tag][i]=attr_root[i]
1995
+ d_root=out[root_tag]
1996
+ for x in root:
1997
+ if isinstance(x,str): continue
1998
+ #Sub Path
1999
+ # for <Subtitle> ... </Subtitle>
2000
+ # for <Text> ... </Text>
2001
+ attr_x=x.attrib
2002
+ x_tag=attr_x.get('name',attr_x.get('id',x.tag)) #<tag name=xxx id=xxx> ... </tag>
2003
+ if path and len(path) > sub+1:
2004
+ if ignore(x_tag,path[sub+1]): continue
2005
+ #Special Tag
2006
+ if x_tag == 'Subtitle': #Subtitle
2007
+ if x_tag not in d_root:
2008
+ d_root[x_tag]=[]
2009
+ d_root[x_tag].append([x.text])
2010
+ elif x_tag == 'Text': #Text
2011
+ if 'Subtitle' in d_root:
2012
+ d_root['Subtitle'][-1].append(x.text) # Adding Text string to Subtitle when exist Subtitle
2013
+ else:
2014
+ if x_tag not in d_root:
2015
+ d_root[x_tag]=[]
2016
+ d_root[x_tag].append(x.text) # Adding Text string to Subtitle when exist Subtitle
2017
+ elif x_tag == 'Option': #option case
2018
+ if x_tag not in d_root:
2019
+ d_root[x_tag]=[]
2020
+ d_root[x_tag].append(attr_x) # {...}
2021
+ x_data=x.text # <>text</> put at "data" key's value
2022
+ sub_data=XML2Dict(x,path=path,sub=sub+1,ignore_value=ignore_value)
2023
+ sub_key=next(iter(sub_data))
2024
+ if sub_data[sub_key]:
2025
+ for i in sub_data[sub_key]:
2026
+ d_root[x_tag][-1][i]=sub_data[sub_key][i]
2027
+ if isinstance(x_data,str):
2028
+ _x_=x_data.strip()
2029
+ if _x_ == '\n' or not _x_ or _x_ in ignore_value:
2030
+ continue
2031
+ d_root[x_tag][-1]['data']=x_data
2032
+ else: #Normal case
2033
+ #Sub's sub
2034
+ #d_root[x_tag]=attr_x # {...}
2035
+ d_root[x_tag]={}
2036
+ for i in attr_x:
2037
+ if path and len(path) > sub+1:
2038
+ if ignore(i,path[sub+1]): continue
2039
+ d_root[x_tag][i]=attr_x[i]
2040
+
2041
+ x_data=x.text # <>text</> put at "data" key's value
2042
+ sub_data=XML2Dict(x,path=path,sub=sub+1,ignore_value=ignore_value)
2043
+ if len(sub_data) == 1:
2044
+ sub_key=next(iter(sub_data))
2045
+ if sub_data[sub_key]:
2046
+ for i in sub_data[sub_key]:
2047
+ d_root[x_tag][i]=sub_data[sub_key][i]
2048
+ if isinstance(x_data,str):
2049
+ _x_=x_data.strip()
2050
+ if _x_ == '\n' or not _x_ or _x_ in ignore_value:
2051
+ continue
2052
+ d_root[x_tag]['data']=x_data
2053
+ return out
2054
+
1957
2055
  def findXML(xmlfile,find_name=None,find_path=None,default=None,out='xmlobj',get_opt=None,find_all=False):
1958
2056
  #<Menu name="Security">
1959
2057
  # <Setting name="Administrator Password" type="Password">
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: kmisc
3
- Version: 2.1.117
3
+ Version: 2.1.119
4
4
  Summary: Enginering useful library
5
5
  Home-page: https://github.com/kagepark/kmisc
6
6
  Author: Kage Park
File without changes
File without changes
File without changes
File without changes
File without changes