Changeset 572
- Timestamp:
- 10/04/07 18:21:14 (1 year ago)
- Files:
-
- pagoda/trunk/Pagoda/pagoda/command.py (modified) (2 diffs)
- pagoda/trunk/Pagoda/pagoda/controllers/root.py (modified) (3 diffs)
- pagoda/trunk/Pagoda/pagoda/site/__init__.py (modified) (1 diff)
- pagoda/trunk/Pagoda/pagoda/site/manager.py (modified) (9 diffs)
- pagoda/trunk/Pagoda/pagoda/site/mapper.py (modified) (2 diffs)
- pagoda/trunk/Pagoda/setup.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
pagoda/trunk/Pagoda/pagoda/command.py
r571 r572 97 97 site['use_app_resources'] 98 98 ) 99 print "Success: Site %r created." % site_name 99 100 100 101 def site_remove(self, *args): … … 119 120 print "Removing site: %s" % site_name 120 121 remove_site(site_name) 122 print "Success: Site removed." % site_name 123 else: 124 print "No changes were made." % site_name 121 125 122 126 pagoda/trunk/Pagoda/pagoda/controllers/root.py
r400 r572 1 1 import turbogears 2 2 import sqlalchemy 3 import cherrypy 3 4 from cherrypy import request, NotFound, tree 4 5 from turbogears.database import metadata, session … … 6 7 from pagoda import config 7 8 from pagoda.plugins.controllers import controller_for_url 9 from pagoda.site import PagodaSiteFilter 8 10 9 11 try: … … 13 15 14 16 __all__ = ['PagodaController'] 17 18 # Enable PagodaSiteFilter to set a X-Pagoda-Site http header for each request 19 cherrypy.filters.input_filters.append('pagoda.site.PagodaSiteFilter') 15 20 16 21 def get_pagoda_mount_point(): pagoda/trunk/Pagoda/pagoda/site/__init__.py
r392 r572 1 1 __import__('pkg_resources').declare_namespace(__name__) 2 3 from pagoda.site.mapper import * 4 from pagoda.site.manager import * 5 from pagoda.site.engines import * 2 6 3 7 # from pagoda import config pagoda/trunk/Pagoda/pagoda/site/manager.py
r571 r572 1 1 import os 2 import sys3 import ConfigParser4 2 import shutil 5 3 import pkg_resources 4 from configobj import ConfigObj 6 5 from turbogears.util import get_package_name 7 6 from pagoda.util import make_module … … 9 8 __all__ = ['is_site', 'create_site', 'list_sites', 'remove_site'] 10 9 11 SITES_ DIR_PREFIX = "sites"10 SITES_CONFIG_SECTION = SITES_DIR_PREFIX = "sites" 12 11 RESOURCES = ['static', 'templates', 'attachments'] 13 12 CONFIG = None 14 13 14 def start_extension(): 15 """TurboGears Extension to merge `sites.cfg` into TurboGears config""" 16 import turbogears 17 config_filepath = get_config_filepath() 18 turbogears.config.update_config(config_filepath) 19 20 def get_config(empty_cache=False): 21 """Read `sites.cfg` and return site configuration as a ConfigObj section 22 23 `sites.cfg` must be ConfigObj (unrepr=True) style ini-file, like such: 24 25 [global] 26 [[sites]] 27 [[[mysite]]] 28 dburi="sqlite:///devdata.sqlite" 29 some_other_key="Some Other Value" 30 31 Note: only the [[sites]] section of this file will be returned! 32 33 """ 34 global CONFIG 35 if CONFIG and not empty_cache: 36 return CONFIG 37 38 config_filepath = get_config_filepath() 39 config = ConfigObj(infile=config_filepath, unrepr=True) 40 try: 41 CONFIG = config.get('global').get(SITES_CONFIG_SECTION) 42 except AttributeError: 43 raise SyntaxError( 44 "Error parsing site configuration file: %r\n" 45 "Must contain [global] and [[sites]] sections!" % config_filepath 46 ) 47 return CONFIG 48 15 49 def is_site(site_name): 16 """Returns True if `site_name` is found in the Pagoda site config file """17 config= get_config()18 if config.has_section(site_name):50 """Returns True if `site_name` is found in the Pagoda site config file.""" 51 sites = get_config() 52 if sites.has_key(site_name): 19 53 return True 20 54 else: 21 55 return False 22 56 23 def get_site_resource(site_name, resource_name):24 """Return the path to a site resource.57 def _get_site_resource(site_name, resource_name): 58 """Return the file path to a site resource. 25 59 26 For the site given by `site_name`, return the 27 path to `resource_name`. 60 For the specified site, return the physical file path to `resource_name`. 28 61 29 Valid resource names are stored in RESOURCES 62 Valid resource names are stored in RESOURCES variable in this module. 63 64 Note: This function assumes `site_name` exists and is configured. 65 30 66 """ 31 67 if resource_name not in RESOURCES: … … 36 72 (site_name, resource_name, RESOURCES) 37 73 ) 38 package = get_package_name() 39 config = get_config() 40 resource_module_path = config.get(site_name, resource_name) 74 sites = get_config() 75 resource_module_path = sites.get(site_name).get(resource_name, None) 76 if resource_module_path is None: 77 raise KeyError( 78 "Resource %r not found in Site %r." % (resource_name, site_name) 79 ) 41 80 return pkg_resources.resource_filename(resource_module_path, '') 42 81 … … 44 83 """Remove a PagodaCMS site. 45 84 46 Permanently delete sall files and configuration associated with85 Permanently delete all files and configuration associated with 47 86 the PagodaCMS site given by `site_name`. 48 87 … … 58 97 59 98 # Remove site-specific resource directories 60 config = get_config()61 99 for resource in RESOURCES: 62 resource_path = get_site_resource(site_name, resource)100 resource_path = _get_site_resource(site_name, resource) 63 101 # If resource is site-specific, remove it 64 102 site_suffix = os.path.join(SITES_DIR_PREFIX, site_name, resource) … … 73 111 ) 74 112 if os.path.exists(site_dir): 75 shutil.rmtree(site_dir) 113 shutil.rmtree(site_dir) 76 114 77 # Remove site from config 78 config.remove_section(site_name) 79 save_site_config(config) 115 # Remove site from config, and write config to disk 116 sites = get_config() 117 del sites[site_name] 118 save_site_config() 80 119 81 120 def list_sites(): 82 """Return s a list of all PagodaCMS site names"""83 config= get_config()84 return config.sections()121 """Return a list of all PagodaCMS site names.""" 122 sites = get_config() 123 return sites.keys() 85 124 86 125 def create_site(site_name, dburi, domain_globs, use_app_resources=False): 87 """Create a new PagodaCMS site using the supplied parameters """126 """Create a new PagodaCMS site using the supplied parameters.""" 88 127 89 128 # Configure new site 90 config= get_config()91 if not config.has_section(site_name):92 config.add_section(site_name)93 config.set(site_name, 'dburi', dburi)94 config.set(site_name, 'domain_globs', domain_globs)129 sites = get_config() 130 if not sites.has_key(site_name): 131 sites[site_name] = dict() 132 sites[site_name]['dburi'] = dburi 133 sites[site_name]['domain_globs'] = domain_globs 95 134 96 135 # Create site directory, if not using existing app resources … … 113 152 for resource in RESOURCES: 114 153 module = ".".join(module_prefix + [resource]) 115 config.set(site_name, resource, module)154 sites[site_name][resource] = module 116 155 path = pkg_resources.resource_filename( 117 156 package, … … 120 159 make_module(path) 121 160 122 123 161 # Write config to disk 124 save_site_config( config)162 save_site_config() 125 163 126 164 def get_config_filepath(): 127 """Return path to `sites.cfg` config file """165 """Return path to `sites.cfg` config file, ensuring it exists.""" 128 166 package = get_package_name() 129 returnos.path.abspath(167 config_filepath = os.path.abspath( 130 168 pkg_resources.resource_filename( 131 169 package, … … 133 171 ) 134 172 ) 135 136 def get_config(): 137 """Create/Obtain `sites.cfg` and return it as a `ConfigParser` object""" 138 # If config was already obtained, return in-memory version 139 global CONFIG 140 if CONFIG: 141 return CONFIG 142 143 config = ConfigParser.ConfigParser() 144 config_filepath = get_config_filepath() 145 146 # If sites.cfg doesn't exist, just make sure parent dir exists 173 # If sites.cfg doesn't exist, create it and its parent directories 147 174 if not os.path.exists(config_filepath): 175 # Ensure parent directories exist 148 176 package = get_package_name() 149 177 make_module(pkg_resources.resource_filename(package, SITES_DIR_PREFIX)) 150 else: 151 config.read([config_filepath]) 178 # Create config file with empty sections 179 config = ConfigObj(unrepr=True) 180 config.filename = config_filepath 181 config['global'] = {SITES_CONFIG_SECTION: {}} 182 config.write() 183 return config_filepath 152 184 153 # Remember and return config 154 CONFIG = config 155 return config 185 def save_site_config(): 186 """Save `sites.cfg` using data from TurboGears configuration.""" 156 187 157 def save_site_config(config): 158 """Save `sites.cfg` using data from the `config` (`ConfigParser`) object""" 159 config_filepath = get_config_filepath() 160 config_file = open(config_filepath, 'w') 161 config.write(config_file) 162 config_file.close() 163 188 # Get config for all sites 189 sites = get_config() 190 191 # Use ConfigObj to save config to storage media 192 config = ConfigObj(unrepr=True) 193 config.filename = get_config_filepath() 194 config['global'] = {SITES_CONFIG_SECTION: sites} 195 config.write() pagoda/trunk/Pagoda/pagoda/site/mapper.py
r391 r572 23 23 24 24 def update_domain_map(domain_map=SITE_DOMAINS): 25 """ 25 """Update the given `domain_map` 26 26 27 Update the given `domain_map` mapping from the current configuration, 27 keyed by site name and mapping to a list of domain globs to match against.28 keyed by site name and mapping to a list of domain globs to match against. 28 29 29 30 `domain_map` is a mapping to update, or the module-level domain map 30 `_site_domains` by default.31 `SITE_DOMAINS` by default. 31 32 32 Returns None after mutating `domain_map`.33 Returns None after mutating `domain_map`. 33 34 34 """35 """ 35 36 domain_map.clear() 36 37 sites = config.get('sites', {}) … … 49 50 domain_map[site_name] = [] 50 51 51 def get_site_name(url, domain_map= _site_domains):52 def get_site_name(url, domain_map=SITE_DOMAINS): 52 53 """ 53 54 Retrieve the site name for `url` using the specified `domain_map` 54 55 to determine which domains map to which sites. By default, `domain_map` 55 points to the module-global domain map ` _site_domains`.56 points to the module-global domain map `SITE_DOMAINS`. 56 57 57 58 Returns the first site name that matches, or None if no match is found. pagoda/trunk/Pagoda/setup.py
r571 r572 56 56 pagoda_models = pagoda.plugins.models 57 57 pagoda_view = pagoda.view 58 pagoda_sites = pagoda.site.manager 58 59 59 60 [turbogears.command]
