diff --git a/swh/web/ui/query.py b/swh/web/ui/query.py index 450ef01a3..ee5fc1d53 100644 --- a/swh/web/ui/query.py +++ b/swh/web/ui/query.py @@ -1,57 +1,40 @@ # Copyright (C) 2015 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information import re from swh.core import hashutil -def parse(query): - """Parse a formalized get query. - - Args: - query, a colon separated value hash - - Returns: - List of hashes - - Raises: - AttributeError if query is None - - """ - return query.split(':') - - # Regexp to filter and check inputs sha256_regexp='[0-9a-f]{64}' sha1_regexp='[0-9a-f]{40}' -def group_by_checksums(hashes): - """Check that the checksums have the right format. +def categorize_hash(hash): + """Categorize the hash string according to what it is. Args: - hashes: a list of string which should represent hashes (sha1 or sha256) + hash: hash string representation (sha1 or sha256) Returns: A dictionary of hash indexed by their nature (sha1, sha256) The dictionary will be empty if nothing matches Raises: None """ - hashes_m = {} - for h in hashes: - try: - if re.search(sha256_regexp, h): - hashes_m.update({'sha256': hashutil.hex_to_hash(h)}) - elif re.search(sha1_regexp, h): - hashes_m.update({'sha1': hashutil.hex_to_hash(h)}) - except ValueError: # ignore silently to check the other inputs - continue - - return hashes_m + try: + h = hashutil.hex_to_hash(hash) + except ValueError: # ignore silently to check the other inputs + return {} + + if re.search(sha256_regexp, hash): + return {'sha256': h} + if re.search(sha1_regexp, hash): + return {'sha1': h} + return {} diff --git a/swh/web/ui/service.py b/swh/web/ui/service.py index 1ca765335..7eb7f4bda 100755 --- a/swh/web/ui/service.py +++ b/swh/web/ui/service.py @@ -1,28 +1,28 @@ # Copyright (C) 2015 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information from swh.web.ui import main from swh.web.ui import query def lookup_hash(q): - """Given a string query q of hashes, lookup its hash to the backend. + """Given a string query q of one hash, lookup its hash to the backend. Args: - query, string of ':' delimited hashes (sha1, sha256, etc...) + query, hash as a string (sha1, sha256, etc...) Returns: a string message (found, not found or a potential error explanation) Raises: OSError (no route to host), etc... Network issues in general """ - hashes = query.group_by_checksums(query.parse(q)) - if hashes != {}: - present = main.storage().content_present(hashes) + hash = query.categorize_hash(q) + if hash != {}: + present = main.storage().content_present(hash) return 'Found!' if present else 'Not Found' return """This is not a hash. Hint: hexadecimal string with length either 20 (sha1) or 32 (sha256).""" diff --git a/swh/web/ui/tests/test_query.py b/swh/web/ui/tests/test_query.py index c3d5284e0..6a2010cb0 100644 --- a/swh/web/ui/tests/test_query.py +++ b/swh/web/ui/tests/test_query.py @@ -1,64 +1,35 @@ # Copyright (C) 2015 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information import unittest from nose.tools import istest from swh.web.ui import query from swh.core import hashutil class QueryTestCase(unittest.TestCase): @istest - def parse(self): - # when - actual_hashes = query.parse("a:b:c") - - # then - self.assertEquals(actual_hashes, ['a', 'b', 'c'], - "Should be a, b, c hashes") - - @istest - def parse_with_error(self): - # when - with self.assertRaises(AttributeError): - query.parse(None) - - @istest - def parse_2(self): - # when - actual_hashes = query.parse("a") + def categorize_hash(self): + input_sha1 = 'f1d2d2f924e986ac86fdf7b36c94bcdf32beec15' - # then - self.assertEquals(actual_hashes, ['a'], "Should be only a hash") + res = query.categorize_hash(input_sha1) + self.assertEquals(res, {'sha1': hashutil.hex_to_hash(input_sha1)}) - @istest - def group_by_checksum(self): - input_sha1 = 'f1d2d2f924e986ac86fdf7b36c94bcdf32beec15' + def categorize_hash_2(self): input_sha256 = \ '084c799cd551dd1d8d5c5f9a5d593b2e931f5e36122ee5c793c1d08a19839cc0' - input_bad_length = '1234567890987654' - res = query.group_by_checksums([input_sha1, - input_sha256, - input_bad_length, - input_sha1]) + res = query.categorize_hash(input_sha256) - self.assertEquals(len(res), 2, - "Only 2 elements because no duplicates and filter out bad inputs.") - self.assertEquals(res['sha1'], hashutil.hex_to_hash(input_sha1)) - self.assertEquals(res['sha256'], hashutil.hex_to_hash(input_sha256)) + self.assertEquals(res, {'sha256': hashutil.hex_to_hash(input_sha256)}) - @istest - def group_by_checksum_with_only_bad_inputs(self): - input_sha1 = 'e986ac86fdf7b36c94bcdf32beec15' + def categorize_hash_3(self): input_bad_length = '1234567890987654' - res = query.group_by_checksums([input_sha1, - input_bad_length, - input_sha1]) + res = query.categorize_hash(input_bad_length) - self.assertEquals(res, {}, "Absolutely no good inputs.") + self.assertEquals(res, {})