PoserIni.py
1    from __future__ import print_function
2    
3    import \
4        os, \
5        errno, \
6        wx, \
7        re, \
8        collections
9    
10   try:
11       import poser
12   except ImportError:
13       import POSER_FAKE as poser
14   
15   re_int_check = re.compile(r"^(-?\d+)$")
16   re_float_check = re.compile(r"^(-?\d+.\d+)$")
17   
18   
19   class PoserInit(collections.OrderedDict):
20       INIFILE = os.path.join(poser.PrefsLocation(), "Poser.ini")
21   
22       def __init__(self, **kwargs):
23           self.__auto_save__ = kwargs.get("autosave", False)
24           self.__save_flag__ = False
25           self.__initialized__ = False
26   
27           if not os.access(self.INIFILE, os.R_OK | os.W_OK):
28               with wx.MessageDialog(None, message="Sorry, can't access Posers INI File.",
29                                     caption="Poser Ini",
30                                     style=wx.ICON_EXCLAMATION) as dlg:
31                   dlg.ShowModal()
32           else:
33               super(PoserInit, self).__init__()
34               self.read_file()
35   
36           self.__initialized__ = True
37   
38 def __del__(self): 39 self.save_file() 40
41 def __getattr__(self, item): 42 if item.upper() in self: 43 res = self[item.upper()] # type: str 44 if re_int_check.match(res): 45 return int(res) 46 if re_float_check.match(res): 47 return float(res) 48 return res 49 return super(self.__class__, self).__getattribute__(item) 50
51 def __setattr__(self, item, value): 52 if item.upper() in self: 53 self[item.upper()] = str(value).strip() 54 else: 55 super(self.__class__, self).__setattr__(item, value) 56
57 def __getitem__(self, item): 58 item = item.upper() 59 return super(self.__class__, self).__getitem__(item) 60
61 def __setitem__(self, key, value): 62 if not self.__initialized__: 63 return super(self.__class__, self).__setitem__(key, value) 64 key = key.upper() 65 if key in self: 66 res = super(self.__class__, self).__setitem__(key, value) 67 self.__save_flag__ = True 68 if self.__auto_save__: 69 self.save_file() 70 return res 71 else: 72 raise KeyError("Unknown keyword: '%s'" % key) 73
74 @property 75 def autosave(self): 76 return self.__auto_save__ 77
78 @autosave.setter 79 def autosave(self, state): 80 self.__auto_save__ = bool(state) 81 if self.__auto_save__ and self.__save_flag__: 82 self.save_file() 83
84 def read_file(self): 85 if self.__save_flag__ and self.__auto_save__: 86 self.save_file() 87 88 with open(self.INIFILE, "r") as fh: 89 for line in fh: 90 cmd, _, parm = line.partition(" ") 91 self[cmd.strip()] = parm.strip() 92
93 def save_file(self): 94 if self.__save_flag__: 95 path = os.path.dirname(self.INIFILE) 96 try: 97 with open(self.INIFILE, "w") as fh: 98 for k, v in self.items(): 99 print("{} {}".format(k, v), file=fh) 100 except IOError as err: 101 if err.errno == errno.ETXTBSY: 102 # busy, try later 103 return 104 elif err.errno == errno.EACCES: 105 with wx.MessageDialog(None, message="No permission to write to path\n%s" 106 % path, caption="Poser Ini", 107 style=wx.ICON_EXCLAMATION) as dlg: 108 self.__auto_save__ = self.__save_flag__ = False 109 return dlg.ShowModal() 110 else: 111 with wx.MessageDialog(None, message="IO Error while writing to\n%s" 112 % path, caption="Poser Ini", 113 style=wx.ICON_EXCLAMATION) as dlg: 114 self.__auto_save__ = self.__save_flag__ = False 115 return dlg.ShowModal() 116 117 self.__save_flag__ = False 118 119 120 init = PoserInit(use_globals=True) 121