kmisc 2.1.116__py3-none-any.whl → 2.1.118__py3-none-any.whl

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.
kmisc/__init__.py CHANGED
@@ -1954,6 +1954,91 @@ 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
+
1977
+ attr_root=root.attrib
1978
+ root_tag=attr_root.get('name',root.tag)
1979
+ out={root_tag:attr_root}
1980
+ d_root=out[root_tag]
1981
+ new_sub=sub+1
1982
+ for x in root:
1983
+ if isinstance(x,str): continue
1984
+ if path:
1985
+ if len(path) <= sub: continue
1986
+ if isinstance(path[sub],tuple): #tuple then choose only same key
1987
+ if root_tag not in path[sub]:
1988
+ continue
1989
+ elif isinstance(path[sub],set): #set then ignore the choosed names, but others are ok
1990
+ if root_tag in path[sub]:
1991
+ continue
1992
+ else:
1993
+ if path[sub] != '*' and root_tag != path[sub]:
1994
+ continue
1995
+
1996
+ # for <Subtitle> ... </Subtitle>
1997
+ # for <Text> ... </Text>
1998
+ attr_x=x.attrib
1999
+ x_tag=attr_x.get('name',attr_x.get('id',x.tag)) #<tag name=xxx id=xxx> ... </tag>
2000
+ #Special Tag
2001
+ if x_tag == 'Subtitle': #Subtitle
2002
+ if x_tag not in d_root:
2003
+ d_root[x_tag]=[]
2004
+ d_root[x_tag].append([x.text])
2005
+ elif x_tag == 'Text': #Text
2006
+ if 'Subtitle' in d_root:
2007
+ d_root['Subtitle'][-1].append(x.text) # Adding Text string to Subtitle when exist Subtitle
2008
+ else:
2009
+ if x_tag not in d_root:
2010
+ d_root[x_tag]=[]
2011
+ d_root[x_tag].append(x.text) # Adding Text string to Subtitle when exist Subtitle
2012
+ elif x_tag == 'Option': #option case
2013
+ if x_tag not in d_root:
2014
+ d_root[x_tag]=[]
2015
+ d_root[x_tag].append(attr_x) # {...}
2016
+ x_data=x.text # <>text</> put at "data" key's value
2017
+ sub_data=XML2Dict(x,path=path,sub=sub+1,ignore_value=ignore_value)
2018
+ sub_key=next(iter(sub_data))
2019
+ if sub_data[sub_key]:
2020
+ for i in sub_data[sub_key]:
2021
+ d_root[x_tag][-1][i]=sub_data[sub_key][i]
2022
+ if isinstance(x_data,str):
2023
+ _x_=x_data.strip()
2024
+ if _x_ == '\n' or not _x_ or _x_ in ignore_value:
2025
+ continue
2026
+ d_root[x_tag][-1]['data']=x_data
2027
+ else: #Normal case
2028
+ d_root[x_tag]=attr_x # {...}
2029
+ x_data=x.text # <>text</> put at "data" key's value
2030
+ sub_data=XML2Dict(x,path=path,sub=sub+1,ignore_value=ignore_value)
2031
+ sub_key=next(iter(sub_data))
2032
+ if sub_data[sub_key]:
2033
+ for i in sub_data[sub_key]:
2034
+ d_root[x_tag][i]=sub_data[sub_key][i]
2035
+ if isinstance(x_data,str):
2036
+ _x_=x_data.strip()
2037
+ if _x_ == '\n' or not _x_ or _x_ in ignore_value:
2038
+ continue
2039
+ d_root[x_tag]['data']=x_data
2040
+ return out
2041
+
1957
2042
  def findXML(xmlfile,find_name=None,find_path=None,default=None,out='xmlobj',get_opt=None,find_all=False):
1958
2043
  #<Menu name="Security">
1959
2044
  # <Setting name="Administrator Password" type="Password">
@@ -2783,7 +2868,7 @@ def kmp(mp={},func=None,name=None,timeout=0,quit=False,log_file=None,log_screen=
2783
2868
  return rc
2784
2869
 
2785
2870
  for n in [k for k in mp]:
2786
- if isinstance(mp[k],dict):
2871
+ if isinstance(mp[n],dict):
2787
2872
  timeout=mp[n].get('timeout',0)
2788
2873
  if quit is True or timeout > 0 and TIME().Int() > timeout:
2789
2874
  if n != 'log':
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: kmisc
3
- Version: 2.1.116
3
+ Version: 2.1.118
4
4
  Summary: Enginering useful library
5
5
  Home-page: https://github.com/kagepark/kmisc
6
6
  Author: Kage Park
@@ -0,0 +1,6 @@
1
+ kmisc/__init__.py,sha256=wybypc4DfANjjg3IalmsLEIsoVfPHjSqFszB838i_GQ,144575
2
+ kmisc-2.1.118.dist-info/LICENSE,sha256=mn9ekhb34HJxsrVhcxrLXJUzy55T62zg-Gh9Ro0mVJI,1066
3
+ kmisc-2.1.118.dist-info/METADATA,sha256=Vw_j4-z3QigzXLGwtKYvzJIs9ltdup9fVKz8xrQIvic,5523
4
+ kmisc-2.1.118.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
5
+ kmisc-2.1.118.dist-info/top_level.txt,sha256=wvdHf5aQTqcGYvxk-F9E_BMWLMhlwC8INBmwO-V6_X4,6
6
+ kmisc-2.1.118.dist-info/RECORD,,
@@ -1,6 +0,0 @@
1
- kmisc/__init__.py,sha256=-z35GjgAzX_LWAy8tm6p3KHixrHDGEcdOLEdW7AzxTQ,141127
2
- kmisc-2.1.116.dist-info/LICENSE,sha256=mn9ekhb34HJxsrVhcxrLXJUzy55T62zg-Gh9Ro0mVJI,1066
3
- kmisc-2.1.116.dist-info/METADATA,sha256=3QeoLeH-ktOpycmC1c1nYxysTa201drbax0MqCG0Bw8,5523
4
- kmisc-2.1.116.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
5
- kmisc-2.1.116.dist-info/top_level.txt,sha256=wvdHf5aQTqcGYvxk-F9E_BMWLMhlwC8INBmwO-V6_X4,6
6
- kmisc-2.1.116.dist-info/RECORD,,