| 
									
										
										
										
											2024-03-11 14:06:26 +01:00
										 |  |  | # SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							| 
									
										
										
										
											2024-06-11 14:39:42 +02:00
										 |  |  | # pylint: disable=missing-module-docstring | 
					
						
							| 
									
										
										
										
											2024-03-11 14:06:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:22:41 +01:00
										 |  |  | import hashlib | 
					
						
							| 
									
										
										
										
											2016-11-19 20:53:51 +01:00
										 |  |  | import random | 
					
						
							|  |  |  | import string | 
					
						
							| 
									
										
										
										
											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',) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-04 09:45:35 +01:00
										 |  |  | random_int_max = 2**31 | 
					
						
							| 
									
										
										
										
											2020-08-06 17:42:46 +02:00
										 |  |  | 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(): | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |     return [random.choice(random_string_letters) for _ in range(random.randint(8, 32))] | 
					
						
							| 
									
										
										
										
											2018-03-24 22:22:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-19 20:53:51 +01:00
										 |  |  | def random_string(): | 
					
						
							| 
									
										
										
										
											2020-08-06 17:42:46 +02:00
										 |  |  |     return ''.join(random_characters()) | 
					
						
							| 
									
										
										
										
											2016-11-19 20:53:51 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def random_float(): | 
					
						
							| 
									
										
										
										
											2020-08-06 17:42:46 +02:00
										 |  |  |     return str(random.random()) | 
					
						
							| 
									
										
										
										
											2016-11-19 20:53:51 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def random_int(): | 
					
						
							| 
									
										
										
										
											2020-08-06 17:42:46 +02:00
										 |  |  |     return str(random.randint(-random_int_max, random_int_max)) | 
					
						
							| 
									
										
										
										
											2016-11-19 20:53:51 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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()) | 
					
						
							| 
									
										
										
										
											2020-08-06 17:42:46 +02:00
										 |  |  |     return str(m.hexdigest()) | 
					
						
							| 
									
										
										
										
											2018-03-24 22:22:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def random_uuid(): | 
					
						
							| 
									
										
										
										
											2020-08-06 17:42:46 +02:00
										 |  |  |     return str(uuid.uuid4()) | 
					
						
							| 
									
										
										
										
											2018-03-24 22:22:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-23 15:41:34 +02:00
										 |  |  | def random_color(): | 
					
						
							|  |  |  |     color = "%06x" % random.randint(0, 0xFFFFFF) | 
					
						
							|  |  |  |     return f"#{color.upper()}" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  | random_types = { | 
					
						
							|  |  |  |     'string': random_string, | 
					
						
							|  |  |  |     'int': random_int, | 
					
						
							|  |  |  |     'float': random_float, | 
					
						
							|  |  |  |     'sha256': random_sha256, | 
					
						
							|  |  |  |     'uuid': random_uuid, | 
					
						
							| 
									
										
										
										
											2023-08-23 15:41:34 +02:00
										 |  |  |     'color': random_color, | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											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(): | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |     return { | 
					
						
							|  |  |  |         'name': gettext('Random value generator'), | 
					
						
							|  |  |  |         'description': gettext('Generate different random values'), | 
					
						
							|  |  |  |         'examples': ['random {}'.format(x) for x in random_types], | 
					
						
							|  |  |  |     } |