11from os import path , listdir , getcwd
2+ from typing import TypedDict
3+
4+
5+ def read_public_keys_from_file (file_path : str ):
6+ with open (file_path , "r" ) as f :
7+ lines = [line .strip () for line in f if line .strip ()]
8+ return lines
29
310
411def load_public_keys_from_files (type : str , dir_path : str = path .join (getcwd (), "tmp" , "public_keys" )):
@@ -8,9 +15,28 @@ def load_public_keys_from_files(type: str, dir_path: str = path.join(getcwd(), "
815 for file in files :
916 if file .startswith (type ):
1017 file_path = path .join (dir_path , file )
11-
12- with open (file_path , "r" ) as f :
13- lines = [line .strip () for line in f if line .strip ()]
14- result .extend (lines )
18+ public_keys = read_public_keys_from_file (file_path )
19+ result .extend (public_keys )
1520
1621 return result
22+
23+
24+ class PublicKeyFile (TypedDict ):
25+ public_keys : list [str ]
26+ file_name : str
27+ type : str
28+
29+
30+ # Seperate function introduced that returns the pubkeys on a per file basis as to not break existing code that uses the previous function
31+ def load_public_key_files (type : str , dir_path : str = path .join (getcwd (), "tmp" , "public_keys" )) -> list [PublicKeyFile ]:
32+ files = listdir (dir_path )
33+ public_key_files : list [PublicKeyFile ] = []
34+
35+ for file in files :
36+ if file .startswith (type ):
37+ file_name = file .split ("." )[0 ] # strip file extension
38+ file_path = path .join (dir_path , file )
39+ public_keys = read_public_keys_from_file (file_path )
40+ public_key_files .append ({"public_keys" : public_keys , "file_name" : file_name , "type" : type })
41+
42+ return public_key_files
0 commit comments