Coverage for pyilper/userconfig.py: 52%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

46 statements  

1#!/usr/bin/python3 

2# -*- coding: utf-8 -*- 

3# userconfig for Linux 

4# 

5# (c) 2015 Joachim Siebold 

6# 

7# This program is free software; you can redistribute it and/or 

8# modify it under the terms of the GNU General Public License 

9# as published by the Free Software Foundation; either version 2 

10# of the License, or (at your option) any later version. 

11# 

12# This program is distributed in the hope that it will be useful, 

13# but WITHOUT ANY WARRANTY; without even the implied warranty of 

14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

15# GNU General Public License for more details. 

16# 

17# You should have received a copy of the GNU General Public License 

18# along with this program; if not, write to the Free Software 

19# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 

20# 

21# userconfig class --------------------------------------------------------- 

22# 

23# Changelog 

24# 06.10.2015 jsi: 

25# - class statement syntax update 

26# 08.02.2016 jsi: 

27# - changed os detection to platform.system() 

28# 14.04.2016 jsi 

29# - use APPDATA environment variable for config directory on Windows 

30# - use json to serialize program configuration 

31# 18.04.2016 jsi 

32# - use pretty print json 

33# 18.09.2016 jsi 

34# - add instance to configuration file name 

35# 14.10.2016 jsi 

36# - added filename parameter to __init__ 

37# 17.08.2016 jsi 

38# - added diagnostics to JSON encode/decode error messages 

39# 12.12.2021 jsi 

40# - add configversion parameter to constructor 

41# - use buildconfigfilename function from pilcore 

42 

43import json 

44import os 

45from .pilcore import buildconfigfilename 

46 

47class ConfigError(Exception): 

48 def __init__(self,msg,add_msg= None): 

49 self.msg= msg 

50 self.add_msg = add_msg 

51 

52 

53class cls_userconfig: 

54 

55 def __init__(self,progname,filename,configversion,instance,production): 

56# 

57# determine config file name 

58# 

59 self.__configfile__,self.__configpath__=buildconfigfilename(progname,filename,configversion,instance,production) 

60 

61# 

62# read configuration, if no configuration exists write default configuration 

63# 

64 def read(self,default): 

65 if not os.path.isfile(self.__configfile__): 

66 if not os.path.exists(self.__configpath__): 

67 try: 

68 os.makedirs(self.__configpath__) 

69 except OSError as e: 

70 raise ConfigError("Cannot create path for configuration file", e.strerror) 

71 try: 

72 self.write(default) 

73 except OSError as e: 

74 raise ConfigError("Cannot write default configuration file", e.strerror) 

75 return default 

76 f=None 

77 try: 

78 f= open(self.__configfile__,"r") 

79 config= json.load(f) 

80 except json.JSONDecodeError as e: 

81 add_msg="File: "+self.__configfile__+". Error: "+e.msg+" at line: "+str(e.lineno) 

82 raise ConfigError("Cannot decode configuration data",add_msg) 

83 except OSError as e: 

84 raise ConfigError("Cannot read configuration file", e.strerror) 

85 finally: 

86 if f is not None: 

87 f.close() 

88 

89 return config 

90# 

91# Store configuration, create file if it does not exist 

92# 

93 def write(self,config): 

94 f=None 

95 try: 

96 f= open(self.__configfile__,"w") 

97 json.dump(config,f,sort_keys=True,indent=3) 

98 except json.JSONDecodeError as e: 

99 add_msg="File: "+self.__configfile__+". Error: "+e.msg+" at line: "+str(e.lineno) 

100 raise ConfigError("Cannot encode configuration data",add_msg) 

101 except OSError as e: 

102 raise ConfigError("Cannot write to configuration file", e.strerror) 

103 finally: 

104 if f is not None: 

105 f.close() 

106