#!/usr/bin/env python3 # notices in the dictionary ZM from sys import executable ZM = { # ZM: Zorro Metadata 'version' : '2' , # zorro.py version 'interpreter_file' : executable , # https://docs.python.org/3/library/sys.html 'title' : 'Zorro module' , # Dublin Core 'description' : 'module for Zorro' , 'date' : '2023-11-01' , 'creator' : 'M.T. Carrasco Benitez' , 'rights' : 'CC BY-SA - Creative Commons Attribution-ShareAlike' } from datetime import datetime TIMESTAMP = datetime.now().strftime ('%y%m%d-%H%M%S') #------------------------------------------------#-------------------------------------------# def progname () : """Get the program name""" from sys import argv return basename ( argv [0] ) #------------------------------------------------#-------------------------------------------# def now () : """Get the current time as YYMMDD-HHMMSS""" from datetime import datetime return datetime.now().strftime ( '%y%m%d-%H%M%S' ) #------------------------------------------------#-------------------------------------------# def utcnow () : """Get the current UTC time as ISO 8601 YYYYMMDDTHHMMSSZ""" from datetime import datetime return datetime.utcnow().strftime ( '%Y%m%dT%H%M%SZ' ) #------------------------------------------------#-------------------------------------------# def basename ( sp ) : # sp=string path - /aa/bb/cc """Get the basename - like the Unix command""" from os import path return path.basename ( path.realpath ( sp )) # /aa/bb #------------------------------------------------#-------------------------------------------# def dirname ( sp ) : # sp=string path - /aa/bb/cc """Return the dirname - like the Unix command""" from os import path s = path.dirname ( sp ) if s == '' : s = '.' # if sp="cc", return "." return s # cc #------------------------------------------------#-------------------------------------------# def mkdir ( dn ) : # dn=directory name """Make a new directory - like the Unix command 'mkdir -p'""" from os import path , makedirs if not path.exists ( dn ) : makedirs ( dn , mode=0o755 , exist_ok=True ) #------------------------------------------------#-------------------------------------------# def append_text ( text , fn ) : # fn=filename """Append text to a file""" with open ( fn , 'a', encoding='utf-8') as so: so.write ( text ) #------------------------------------------------#-------------------------------------------# def read_text ( fn ) : # fn=filename """Read text from a file""" with open ( fn , mode = 'r' ) as so : return str (so.read () ).rstrip () #------------------------------------------------#-------------------------------------------# def write_text ( text , fn ) : # fn=filename """Overwrite text to a file""" with open ( fn , 'w', encoding='utf-8') as so: so.write ( text ) #------------------------------------------------#-------------------------------------------# def read_record_jar ( fn ) : # fn=filename """Read record-jar file and return as a dict""" try : rec = 1 tab = {} with open ( fn , mode = 'r' ) as so : for line in so : line = line.rstrip () if line == '%%' : rec += 1 ; continue key , val = line.partition (":") [::2] if rec not in tab : tab [rec] = {} tab [rec][key] = val return tab except : print ( 'error_read_record_jar' , fn ) exit ( 1 ) #------------------------------------------------#-------------------------------------------# def read_json ( fn ) : # fn=filename """Read json file and return as a dict""" from json import load try : with open ( fn , 'r' ) as fo : return load ( fo ) except : print ( 'error_read_json' , fn ) exit ( 1 ) #------------------------------------------------#-------------------------------------------# def write_json ( dic , fn ) : # dic=dictionary fn=filename """Write dict to json file""" from json import dump mkdir ( dirname ( fn )) # fn=filename with open ( fn , 'w' , encoding='utf-8' ) as fo : dump ( dic , fo , indent=2 , ensure_ascii=False ) nkeys = len ( dic.keys ()) return nkeys #------------------------------------------------#-------------------------------------------# def writezip ( indir , outzip ) : # indir=input directory - outzip=output zip file """Create a zip file""" from os import getcwd , chdir , walk , path from zipfile import ZipFile oldpwd = getcwd () # indir: /xx/yy/zz indir_d = path.dirname ( indir ) # indir_d=indir dirname - /xx/yy indir_b = basename ( indir ) # indir_b=indir basename - zz chdir ( indir_d ) with ZipFile ( outzip , 'w' ) as so : for dname , dirs , files in walk ( indir_b ) : for bname in files: ppath = path.join ( dname , bname ) so.write ( ppath ) chdir ( oldpwd ) #------------------------------------------------#-------------------------------------------# def delete_nlines ( q , ln , fn ) : ''' Delete q lines from ln in file fn q:quantity of lines to delete | ln:line number | fn: filename ''' i = 0 buf = '' with open ( fn , mode = 'r' ) as fo : for line in fo : i += 1 if i >= ln and i <= ln + q : continue buf = buf + line with open ( fn , 'w', encoding='utf-8') as fo: fo.write ( buf ) ##