| 
									
										
										
										
											2018-03-24 22:22:41 +01:00
										 |  |  | import hashlib | 
					
						
							| 
									
										
										
										
											2016-11-19 20:53:51 +01:00
										 |  |  | import random | 
					
						
							|  |  |  | import string | 
					
						
							| 
									
										
										
										
											2016-11-30 18:43:03 +01:00
										 |  |  | import sys | 
					
						
							| 
									
										
										
										
											2018-03-24 22:22:41 +01:00
										 |  |  | import uuid | 
					
						
							| 
									
										
										
										
											2016-11-19 20:53:51 +01:00
										 |  |  | from flask_babel import gettext | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # required answerer attribute | 
					
						
							|  |  |  | # specifies which search query keywords triggers this answerer | 
					
						
							|  |  |  | keywords = ('random',) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | random_int_max = 2**31 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-30 18:43:03 +01:00
										 |  |  | if sys.version_info[0] == 2: | 
					
						
							|  |  |  |     random_string_letters = string.lowercase + string.digits + string.uppercase | 
					
						
							|  |  |  | else: | 
					
						
							|  |  |  |     unicode = str | 
					
						
							|  |  |  |     random_string_letters = string.ascii_lowercase + string.digits + string.ascii_uppercase | 
					
						
							| 
									
										
										
										
											2016-11-19 20:53:51 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:22:41 +01:00
										 |  |  | def random_characters(): | 
					
						
							|  |  |  |     return [random.choice(random_string_letters) | 
					
						
							|  |  |  |             for _ in range(random.randint(8, 32))] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-19 20:53:51 +01:00
										 |  |  | def random_string(): | 
					
						
							| 
									
										
										
										
											2018-03-24 22:22:41 +01:00
										 |  |  |     return u''.join(random_characters()) | 
					
						
							| 
									
										
										
										
											2016-11-19 20:53:51 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def random_float(): | 
					
						
							|  |  |  |     return unicode(random.random()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def random_int(): | 
					
						
							|  |  |  |     return unicode(random.randint(-random_int_max, random_int_max)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:22:41 +01:00
										 |  |  | def random_sha256(): | 
					
						
							|  |  |  |     m = hashlib.sha256() | 
					
						
							| 
									
										
										
										
											2020-08-04 14:47:41 +02:00
										 |  |  |     m.update(''.join(random_characters()).encode()) | 
					
						
							| 
									
										
										
										
											2018-03-24 22:22:41 +01:00
										 |  |  |     return unicode(m.hexdigest()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def random_uuid(): | 
					
						
							|  |  |  |     return unicode(uuid.uuid4()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-30 18:43:03 +01:00
										 |  |  | random_types = {b'string': random_string, | 
					
						
							|  |  |  |                 b'int': random_int, | 
					
						
							| 
									
										
										
										
											2018-03-24 22:22:41 +01:00
										 |  |  |                 b'float': random_float, | 
					
						
							|  |  |  |                 b'sha256': random_sha256, | 
					
						
							|  |  |  |                 b'uuid': random_uuid} | 
					
						
							| 
									
										
										
										
											2016-11-19 20:53:51 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # required answerer function | 
					
						
							|  |  |  | # can return a list of results (any result type) for a given query | 
					
						
							|  |  |  | def answer(query): | 
					
						
							|  |  |  |     parts = query.query.split() | 
					
						
							|  |  |  |     if len(parts) != 2: | 
					
						
							|  |  |  |         return [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if parts[1] not in random_types: | 
					
						
							|  |  |  |         return [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return [{'answer': random_types[parts[1]]()}] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # required answerer function | 
					
						
							|  |  |  | # returns information about the answerer | 
					
						
							|  |  |  | def self_info(): | 
					
						
							|  |  |  |     return {'name': gettext('Random value generator'), | 
					
						
							|  |  |  |             'description': gettext('Generate different random values'), | 
					
						
							| 
									
										
										
										
											2019-07-28 07:53:46 +02:00
										 |  |  |             'examples': [u'random {}'.format(x.decode('utf-8')) for x in random_types]} |