def check(fname, verbose=False): '''Function checks a file for existence''' ok = False console.log(f'Check: {fname}', verbose) try: path = Path(fname) # Python 3.4 if path.exists(): # Check if is there file ok = True else: raise ValueError(f'File does not exist') except ValueError as e: console.log(vt.error('Check', e), verbose) except Exception as e: console.log(vt.error('Check', e), verbose) else: console.log(vt.succes('Check'), verbose) return ok
def open_with_app(fname, verbose=False): '''Function opens a file''' try: # should work on Windows os.startfile(fname) except AttributeError: try: # Linux subprocess.call(['open', fname]) except Exception as e: console.log(vt.error('Open', e), verbose) else: console.log(vt.succes('Check'), verbose)
def download(url, file, verbose=False): ok = False console.log(f'Download: {url}', verbose) console.log(f'To: {file}', verbose) try: with threading.Lock(): urllib.request.urlretrieve(url, file) except Exception as e: console.log(vt.error('Download', e), verbose) else: console.log(vt.succes('Download'), verbose) ok = True return ok
def read(fname, verbose=False): '''Reads data content from a file''' ok, t = False, '' console.log(f'Read: {fname}', verbose) if check(fname): try: with open(fname, 'r') as f: t = f.read() except Exception as e: console.log(vt.error('Read', e), verbose) else: console.log(vt.succes('Read'), verbose) ok = True return ok, t
def unzip(zip, txt, verbose=False): ok = False console.log(f'Unzip: {zip}', verbose) console.log(f'To: {txt}', verbose) # TODO force to txt file try: with threading.Lock(): dir_txt = os.path.dirname(txt) with ZipFile(zip, 'r') as z: z.extractall(dir_txt) except Exception as e: console.log(vt.error('Unzip', e), verbose) else: console.log(vt.succes('Unzip'), verbose) ok = True return ok
def delete(fname, verbose=False): '''Function deletes a file if exists''' ok = False console.log(f'Delete: {fname}', verbose) if check(fname): try: Path(fname).unlink() # Remove file except Exception as e: console.log(vt.error('Delete', e), verbose) else: console.log(vt.succes('Delete'), verbose) ok = True else: console.log(f'Delete cannot. File does not exist', verbose) return ok
def mk_dir(path, verbose=False): '''Function makes a map if not already exists''' ok = False console.log(f'Make dir: {path}', verbose) try: if os.path.isdir(path): console.log('Map not made because it already exists.', verbose) return True else: os.makedirs(path) except Exception as e: console.log(vt.error('Make directory', e), verbose) else: console.log(vt.succes('Make directory'), verbose) ok = True return ok
def write(fname='dummy.txt', content='', prefix='w', encoding='utf-8', verbose=False): '''Function writes or makes a file''' ok = False console.log(f'Write: {fname}', verbose) try: with open(fname, prefix, encoding=encoding) as f: f.write(content) except Exception as e: console.log(vt.error('Write', e), verbose) else: console.log(vt.succes('Write'), verbose) ok = True return ok
def request(url, type='txt', verbose=False): '''Function makes the request based on the url given as parameter The return values are: ok, True if success else False... And the text From the request.''' ok, t = False, '' console.log(f'Request: {url}', verbose) try: with threading.Lock(): resp = urllib.request.urlopen(url) data = resp.read() if type == 'text': t = data elif type == 'json': t = json.loads(data) except Exception as e: console.log(vt.error('Request', e), verbose) else: console.log(vt.succes('Request'), verbose) ok = True return ok, t
def read( station ): '''Reads data dayvalues from the knmi into a list''' ok, data, file_name = '', False, station.data_txt_path console.log(f'Read {station.wmo} {station.place}') with threading.Lock(): try: data = np.genfromtxt( file_name, dtype=np.float64, delimiter=station.data_delimiter, missing_values=station.data_missing, filling_values=np.nan, skip_header=station.data_skip_header, skip_footer=station.data_skip_footer, comments=station.data_comments_sign, autostrip=True, usemask=True ) except Exception as e: console.log(vt.error('Read', e)) else: console.log(vt.succes('Read')) ok = True return ok, data
def yyyymmdd(ymd): '''Function validates date''' ok = False ymd = str(ymd) console.log(f'Validate {ymd}') if len(ymd) != 8: console.log( 'Date has wrong length. Use format of yyyymmdd with length is 8') elif not ymd.isdigit(): console.log('Date has wrong chars. Date must only contain digits') else: try: y, m, d = int(ymd[:4]), int(ymd[4:6]), int(ymd[6:8]) date = datetime.datetime(y, m, d) except Exception as e: console.log(vt.error('Validate', e)) else: # Geen datum in de toekomst if date > datetime.datetime.now(): console.log('Date is in the future. Try again later... ;-)') else: console.log(vt.succes('Validate')) ok = True return ok