Changeset 572

Show
Ignore:
Timestamp:
10/04/07 18:21:14 (1 year ago)
Author:
ian
Message:

Site manager modified so site configuration integrates into TurboGears? configuration.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • pagoda/trunk/Pagoda/pagoda/command.py

    r571 r572  
    9797            site['use_app_resources'] 
    9898        ) 
     99        print "Success: Site %r created." % site_name 
    99100 
    100101    def site_remove(self, *args): 
     
    119120            print "Removing site: %s" % site_name 
    120121            remove_site(site_name) 
     122            print "Success: Site removed." % site_name 
     123        else: 
     124            print "No changes were made." % site_name 
    121125 
    122126         
  • pagoda/trunk/Pagoda/pagoda/controllers/root.py

    r400 r572  
    11import turbogears 
    22import sqlalchemy 
     3import cherrypy 
    34from cherrypy import request, NotFound, tree 
    45from turbogears.database import metadata, session 
     
    67from pagoda import config 
    78from pagoda.plugins.controllers import controller_for_url 
     9from pagoda.site import PagodaSiteFilter 
    810 
    911try: 
     
    1315 
    1416__all__ = ['PagodaController'] 
     17 
     18# Enable PagodaSiteFilter to set a X-Pagoda-Site http header for each request 
     19cherrypy.filters.input_filters.append('pagoda.site.PagodaSiteFilter') 
    1520 
    1621def get_pagoda_mount_point(): 
  • pagoda/trunk/Pagoda/pagoda/site/__init__.py

    r392 r572  
    11__import__('pkg_resources').declare_namespace(__name__) 
     2 
     3from pagoda.site.mapper import * 
     4from pagoda.site.manager import * 
     5from pagoda.site.engines import * 
    26 
    37# from pagoda import config 
  • pagoda/trunk/Pagoda/pagoda/site/manager.py

    r571 r572  
    11import os 
    2 import sys 
    3 import ConfigParser 
    42import shutil 
    53import pkg_resources 
     4from configobj import ConfigObj 
    65from turbogears.util import get_package_name 
    76from pagoda.util import make_module 
     
    98__all__ = ['is_site', 'create_site', 'list_sites', 'remove_site'] 
    109 
    11 SITES_DIR_PREFIX = "sites" 
     10SITES_CONFIG_SECTION = SITES_DIR_PREFIX = "sites" 
    1211RESOURCES = ['static', 'templates', 'attachments'] 
    1312CONFIG = None 
    1413 
     14def 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 
     20def 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 
    1549def 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): 
    1953        return True 
    2054    else: 
    2155        return False 
    2256 
    23 def get_site_resource(site_name, resource_name): 
    24     """Return the path to a site resource. 
     57def _get_site_resource(site_name, resource_name): 
     58    """Return the file path to a site resource. 
    2559 
    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`. 
    2861 
    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 
    3066    """ 
    3167    if resource_name not in RESOURCES: 
     
    3672            (site_name, resource_name, RESOURCES) 
    3773        ) 
    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        ) 
    4180    return pkg_resources.resource_filename(resource_module_path, '') 
    4281 
     
    4483    """Remove a PagodaCMS site. 
    4584 
    46     Permanently deletes all files and configuration associated with  
     85    Permanently delete all files and configuration associated with  
    4786    the PagodaCMS site given by `site_name`.   
    4887 
     
    5897 
    5998    # Remove site-specific resource directories 
    60     config = get_config() 
    6199    for resource in RESOURCES: 
    62         resource_path = get_site_resource(site_name, resource) 
     100        resource_path = _get_site_resource(site_name, resource) 
    63101        # If resource is site-specific, remove it 
    64102        site_suffix = os.path.join(SITES_DIR_PREFIX, site_name, resource) 
     
    73111    ) 
    74112    if os.path.exists(site_dir): 
    75         shutil.rmtree(site_dir)         
     113        shutil.rmtree(site_dir) 
    76114 
    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() 
    80119 
    81120def list_sites(): 
    82     """Returns 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() 
    85124 
    86125def 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.""" 
    88127 
    89128    # 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 
    95134 
    96135    # Create site directory, if not using existing app resources 
     
    113152    for resource in RESOURCES: 
    114153        module = ".".join(module_prefix + [resource]) 
    115         config.set(site_name, resource, module) 
     154        sites[site_name][resource] = module 
    116155        path = pkg_resources.resource_filename( 
    117156            package, 
     
    120159        make_module(path) 
    121160 
    122  
    123161    # Write config to disk 
    124     save_site_config(config
     162    save_site_config(
    125163 
    126164def get_config_filepath(): 
    127     """Return path to `sites.cfg` config file""" 
     165    """Return path to `sites.cfg` config file, ensuring it exists.""" 
    128166    package = get_package_name() 
    129     return os.path.abspath( 
     167    config_filepath = os.path.abspath( 
    130168        pkg_resources.resource_filename( 
    131169            package,  
     
    133171        ) 
    134172    ) 
    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 
    147174    if not os.path.exists(config_filepath): 
     175        # Ensure parent directories exist 
    148176        package = get_package_name() 
    149177        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 
    152184 
    153     # Remember and return config 
    154     CONFIG = config 
    155     return config 
     185def save_site_config(): 
     186    """Save `sites.cfg` using data from TurboGears configuration.""" 
    156187 
    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  
    2323 
    2424def update_domain_map(domain_map=SITE_DOMAINS): 
    25     """ 
     25    """Update the given `domain_map` 
     26 
    2627    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. 
    2829         
    2930    `domain_map` is a mapping to update, or the module-level domain map 
    30        `_site_domains` by default. 
     31    `SITE_DOMAINS` by default. 
    3132         
    32        Returns None after mutating `domain_map`. 
     33    Returns None after mutating `domain_map`. 
    3334         
    34        """ 
     35    """ 
    3536    domain_map.clear() 
    3637    sites = config.get('sites', {}) 
     
    4950            domain_map[site_name] = [] 
    5051 
    51 def get_site_name(url, domain_map=_site_domains): 
     52def get_site_name(url, domain_map=SITE_DOMAINS): 
    5253    """ 
    5354    Retrieve the site name for `url` using the specified `domain_map` 
    5455    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`. 
    5657         
    5758    Returns the first site name that matches, or None if no match is found. 
  • pagoda/trunk/Pagoda/setup.py

    r571 r572  
    5656        pagoda_models = pagoda.plugins.models 
    5757        pagoda_view = pagoda.view 
     58        pagoda_sites = pagoda.site.manager 
    5859 
    5960        [turbogears.command] 

Log in as guest/pagoda to create tickets