Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# -*- coding: utf-8 -*-
2"""
3@file
4@brief Common functions used for @see cl FrameFunction and @see cl FrameParams.
5"""
6import os
7import copy
8import datetime
9import hashlib
11from pyquickhelper.loghelper.convert_helper import str2datetime
12from pyquickhelper.loghelper.flog import fLOG, guess_machine_parameter
15def _private_store(function_name, param):
16 """
17 Stores the parameters into a file, the function adds the
18 parameter in a new line. It a parameter is a password
19 (*name=password*, the password will be encrypted using sha1).
21 @param function_name name of the function (a unique string, the function is not called)
22 @param param (dict) list of parameters
24 The function replaces every end of line into ``#*###n####*#``.
25 """
26 param = copy.copy(param)
28 rem = []
29 for r in param:
30 if r.startswith("_"):
31 rem.append(r)
32 for r in rem:
33 del param[r]
35 values = guess_machine_parameter()
36 filename = os.path.join(
37 values["TEMP"], function_name + ".tkinterquickhelper.txt")
38 fLOG("FrameWindows: storing parameters in file: ",
39 os.path.abspath(filename))
41 if "password" in param or \
42 "password1" in param or \
43 "password2" in param or \
44 "password3" in param:
45 param = param.copy()
46 for k in ["password", "password1", "password2", "password3"]:
47 if k in param:
48 if param[k] is None:
49 param[k] = ""
50 else:
51 param[k] = hashlib.sha1(
52 param[k].encode("utf8")).hexdigest()
53 fLOG(
54 "this class contains a parameter 'password' --> it will be encrypted")
56 history = _private_restore(function_name, pwd=False)
58 found = None
59 for i, v in enumerate(history):
60 if v == param:
61 found = i
62 break
64 if found is not None:
65 fLOG("removing one element from history")
66 del history[found]
67 else:
68 fLOG("history length ", len(history))
70 history.append(param)
72 with open(filename, "w", encoding="utf8") as f:
73 for param_ in history:
74 spar = str(param_).replace("\n", "#*###n####*#")
75 f.write(spar + "\n")
78def _private_restore(function_name, pwd=True):
79 """
80 Restores the parameters stored by *_private_store*,
81 returns a list of dictionaries (one of each line stored
82 by *_private_store*).
84 @param function_name name of the function (a unique string, the function is not called)
85 @param pwd empty every password
86 @return list of dictionaries
88 The function replaces every substring ``#*###n####*#`` y end of line.
89 """
90 values = guess_machine_parameter()
91 filename = os.path.join(
92 values["TEMP"], function_name + ".tkinterquickhelper.txt")
93 if not os.path.exists(filename):
94 fLOG("FrameWindows: unable to find file ", os.path.abspath(filename))
95 return []
97 fLOG("FrameWindows: loading parameters from file: ",
98 os.path.abspath(filename))
99 f = open(filename, "r", encoding="utf8")
100 s = f.readlines()
101 f.close()
103 ans = []
104 try:
105 for line in s:
106 ev = eval(line.replace("#*###n####*#", "\n"))
107 if pwd:
108 if "password3" in ev:
109 ev["password3"] = ""
110 if "password2" in ev:
111 ev["password2"] = ""
112 if "password1" in ev:
113 ev["password1"] = ""
114 if "password" in ev:
115 ev["password"] = ""
116 ans.append(ev)
117 except Exception as e:
118 raise Exception("problem in file " + filename) from e
120 return ans
123def get_icon():
124 """
125 Returns a filename corresponding the tkinterquickhelper icon.
127 @return filename
128 """
129 ico = os.path.realpath(
130 os.path.join(os.path.split(__file__)[0], "project_ico.ico"))
131 return ico
134def interpret_parameter(ty, s):
135 """
136 Interprets a parameter.
138 @param ty type (the return type)
139 @param s value to interpret (a string)
140 @return value
141 """
142 try:
143 if ty in [bool]:
144 return s in [True, "True", "true", "TRUE", "1", 1]
145 elif ty == datetime.datetime:
146 if s is None or len(s) == 0 or s == "None":
147 return None
148 else:
149 return str2datetime(s)
150 elif ty in [int, float]:
151 return ty(s)
152 elif ty == str:
153 if s is None or len(s) == 0 or s == "None":
154 return None
155 else:
156 return s
157 elif ty in [None]:
158 return None
159 else:
160 try:
161 return eval(s)
162 except Exception as ee:
163 fLOG("unable to evaluation ", ee)
164 return None
165 except Exception:
166 fLOG("unable to process value ", ty, " v= ", s, " --> ", None)
167 return None