3 # Copyright 2020 Oskar Sharipov <oskar.sharipov[at]tuta.io>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
18 Generates static html file of messages signed by signify(1).
21 canaries [-q] <msgfolder>...
22 canaries (-h | --help)
27 -h --help Show this screen.
28 --verion Show version.
32 1 - invalid message path.
33 2 - invalid files in path.
36 Generate a new html file of messages in msg/ directory:
38 Look at result in your web-browser:
46 from typing import List, Dict
48 from docopt import docopt
54 'grab_messages_with_signatures',
58 logger = logging.getLogger('canaries')
59 _handler = logging.StreamHandler()
60 _handler.setLevel(logging.INFO)
61 logger.addHandler(_handler)
64 def grab_messages_with_signatures(folder: str) -> List[Dict[str, str]]:
65 ''' searches for '*.sig' signs and its messages in `folder` directory '''
67 glob(os.path.join(folder, '*.sig')),
73 logger.error(f'There is no signatures in `{folder}".')
76 messages_with_signatures = list()
77 for signature_file in signatures:
78 message_file = signature_file[: -len('.sig')]
79 if not os.path.isfile(message_file):
81 f'`{message_file}` doesn\'t exist or it\'s not a file.'
85 with open(signature_file, 'r') as file:
86 signature = file.read()
87 with open(message_file, 'r') as file:
90 logger.info(f"Grabbed `{name}` message")
92 messages_with_signatures.append(
93 dict(name=name, message=message, signature=signature)
95 return messages_with_signatures
98 def join_messages(signed_messages: List[Dict[str, str]]) -> str:
99 ''' renders `signed_messages` by jinja2 template system '''
100 with open('template.html', 'r') as file:
101 template = jinja2.Template(file.read())
102 return template.render(signed_messages=signed_messages)
106 ''' parses args and does all magic '''
107 args: Dict[str, str] = docopt(__doc__, version=__version__)
109 logger.setLevel(logging.ERROR if args['-q'] else logging.INFO)
110 signed_messages = list()
111 for folder in args['<msgfolder>']:
112 if not os.path.isdir(folder):
113 logger.error(f'`{folder}` doesn\'t exist or it\'s not a folder.')
115 signed_messages.extend(grab_messages_with_signatures(folder))
117 main_page = join_messages(signed_messages)
118 with open('canaries.html', 'w') as file:
119 file.write(main_page)
120 logger.info('Wrote html to `canaries.html`.')
123 if __name__ == '__main__':