Package arcmap :: Module preferences
[hide private]
[frames] | no frames]

Source Code for Module arcmap.preferences

  1  ################################################################################ 
  2  # Authors: Brian Schott (Sir Alaran) 
  3  # Copyright: Brian Schott (Sir Alaran) 
  4  # Date: Sep 26 2009 
  5  # License: 
  6  # 
  7  # This program is free software: you can redistribute it and/or modify 
  8  # it under the terms of the GNU General Public License as published by 
  9  # the Free Software Foundation, either version 3 of the License, or 
 10  # (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, see <http://www.gnu.org/licenses/>. 
 19  ################################################################################ 
 20   
 21   
 22  """ 
 23  Application preferences 
 24  """ 
 25   
 26  import os 
 27  import logging 
 28  import ConfigParser 
 29   
 30  import graphics 
 31  import datafiles 
 32   
 33  log = logging.getLogger("preferences") 
 34   
 35   
 36  # Zoom levels available for TileGrid instances 
 37  zoomLevels = [ 
 38          .25, 
 39          .5, 
 40          .75, 
 41          1.0, 
 42          1.5, 
 43          2.0, 
 44          4.0, 
 45  ] 
 46  zoomNormalIndex = zoomLevels.index(1.0) 
 47   
 48  visual = { 
 49          "handle_size" : 12, 
 50          "valid_outline" : 0x00ff00ff, 
 51          "valid_fill" : 0x00ff0080, 
 52          "invalid_outline" : 0xff0000ff, 
 53          "invalid_fill" : 0xff000080, 
 54          "stipple_length" : 4, 
 55          "stipple_gap" : 2, 
 56  } 
 57   
 58  # Default values for physics. (static geometry) 
 59  physics = { 
 60          "default_friction" : 1.0, 
 61          "default_restitution" : 0.0, 
 62  } 
 63   
 64  files = { 
 65          "tileset_prefix" : os.path.join("images", "tiles"), 
 66          "parallax_prefix" : os.path.join("images", "parallax"), 
 67          "data_prefix" : "", 
 68          "use_prefixes" : False 
 69  } 
 70   
71 -def save():
72 """ 73 Saves the user's preferences to a file named "config.ini" 74 """ 75 config = ConfigParser.SafeConfigParser() 76 77 config.add_section("Visual") 78 config.set("Visual", "handle_size", str(visual["handle_size"])) 79 config.set("Visual", "stipple_length", str(visual["stipple_length"])) 80 config.set("Visual", "stipple_gap", str(visual["stipple_gap"])) 81 config.set("Visual", "valid_outline", "0x%08x" % visual["valid_outline"]) 82 config.set("Visual", "valid_fill", "0x%08x" % visual["valid_fill"]) 83 config.set("Visual", "invalid_outline", 84 "0x%08x" % visual["invalid_outline"]) 85 config.set("Visual", "invalid_fill", "0x%08x" % visual["invalid_fill"]) 86 87 config.add_section("Physics") 88 config.set("Physics", "default_restitution", 89 str(physics["default_restitution"])) 90 config.set("Physics", "default_friction", 91 str(physics["default_friction"])) 92 93 config.add_section("Files") 94 config.set("Files", "tileset_prefix", str(files["tileset_prefix"])) 95 config.set("Files", "parallax_prefix", str(files["parallax_prefix"])) 96 config.set("Files", "data_prefix", str(files["data_prefix"])) 97 config.set("Files", "use_prefixes", str(files["use_prefixes"])) 98 99 if os.path.exists(datafiles.userConfigPath()) == False: 100 try: 101 os.makedirs(datafiles.userConfigPath(), 0700) 102 except Exception as e: 103 log.error("Creation of user configuration directory failed with" + 104 " error: \"%s\'""" % e) 105 return 106 f = open(os.path.join(datafiles.userConfigPath(), "config.ini"), "w") 107 config.write(f)
108
109 -def load():
110 """ 111 Loads the user's preferences 112 '""" 113 path = os.path.join(datafiles.userConfigPath(), "config.ini") 114 if os.path.exists(path): 115 f = open(path, "r") 116 config = ConfigParser.SafeConfigParser() 117 config.readfp(f) 118 119 def getIntOption(section, sectionName, name): 120 try: 121 section[name] = config.getint(sectionName, name) 122 except ConfigParser.NoOptionError: 123 pass 124 except ValueError: 125 pass
126 127 def getFloatOption(section, sectionName, name): 128 try: 129 section[name] = config.getfloat(sectionName, name) 130 except ConfigParser.NoOptionError: 131 pass 132 133 def getHexOption(section, sectionName, name): 134 try: 135 section[name] = config.get(sectionName, name) 136 except ConfigParser.NoOptionError: 137 pass 138 try: 139 section[name] = int(section[name], 16) 140 except ValueError: 141 pass 142 143 def getBoolOption(section, sectionName, name): 144 try: 145 section[name] = config.getboolean(sectionName, name) 146 except ConfigParser.NoOptionError: 147 pass 148 except ValueError: 149 pass 150 151 def getStringOption(section, sectionName, name): 152 try: 153 section[name] = config.get(sectionName, name) 154 except ConfigParser.NoOptionError: 155 pass 156 157 getIntOption(visual, "Visual", "handle_size") 158 getIntOption(visual, "Visual", "stipple_length") 159 getIntOption(visual, "Visual", "stipple_gap") 160 getHexOption(visual, "Visual", "valid_fill") 161 getHexOption(visual, "Visual", "valid_outline") 162 getHexOption(visual, "Visual", "invalid_fill") 163 getHexOption(visual, "Visual", "invalid_outline") 164 165 getFloatOption(physics, "Physics", "default_friction") 166 getFloatOption(physics, "Physics", "default_restitution") 167 168 getStringOption(files, "Files", "tileset_prefix") 169 getStringOption(files, "Files", "parallax_prefix") 170 getStringOption(files, "Files", "data_prefix") 171 getBoolOption(files, "Files", "use_prefixes") 172 173 else: 174 log.info("Could not open user configuration file. Using defaults") 175