kmisc 2.1.100__tar.gz → 2.1.103__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.100
3
+ Version: 2.1.103
4
4
  Summary: Enginering useful library
5
5
  Home-page: https://github.com/kagepark/kmisc
6
6
  Author: Kage Park
@@ -1859,14 +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(email_func=None,email=None,email_title=None,email_server=None,log=None,log_msg='',default=None):
1863
- log_msg=ExceptMessage(msg=log_msg,default=default)
1864
- if log_msg != default:
1865
- if log: log('\n!!ERROR!!: {}'.format(log_msg),log_level=1)
1866
- if email_func and email and email_title:
1867
- a=email_func(email,email_title,log_msg,dj_ip=email_server)
1868
- TIME().Sleep(5)
1869
- return default
1862
+ def code_error(log_msg=None,**opts):
1863
+ return ExceptMessage(msg=log_msg,default=opts.get('default'))
1870
1864
 
1871
1865
  def DirName(src,default=None):
1872
1866
  dirname=Path(src,default=default)
@@ -1938,7 +1932,7 @@ def Keys(src,find=None,start=None,end=None,sym='\n',default=[],word=False,patter
1938
1932
  return rt
1939
1933
  return default
1940
1934
 
1941
- 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):
1942
1936
  #<Menu name="Security">
1943
1937
  # <Setting name="Administrator Password" type="Password">
1944
1938
  # <Information>
@@ -1948,6 +1942,9 @@ def findXML(xmlfile,find_name=None,find_path=None,default=None,out='xmlobj',get_
1948
1942
  #</Menu>
1949
1943
  #findXML(cfg_file,find_name='Administrator Password',find_path='./Information/HasPassword',out='data'))
1950
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)
1951
1948
  if os.path.isfile(xmlfile):
1952
1949
  try:
1953
1950
  tree=ET.parse(xmlfile)
@@ -1959,53 +1956,90 @@ def findXML(xmlfile,find_name=None,find_path=None,default=None,out='xmlobj',get_
1959
1956
  root=ET.fromstring(xmlfile)
1960
1957
  except:
1961
1958
  return default
1962
- def find(tr,find_name):
1959
+ def find_root(tr,find_name):
1963
1960
  for x in tr:
1964
1961
  if x.attrib.get('name') == find_name:
1965
1962
  return x,x.tag
1966
- rt,pp=find(x,find_name)
1963
+ rt,pp=find_root(x,find_name)
1967
1964
  if rt:
1968
1965
  return rt,'{}/{}'.format(x.tag,pp)
1969
1966
  return None,None
1970
- 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
1971
1989
  if find_name:
1972
- found=find(root,find_name)
1973
- if found[0]:
1974
- found_root=found[0]
1975
- if find_path and isinstance(find_path,str):
1976
- #ex: root.findall('./Menu/Setting/[@name="Administrator Password"]/Information/HasPassword'):
1977
- if not found_root: found_root=root
1978
- found_result=found_root.findall(find_path)
1979
- # <element>.tag: name, .text: data, .attrib: dict
1980
- rt=[]
1981
- if out in ['tag','name']:
1982
- for ii in found_result:
1983
- rt.append(ii.tag)
1984
- elif out in ['text','data']:
1985
- 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']):
1986
1998
  if get_opt:
1987
- rt.append(ii.get(get_opt,default))
1999
+ o.append(i.get(get_opt,default))
1988
2000
  else:
1989
- rt.append(ii.text)
1990
- elif out in ['attrib','att']:
1991
- for ii in found_result:
1992
- rt.append(ii.attrib)
1993
- if rt:
1994
- return rt
1995
- else:
1996
- 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
1997
2007
  else:
1998
- if found_root:
1999
- if out in ['tag','name']:
2000
- return found_root.tag
2001
- elif out in ['text','data']:
2002
- if get_opt:
2003
- return found_root.get(get_opt,default)
2004
- else:
2005
- return found_root.text
2006
- elif out in ['attrib','att']:
2007
- return found_root.attrib
2008
- 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
+ #ex: root.findall('./Menu/Setting/[@name="Administrator Password"]/Information/HasPassword'):
2012
+ found_path=find_item(found_root[0],find_path.split('/'))
2013
+ # <element>.tag: name, .text: data, .attrib: dict
2014
+ if found_path[0] is not None:
2015
+ if IsIn(out,['tag','name']):
2016
+ return found_path[0].tag
2017
+ elif IsIn(out,['text','data','value']):
2018
+ if get_opt:
2019
+ return found_path[0].get(get_opt,default)
2020
+ return found_path[0].text
2021
+ elif IsIn(out,['attrib','att','attr']):
2022
+ return found_path[0].attrib
2023
+ elif IsIn(out,['path','key','keys']):
2024
+ print('>>',found_root[1],':',found_path[1])
2025
+ if found_root[1] is None:
2026
+ return found_path[1]
2027
+ return os.path.join(found_root[1],found_path[1])
2028
+ return found_root[0]
2029
+ else:
2030
+ if found_root[0] is not None:
2031
+ if IsIn(out,['tag','name']):
2032
+ return found_root[0].tag
2033
+ elif IsIn(out,['text','data','value']):
2034
+ if get_opt:
2035
+ return found_root[0].get(get_opt,default)
2036
+ else:
2037
+ return found_root[0].text
2038
+ elif IsIn(out,['attrib','att','attr']):
2039
+ return found_root[0].attrib
2040
+ elif IsIn(out,['path','key','keys']):
2041
+ return found_root[1]
2042
+ return found_root[0]
2009
2043
  return default
2010
2044
 
2011
2045
  def Compress(data,mode='lz4'):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: kmisc
3
- Version: 2.1.100
3
+ Version: 2.1.103
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