Пример #1
0
  filename:str = output filename
  items:str|dict = set of items to be written in file
    if items:str, it is written unchanged at 'start' line index
    if items:dict, it is first converted into an INI string
  (start,stop,step):(int,int,int) = standard slice parameters for lines
    default values for (start,stop,step) replaces the whole file content
    default values for (stop,step) inserts items at line index 'start'
    any other combination replaces the lines in slice(start,stop,step)
  sep:str = line separator string
  Note: the function always returns the whole file content as a string
  """
    # nosections: check whether there are sections or not in input dictionary
    nosections = lambda dic: not any(isinstance(d, dict) for d in dic.values())
    # order: return items from dictionary with alphabetically ordered keys
    order = lambda dic: sorted(dic.items())
    if isinstance(items, dict):
        if nosections(items): items = {'': items}  # create a default section
        items = '\n'.join(("\n[%s]\n" % sect if sect else '') +
                          '\n'.join("%s = %s" % (n, v) for n, v in order(prop))
                          for sect, prop in order(items))
    else:
        items = str(items)
    return write_txt(filename, items, start, stop, step, sep)


# ==============================================================================
if __name__ == '__main__':
    from ezCLIdemo import ezCLIdemo
    ezCLIdemo()
# ==============================================================================
Пример #2
0
def write_ini(filename, items, start=None, stop=0, step=1, sep='\n'):
  """replace or insert a set of items in the content of an INI file

  filename:str = output filename
  items:str|dict = set of items to be written in file
    if items:str, it is written unchanged at 'start' line index
    if items:dict, it is first converted into an INI string
  (start,stop,step):(int,int,int) = standard slice parameters for lines
    default values for (start,stop,step) replaces the whole file content
    default values for (stop,step) inserts items at line index 'start'
    any other combination replaces the lines in slice(start,stop,step)
  sep:str = line separator string
  Note: the function always returns the whole file content as a string
  """
  # nosections: check whether there are sections or not in input dictionary
  nosections = lambda dic: not any(isinstance(d,dict) for d in dic.values())
  # order: return items from dictionary with alphabetically ordered keys
  order = lambda dic: sorted(dic.items())
  if isinstance(items,dict):
    if nosections(items): items = {'': items} # create a default section
    items = '\n'.join(("\n[%s]\n" % sect if sect else '') + '\n'.join("%s = %s"
            % (n,v) for n,v in order(prop)) for sect, prop in order(items))
  else: items = str(items)
  return write_txt(filename, items, start, stop, step, sep) 

# ==============================================================================
if __name__ == '__main__':
  from ezCLIdemo import ezCLIdemo
  ezCLIdemo()
# ==============================================================================