Source code for tkinterquickhelper.funcwin.storing_functions

# -*- coding: utf-8 -*-
"""
Common functions used for :class:`FrameFunction <tkinterquickhelper.funcwin.frame_function.FrameFunction>` and :class:`FrameParams`.


:githublink:`%|py|6`
"""
import os
import copy
import datetime
import hashlib

from pyquickhelper.loghelper.convert_helper import str2datetime
from pyquickhelper.loghelper.flog import fLOG, guess_machine_parameter


[docs]def _private_store(function_name, param): """ Stores the parameters into a file, the function adds the parameter in a new line. It a parameter is a password (*name=password*, the password will be encrypted using sha1). :param function_name: name of the function (a unique string, the function is not called) :param param: (dict) list of parameters The function replaces every end of line into ``#*###n####*#``. :githublink:`%|py|25` """ param = copy.copy(param) rem = [] for r in param: if r.startswith("_"): rem.append(r) for r in rem: del param[r] values = guess_machine_parameter() filename = os.path.join( values["TEMP"], function_name + ".tkinterquickhelper.txt") fLOG("FrameWindows: storing parameters in file: ", os.path.abspath(filename)) if "password" in param or \ "password1" in param or \ "password2" in param or \ "password3" in param: param = param.copy() for k in ["password", "password1", "password2", "password3"]: if k in param: if param[k] is None: param[k] = "" else: param[k] = hashlib.sha1( param[k].encode("utf8")).hexdigest() fLOG( "this class contains a parameter 'password' --> it will be encrypted") history = _private_restore(function_name, pwd=False) found = None for i, v in enumerate(history): if v == param: found = i break if found is not None: fLOG("removing one element from history") del history[found] else: fLOG("history length ", len(history)) history.append(param) typstr = str # unicode# with open(filename, "w", encoding="utf8") as f: for param_ in history: spar = typstr(param_).replace("\n", "#*###n####*#") f.write(spar + "\n")
[docs]def _private_restore(function_name, pwd=True): """ Restores the parameters stored by *_private_store*, returns a list of dictionaries (one of each line stored by *_private_store*). :param function_name: name of the function (a unique string, the function is not called) :param pwd: empty every password :return: list of dictionaries The function replaces every substring ``#*###n####*#`` y end of line. :githublink:`%|py|90` """ values = guess_machine_parameter() filename = os.path.join( values["TEMP"], function_name + ".tkinterquickhelper.txt") if not os.path.exists(filename): fLOG("FrameWindows: unable to find file ", os.path.abspath(filename)) return [] fLOG("FrameWindows: loading parameters from file: ", os.path.abspath(filename)) f = open(filename, "r", encoding="utf8") s = f.readlines() f.close() ans = [] try: for line in s: ev = eval(line.replace("#*###n####*#", "\n")) if pwd: if "password3" in ev: ev["password3"] = "" if "password2" in ev: ev["password2"] = "" if "password1" in ev: ev["password1"] = "" if "password" in ev: ev["password"] = "" ans.append(ev) except Exception as e: raise Exception("problem in file " + filename) from e return ans
[docs]def get_icon(): """ Returns a filename corresponding the tkinterquickhelper icon. :return: filename :githublink:`%|py|129` """ ico = os.path.realpath( os.path.join(os.path.split(__file__)[0], "project_ico.ico")) return ico
[docs]def interpret_parameter(ty, s): """ Interprets a parameter. :param ty: type (the return type) :param s: value to interpret (a string) :return: value :githublink:`%|py|142` """ typstr = str # unicode# try: if ty in [bool]: return s in [True, "True", "true", "TRUE", "1", 1] elif ty == datetime.datetime: if s is None or len(s) == 0 or s == "None": return None else: return str2datetime(s) elif ty in [int, float]: return ty(s) elif ty == typstr: if s is None or len(s) == 0 or s == "None": return None else: return s elif ty in [None]: return None else: try: return eval(s) except Exception as ee: fLOG("unable to evaluation ", ee) return None except Exception: fLOG("unable to process value ", ty, " v= ", s, " --> ", None) return None