|
9 | 9 | from OCP.BRepTools import BRepTools |
10 | 10 | from OCP.BRep import BRep_Builder |
11 | 11 | from OCP.TopoDS import TopoDS_Shape |
| 12 | +from itertools import chain |
| 13 | +import hashlib |
12 | 14 |
|
13 | 15 |
|
14 | 16 | TEMPDIR_PATH = tempfile.gettempdir() |
@@ -74,24 +76,19 @@ def build_file_name(fct, *args, **kwargs): |
74 | 76 | Returns a file name given the specified function and args. |
75 | 77 | If the function and the args are the same this function returns the same filename |
76 | 78 | """ |
77 | | - SPACER = "_" |
78 | | - file_name = fct.__name__ |
79 | | - for arg in args: |
80 | | - if isinstance(arg, cq.Workplane): |
81 | | - raise TypeError( |
82 | | - "Can not cache a function that accepts Workplane objects as argument" |
83 | | - ) |
84 | | - file_name += SPACER + str(hash(arg)) |
85 | | - for kwarg_value in kwargs.values(): |
86 | | - if isinstance(kwarg_value, cq.Workplane): |
87 | | - raise TypeError( |
88 | | - "Can not cache a function that accepts Workplane objects as argument" |
89 | | - ) |
90 | | - file_name += SPACER + str(hash(kwarg_value)) |
91 | | - file_name = bytes(file_name, "utf-8") |
92 | | - return base64.urlsafe_b64encode(file_name).decode( |
93 | | - "utf-8" |
94 | | - ) # compacts the long string of hash ints into a urlsafe string |
| 79 | + if cq.Workplane in (type(x) for x in chain(args, kwargs.values())): |
| 80 | + raise TypeError( |
| 81 | + "Can not cache a function that accepts Workplane objects as argument" |
| 82 | + ) |
| 83 | + |
| 84 | + # hash all relevant variables |
| 85 | + hasher = hashlib.md5() |
| 86 | + for val in [fct.__name__, args.__repr__(), kwargs.__repr__()]: |
| 87 | + hasher.update(bytes(val, "utf-8")) |
| 88 | + # encode the hash as a filesystem safe string |
| 89 | + filename = base64.urlsafe_b64encode(hasher.digest()).decode("utf-8") |
| 90 | + # strip the padding |
| 91 | + return filename.rstrip("=") |
95 | 92 |
|
96 | 93 |
|
97 | 94 | def clear_cq_cache(): |
|
0 commit comments