Ticket #1051: trac-global-config-4.diff
| File trac-global-config-4.diff, 8.5 KB (added by trac-form@…, 7 years ago) |
|---|
-
setup.py
35 35 htdocs_dir = os.path.join(self.prefix, 'share', 'trac', 'htdocs') 36 36 wiki_dir = os.path.join(self.prefix, 'share', 'trac', 'wiki-default') 37 37 macros_dir = os.path.join(self.prefix, 'share', 'trac', 'wiki-macros') 38 config_dir = os.path.join(self.prefix, 'share', 'trac', 'conf') 38 39 f = open(_p('trac/siteconfig.py'),'w') 39 40 f.write(""" 40 41 # PLEASE DO NOT EDIT THIS FILE! … … 44 45 __default_htdocs_dir__ = %(htdocs)r 45 46 __default_wiki_dir__ = %(wiki)r 46 47 __default_macros_dir__ = %(macros)r 48 __default_config_dir__ = %(config)r 47 49 48 50 """ % {'trac':PACKAGE, 'ver':VERSION, 'templates':_p(templates_dir), 49 'htdocs':_p(htdocs_dir), 'wiki':_p(wiki_dir), 'macros':_p(macros_dir)}) 51 'htdocs':_p(htdocs_dir), 'wiki':_p(wiki_dir), 'macros':_p(macros_dir), 52 'config':_p(config_dir)}) 50 53 f.close() 51 54 52 55 # Run actual install -
trac/config.py
17 17 from __future__ import generators 18 18 19 19 from ConfigParser import ConfigParser 20 import os.path 20 import os.path, sys 21 21 22 def default_dir(name): 23 try: 24 from trac import siteconfig 25 return getattr(siteconfig, '__default_%s_dir__' % name) 26 except ImportError: 27 # This is not a regular install with a generated siteconfig.py file, 28 # so try to figure out the directory based on common setups 29 import os.path, sys 30 special_dirs = {'wiki': 'wiki-default', 'macros': 'wiki-macros'} 31 dirname = special_dirs.get(name, name) 22 32 33 # First assume we're being executing directly form the source directory 34 import trac 35 path = os.path.join(os.path.split(os.path.dirname(trac.__file__))[0], 36 dirname) 37 if not os.path.isdir(path): 38 # Not being executed from the source directory, so assume the 39 # default installation prefix 40 path = os.path.join(sys.prefix, 'share', 'trac', dirname) 41 42 return path 43 23 44 class Configuration: 24 45 """ 25 46 Thin layer over ConfigParser from the Python standard library. … … 28 49 when the file has changed. 29 50 """ 30 51 31 def __init__(self, filename ):52 def __init__(self, filename, site_filename = os.path.join(default_dir('config'), 'trac.ini')): 32 53 self.filename = filename 54 self.site_filename = site_filename 33 55 self.parser = ConfigParser() 56 self.site_parser = ConfigParser() 34 57 self.__defaults = {} 35 58 self.__lastmtime = 0 59 self.__lastsitemtime = 0 36 60 self.parse_if_needed() 37 61 38 62 def get(self, section, name, default=None): … … 43 67 return self.parser.get(section, name) 44 68 45 69 def setdefault(self, section, name, value): 46 self.__defaults[(section, name)] = value 70 if (section, name) not in self.__defaults: 71 self.__defaults[(section, name)] = value 47 72 48 73 def set(self, section, name, value): 49 74 """ … … 55 80 return self.parser.set(section, name, value) 56 81 57 82 def options(self, section): 58 if not self.parser.has_section(section): 59 return [] 60 try: 61 return self.parser.items(section) 62 except AttributeError: 63 options = [] 83 options = [] 84 if self.parser.has_section(section): 64 85 for option in self.parser.options(section): 65 86 options.append((option, self.parser.get(section, option))) 66 return options 87 for option, value in self.__defaults.iteritems(): 88 if option[0] == section: 89 if not [exists for exists in options if exists[0] == option[1]]: 90 options.append((option[1], value)) 91 return options 67 92 68 93 def __contains__(self, name): 69 94 return self.parser.has_section(name) … … 81 106 self.parser.write(open(self.filename, 'w')) 82 107 83 108 def parse_if_needed(self): 109 if self.site_filename: 110 try: 111 modtime = os.path.getmtime(self.site_filename) 112 if modtime > self.__lastsitemtime: 113 self.site_parser.readfp(open(self.site_filename)) 114 # Import global configuration into __defaults 115 for section in self.site_parser.sections(): 116 for option in self.site_parser.options(section): 117 self.__defaults[(section, option)] = self.site_parser.get(section, option) 118 self.__lastsitemtime = modtime 119 except (IOError, OSError): 120 pass 84 121 if not self.filename: 85 122 return 86 123 modtime = os.path.getmtime(self.filename) 87 124 if modtime > self.__lastmtime: 88 125 self.parser.readfp(open(self.filename)) 89 126 self.__lastmtime = modtime 90 91 92 def default_dir(name):93 try:94 from trac import siteconfig95 return getattr(siteconfig, '__default_%s_dir__' % name)96 except ImportError:97 # This is not a regular install with a generated siteconfig.py file,98 # so try to figure out the directory based on common setups99 import os.path, sys100 special_dirs = {'wiki': 'wiki-default', 'macros': 'wiki-macros'}101 dirname = special_dirs.get(name, name)102 103 # First assume we're being executing directly form the source directory104 import trac105 path = os.path.join(os.path.split(os.path.dirname(trac.__file__))[0],106 dirname)107 if not os.path.isdir(path):108 # Not being executed from the source directory, so assume the109 # default installation prefix110 path = os.path.join(sys.prefix, 'share', 'trac', dirname)111 112 return path -
trac/tests/config.py
33 33 os.remove(self.filename) 34 34 35 35 def test_default(self): 36 config = Configuration(self.filename )36 config = Configuration(self.filename, None) 37 37 self.assertEquals('', config.get('a', 'option')) 38 38 self.assertEquals('value', config.get('a', 'option', 'value')) 39 39 … … 45 45 configfile.writelines(['[a]\n', 'option = x\n', '\n']) 46 46 configfile.close() 47 47 48 config = Configuration(self.filename )48 config = Configuration(self.filename, None) 49 49 self.assertEquals('x', config.get('a', 'option')) 50 50 self.assertEquals('x', config.get('a', 'option', 'y')) 51 51 … … 53 53 configfile = open(self.filename, 'w') 54 54 configfile.close() 55 55 56 config = Configuration(self.filename )56 config = Configuration(self.filename, None) 57 57 config.set('a', 'option', 'x') 58 58 self.assertEquals('x', config.get('a', 'option')) 59 59 config.save() … … 69 69 '[b]\n', 'option = y\n']) 70 70 configfile.close() 71 71 72 config = Configuration(self.filename )72 config = Configuration(self.filename, None) 73 73 self.assertEquals(['a', 'b'], config.sections()) 74 74 75 75 def test_options(self): … … 78 78 '[b]\n', 'option = y\n']) 79 79 configfile.close() 80 80 81 config = Configuration(self.filename )81 config = Configuration(self.filename, None) 82 82 self.assertEquals(('option', 'x'), iter(config.options('a')).next()) 83 83 self.assertEquals(('option', 'y'), iter(config.options('b')).next()) 84 84 self.assertRaises(StopIteration, iter(config.options('c')).next) … … 88 88 configfile.writelines(['[a]\n', 'option = x\n', '\n']) 89 89 configfile.close() 90 90 91 config = Configuration(self.filename )91 config = Configuration(self.filename, None) 92 92 self.assertEquals('x', config.get('a', 'option')) 93 93 time.sleep(1) # needed because of low mtime granularity 94 94 -
trac/test.py
125 125 self.db = InMemoryDatabase() 126 126 127 127 from trac.config import Configuration 128 self.config = Configuration(None )128 self.config = Configuration(None, None) 129 129 130 130 from trac.log import logger_factory 131 131 self.log = logger_factory('test')
