mirror of
				https://github.com/KevinMidboe/delugeClient.git
				synced 2025-10-29 12:00:13 +00:00 
			
		
		
		
	Utils script for coloring log based on logger status filter and convert function for converting byte dict to a dict of strings.
This commit is contained in:
		
							
								
								
									
										42
									
								
								utils.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								utils.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,42 @@ | ||||
| #!/usr/bin/env python3 | ||||
| # -*- coding: utf-8 -*- | ||||
| # @Author: kevinmidboe | ||||
| # @Date:   2018-04-17 19:55:38 | ||||
| # @Last Modified by:   KevinMidboe | ||||
| # @Last Modified time: 2018-05-04 00:04:25 | ||||
|  | ||||
| import logging | ||||
| import colored | ||||
| import json | ||||
| from pprint import pprint | ||||
|  | ||||
| from colored import stylize | ||||
|  | ||||
| __all__ = ('ColorizeFilter', ) | ||||
|  | ||||
| class ColorizeFilter(logging.Filter): | ||||
|    """ | ||||
|    Class for setting specific colors to levels of severity for log output | ||||
|    """ | ||||
|    color_by_level = { | ||||
|       10: 'chartreuse_3b', | ||||
|       20: 'white', | ||||
|       30: 'orange_1', | ||||
|       40: 'red' | ||||
|    } | ||||
|     | ||||
|    logger = logging.getLogger('deluge_cli') | ||||
|  | ||||
|    def filter(self, record): | ||||
|       record.raw_msg = record.msg | ||||
|       color = self.color_by_level.get(record.levelno) | ||||
|       if color: | ||||
|          record.msg = stylize(record.msg, colored.fg(color)) | ||||
|       return True | ||||
|  | ||||
| def convert(data): | ||||
|     if isinstance(data, bytes):  return data.decode('ascii') | ||||
|     if isinstance(data, dict):   return dict(map(convert, data.items())) | ||||
|     if isinstance(data, tuple):  return map(convert, data) | ||||
|     json_data = json.dumps(data) | ||||
|     return json_data | ||||
		Reference in New Issue
	
	Block a user