kmisc 2.1.102__tar.gz → 2.1.104__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.102
3
+ Version: 2.1.104
4
4
  Summary: Enginering useful library
5
5
  Home-page: https://github.com/kagepark/kmisc
6
6
  Author: Kage Park
@@ -1859,8 +1859,8 @@ def cut_string(string,max_len=None,sub_len=None,new_line='\n',front_space=False,
1859
1859
  def FirstKey(src,default=None):
1860
1860
  return Next(src,default=default)
1861
1861
 
1862
- def code_error(log_msg=None):
1863
- return ExceptMessage(msg=log_msg)
1862
+ def code_error(log_msg=None,**opts):
1863
+ return ExceptMessage(msg=log_msg,default=opts.get('default'))
1864
1864
 
1865
1865
  def DirName(src,default=None):
1866
1866
  dirname=Path(src,default=default)
@@ -1932,7 +1932,7 @@ def Keys(src,find=None,start=None,end=None,sym='\n',default=[],word=False,patter
1932
1932
  return rt
1933
1933
  return default
1934
1934
 
1935
- def findXML(xmlfile,find_name=None,find_path=None,default=None,out='xmlobj',get_opt=None):
1935
+ def findXML(xmlfile,find_name=None,find_path=None,default=None,out='xmlobj',get_opt=None,find_all=False):
1936
1936
  #<Menu name="Security">
1937
1937
  # <Setting name="Administrator Password" type="Password">
1938
1938
  # <Information>
@@ -1942,6 +1942,9 @@ def findXML(xmlfile,find_name=None,find_path=None,default=None,out='xmlobj',get_
1942
1942
  #</Menu>
1943
1943
  #findXML(cfg_file,find_name='Administrator Password',find_path='./Information/HasPassword',out='data'))
1944
1944
  # => False
1945
+ # find_name : for finding XML root with maching data of tag name 'name'
1946
+ # find_path : it searching item keys(ex: Setting/Information/HasPassword) from found XML root
1947
+ # get_opt : tag name (ex: name, type)
1945
1948
  if os.path.isfile(xmlfile):
1946
1949
  try:
1947
1950
  tree=ET.parse(xmlfile)
@@ -1953,53 +1956,91 @@ def findXML(xmlfile,find_name=None,find_path=None,default=None,out='xmlobj',get_
1953
1956
  root=ET.fromstring(xmlfile)
1954
1957
  except:
1955
1958
  return default
1956
- def find(tr,find_name):
1959
+ def find_root(tr,find_name):
1957
1960
  for x in tr:
1958
1961
  if x.attrib.get('name') == find_name:
1959
1962
  return x,x.tag
1960
- rt,pp=find(x,find_name)
1963
+ rt,pp=find_root(x,find_name)
1961
1964
  if rt:
1962
1965
  return rt,'{}/{}'.format(x.tag,pp)
1963
1966
  return None,None
1964
- found_root=None
1967
+ def find_item(tr,find_path):
1968
+ for x in tr:
1969
+ if x.tag == find_path[0]:
1970
+ if find_path[1:]:
1971
+ a,p=find_item(x,find_path[1:])
1972
+ if a is not None:
1973
+ return a,'{}/{}'.format(find_path[0],p)
1974
+ else:
1975
+ return x,find_path[0]
1976
+ return None,None
1977
+ def find_all_items(tr,find_path):
1978
+ out=[]
1979
+ for i in tr:
1980
+ a=i.findall(find_path)
1981
+ if a:
1982
+ out=out+a
1983
+ o=find_all_items(i,find_path)
1984
+ if o:
1985
+ out=out+o
1986
+ return out
1987
+ found_root=None,None
1988
+ #find XML root with name tag information or root's tag name
1965
1989
  if find_name:
1966
- found=find(root,find_name)
1967
- if found[0]:
1968
- found_root=found[0]
1969
- if find_path and isinstance(find_path,str):
1970
- #ex: root.findall('./Menu/Setting/[@name="Administrator Password"]/Information/HasPassword'):
1971
- if not found_root: found_root=root
1972
- found_result=found_root.findall(find_path)
1973
- # <element>.tag: name, .text: data, .attrib: dict
1974
- rt=[]
1975
- if out in ['tag','name']:
1976
- for ii in found_result:
1977
- rt.append(ii.tag)
1978
- elif out in ['text','data']:
1979
- for ii in found_result:
1990
+ found_root=find_root(root,find_name)
1991
+ if found_root[0] is None: found_root=root,None
1992
+ if find_all:
1993
+ o=[]
1994
+ for i in find_all_items(found_root[0],find_path):
1995
+ if IsIn(out,['tag','name']):
1996
+ o.append(i.tag)
1997
+ elif IsIn(out,['text','data','value']):
1980
1998
  if get_opt:
1981
- rt.append(ii.get(get_opt,default))
1999
+ o.append(i.get(get_opt,default))
1982
2000
  else:
1983
- rt.append(ii.text)
1984
- elif out in ['attrib','att']:
1985
- for ii in found_result:
1986
- rt.append(ii.attrib)
1987
- if rt:
1988
- return rt
1989
- else:
1990
- return found_result
2001
+ o.append(i.text)
2002
+ elif IsIn(out,['attrib','att','attr']):
2003
+ o.append(i.attrib)
2004
+ else:
2005
+ o.append(i)
2006
+ return o
1991
2007
  else:
1992
- if found_root:
1993
- if out in ['tag','name']:
1994
- return found_root.tag
1995
- elif out in ['text','data']:
1996
- if get_opt:
1997
- return found_root.get(get_opt,default)
1998
- else:
1999
- return found_root.text
2000
- elif out in ['attrib','att']:
2001
- return found_root.attrib
2002
- return found_root
2008
+ # Searching path from found root or original root
2009
+ if find_path and isinstance(find_path,str):
2010
+ if find_path[0] == '/': find_path=find_path[1:]
2011
+ elif len(find_path) > 2 and find_path[:2] == './': find_path=find_path[2:]
2012
+ #ex: root.findall('./Menu/Setting/[@name="Administrator Password"]/Information/HasPassword'):
2013
+ found_path=find_item(found_root[0],find_path.split('/'))
2014
+ # <element>.tag: name, .text: data, .attrib: dict
2015
+ if found_path[0] is not None:
2016
+ if IsIn(out,['tag','name']):
2017
+ return found_path[0].tag
2018
+ elif IsIn(out,['text','data','value']):
2019
+ if get_opt:
2020
+ return found_path[0].get(get_opt,default)
2021
+ return found_path[0].text
2022
+ elif IsIn(out,['attrib','att','attr']):
2023
+ return found_path[0].attrib
2024
+ elif IsIn(out,['path','key','keys']):
2025
+ print('>>',found_root[1],':',found_path[1])
2026
+ if found_root[1] is None:
2027
+ return found_path[1]
2028
+ return os.path.join(found_root[1],found_path[1])
2029
+ return found_root[0]
2030
+ else:
2031
+ if found_root[0] is not None:
2032
+ if IsIn(out,['tag','name']):
2033
+ return found_root[0].tag
2034
+ elif IsIn(out,['text','data','value']):
2035
+ if get_opt:
2036
+ return found_root[0].get(get_opt,default)
2037
+ else:
2038
+ return found_root[0].text
2039
+ elif IsIn(out,['attrib','att','attr']):
2040
+ return found_root[0].attrib
2041
+ elif IsIn(out,['path','key','keys']):
2042
+ return found_root[1]
2043
+ return found_root[0]
2003
2044
  return default
2004
2045
 
2005
2046
  def Compress(data,mode='lz4'):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: kmisc
3
- Version: 2.1.102
3
+ Version: 2.1.104
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