Gracefully handle binary files for file/<file>.gph - stahg-gopher - Static Mercurial page generator for gopher HTML hg clone https://bitbucket.org/iamleot/stahg-gopher DIR Log DIR Files DIR Refs DIR README DIR LICENSE --- DIR changeset 12d7dd66c5a3153b8ed11b3af5f7df8f0ce140eb DIR parent 7b3ce6eee6ce5e9373af06204a9df09636454e47 HTML Author: Leonardo Taccari <iamleot@gmail.com> Date: Mon, 13 May 2019 15:35:55 Gracefully handle binary files for file/<file>.gph (Strictly speaking that's not binary file but everything that could not be decode()-ed.) Diffstat: TODO | 3 --- stahg-gopher.py | 12 ++++++++---- 2 files changed, 8 insertions(+), 7 deletions(-) --- diff -r 7b3ce6eee6ce -r 12d7dd66c5a3 TODO --- a/TODO Mon May 13 15:20:53 2019 +0200 +++ b/TODO Mon May 13 15:35:55 2019 +0200 @@ -4,9 +4,6 @@ Missing features/bugs --------------------- - - Gracefully handle binary files in file/<file>.gph - In stagit-gopher instead of the contents of the file a - `Binary file.' is present instead. - Implement refs(), to list all branches and tags - Write a stahg-gopher-index - Write documentation and man page(s) diff -r 7b3ce6eee6ce -r 12d7dd66c5a3 stahg-gopher.py --- a/stahg-gopher.py Mon May 13 15:20:53 2019 +0200 +++ b/stahg-gopher.py Mon May 13 15:35:55 2019 +0200 @@ -286,10 +286,14 @@ print('---', file=f) files = [self.client.root() + os.sep.encode() + file] - for num, line in enumerate(self.client.cat(files).decode().splitlines(), start=1): - print('{num:6d} {line}'.format( - num=num, - line=gph_escape_text(line)), file=f) + try: + content = self.client.cat(files).decode() + for num, line in enumerate(content.splitlines(), start=1): + print('{num:6d} {line}'.format( + num=num, + line=gph_escape_text(line)), file=f) + except: + print('Binary file.', file=f) if __name__ == '__main__':