/src/googlecl/config/__init__.py
Python | 93 lines | 56 code | 6 blank | 31 comment | 5 complexity | d581946cb240fe2538cbaaa9632f695f MD5 | raw file
1# Copyright (C) 2010 Google Inc. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14import ConfigParser 15import googlecl 16import parser 17 18 19def _create_basic_options(): 20 """Set the most basic options in the config file.""" 21 import googlecl.docs 22 import googlecl.contacts 23 import googlecl.calendar 24 import googlecl.sites 25 import googlecl.youtube 26 import getpass 27 import socket 28 # These may be useful to define at the module level, but for now, 29 # keep them here. 30 # REMEMBER: updating these means you need to update the CONFIG readme. 31 default_hostid = getpass.getuser() + '@' + socket.gethostname() 32 _youtube = {'max_results': '50'} 33 _contacts = {'fields': 'name,email'} 34 _calendar = {'fields': 'title,when'} 35 _sites = {'fields': 'title,page_name,id'} 36 _general = {'max_retries': '2', 37 'retry_delay': '0.5', 38 'regex': 'True', 39 'url_field': 'site', 40 'fields': 'title,url-site', 41 'missing_field_value': 'N/A', 42 'date_print_format': '%b %d %H:%M', 43 'cap_results': 'False', 44 'hostid': default_hostid} 45 _docs = {'document_format': 'txt', 46 'spreadsheet_format': 'xls', 47 'presentation_format': 'ppt', 48 'drawing_format': 'png', 49 'format': 'txt', 50 'spreadsheet_editor': 'openoffice.org', 51 'presentation_editor': 'openoffice.org'} 52 return {googlecl.docs.SECTION_HEADER: _docs, 53 googlecl.contacts.SECTION_HEADER: _contacts, 54 googlecl.calendar.SECTION_HEADER: _calendar, 55 googlecl.sites.SECTION_HEADER: _sites, 56 googlecl.youtube.SECTION_HEADER: _youtube, 57 'GENERAL': _general} 58 59 60def get_config_path(filename='config', 61 default_directories=None, 62 create_missing_dir=False): 63 """Get the full path to the configuration file. 64 65 See googlecl.get_xdg_path() 66 """ 67 return googlecl.get_xdg_path(filename, 'CONFIG', default_directories, 68 create_missing_dir) 69 70 71def load_configuration(path=None): 72 """Loads configuration file. 73 74 Args: 75 path: Path to the configuration file. Default None for the default location. 76 77 Returns: 78 Configuration parser. 79 """ 80 if not path: 81 path = get_config_path(create_missing_dir=True) 82 if not path: 83 LOG.error('Could not create config directory!') 84 return False 85 config = parser.ConfigParser(ConfigParser.ConfigParser) 86 config.associate(path) 87 made_changes = config.ensure_basic_options(_create_basic_options()) 88 if made_changes: 89 config.write_out_parser() 90 # Set the encoding again, now that the config file is loaded. 91 # (the config file may have a default encoding setting) 92 googlecl.TERMINAL_ENCODING = googlecl.determine_terminal_encoding(config) 93 return config