25Collection of Python helper functions called from the ScriptForge Basic libraries
26to execute specific services that are not or not easily available from Basic directly.
27When relevant, the methods present in the ScripForge Python module might call the
28functions below for compatibility reasons.
42 A Singleton design pattern
43 Credits: « Python in a Nutshell » by Alex Martelli, O
'Reilly
60 Given an array of PropertyValues as argument, convert it to a JSON string
65 pvDict[pv.Name] = pv.Value
66 return json.dumps(pvDict, indent=indent, skipkeys=
True)
71 Given a JSON string as argument, convert it to a list of tuples (name, value)
72 The value must
not be a (sub)dict. This doesn
't pass the python-basic bridge.
75 dico = json.loads(jsonstr)
77 for key
in iter(dico):
80 if isinstance(value, dict):
82 elif isinstance(value, list):
83 for i
in range(len(value)):
84 if isinstance(value[i], dict): value[i] =
None
85 result.append((key, item))
96 Write the argument to stdout.
97 If the APSO shell console is active, the argument will be displayed
in the console window
110 Compare the 2 files, returning True if they seem equal,
False otherwise.
111 By default, only their signatures (modification time, ...) are compared.
112 When comparecontents ==
True, their contents are compared.
115 return filecmp.cmp(filename1, filename2,
not comparecontents)
121 return str(os.path.getsize(systemfilepath))
126 Hash a given file with the given hashing algorithm
127 cfr. https://www.pythoncentral.io/hashing-files-
with-python/
131 algo = algorithm.lower()
133 if algo
in hashlib.algorithms_guaranteed:
136 hasher = hashlib.md5()
138 hasher = hashlib.sha1()
139 elif algo ==
'sha224':
140 hasher = hashlib.sha224()
141 elif algo ==
'sha256':
142 hasher = hashlib.sha256()
143 elif algo ==
'sha384':
144 hasher = hashlib.sha384()
145 elif algo ==
'sha512':
146 hasher = hashlib.sha512()
149 with open(filename,
'rb')
as file:
150 buffer = file.read(BLOCKSIZE)
151 while len(buffer) > 0:
152 hasher.update(buffer)
153 buffer = file.read(BLOCKSIZE)
154 return hasher.hexdigest()
164 Normalize a pathname by collapsing redundant separators and up-level references so that
165 A//B, A/B/, A/./B
and A/foo/../B all become A/B.
166 On Windows, it converts forward slashes to backward slashes.
168 return os.path.normpath(systemfilepath)
177 Switch between SF_Platform properties (read the documentation about the ScriptForge.Platform service)
180 if propertyname ==
'Architecture':
181 return pf.Architecture
182 elif propertyname ==
'ComputerName':
183 return pf.ComputerName
184 elif propertyname ==
'CPUCount':
186 elif propertyname ==
'CurrentUser':
187 return pf.CurrentUser
188 elif propertyname ==
'Machine':
190 elif propertyname ==
'OSName':
192 elif propertyname ==
'OSPlatform':
194 elif propertyname ==
'OSRelease':
196 elif propertyname ==
'OSVersion':
198 elif propertyname ==
'Processor':
200 elif propertyname ==
'PythonVersion':
201 return pf.PythonVersion
219 return getpass.getuser()
227 def OSName(self):
return platform.system().replace(
'Darwin',
'macOS')
230 def OSPlatform(self):
return platform.platform(aliased =
True)
251 Display url using the default browser
254 webbrowser.open(url, new = 2)
265 Hash a given UTF-8 string with the given hashing algorithm
269 algo = algorithm.lower()
271 if algo
in hashlib.algorithms_guaranteed:
273 bytestring = string.encode(ENCODING)
275 hasher = hashlib.md5(bytestring)
277 hasher = hashlib.sha1(bytestring)
278 elif algo ==
'sha224':
279 hasher = hashlib.sha224(bytestring)
280 elif algo ==
'sha256':
281 hasher = hashlib.sha256(bytestring)
282 elif algo ==
'sha384':
283 hasher = hashlib.sha384(bytestring)
284 elif algo ==
'sha512':
285 hasher = hashlib.sha512(bytestring)
288 return hasher.hexdigest()
299g_exportedScripts = ()
301if __name__ ==
"__main__":
314 print(hashlib.algorithms_guaranteed)
324 {"firstName":
"John",
"lastName":
"Smith",
"isAlive": true,
"age": 27,
325 "address": {
"streetAddress":
"21 2nd Street",
"city":
"New York",
"state":
"NY",
"postalCode":
"10021-3100"},
326 "phoneNumbers": [{
"type":
"home",
"number":
"212 555-1234"},{
"type":
"office",
"number":
"646 555-4567"}],
327 "children": [
"Q",
"M",
"G",
"T"],
"spouse": null}
def __call__(cls, *args, **kwargs)
bool _SF_Exception__PythonPrint(str string)
str _SF_FileSystem__GetFilelen(str systemfilepath)
str _SF_Dictionary__ConvertToJson(propval, indent=None)
def _SF_Platform(str propertyname)
str _SF_FileSystem__Normalize(str systemfilepath)
str _SF_String__HashStr(str string, str algorithm)
def _SF_Session__OpenURLInBrowser(str url)
bool _SF_FileSystem__CompareFiles(str filename1, str filename2, comparecontents=True)
str _SF_FileSystem__HashFile(str filename, str algorithm)
def _SF_Dictionary__ImportFromJson(str jsonstr)