From cf88d8e9c7dff32a91019252bea6f3dab1131184 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jul 11 2015 08:41:43 +0000 Subject: Add a method to try to find out if a given text is a binary file or not --- diff --git a/pagure/lib/__init__.py b/pagure/lib/__init__.py index 139a5c7..4ba145a 100644 --- a/pagure/lib/__init__.py +++ b/pagure/lib/__init__.py @@ -2366,3 +2366,11 @@ def clean_input(text): ], attributes=attrs, ) + + +def is_binary(text): + """ Returns wether we think this text correspond to a binary file or not + """ + textchars = bytearray([7,8,9,10,12,13,27]) + bytearray(range(0x20, 0x100)) + is_binary_string = lambda bytes: bool(bytes.translate(None, textchars)) + return is_binary_string(text)