| 
									
										
										
										
											2020-12-03 12:05:46 -05:00
										 |  |  | # Save arbitrary variables so that values can be kept across restarts. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Copyright (C) 2020 Dushyant Ahuja <dusht.ahuja@gmail.com> | 
					
						
							|  |  |  | # Copyright (C) 2016-2020  Kevin O'Connor <kevin@koconnor.net> | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # This file may be distributed under the terms of the GNU GPLv3 license. | 
					
						
							| 
									
										
										
										
											2021-10-01 19:30:48 -04:00
										 |  |  | import os, logging, ast, configparser | 
					
						
							| 
									
										
										
										
											2020-12-03 12:05:46 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | class SaveVariables: | 
					
						
							|  |  |  |     def __init__(self, config): | 
					
						
							|  |  |  |         self.printer = config.get_printer() | 
					
						
							|  |  |  |         self.filename = os.path.expanduser(config.get('filename')) | 
					
						
							|  |  |  |         self.allVariables = {} | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             self.loadVariables() | 
					
						
							|  |  |  |         except self.printer.command_error as e: | 
					
						
							|  |  |  |             raise config.error(str(e)) | 
					
						
							|  |  |  |         gcode = self.printer.lookup_object('gcode') | 
					
						
							|  |  |  |         gcode.register_command('SAVE_VARIABLE', self.cmd_SAVE_VARIABLE, | 
					
						
							|  |  |  |                                desc=self.cmd_SAVE_VARIABLE_help) | 
					
						
							|  |  |  |     def loadVariables(self): | 
					
						
							|  |  |  |         allvars = {} | 
					
						
							| 
									
										
										
										
											2021-01-17 22:46:04 -05:00
										 |  |  |         varfile = configparser.ConfigParser() | 
					
						
							| 
									
										
										
										
											2020-12-03 12:05:46 -05:00
										 |  |  |         try: | 
					
						
							|  |  |  |             varfile.read(self.filename) | 
					
						
							|  |  |  |             if varfile.has_section('Variables'): | 
					
						
							|  |  |  |                 for name, val in varfile.items('Variables'): | 
					
						
							|  |  |  |                     allvars[name] = ast.literal_eval(val) | 
					
						
							|  |  |  |         except: | 
					
						
							|  |  |  |             msg = "Unable to parse existing variable file" | 
					
						
							|  |  |  |             logging.exception(msg) | 
					
						
							|  |  |  |             raise self.printer.command_error(msg) | 
					
						
							|  |  |  |         self.allVariables = allvars | 
					
						
							|  |  |  |     cmd_SAVE_VARIABLE_help = "Save arbitrary variables to disk" | 
					
						
							|  |  |  |     def cmd_SAVE_VARIABLE(self, gcmd): | 
					
						
							|  |  |  |         varname = gcmd.get('VARIABLE') | 
					
						
							|  |  |  |         value = gcmd.get('VALUE') | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             value = ast.literal_eval(value) | 
					
						
							|  |  |  |         except ValueError as e: | 
					
						
							|  |  |  |             raise gcmd.error("Unable to parse '%s' as a literal" % (value,)) | 
					
						
							|  |  |  |         newvars = dict(self.allVariables) | 
					
						
							|  |  |  |         newvars[varname] = value | 
					
						
							|  |  |  |         # Write file | 
					
						
							| 
									
										
										
										
											2021-01-17 22:46:04 -05:00
										 |  |  |         varfile = configparser.ConfigParser() | 
					
						
							| 
									
										
										
										
											2020-12-03 12:05:46 -05:00
										 |  |  |         varfile.add_section('Variables') | 
					
						
							|  |  |  |         for name, val in sorted(newvars.items()): | 
					
						
							|  |  |  |             varfile.set('Variables', name, repr(val)) | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             f = open(self.filename, "w") | 
					
						
							|  |  |  |             varfile.write(f) | 
					
						
							|  |  |  |             f.close() | 
					
						
							|  |  |  |         except: | 
					
						
							|  |  |  |             msg = "Unable to save variable" | 
					
						
							|  |  |  |             logging.exception(msg) | 
					
						
							|  |  |  |             raise gcmd.error(msg) | 
					
						
							|  |  |  |         self.loadVariables() | 
					
						
							|  |  |  |     def get_status(self, eventtime): | 
					
						
							| 
									
										
										
										
											2021-01-14 22:13:50 -05:00
										 |  |  |         return {'variables': self.allVariables} | 
					
						
							| 
									
										
										
										
											2020-12-03 12:05:46 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | def load_config(config): | 
					
						
							|  |  |  |     return SaveVariables(config) |