a little nicer wrt pep8
[sfa.git] / sfa / trust / trustedroots.py
1 import os.path
2 import glob
3
4 from sfa.trust.gid import GID
5 from sfa.util.sfalogging import logger
6
7
8 class TrustedRoots:
9
10     # we want to avoid reading all files in the directory
11     # this is because it's common to have backups of all kinds
12     # e.g. *~, *.hide, *-00, *.bak and the like
13     supported_extensions = ['gid', 'cert', 'pem']
14
15     def __init__(self, dir):
16         self.basedir = dir
17         # create the directory to hold the files, if not existing
18         if not os.path.isdir(self.basedir):
19             os.makedirs(self.basedir)
20
21     def add_gid(self, gid):
22         fn = os.path.join(self.basedir, gid.get_hrn() + ".gid")
23         gid.save_to_file(fn)
24
25     def get_list(self):
26         gid_list = [GID(filename=cert_file)
27                     for cert_file in self.get_file_list()]
28         return gid_list
29
30     def get_file_list(self):
31         file_list = []
32         pattern = os.path.join(self.basedir, "*")
33         for cert_file in glob.glob(pattern):
34             if os.path.isfile(cert_file):
35                 if self.has_supported_extension(cert_file):
36                     file_list.append(cert_file)
37                 else:
38                     logger.warning("File {} ignored - supported extensions are {}"
39                                    .format(cert_file, TrustedRoots.supported_extensions))
40         return file_list
41
42     def has_supported_extension(self, path):
43         _, ext = os.path.splitext(path)
44         ext = ext.replace('.', '').lower()
45         return ext in TrustedRoots.supported_extensions