<commit_before># Module:	__init__
# Date:		3rd October 2008
# Author:	James Mills, prologic at shortcircuit dot net dot au

"""Circuits Library - Web

circuits.web contains the circuits full stack web server that is HTTP
and WSGI compliant.
"""

from loggers import Logger
from core import Controller
from sessions import Sessions
from events import Request, Response
from servers import BaseServer, Server
from errors import HTTPError, Forbidden, NotFound, Redirect
from dispatchers import Static, Dispatcher, VirtualHosts, XMLRPC

try:
    from dispatchers import JSONRPC
except ImportError:
    pass
<commit_msg>circuits.web: Add url and expose to this namesapce<commit_after># Module:	__init__
# Date:		3rd October 2008
# Author:	James Mills, prologic at shortcircuit dot net dot au

"""Circuits Library - Web

circuits.web contains the circuits full stack web server that is HTTP
and WSGI compliant.
"""

from utils import url
from loggers import Logger
from sessions import Sessions
from core import expose, Controller
from events import Request, Response
from servers import BaseServer, Server
from errors import HTTPError, Forbidden, NotFound, Redirect
from dispatchers import Static, Dispatcher, VirtualHosts, XMLRPC

try:
    from dispatchers import JSONRPC
except ImportError:
    pass
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
Script that scrapes commit info from a git repo logs, formats the info into
a tweet and then tweets it.

Only works on Python 3.4 and requires Tweepy, GitPython and a secrets.py
file to handle the twitter api keys. It is assumed that the machine this script
is run on has github authentication configured.

Call at the terminal with the path to the repo to tweet from or put it in your
crontab.

SWDG 18/1/17
"""
import tweepy
import random
import git
import sys
import os.path as op
from secrets import *
from datetime import datetime

def ScrapeGIT(repoURL):
    """
    Given a path to a local git repo, return the commit message and hash.
    """
    g = git.cmd.Git(repoURL)
    g.pull()
    Message = (g.log('-1', '--pretty=format:%s'))
    Hash = (g.log('-1', '--pretty=format:%h'))

    return Hash, Message

def reduce_length_for_tweet(Hash, Message, Reduced=False):
    """
    Recursively remove a word from the message until it is small enough to tweet
    Reduced is a boolean testing if the string has been shortened. If it
    has, an ellipsis (…) will be placed at the end of the tweet.
    """
    if len(Hash) + len(Message) > 133:
        # get rid of a word
        Message = ' '.join(Message.split(' ')[:-1])
        Reduced = True
        return reduce_length_for_tweet(Hash, Message, Reduced)

    return Hash, Message, Reduced

def make_a_tweet(Hash, Message, Reduced):
    """
    Generate a valid tweet using the info passed in.
    """
    tweet = Hash + ': ' + Message
    if Reduced:
        tweet += '…'
    return tweet

def ConvertHashToDate(Hash):
    """
    Converts a unix datestamp given in hexadecimal to
    a formatted date dd/mm/YYYY
    """
    return datetime.utcfromtimestamp(int(Hash, 16)).strftime('%d/%m/%Y')

def Tweet(Tweet, Hash=''):
    """
    Tweet the commit message and wrie the revision number to a file.
    """
    # Twitter authentication
    auth = tweepy.OAuthHandler(C_KEY, C_SECRET)
    auth.set_access_token(A_TOKEN, A_TOKEN_SECRET)
    api = tweepy.API(auth)
    api.update_status(Tweet)

    print (Tweet)
    if Hash:
        # store the hash in a file
        with open('.hash', 'w') as f:
            f.write(Hash)

def GetRecentTweets():
    """
    Load the most recent tweets from a file.
    """
    if op.isfile('.recent'):
        with open('.recent', 'r') as f:
            return f.readlines()
    else:
        return ['']

def OtherTweets():
    '''
    Load a random tweet from the file, check its length and if it has been
    recently tweeted.
    '''
    with open('Tweets.txt', 'r') as f:
        Tweets = f.readlines()

    Recent = GetRecentTweets()

    # cycle through random tweets until one meets the criteria.
    while True:
        text = random.choice(Tweets)

        if (text not in Recent) and CheckLength(text):
            break

    return text

def CheckLength(tweet):
    """
    Check the prospective tweet is not too long.
    """
    if len(tweet) > 139:
        return False
    else:
        return True

def WriteRecent(tweet):
    """
    Write the most recent tweet to a file, keeping the n most recent tweets.
    Adds tweet to front of file and pops the oldest recent tweet off the file.
    """

    if op.isfile('.recent'):
        recent = GetRecentTweets()
    else:
        recent = []

    # if the list is longer than 4, remove the last entry and add the tweet
    # otherwise just add the tweet to the list
    # THIS VALUE MUST BE LESS THAN THE NUMBER OF TWEETS IN THE FILE TO AVOID
    # AN INFINITE LOOP
    if len(recent) >= 2:
        recent = [''] + recent[:-1]
        recent[0] = tweet
    else:
        recent = [''] + recent[:]
        recent[0] = tweet

    with open('.recent', 'w') as f:
        for r in recent:
            f.write(r)

def CheckForNewCommit(Hash):
    """
    Avoid repeating the same tweet by comparing hashes.
    Returns True if values are different.
    """
    if op.isfile('.hash'):
        with open('.hash', 'r') as f:
            CurrentRev = f.readline()
    else:
        # if the file is not present assume commit is new,
        # set value to zero so function returns True
        CurrentRev = '0'

    return CurrentRev != Hash

def Run(repoPath):
    """
    Wrapper to run all of the steps to send out a tweet.
    """
    Hash, Message = ScrapeGIT(repoPath)
    Hash, Message, Reduced = reduce_length_for_tweet(Hash, Message)

    if CheckForNewCommit(Hash):
        FinalTweet = make_a_tweet(Hash, Message, Reduced)
        Tweet(FinalTweet, Hash)
    else:
        # tweet something else?
        tweet = OtherTweets()
        Tweet(tweet)
        WriteRecent(tweet)

if __name__ == "__main__":
    if len(sys.argv) == 2:
        Run(sys.argv[1])
    else:
        sys.exit('%s needs 1 argument:\n\n[1] The path to the git'
                 ' repository\n' % sys.argv[0])
<commit_msg>Fixing path errors<commit_after># -*- coding: utf-8 -*-
"""
Script that scrapes commit info from a git repo logs, formats the info into
a tweet and then tweets it.

Only works on Python 3.4 and requires Tweepy, GitPython and a secrets.py
file to handle the twitter api keys. It is assumed that the machine this script
is run on has github authentication configured.

Call at the terminal with the path to the repo to tweet from or put it in your
crontab.

SWDG 18/1/17
"""
import tweepy
import random
import git
import sys
import os
import os.path as op
from secrets import *
from datetime import datetime

def ScrapeGIT(repoURL):
    """
    Given a path to a local git repo, return the commit message and hash.
    """
    g = git.cmd.Git(repoURL)
    g.pull()
    Message = (g.log('-1', '--pretty=format:%s'))
    Hash = (g.log('-1', '--pretty=format:%h'))

    return Hash, Message

def reduce_length_for_tweet(Hash, Message, Reduced=False):
    """
    Recursively remove a word from the message until it is small enough to tweet
    Reduced is a boolean testing if the string has been shortened. If it
    has, an ellipsis (…) will be placed at the end of the tweet.
    """
    if len(Hash) + len(Message) > 133:
        # get rid of a word
        Message = ' '.join(Message.split(' ')[:-1])
        Reduced = True
        return reduce_length_for_tweet(Hash, Message, Reduced)

    return Hash, Message, Reduced

def make_a_tweet(Hash, Message, Reduced):
    """
    Generate a valid tweet using the info passed in.
    """
    tweet = Hash + ': ' + Message
    if Reduced:
        tweet += '…'
    return tweet

def ConvertHashToDate(Hash):
    """
    Converts a unix datestamp given in hexadecimal to
    a formatted date dd/mm/YYYY
    """
    return datetime.utcfromtimestamp(int(Hash, 16)).strftime('%d/%m/%Y')

def Tweet(Tweet, Hash=''):
    """
    Tweet the commit message and wrie the revision number to a file.
    """
    # Twitter authentication
    auth = tweepy.OAuthHandler(C_KEY, C_SECRET)
    auth.set_access_token(A_TOKEN, A_TOKEN_SECRET)
    api = tweepy.API(auth)
    api.update_status(Tweet)

    print (Tweet)
    if Hash:
        # store the hash in a file
        path = op.realpath(op.join(os.getcwd(), op.dirname(__file__)))
        with open(op.join(path, '.hash'), 'w') as f:
            f.write(Hash)

def GetRecentTweets():
    """
    Load the most recent tweets from a file.
    """
    path = op.realpath(op.join(os.getcwd(), op.dirname(__file__)))
    recentFile = op.join(path, '.recent')
    if op.isfile(recentFile):
        with open(recentFile, 'r') as f:
            return f.readlines()
    else:
        return ['']

def OtherTweets():
    '''
    Load a random tweet from the file, check its length and if it has been
    recently tweeted.
    '''
    path = op.realpath(op.join(os.getcwd(), op.dirname(__file__)))
    with open(op.join(path, 'Tweets.txt'), 'r') as f:
        Tweets = f.readlines()

    Recent = GetRecentTweets()

    # cycle through random tweets until one meets the criteria.
    while True:
        text = random.choice(Tweets)

        if (text not in Recent) and CheckLength(text):
            break

    return text

def CheckLength(tweet):
    """
    Check the prospective tweet is not too long.
    """
    if len(tweet) > 139:
        return False
    else:
        return True

def WriteRecent(tweet):
    """
    Write the most recent tweet to a file, keeping the n most recent tweets.
    Adds tweet to front of file and pops the oldest recent tweet off the file.
    """
    path = op.realpath(op.join(os.getcwd(), op.dirname(__file__)))
    recentFile = op.join(path, '.recent')
    if op.isfile(recentFile):
        recent = GetRecentTweets()
    else:
        recent = []

    # if the list is longer than 15, remove the last entry and add the tweet
    # otherwise just add the tweet to the list
    # THIS VALUE MUST BE LESS THAN THE NUMBER OF TWEETS IN THE FILE TO AVOID
    # AN INFINITE LOOP
    if len(recent) >= 15:
        recent = [''] + recent[:-1]
        recent[0] = tweet
    else:
        recent = [''] + recent[:]
        recent[0] = tweet

    with open(recentFile, 'w') as f:
        for r in recent:
            f.write(r)

def CheckForNewCommit(Hash):
    """
    Avoid repeating the same tweet by comparing hashes.
    Returns True if values are different.
    """
    path = op.realpath(op.join(os.getcwd(), op.dirname(__file__)))
    hashFile = op.join(path, '.hash')
    if op.isfile(hashFile):
        with open(hashFile, 'r') as f:
            CurrentRev = f.readline()
    else:
        # if the file is not present assume commit is new,
        # set value to zero so function returns True
        CurrentRev = '0'

    return CurrentRev != Hash

def Run(repoPath):
    """
    Wrapper to run all of the steps to send out a tweet.
    """
    Hash, Message = ScrapeGIT(repoPath)
    Hash, Message, Reduced = reduce_length_for_tweet(Hash, Message)

    if CheckForNewCommit(Hash):
        FinalTweet = make_a_tweet(Hash, Message, Reduced)
        Tweet(FinalTweet, Hash)
    else:
        # tweet something else?
        tweet = OtherTweets()
        Tweet(tweet)
        WriteRecent(tweet)

if __name__ == "__main__":
    if len(sys.argv) == 2:
        Run(sys.argv[1])
    else:
        sys.exit('%s needs 1 argument:\n\n[1] The path to the git'
                 ' repository\n' % sys.argv[0])
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

# Copyright (c) 2014 CoNWeT Lab., Universidad Politécnica de Madrid

# This file is part of OAuth2 CKAN Extension.

# OAuth2 CKAN Extension is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# OAuth2 CKAN Extension is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with OAuth2 CKAN Extension.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import unicode_literals

import ckanext.oauth2.repozewho as oauth2_repozewho
import logging
import oauth2

from functools import partial
from ckan import plugins
from ckan.common import session
from ckan.plugins import toolkit
from pylons import config

log = logging.getLogger(__name__)

def _no_permissions(context, msg):
    user = context['user']
    return {'success': False, 'msg': msg.format(user=user)}

@toolkit.auth_sysadmins_check
def user_create(context, data_dict):
    msg = toolkit._('Users cannot be created.')
    return _no_permissions(context, msg)

@toolkit.auth_sysadmins_check
def user_update(context, data_dict):
    msg = toolkit._('Users cannot be edited.')
    return _no_permissions(context, msg)

@toolkit.auth_sysadmins_check
def user_reset(context, data_dict):
    msg = toolkit._('Users cannot reset passwords.')
    return _no_permissions(context, msg)

@toolkit.auth_sysadmins_check
def request_reset(context, data_dict):
    msg = toolkit._('Users cannot reset passwords.')
    return _no_permissions(context, msg)

class OAuth2Plugin(plugins.SingletonPlugin):

    plugins.implements(plugins.IAuthenticator, inherit=True)
    plugins.implements(plugins.IAuthFunctions, inherit=True)
    plugins.implements(plugins.IRoutes, inherit=True)
    plugins.implements(plugins.IConfigurer)

    def __init__(self, name=None):
        '''Store the OAuth 2 client configuration'''
        log.debug('Init OAuth2 extension')

        self.register_url = config.get('ckan.oauth2.register_url', None)
        self.reset_url = config.get('ckan.oauth2.reset_url', None)
        self.edit_url = config.get('ckan.oauth2.edit_url', None)

        self.oauth2helper = oauth2.OAuth2Helper()

    def before_map(self, m):
        log.debug('Setting up the redirections to the OAuth2 service')

        # We need to handle petitions received to the Callback URL
        # since some error can arise and we need to process them
        m.connect(oauth2_repozewho.REDIRECT_URL,
                  controller='ckanext.oauth2.controller:OAuth2Controller',
                  action='callback')

        # # Redirect the user to the OAuth service register page
        if self.register_url:
            m.redirect('/user/register', self.register_url)

        # Redirect the user to the OAuth service reset page
        if self.reset_url:
            m.redirect('/user/reset', self.reset_url)

        # Redirect the user to the OAuth service reset page
        if self.edit_url:
            m.redirect('/user/edit/{user}', self.edit_url)

        return m

    def identify(self):
        log.debug('identify')

        # Create session if it does not exist. Workaround to show flash messages
        session.save()

        def _refresh_and_save_token(user_name):
            new_token = self.oauth2helper.refresh_token(user_name)
            if new_token:
                toolkit.c.usertoken = new_token

        environ = toolkit.request.environ
        if 'repoze.who.identity' in environ:
            repoze_userid = environ['repoze.who.identity']['repoze.who.userid']
            log.debug('User logged %r' % repoze_userid)
            toolkit.c.user = repoze_userid
            toolkit.c.usertoken = self.oauth2helper.get_stored_token(repoze_userid)
            toolkit.c.usertoken_refresh = partial(_refresh_and_save_token, repoze_userid)
        else:
            log.warn('The user is not currently logged...')

    def login(self):
        log.debug('login')

        if not toolkit.c.user:
            self.oauth2helper.challenge()
        else:
            redirect_to = toolkit.request.headers.get('Referer', '/')
            toolkit.redirect_to(bytes(redirect_to))

    def get_auth_functions(self):
        # we need to prevent some actions being authorized.
        return {
            'user_create': user_create,
            'user_update': user_update,
            'user_reset': user_reset,
            'request_reset': request_reset,
        }

    def update_config(self, config):
        # Add this plugin's templates dir to CKAN's extra_template_paths, so
        # that CKAN will use this plugin's custom templates.
        plugins.toolkit.add_template_directory(config, 'templates')
<commit_msg>Remove unneeded import<commit_after># -*- coding: utf-8 -*-

# Copyright (c) 2014 CoNWeT Lab., Universidad Politécnica de Madrid

# This file is part of OAuth2 CKAN Extension.

# OAuth2 CKAN Extension is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# OAuth2 CKAN Extension is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with OAuth2 CKAN Extension.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import unicode_literals

import logging
import oauth2

from functools import partial
from ckan import plugins
from ckan.common import session
from ckan.plugins import toolkit
from pylons import config

log = logging.getLogger(__name__)

def _no_permissions(context, msg):
    user = context['user']
    return {'success': False, 'msg': msg.format(user=user)}

@toolkit.auth_sysadmins_check
def user_create(context, data_dict):
    msg = toolkit._('Users cannot be created.')
    return _no_permissions(context, msg)

@toolkit.auth_sysadmins_check
def user_update(context, data_dict):
    msg = toolkit._('Users cannot be edited.')
    return _no_permissions(context, msg)

@toolkit.auth_sysadmins_check
def user_reset(context, data_dict):
    msg = toolkit._('Users cannot reset passwords.')
    return _no_permissions(context, msg)

@toolkit.auth_sysadmins_check
def request_reset(context, data_dict):
    msg = toolkit._('Users cannot reset passwords.')
    return _no_permissions(context, msg)

class OAuth2Plugin(plugins.SingletonPlugin):

    plugins.implements(plugins.IAuthenticator, inherit=True)
    plugins.implements(plugins.IAuthFunctions, inherit=True)
    plugins.implements(plugins.IRoutes, inherit=True)
    plugins.implements(plugins.IConfigurer)

    def __init__(self, name=None):
        '''Store the OAuth 2 client configuration'''
        log.debug('Init OAuth2 extension')

        self.register_url = config.get('ckan.oauth2.register_url', None)
        self.reset_url = config.get('ckan.oauth2.reset_url', None)
        self.edit_url = config.get('ckan.oauth2.edit_url', None)

        self.oauth2helper = oauth2.OAuth2Helper()

    def before_map(self, m):
        log.debug('Setting up the redirections to the OAuth2 service')

        # We need to handle petitions received to the Callback URL
        # since some error can arise and we need to process them
        m.connect(oauth2_repozewho.REDIRECT_URL,
                  controller='ckanext.oauth2.controller:OAuth2Controller',
                  action='callback')

        # # Redirect the user to the OAuth service register page
        if self.register_url:
            m.redirect('/user/register', self.register_url)

        # Redirect the user to the OAuth service reset page
        if self.reset_url:
            m.redirect('/user/reset', self.reset_url)

        # Redirect the user to the OAuth service reset page
        if self.edit_url:
            m.redirect('/user/edit/{user}', self.edit_url)

        return m

    def identify(self):
        log.debug('identify')

        # Create session if it does not exist. Workaround to show flash messages
        session.save()

        def _refresh_and_save_token(user_name):
            new_token = self.oauth2helper.refresh_token(user_name)
            if new_token:
                toolkit.c.usertoken = new_token

        environ = toolkit.request.environ
        if 'repoze.who.identity' in environ:
            repoze_userid = environ['repoze.who.identity']['repoze.who.userid']
            log.debug('User logged %r' % repoze_userid)
            toolkit.c.user = repoze_userid
            toolkit.c.usertoken = self.oauth2helper.get_stored_token(repoze_userid)
            toolkit.c.usertoken_refresh = partial(_refresh_and_save_token, repoze_userid)
        else:
            log.warn('The user is not currently logged...')

    def login(self):
        log.debug('login')

        if not toolkit.c.user:
            self.oauth2helper.challenge()
        else:
            redirect_to = toolkit.request.headers.get('Referer', '/')
            toolkit.redirect_to(bytes(redirect_to))

    def get_auth_functions(self):
        # we need to prevent some actions being authorized.
        return {
            'user_create': user_create,
            'user_update': user_update,
            'user_reset': user_reset,
            'request_reset': request_reset,
        }

    def update_config(self, config):
        # Add this plugin's templates dir to CKAN's extra_template_paths, so
        # that CKAN will use this plugin's custom templates.
        plugins.toolkit.add_template_directory(config, 'templates')
<|endoftext|>


<commit_before>count_all_fmt = "SELECT COUNT(*) FROM {tbl};"
func_store_create_table_fmt = (
    "CREATE TABLE {tbl} ("
    "name TEXT NOT NULL PRIMARY KEY, "
    "code TEXT NOT NULL);"
)
func_table_items_fmt = "SELECT name, code FROM {tbl} ORDER BY name;"
func_table_get_fmt = "SELECT code FROM {tbl} WHERE name=?;"
func_table_ins_fmt = "INSERT INTO {tbl} (name, code) VALUES (?, ?);"
func_table_upd_fmt = "UPDATE {tbl} SET code=? WHERE name=?;"
func_table_del_fmt = "DELETE FROM {tbl} wHERE name=?;"
string_store_create_table_fmt = (
    "CREATE TABLE {tbl} ("
    "id TEXT NOT NULL, "
    "language TEXT NOT NULL DEFAULT 'eng', "
    "string TEXT NOT NULL DEFAULT '', "
    "PRIMARY KEY(id, language)"
    ");"
)
string_table_lang_items_fmt = (
    "SELECT id, string FROM {tbl} "
    "WHERE language=? ORDER BY id;"
)
string_table_get_fmt = (
    "SELECT string FROM {tbl} WHERE "
    "language=? AND "
    "id=?;"
)
string_table_ins_fmt = (
    "INSERT INTO {tbl} (id, language, string) "
    "VALUES (?, ?, ?);"
)
string_table_upd_fmt = (
    "UPDATE {tbl} SET string=? WHERE "
    "language=? AND "
    "id=?;"
)
string_table_del_fmt = (
    "DELETE FROM {tbl} WHERE "
    "language=? AND "
    "id=?;"
)
universal_items = (
    "SELECT lise_globals.key, lise_globals.value "
    "FROM lise_globals JOIN "
    "(SELECT key, branch, MAX(tick) AS tick "
    "FROM lise_globals "
    "WHERE branch=? "
    "AND tick<=? "
    "GROUP BY key, branch) AS hitick "
    "ON lise_globals.key=hitick.key "
    "AND lise_globals.branch=hitick.branch "
    "AND lise_globals.tick=hitick.tick;"
)
universal_get = (
    "SELECT lise_globals.value FROM lise_globals JOIN "
    "(SELECT key, branch, MAX(tick) AS tick "
    "FROM lise_globals "
    "WHERE key=? "
    "AND branch=? "
    "AND tick<=? "
    "GROUP BY key, branch) AS hitick "
    "ON lise_globals.key=hitick.key "
    "AND lise_globals.branch=hitick.branch "
    "AND lise_globals.tick=hitick.tick;"
)
universal_ins = (
    "INSERT INTO lise_globals (key, branch, tick, value) "
    "VALUES (?, ?, ?, ?);"
)
universal_upd = (
    "UPDATE lise_globals SET value=? WHERE "
    "key=? AND "
    "branch=? AND "
    "tick=?;"
)
characters = "SELECT character FROM characters;"
ct_characters = "SELECT COUNT(*) FROM characters;"
ct_character = "SELECT COUNT(*) FROM characters WHERE character=?;"
char_del_fmt = "DELETE FROM {tbl} WHERE character=?;"
poll_node_rules = (
    "SELECT "
    "node_rulebook.character, "
    "node_rulebook.node, "
    "node_rulebook.rulebook, "
    "active_rules.rule, "
    "active_rules.active, "
    "handle.handled "
    "FROM node_rulebook JOIN active_rules ON "
    "node_rulebook.rulebook=active_rules.rulebook "
    "JOIN "
    "(SELECT rulebook, rule, branch, MAX(tick) AS tick "
    "FROM active_rules WHERE "
    "branch=? AND "
    "tick<=? GROUP BY rulebook, rule, branch) AS hitick "
    "ON active_rules.rulebook=hitick.rulebook "
    "AND active_rules.rule=hitick.rule "
    "AND active_rules.branch=hitick.branch "
    "AND active_rules.tick=hitick.tick "
    "LEFT OUTER JOIN rulebooks "
    "ON rulebooks.rulebook=node_rulebook.rulebook "
    "AND rulebooks.rule=active_rules.rule "
    "LEFT OUTER JOIN "
    "(SELECT character, node, rulebook, rule, 1 AS handled "
    "FROM node_rules_handled "
    "WHERE branch=? AND tick=?) "
    "AS handle ON "
    "handle.character=node_rulebook.character AND "
    "handle.node=node_rulebook.node AND "
    "handle.rulebook=node_rulebook.rulebook AND "
    "handle.rule=active_rules.rule "
    "WHERE handle.handled IS NULL "
    "ORDER BY "
    "node_rulebook.character, "
    "node_rulebook.node, "
    "rulebooks.rulebook, "
    "rulebooks.idx "
    "ASC"
    ";"
)
poll_portal_rules = (
    "SELECT "
    "portal_rulebook.character, "
    "portal_rulebook.nodeA, "
    "portal_rulebook.nodeB, "
    "portal_rulebook.idx, "
    "active_rules.rule, "
    "active_rules.active, "
    "handle.handled "
    "FROM portal_rulebook JOIN active_rules ON "
    "portal_rulebook.rulebook=active_rules.rulebook "
    "JOIN "
    "(SELECT rulebook, rule, branch, MAX(tick) AS tick "
    "FROM active_rules WHERE "
    "branch=? AND "
    "tick<=? GROUP BY rulebook, rule, branch) AS hitick "
    "ON active_rules.rulebook=hitick.rulebook "
    "AND active_rules.rule=hitick.rule "
    "AND active_rules.branch=hitick.branch "
    "AND active_rules.tick=hitick.tick "
    "LEFT OUTER JOIN rulebooks "
    "ON rulebooks.rulebook=portal_rulebook.rulebook "
    "AND rulebooks.rule=active_rules.rule "
    "LEFT OUTER JOIN "
    "(SELECT "
    "character, "
    "nodeA, "
    "nodeB, "
    "idx, "
    "rulebook, "
    "rule, "
    "1 AS handled FROM "
    "portal_rules_handled WHERE branch=? AND tick=?) "
    "AS handle ON "
    "handle.character=portal_rulebook.character AND "
    "handle.nodeA=portal_rulebook.nodeA AND "
    "handle.nodeB=portal_rulebook.nodeB AND "
    "handle.idx=portal_rulebook.idx AND "
    "handle.rulebook=portal_rulebook.rulebook AND "
    "handle.rule=active_rules.rule "
    "WHERE handle.handled IS NULL "
    "ORDER BY "
    "portal_rulebook.character, "
    "portal_rulebook.nodeA, "
    "portal_rulebook.nodeB, "
    "portal_rulebook.idx, "
    "rulebooks.rulebook, "
    "rulebooks.idx "
    "ASC"
    ";"
)
poll_char_rules_fmt = (
    "SELECT "
    "characters.character, "
    "characters.{tbl}_rulebook, "
    "active_rules.rule, "
    "active_rules.active, "
    "handle.handled "
    "FROM characters JOIN active_rules ON "
    "characters.{tbl}_rulebook=active_rules.rulebook "
    "JOIN "
    "(SELECT rulebook, rule, branch, MAX(tick) AS tick "
    "FROM active_rules WHERE "
    "branch=? AND "
    "tick<=? GROUP BY rulebook, rule, branch) AS hitick "
    "ON active_rules.rulebook=hitick.rulebook "
    "AND active_rules.rule=hitick.rule "
    "AND active_rules.branch=hitick.branch "
    "AND active_rules.tick=hitick.tick "
    "LEFT OUTER JOIN rulebooks "
    "ON rulebooks.rulebook=characters.{tbl}_rulebook "
    "AND rulebooks.rule=active_rules.rule "
    "LEFT OUTER JOIN "
    "(SELECT character, rulebook, rule, "
    "1 AS handled FROM {tbl}_rules_handled "
    "WHERE branch=? AND tick=?) "
    "AS handle ON "
    "handle.character=characters.character AND "
    "handle.rulebook=characters.{tbl}_rulebook AND "
    "handle.rule=active_rules.rule "
    "WHERE handle.handled IS NULL "
    "ORDER BY "
    "characters.character, "
    "rulebooks.rulebook, "
    "rulebooks.idx "
    "ASC"
    ";"
)
handled_character_rule_fmt = (
    "INSERT INTO {ruletyp}_rules_handled "
    "(character, rulebook, rule, branch, tick) "
    "VALUES (?, ?, ?, ?, ?);"
)
handled_thing_rule = (
    "INSERT INTO thing_rules_handled "
    "(character, thing, rulebook, rule, branch, tick) "
    "VALUES (?, ?, ?, ?, ?, ?);"
)
handled_place_rule = (
    "INSERT INTO place_rules_handled "
    "(character, place, rulebook, rule, branch, tick) "
    "VALUES (?, ?, ?, ?, ?, ?);"
)
handled_portal_rule = (
    "INSERT INTO portal_rules_handled "
    "(character, nodeA, nodeB, idx, rulebook, rule, branch, tick) "
    "VALUES (?, ?, ?, ?, ?, ?, ?, ?);"
)
active_rules_rulebook = (
    "SELECT active_rules.rule, active_rules.active "
    "FROM active_rules JOIN "
    "(SELECT rulebook, rule, branch, MAX(tick) AS tick "
    "FROM active_rules WHERE "
    "rulebook=? AND "
    "branch=? AND "
    "tick<=? GROUP BY rulebook, rule, branch) AS hitick "
    "ON active_rules.rulebook=hitick.rulebook "
    "AND active_rules.rule=hitick.rule "
    "AND active_rules.branch=hitick.branch "
    "AND active_rules.tick=hitick.tick;"
)
active_rule_rulebook = (
    "SELECT active_rules.active FROM active_rules JOIN "
    "(SELECT rulebook, rule, branch, MAX(tick) AS tick "
    "FROM active_rules WHERE "
    "rulebook=? AND "
    "rule=? AND "
    "branch=? AND "
    "tick<=? GROUP BY rulebook, rule, branch) AS hitick "
    "ON active_rules.rulebook=hitick.rulebook "
    "AND active_rules.rule=hitick.rule "
    "AND active_rules.branch=hitick.branch "
    "AND active_rules.tick=hitick.tick;"
)
active_rules_char_fmt = (
    "SELECT active_rules.rule, active_rules.active "
    "FROM active_rules JOIN "
    "(SELECT rulebook, rule, branch, MAX(tick) AS tick "
    "FROM {tbl} WHERE "
    "character=? AND "
    "rulebook=? AND "
    "branch=? AND "
    "tick<=? GROUP BY rulebook, rule, branch) AS hitick "
    "ON active_rules.rulebook=hitick.rulebook "
    "AND active_rules.branch=hitick.branch "
    "AND active_rules.tick=hitick.tick;"
)
active_rule_char_fmt = (
    "SELECT active_rules.active FROM active_rules JOIN ("
    "SELECT rulebook, rule, branch, MAX(tick) AS tick "
    "FROM {tbl} WHERE "
    "character=? AND "
    "rulebook=? AND "
    "rule=? AND "
    "branch=? AND "
    "tick<=? GROUP BY rulebook, rule, branch) AS hitick "
    "ON active_rules.rulebook=hitick.rulebook "
    "AND active_rules.rule=hitick.rule "
    "AND active_rules.branch=hitick.branch "
    "AND active_rules.tick=hitick.tick;"
)
rule_ins_fmt = (
    "INSERT INTO active_rules "
    "(rulebook, rule, branch, tick, active) "
    "VALUES (?, ?, ?, ?, ?);"
)
rule_upd_fmt = (
    "UPDATE active_rules SET active=? WHERE "
    "rulebook=? AND "
    "rule=? AND "
    "branch=? AND "
    "tick=?;"
)
del_char_things = "DELETE FROM things WHERE graph=?;",
del_char_avatars = "DELETE FROM avatars WHERE character_graph=?;"
node_is_thing = (
    "SELECT location FROM things JOIN ("
    "SELECT character, thing, branch, MAX(tick) AS tick "
    "FROM things WHERE "
    "character=? "
    "AND thing=? "
    "AND branch=? "
    "AND tick<=? "
    "GROUP BY character, thing, branch) AS hitick "
    "ON things.character=hitick.character "
    "AND things.thing=hitick.thing "
    "AND things.branch=hitick.branch "
    "AND things.tick=hitick.tick;"
)
rulebook_get_char_fmt = (
    "SELECT {rulemap}_rulebook FROM characters WHERE character=?;"
)
upd_rulebook_char_fmt = (
    "UPDATE characters SET {rulemap}_ruleboook=? WHERE character=?"
)
avatar_users = (
    "SELECT avatars.avatar_graph FROM avatars JOIN ("
    "SELECT character_graph, avatar_graph, avatar_node, "
    "branch, MAX(tick) AS tick "
    "FROM avatars WHERE "
    "avatar_graph=? AND "
    "avatar_node=? AND "
    "branch=? AND "
    "tick<=? GROUP BY "
    "character_graph, avatar_graph, avatar_node, "
    "branch) AS hitick "
    "ON avatars.character_graph=hitick.character_graph "
    "AND avatars.avatar_graph=hitick.avatar_graph "
    "AND avatars.avatar_node=hitick.avatar_node "
    "AND avatars.branch=hitick.branch "
    "AND avatars.tick=hitick.tick;"
)
arrival_time_get = (
    "SELECT MAX(tick) FROM things "
    "WHERE character=? "
    "AND thing=? "
    "AND location=? "
    "AND branch=? "
    "AND tick<=?;"
)
next_arrival_time_get = (
    "SELECT MIN(tick) FROM things "
    "WHERE character=? "
    "AND thing=? "
    "AND location=? "
    "AND branch=? "
    "AND tick>?;"
)
thing_loc_and_next_get = (
    "SELECT location, next_location FROM things JOIN ("
    "SELECT character, thing, branch, MAX(tick) AS tick "
    "FROM things "
    "WHERE character=? "
    "AND thing=? "
    "AND branch=? "
    "AND tick<=? "
    "GROUP BY character, thing, branch) AS hitick "
    "ON things.character=hitick.character "
    "AND things.thing=hitick.thing "
    "AND things.branch=hitick.branch "
    "AND things.tick=hitick.tick;"
)
thing_loc_and_next_ins = (
    "INSERT INTO things ("
    "character, "
    "thing, "
    "branch, "
    "tick, "
    "location, "
    "next_location) VALUES ("
    "?, ?, ?, ?, ?, ?);"
)
thing_loc_and_next_upd = (
    "UPDATE things SET location=?, next_location=? "
    "WHERE character=? "
    "AND thing=? "
    "AND branch=? "
    "AND tick=?;"
)
thing_loc_items = (
    "SELECT things.thing, things.location FROM things JOIN ("
    "SELECT character, thing, branch, MAX(tick) AS tick "
    "FROM things "
    "WHERE character=? "
    "AND branch=? "
    "AND tick<=? "
    "GROUP BY character, thing, branch) AS hitick "
    "ON things.character=hitick.character "
    "AND things.thing=hitick.thing "
    "AND things.branch=hitick.branch "
    "AND things.tick=hitick.tick "
    "LEFT OUTER JOIN "
    "(SELECT nodes.graph, nodes.node, nodes.branch, nodes.rev, "
    "nodes.extant FROM nodes JOIN "
    "(SELECT graph, node, branch, MAX(rev) AS rev FROM nodes "
    "WHERE graph=? "
    "AND branch=? "
    "AND rev<=? GROUP BY graph, node, branch) AS hirev ON "
    "nodes.graph=hirev.graph AND "
    "nodes.node=hirev.node AND "
    "nodes.branch=hirev.branch AND "
    "nodes.rev=hirev.rev) AS existence ON "
    "things.character=existence.graph AND "
    "things.thing=existence.node "
    "WHERE existence.extant;"
)
thing_and_loc = (
    "SELECT things.thing, things.location FROM things JOIN ("
    "SELECT character, thing, branch, MAX(tick) AS tick "
    "FROM things "
    "WHERE character=? "
    "AND thing=? "
    "AND branch=? "
    "AND tick<=? "
    "GROUP BY character, thing, branch) AS hitick "
    "ON things.character=hitick.character "
    "AND things.thing=hitick.thing "
    "AND things.branch=hitick.branch "
    "AND things.tick=hitick.tick "
    "LEFT OUTER JOIN "
    "(SELECT nodes.graph, nodes.node, nodes.branch, "
    "nodes.rev, nodes.extant "
    "FROM nodes JOIN "
    "(SELECT graph, node, branch, MAX(rev) AS rev "
    "FROM nodes "
    "WHERE graph=? "
    "AND node=? "
    "AND branch=? "
    "AND rev<=? GROUP BY graph, node, branch) AS hirev ON "
    "nodes.graph=hirev.graph AND "
    "nodes.node=hirev.node AND "
    "nodes.branch=hirev.branch AND "
    "nodes.rev=hirev.rev) AS existence ON "
    "things.character=existence.graph AND "
    "things.thing=existence.node "
    "WHERE existence.extant;"
)
character_things_items = (
    "SELECT things.thing, things.location FROM things JOIN ("
    "SELECT character, thing, branch, MAX(tick) AS tick "
    "FROM things "
    "WHERE character=? "
    "AND branch=? "
    "AND tick<=? "
    "GROUP BY character, thing, branch) AS hitick ON "
    "things.character=hitick.character "
    "AND things.thing=hitick.thing "
    "AND things.branch=hitick.branch "
    "AND things.tick=hitick.tick;"
)
avatarness = (
    "SELECT "
    "avatars.avatar_graph, "
    "avatars.avatar_node, "
    "avatars.is_avatar FROM avatars "
    "JOIN ("
    "SELECT character_graph, avatar_graph, avatar_node, "
    "branch, MAX(tick) AS tick FROM avatars WHERE "
    "character_graph=? AND "
    "branch=? AND "
    "tick<=? GROUP BY character_graph, avatar_graph, "
    "avatar_node, branch) AS hitick ON "
    "avatars.character_graph=hitick.character_graph AND "
    "avatars.avatar_graph=hitick.avatar_graph AND "
    "avatars.avatar_node=hitick.avatar_node AND "
    "avatars.branch=hitick.branch AND "
    "avatars.tick=hitick.tick;"
)
avatars_now = (
    "SELECT avatars.avatar_graph, avatars.avatar_node, is_avatar "
    "FROM avatars JOIN "
    "(SELECT character_graph, avatar_graph, avatar_node, branch, "
    "MAX(tick) AS tick FROM avatars "
    "WHERE character_graph=? "
    "AND branch=? "
    "AND tick<=? GROUP BY "
    "character_graph, avatar_graph, avatar_node, branch) "
    "AS hitick "
    "ON avatars.character_graph=hitick.character_graph "
    "AND avatars.avatar_graph=hitick.avatar_graph "
    "AND avatars.branch=hitick.branch "
    "AND avatars.tick=hitick.tick "
    "LEFT OUTER JOIN "
    "(SELECT nodes.graph, nodes.node, "
    "nodes.branch, nodes.rev, nodes.extant FROM nodes JOIN "
    "(SELECT graph, node, branch, MAX(rev) AS rev "
    "FROM nodes WHERE "
    "branch=? AND "
    "rev<=? GROUP BY graph, node, branch) AS hirev ON "
    "nodes.graph=hirev.graph AND "
    "nodes.node=hirev.node AND "
    "nodes.branch=hirev.branch AND "
    "nodes.rev=hirev.rev) AS existence ON "
    "avatars.avatar_graph=existence.graph AND "
    "avatars.avatar_node=existence.node WHERE "
    "existence.extant;"
)
avatars_ever = (
    "SELECT avatar_graph, avatar_node, branch, tick, is_avatar "
    "FROM avatars WHERE "
    "character_graph=?;"
)
is_avatar_of = (
    "SELECT avatars.is_avatar FROM avatars JOIN ("
    "SELECT character_graph, avatar_graph, avatar_node, "
    "branch, MAX(tick) AS tick FROM avatars "
    "WHERE character_graph=? "
    "AND avatar_graph=? "
    "AND avatar_node=? "
    "AND branch=? "
    "AND tick<=? GROUP BY "
    "character_graph, avatar_graph, avatar_node, "
    "branch) AS hitick ON "
    "avatars.character_graph=hitick.character_graph "
    "AND avatars.avatar_graph=hitick.avatar_graph "
    "AND avatars.avatar_node=hitick.avatar_node "
    "AND avatars.branch=hitick.branch "
    "AND avatars.tick=hitick.tick;"
)
sense_func_get = (
    "SELECT function FROM senses JOIN "
    "(SELECT character, sense, branch, MAX(tick) AS tick "
    "FROM senses WHERE "
    "character=? AND "
    "sense=? AND "
    "branch=? AND "
    "tick<=? GROUP BY character, sense, branch) AS hitick "
    "ON senses.character=hitick.character "
    "AND senses.sense=hitick.sense "
    "AND senses.branch=hitick.branch "
    "AND senses.tick=hitick.tick;"
)
sense_active_items = (
    "SELECT sense, active FROM senses JOIN ("
    "SELECT character, sense, branch, MAX(tick) AS tick "
    "FROM senses WHERE "
    "(character='' OR character=?) "
    "AND branch=? "
    "AND tick<=? "
    "GROUP BY character, sense, branch) AS hitick "
    "ON senses.character=hitick.character "
    "AND senses.sense=hitick.sense "
    "AND senses.branch=hitick.branch "
    "AND senses.tick=hitick.tick;"
)
sense_is_active = (
    "SELECT active FROM senses JOIN ("
    "SELECT character, sense, branch, MAX(tick) AS tick "
    "FROM senses WHERE "
    "character IN ('', ?) "
    "AND sense=? "
    "AND branch=? "
    "AND tick<=? "
    "GROUP BY character, sense, branch) AS hitick "
    "ON senses.character=hitick.character "
    "AND senses.sense=hitick.sense "
    "AND senses.branch=hitick.branch "
    "AND senses.tick=hitick.tick;"
)
sense_fun_ins = (
    "INSERT INTO senses "
    "(character, sense, branch, tick, function, active) "
    "VALUES "
    "(?, ?, ?, ?, ?, ?);"
)
sense_fun_upd = (
    "UPDATE senses SET function=?, active=? "
    "WHERE character=? "
    "AND sense=? "
    "AND branch=? "
    "AND tick=?;"
)
sense_ins = (
    "INSERT INTO senses "
    "(character, sense, branch, tick, active) "
    "VALUES "
    "(?, ?, ?, ?, ?);"
)
sense_upd = (
    "UPDATE senses SET active=? "
    "WHERE character=? "
    "AND sense=? "
    "AND branch=? "
    "AND tick=?;"
)
character_ins = (
    "INSERT INTO characters "
    "(character, "
    "character_rulebook, "
    "avatar_rulebook, "
    "character_thing_rulebook, "
    "character_place_rulebook, "
    "character_portal_rulebook) "
    "VALUES (?, ?, ?, ?, ?, ?);"
)
avatar_ins = (
    "INSERT INTO avatars ("
    "character_graph, avatar_graph, avatar_node, "
    "branch, tick, is_avatar"
    ") VALUES (?, ?, ?, ?, ?, ?);"
)
avatar_upd = (
    "UPDATE avatars SET is_avatar=? WHERE "
    "character_graph=? AND "
    "avatar_graph=? AND "
    "avatar_node=? AND "
    "branch=? AND "
    "tick=?;"
)
rulebook_rules = (
    "SELECT rule FROM rulebooks WHERE rulebook=? ORDER BY idx ASC;"
)
ct_rulebook_rules = "SELECT COUNT(*) FROM rulebooks WHERE rulebook=?;"
rulebook_get = "SELECT rule FROM rulebooks WHERE rulebook=? AND idx=?;"
rulebook_ins = "INSERT INTO rulebooks (rulebook, idx, rule) VALUES (?, ?, ?);"
rulebook_upd = "UPDATE rulebooks SET rule=? WHERE rulebook=? AND idx=?;"
rulebook_inc = "UPDATE rulebooks SET idx=idx+1 WHERE rulebook=? AND idx>=?;"
rulebook_dec = "UPDATE rulebooks SET idx=idx-1 WHERE rulebook=? AND idx>?;"
rulebook_del = "DELETE FROM rulebooks WHERE rulebook=? AND idx=?"
allrules = "SELECT rule FROM rules;"
haverule = "SELECT rule FROM rules WHERE rule=?;"
ctrules = "SELECT COUNT(*) FROM rules;"
ruleins = "INSERT INTO rules (rule) VALUES (?);"
ruledel = "DELETE FROM rules WHERE rule=?;"
avatar_branch_data = (
    "SELECT "
    "avatars.avatar_node, "
    "avatars.is_avatar FROM avatars JOIN ("
    "SELECT character_graph, avatar_graph, avatar_node, "
    "branch, MAX(tick) AS tick FROM avatars "
    "WHERE character_graph=? "
    "AND avatar_graph=? "
    "AND branch=? "
    "AND tick<=? GROUP BY "
    "character_graph, avatar_graph, avatar_node, branch"
    ") AS hitick ON "
    "avatars.character_graph=hitick.character_graph "
    "AND avatars.avatar_graph=hitick.avatar_graph "
    "AND avatars.avatar_node=hitick.avatar_node "
    "AND avatars.branch=hitick.branch "
    "AND avatars.tick=hitick.tick;"
)
thing_locs_data = (
    "SELECT tick, location, next_location FROM things "
    "WHERE character=? "
    "AND thing=? "
    "AND branch=?;"
)
node_var_data_branch = (
    "SELECT key, rev, value FROM node_val "
    "WHERE graph=? "
    "AND node=? "
    "AND branch=? "
    "ORDER BY rev ASC"
    ";"
)
node_rulebook = (
    "SELECT rulebook "
    "FROM node_rulebook WHERE character=? AND node=?;"
)
ins_node_rulebook = (
    "INSERT INTO node_rulebook "
    "(character, node, rulebook) "
    "VALUES (?, ?, ?);"
)
upd_node_rulebook = (
    "UPDATE node_rulebook SET rulebook=? "
    "WHERE character=? AND node=?;"
)
portal_rulebook = (
    "SELECT rulebook "
    "FROM portal_rulebook WHERE "
    "character=? AND "
    "nodeA=? AND "
    "nodeB=? AND "
    "idx=?;"
)
ins_portal_rulebook = (
    "INSERT INTO portal_rulebook "
    "(character, nodeA, nodeB, idx, rulebook) "
    "VALUES (?, ?, ?, ?, ?);"
)
upd_portal_rulebook = (
    "UPDATE portal_rulebook SET rulebook=? "
    "WHERE character=? "
    "AND nodeA=? "
    "AND nodeB=? "
    "AND idx=?;"
)
<commit_msg>Hardcoded SQL no longer in use.<commit_after><|endoftext|>


<commit_before># More structured (slower) alternative to mini

import sys, imp, __builtin__, string

# Replacement for __import__()
def import_hook(name, globals=None, locals=None, fromlist=None):
    parent = determine_parent(globals)
    q, tail = find_head_package(parent, name)
    m = load_tail(q, tail)
    if not fromlist:
	return q
    if hasattr(m, "__path__"):
	ensure_fromlist(m, fromlist)
    return m

def determine_parent(globals):
    if not globals or  not globals.has_key("__name__"):
	return None
    pname = globals['__name__']
    if globals.has_key("__path__"):
	parent = sys.modules[pname]
	assert globals is parent.__dict__
	return parent
    if '.' in pname:
	i = string.rfind(pname, '.')
	pname = pname[:i]
	parent = sys.modules[pname]
	assert parent.__name__ == pname
	return parent
    return None

def find_head_package(parent, name):
    if '.' in name:
	i = string.find(name, '.')
	head = name[:i]
	tail = name[i+1:]
    else:
	head = name
	tail = ""
    if parent:
	qname = "%s.%s" % (parent.__name__, head)
    else:
	qname = head
    q = import_module(head, qname, parent)
    if q: return q, tail
    if parent:
	qname = head
	parent = None
	q = import_module(head, qname, parent)
	if q: return q, tail
    raise ImportError, "No module named " + qname

def load_tail(q, tail):
    m = q
    while tail:
	i = string.find(tail, '.')
	if i < 0: i = len(tail)
	head, tail = tail[:i], tail[i+1:]
	mname = "%s.%s" % (m.__name__, head)
	m = import_module(head, mname, m)
	if not m:
	    raise ImportError, "No module named " + mname
    return m

def ensure_fromlist(m, fromlist):
    for sub in fromlist:
	if sub != "*" and not hasattr(m, sub):
	    subname = "%s.%s" % (m.__name__, sub)
	    submod = import_module(sub, subname, m)
## 	    if not submod:
## 		raise ImportError, "No module named " + subname

def import_module(partname, fqname, parent):
    try:
	return sys.modules[fqname]
    except KeyError:
	pass
    try:
	fp, pathname, stuff = imp.find_module(partname,
					      parent and parent.__path__)
    except ImportError:
	return None
    try:
	m = imp.load_module(fqname, fp, pathname, stuff)
    finally:
	if fp: fp.close()
    if parent:
	setattr(parent, partname, m)
    return m

# Replacement for reload()
def reload_hook(module):
    name = module.__name__
    if '.' not in name:
	return import_module(name, name, None)
    i = string.rfind(name, '.')
    pname = name[:i]
    parent = sys.modules[pname]
    return import_module(name[i+1:], name, parent)

# Save the original hooks
original_import = __builtin__.__import__
original_reload = __builtin__.reload

# Now install our hooks
__builtin__.__import__ = import_hook
__builtin__.reload = reload_hook

# pretend we're ni, too
sys.modules['ni'] = sys.modules[__name__]
def ni(): pass
def install(): pass
<commit_msg>Make functionality more closely the same as what's implemented by default.<commit_after># More structured (slower) alternative to mini

import sys, imp, __builtin__, string

# Replacement for __import__()
def import_hook(name, globals=None, locals=None, fromlist=None):
    parent = determine_parent(globals)
    q, tail = find_head_package(parent, name)
    m = load_tail(q, tail)
    if not fromlist:
	return q
    if hasattr(m, "__path__"):
	ensure_fromlist(m, fromlist)
    return m

def determine_parent(globals):
    if not globals or  not globals.has_key("__name__"):
	return None
    pname = globals['__name__']
    if globals.has_key("__path__"):
	parent = sys.modules[pname]
	assert globals is parent.__dict__
	return parent
    if '.' in pname:
	i = string.rfind(pname, '.')
	pname = pname[:i]
	parent = sys.modules[pname]
	assert parent.__name__ == pname
	return parent
    return None

def find_head_package(parent, name):
    if '.' in name:
	i = string.find(name, '.')
	head = name[:i]
	tail = name[i+1:]
    else:
	head = name
	tail = ""
    if parent:
	qname = "%s.%s" % (parent.__name__, head)
    else:
	qname = head
    q = import_module(head, qname, parent)
    if q: return q, tail
    if parent:
	qname = head
	parent = None
	q = import_module(head, qname, parent)
	if q: return q, tail
    raise ImportError, "No module named " + qname

def load_tail(q, tail):
    m = q
    while tail:
	i = string.find(tail, '.')
	if i < 0: i = len(tail)
	head, tail = tail[:i], tail[i+1:]
	mname = "%s.%s" % (m.__name__, head)
	m = import_module(head, mname, m)
	if not m:
	    raise ImportError, "No module named " + mname
    return m

def ensure_fromlist(m, fromlist, recursive=0):
    for sub in fromlist:
	if sub == "*":
	    if not recursive:
		try:
		    all = m.__all__
		except AttributeError:
		    pass
		else:
		    ensure_fromlist(m, all, 1)
	    continue
	if sub != "*" and not hasattr(m, sub):
	    subname = "%s.%s" % (m.__name__, sub)
	    submod = import_module(sub, subname, m)
	    if not submod:
		raise ImportError, "No module named " + subname

def import_module(partname, fqname, parent):
    try:
	return sys.modules[fqname]
    except KeyError:
	pass
    try:
	fp, pathname, stuff = imp.find_module(partname,
					      parent and parent.__path__)
    except ImportError:
	return None
    try:
	m = imp.load_module(fqname, fp, pathname, stuff)
    finally:
	if fp: fp.close()
    if parent:
	setattr(parent, partname, m)
    return m

# Replacement for reload()
def reload_hook(module):
    name = module.__name__
    if '.' not in name:
	return import_module(name, name, None)
    i = string.rfind(name, '.')
    pname = name[:i]
    parent = sys.modules[pname]
    return import_module(name[i+1:], name, parent)

# Save the original hooks
original_import = __builtin__.__import__
original_reload = __builtin__.reload

# Now install our hooks
__builtin__.__import__ = import_hook
__builtin__.reload = reload_hook
<|endoftext|>


<commit_before>"""Redo the `...` (representation) but with limits on most sizes."""

__all__ = ["Repr","repr"]

import __builtin__
from itertools import islice

class Repr:

    def __init__(self):
        self.maxlevel = 6
        self.maxtuple = 6
        self.maxlist = 6
        self.maxarray = 5
        self.maxdict = 4
        self.maxset = 6
        self.maxfrozenset = 6
        self.maxdeque = 6
        self.maxstring = 30
        self.maxlong = 40
        self.maxother = 20

    def repr(self, x):
        return self.repr1(x, self.maxlevel)

    def repr1(self, x, level):
        typename = type(x).__name__
        if ' ' in typename:
            parts = typename.split()
            typename = '_'.join(parts)
        if hasattr(self, 'repr_' + typename):
            return getattr(self, 'repr_' + typename)(x, level)
        else:
            s = __builtin__.repr(x)
            if len(s) > self.maxother:
                i = max(0, (self.maxother-3)//2)
                j = max(0, self.maxother-3-i)
                s = s[:i] + '...' + s[len(s)-j:]
            return s

    def _repr_iterable(self, x, level, left, right, maxiter, trail=''):
        n = len(x)
        if level <= 0 and n:
            s = '...'
        else:
            newlevel = level - 1
            repr1 = self.repr1
            pieces = [repr1(elem, newlevel) for elem in islice(x, maxiter)]
            if n > maxiter:  pieces.append('...')
            s = ', '.join(pieces)
            if n == 1 and trail:  right = trail + right
        return '%s%s%s' % (left, s, right)

    def repr_tuple(self, x, level):
        return self._repr_iterable(x, level, '(', ')', self.maxlist, ',')

    def repr_list(self, x, level):
        return self._repr_iterable(x, level, '[', ']', self.maxlist)

    def repr_array(self, x, level):
        header = "array('%s', [" % x.typecode
        return self._repr_iterable(x, level, header, '])', self.maxarray)

    def repr_set(self, x, level):
        return self._repr_iterable(x, level, 'set([', '])', self.maxset)

    def repr_frozenset(self, x, level):
        return self._repr_iterable(x, level, 'frozenset([', '])',
                                   self.maxfrozenset)

    def repr_deque(self, x, level):
        return self._repr_iterable(x, level, 'deque([', '])', self.maxdeque)

    def repr_dict(self, x, level):
        n = len(x)
        if n == 0: return '{}'
        if level <= 0: return '{...}'
        newlevel = level - 1
        repr1 = self.repr1
        pieces = []
        for key in islice(sorted(x), self.maxdict):
            keyrepr = repr1(key, newlevel)
            valrepr = repr1(x[key], newlevel)
            pieces.append('%s: %s' % (keyrepr, valrepr))
        if n > self.maxdict: pieces.append('...')
        s = ', '.join(pieces)
        return '{%s}' % (s,)

    def repr_str(self, x, level):
        s = __builtin__.repr(x[:self.maxstring])
        if len(s) > self.maxstring:
            i = max(0, (self.maxstring-3)//2)
            j = max(0, self.maxstring-3-i)
            s = __builtin__.repr(x[:i] + x[len(x)-j:])
            s = s[:i] + '...' + s[len(s)-j:]
        return s

    def repr_long(self, x, level):
        s = __builtin__.repr(x) # XXX Hope this isn't too slow...
        if len(s) > self.maxlong:
            i = max(0, (self.maxlong-3)//2)
            j = max(0, self.maxlong-3-i)
            s = s[:i] + '...' + s[len(s)-j:]
        return s

    def repr_instance(self, x, level):
        try:
            s = __builtin__.repr(x)
            # Bugs in x.__repr__() can cause arbitrary
            # exceptions -- then make up something
        except:
            return '<' + x.__class__.__name__ + ' instance at ' + \
                      hex(id(x))[2:] + '>'
        if len(s) > self.maxstring:
            i = max(0, (self.maxstring-3)//2)
            j = max(0, self.maxstring-3-i)
            s = s[:i] + '...' + s[len(s)-j:]
        return s

aRepr = Repr()
repr = aRepr.repr
<commit_msg>Make sure sets are printed in sorted order<commit_after>"""Redo the `...` (representation) but with limits on most sizes."""

__all__ = ["Repr","repr"]

import __builtin__
from itertools import islice

class Repr:

    def __init__(self):
        self.maxlevel = 6
        self.maxtuple = 6
        self.maxlist = 6
        self.maxarray = 5
        self.maxdict = 4
        self.maxset = 6
        self.maxfrozenset = 6
        self.maxdeque = 6
        self.maxstring = 30
        self.maxlong = 40
        self.maxother = 20

    def repr(self, x):
        return self.repr1(x, self.maxlevel)

    def repr1(self, x, level):
        typename = type(x).__name__
        if ' ' in typename:
            parts = typename.split()
            typename = '_'.join(parts)
        if hasattr(self, 'repr_' + typename):
            return getattr(self, 'repr_' + typename)(x, level)
        else:
            s = __builtin__.repr(x)
            if len(s) > self.maxother:
                i = max(0, (self.maxother-3)//2)
                j = max(0, self.maxother-3-i)
                s = s[:i] + '...' + s[len(s)-j:]
            return s

    def _repr_iterable(self, x, level, left, right, maxiter, trail=''):
        n = len(x)
        if level <= 0 and n:
            s = '...'
        else:
            newlevel = level - 1
            repr1 = self.repr1
            pieces = [repr1(elem, newlevel) for elem in islice(x, maxiter)]
            if n > maxiter:  pieces.append('...')
            s = ', '.join(pieces)
            if n == 1 and trail:  right = trail + right
        return '%s%s%s' % (left, s, right)

    def repr_tuple(self, x, level):
        return self._repr_iterable(x, level, '(', ')', self.maxlist, ',')

    def repr_list(self, x, level):
        return self._repr_iterable(x, level, '[', ']', self.maxlist)

    def repr_array(self, x, level):
        header = "array('%s', [" % x.typecode
        return self._repr_iterable(x, level, header, '])', self.maxarray)

    def repr_set(self, x, level):
        x = sorted(x)
        return self._repr_iterable(x, level, 'set([', '])', self.maxset)

    def repr_frozenset(self, x, level):
        x = sorted(x)
        return self._repr_iterable(x, level, 'frozenset([', '])',
                                   self.maxfrozenset)

    def repr_deque(self, x, level):
        return self._repr_iterable(x, level, 'deque([', '])', self.maxdeque)

    def repr_dict(self, x, level):
        n = len(x)
        if n == 0: return '{}'
        if level <= 0: return '{...}'
        newlevel = level - 1
        repr1 = self.repr1
        pieces = []
        for key in islice(sorted(x), self.maxdict):
            keyrepr = repr1(key, newlevel)
            valrepr = repr1(x[key], newlevel)
            pieces.append('%s: %s' % (keyrepr, valrepr))
        if n > self.maxdict: pieces.append('...')
        s = ', '.join(pieces)
        return '{%s}' % (s,)

    def repr_str(self, x, level):
        s = __builtin__.repr(x[:self.maxstring])
        if len(s) > self.maxstring:
            i = max(0, (self.maxstring-3)//2)
            j = max(0, self.maxstring-3-i)
            s = __builtin__.repr(x[:i] + x[len(x)-j:])
            s = s[:i] + '...' + s[len(s)-j:]
        return s

    def repr_long(self, x, level):
        s = __builtin__.repr(x) # XXX Hope this isn't too slow...
        if len(s) > self.maxlong:
            i = max(0, (self.maxlong-3)//2)
            j = max(0, self.maxlong-3-i)
            s = s[:i] + '...' + s[len(s)-j:]
        return s

    def repr_instance(self, x, level):
        try:
            s = __builtin__.repr(x)
            # Bugs in x.__repr__() can cause arbitrary
            # exceptions -- then make up something
        except:
            return '<' + x.__class__.__name__ + ' instance at ' + \
                      hex(id(x))[2:] + '>'
        if len(s) > self.maxstring:
            i = max(0, (self.maxstring-3)//2)
            j = max(0, self.maxstring-3-i)
            s = s[:i] + '...' + s[len(s)-j:]
        return s

aRepr = Repr()
repr = aRepr.repr
<|endoftext|>


<commit_before>"""Append module search paths for third-party packages to sys.path.

****************************************************************
* This module is automatically imported during initialization. *
****************************************************************

In earlier versions of Python (up to 1.5a3), scripts or modules that
needed to use site-specific modules would place ``import site''
somewhere near the top of their code.  Because of the automatic
import, this is no longer necessary (but code that does it still
works).

This will append site-specific paths to the module search path.  On
Unix (including Mac OSX), it starts with sys.prefix and
sys.exec_prefix (if different) and appends
lib/python<version>/site-packages as well as lib/site-python.
On other platforms (such as Windows), it tries each of the
prefixes directly, as well as with lib/site-packages appended.  The
resulting directories, if they exist, are appended to sys.path, and
also inspected for path configuration files.

A path configuration file is a file whose name has the form
<package>.pth; its contents are additional directories (one per line)
to be added to sys.path.  Non-existing directories (or
non-directories) are never added to sys.path; no directory is added to
sys.path more than once.  Blank lines and lines beginning with
'#' are skipped. Lines starting with 'import' are executed.

For example, suppose sys.prefix and sys.exec_prefix are set to
/usr/local and there is a directory /usr/local/lib/python2.5/site-packages
with three subdirectories, foo, bar and spam, and two path
configuration files, foo.pth and bar.pth.  Assume foo.pth contains the
following:

  # foo package configuration
  foo
  bar
  bletch

and bar.pth contains:

  # bar package configuration
  bar

Then the following directories are added to sys.path, in this order:

  /usr/local/lib/python2.5/site-packages/bar
  /usr/local/lib/python2.5/site-packages/foo

Note that bletch is omitted because it doesn't exist; bar precedes foo
because bar.pth comes alphabetically before foo.pth; and spam is
omitted because it is not mentioned in either path configuration file.

After these path manipulations, an attempt is made to import a module
named sitecustomize, which can perform arbitrary additional
site-specific customizations.  If this import fails with an
ImportError exception, it is silently ignored.

"""

import sys
import os
import __builtin__

def makepath(*paths):
    dir = os.path.abspath(os.path.join(*paths))
    return dir, os.path.normcase(dir)

def abs__file__():
    """Set all module' __file__ attribute to an absolute path"""
    for m in sys.modules.values():
        if hasattr(m, '__loader__'):
            continue   # don't mess with a PEP 302-supplied __file__
        try:
            m.__file__ = os.path.abspath(m.__file__)
        except AttributeError:
            continue

def removeduppaths():
    """ Remove duplicate entries from sys.path along with making them
    absolute"""
    # This ensures that the initial path provided by the interpreter contains
    # only absolute pathnames, even if we're running from the build directory.
    L = []
    known_paths = set()
    for dir in sys.path:
        # Filter out duplicate paths (on case-insensitive file systems also
        # if they only differ in case); turn relative paths into absolute
        # paths.
        dir, dircase = makepath(dir)
        if not dircase in known_paths:
            L.append(dir)
            known_paths.add(dircase)
    sys.path[:] = L
    return known_paths

# XXX This should not be part of site.py, since it is needed even when
# using the -S option for Python.  See http://www.python.org/sf/586680
def addbuilddir():
    """Append ./build/lib.<platform> in case we're running in the build dir
    (especially for Guido :-)"""
    from distutils.util import get_platform
    s = "build/lib.%s-%.3s" % (get_platform(), sys.version)
    s = os.path.join(os.path.dirname(sys.path[-1]), s)
    sys.path.append(s)

def _init_pathinfo():
    """Return a set containing all existing directory entries from sys.path"""
    d = set()
    for dir in sys.path:
        try:
            if os.path.isdir(dir):
                dir, dircase = makepath(dir)
                d.add(dircase)
        except TypeError:
            continue
    return d

def addpackage(sitedir, name, known_paths):
    """Process a .pth file within the site-packages directory:
       For each line in the file, either combine it with sitedir to a path
       and add that to known_paths, or execute it if it starts with 'import '.
    """
    if known_paths is None:
        _init_pathinfo()
        reset = 1
    else:
        reset = 0
    fullname = os.path.join(sitedir, name)
    try:
        f = open(fullname, "rU")
    except IOError:
        return
    try:
        for line in f:
            if line.startswith("#"):
                continue
            if line.startswith("import ") or line.startswith("import\t"):
                exec line
                continue
            line = line.rstrip()
            dir, dircase = makepath(sitedir, line)
            if not dircase in known_paths and os.path.exists(dir):
                sys.path.append(dir)
                known_paths.add(dircase)
    finally:
        f.close()
    if reset:
        known_paths = None
    return known_paths

def addsitedir(sitedir, known_paths=None):
    """Add 'sitedir' argument to sys.path if missing and handle .pth files in
    'sitedir'"""
    if known_paths is None:
        known_paths = _init_pathinfo()
        reset = 1
    else:
        reset = 0
    sitedir, sitedircase = makepath(sitedir)
    if not sitedircase in known_paths:
        sys.path.append(sitedir)        # Add path component
    try:
        names = os.listdir(sitedir)
    except os.error:
        return
    names.sort()
    for name in names:
        if name.endswith(os.extsep + "pth"):
            addpackage(sitedir, name, known_paths)
    if reset:
        known_paths = None
    return known_paths

def addsitepackages(known_paths):
    """Add site-packages (and possibly site-python) to sys.path"""
    prefixes = [sys.prefix]
    if sys.exec_prefix != sys.prefix:
        prefixes.append(sys.exec_prefix)
    for prefix in prefixes:
        if prefix:
            if sys.platform in ('os2emx', 'riscos'):
                sitedirs = [os.path.join(prefix, "Lib", "site-packages")]
            elif os.sep == '/':
                sitedirs = [os.path.join(prefix,
                                         "lib",
                                         "python" + sys.version[:3],
                                         "site-packages"),
                            os.path.join(prefix, "lib", "site-python")]
            else:
                sitedirs = [prefix, os.path.join(prefix, "lib", "site-packages")]
            if sys.platform == 'darwin':
                # for framework builds *only* we add the standard Apple
                # locations. Currently only per-user, but /Library and
                # /Network/Library could be added too
                if 'Python.framework' in prefix:
                    home = os.environ.get('HOME')
                    if home:
                        sitedirs.append(
                            os.path.join(home,
                                         'Library',
                                         'Python',
                                         sys.version[:3],
                                         'site-packages'))
            for sitedir in sitedirs:
                if os.path.isdir(sitedir):
                    addsitedir(sitedir, known_paths)
    return None

def setBEGINLIBPATH():
    """The OS/2 EMX port has optional extension modules that do double duty
    as DLLs (and must use the .DLL file extension) for other extensions.
    The library search path needs to be amended so these will be found
    during module import.  Use BEGINLIBPATH so that these are at the start
    of the library search path.

    """
    dllpath = os.path.join(sys.prefix, "Lib", "lib-dynload")
    libpath = os.environ['BEGINLIBPATH'].split(';')
    if libpath[-1]:
        libpath.append(dllpath)
    else:
        libpath[-1] = dllpath
    os.environ['BEGINLIBPATH'] = ';'.join(libpath)

def setquit():
    """Define new built-ins 'quit' and 'exit'.
    These are simply strings that display a hint on how to exit.

    """
    if os.sep == ':':
        eof = 'Cmd-Q'
    elif os.sep == '\\':
        eof = 'Ctrl-Z plus Return'
    else:
        eof = 'Ctrl-D (i.e. EOF)'

    class Quitter(object):
        def __init__(self, name):
            self.name = name
        def __repr__(self):
            return 'Use %s() or %s to exit' % (self.name, eof)
        def __call__(self, code=None):
            # Shells like IDLE catch the SystemExit, but listen when their
            # stdin wrapper is closed.
            try:
                sys.stdin.close()
            except:
                pass
            raise SystemExit(code)
    __builtin__.quit = Quitter('quit')
    __builtin__.exit = Quitter('exit')

class _Printer(object):
    """interactive prompt objects for printing the license text, a list of
    contributors and the copyright notice."""

    MAXLINES = 23

    def __init__(self, name, data, files=(), dirs=()):
        self.__name = name
        self.__data = data
        self.__files = files
        self.__dirs = dirs
        self.__lines = None

    def __setup(self):
        if self.__lines:
            return
        data = None
        for dir in self.__dirs:
            for filename in self.__files:
                filename = os.path.join(dir, filename)
                try:
                    fp = file(filename, "rU")
                    data = fp.read()
                    fp.close()
                    break
                except IOError:
                    pass
            if data:
                break
        if not data:
            data = self.__data
        self.__lines = data.split('\n')
        self.__linecnt = len(self.__lines)

    def __repr__(self):
        self.__setup()
        if len(self.__lines) <= self.MAXLINES:
            return "\n".join(self.__lines)
        else:
            return "Type %s() to see the full %s text" % ((self.__name,)*2)

    def __call__(self):
        self.__setup()
        prompt = 'Hit Return for more, or q (and Return) to quit: '
        lineno = 0
        while 1:
            try:
                for i in range(lineno, lineno + self.MAXLINES):
                    print self.__lines[i]
            except IndexError:
                break
            else:
                lineno += self.MAXLINES
                key = None
                while key is None:
                    key = raw_input(prompt)
                    if key not in ('', 'q'):
                        key = None
                if key == 'q':
                    break

def setcopyright():
    """Set 'copyright' and 'credits' in __builtin__"""
    __builtin__.copyright = _Printer("copyright", sys.copyright)
    if sys.platform[:4] == 'java':
        __builtin__.credits = _Printer(
            "credits",
            "Jython is maintained by the Jython developers (www.jython.org).")
    else:
        __builtin__.credits = _Printer("credits", """\
    Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands
    for supporting Python development.  See www.python.org for more information.""")
    here = os.path.dirname(os.__file__)
    __builtin__.license = _Printer(
        "license", "See http://www.python.org/%.3s/license.html" % sys.version,
        ["LICENSE.txt", "LICENSE"],
        [os.path.join(here, os.pardir), here, os.curdir])

class _Helper(object):
    """Define the built-in 'help'.
    This is a wrapper around pydoc.help (with a twist).

    """

    def __repr__(self):
        return "Type help() for interactive help, " \
               "or help(object) for help about object."
    def __call__(self, *args, **kwds):
        import pydoc
        return pydoc.help(*args, **kwds)

def sethelper():
    __builtin__.help = _Helper()

def aliasmbcs():
    """On Windows, some default encodings are not provided by Python,
    while they are always available as "mbcs" in each locale. Make
    them usable by aliasing to "mbcs" in such a case."""
    if sys.platform == 'win32':
        import locale, codecs
        enc = locale.getdefaultlocale()[1]
        if enc.startswith('cp'):            # "cp***" ?
            try:
                codecs.lookup(enc)
            except LookupError:
                import encodings
                encodings._cache[enc] = encodings._unknown
                encodings.aliases.aliases[enc] = 'mbcs'

def setencoding():
    """Set the string encoding used by the Unicode implementation.  The
    default is 'ascii', but if you're willing to experiment, you can
    change this."""
    encoding = "ascii" # Default value set by _PyUnicode_Init()
    if 0:
        # Enable to support locale aware default string encodings.
        import locale
        loc = locale.getdefaultlocale()
        if loc[1]:
            encoding = loc[1]
    if 0:
        # Enable to switch off string to Unicode coercion and implicit
        # Unicode to string conversion.
        encoding = "undefined"
    if encoding != "ascii":
        # On Non-Unicode builds this will raise an AttributeError...
        sys.setdefaultencoding(encoding) # Needs Python Unicode build !

def execsitecustomize():
    """Run custom site specific code, if available."""
    try:
        import sitecustomize
    except ImportError:
        pass

def main():
    abs__file__()
    paths_in_sys = removeduppaths()
    if (os.name == "posix" and sys.path and
        os.path.basename(sys.path[-1]) == "Modules"):
        addbuilddir()
    paths_in_sys = addsitepackages(paths_in_sys)
    if sys.platform == 'os2emx':
        setBEGINLIBPATH()
    setquit()
    setcopyright()
    sethelper()
    aliasmbcs()
    setencoding()
    execsitecustomize()
    # Remove sys.setdefaultencoding() so that users cannot change the
    # encoding after initialization.  The test for presence is needed when
    # this module is run as a script, because this code is executed twice.
    if hasattr(sys, "setdefaultencoding"):
        del sys.setdefaultencoding

main()

def _test():
    print "sys.path = ["
    for dir in sys.path:
        print "    %r," % (dir,)
    print "]"

if __name__ == '__main__':
    _test()
<commit_msg>Add the correct build dir when building with pydebug.<commit_after>"""Append module search paths for third-party packages to sys.path.

****************************************************************
* This module is automatically imported during initialization. *
****************************************************************

In earlier versions of Python (up to 1.5a3), scripts or modules that
needed to use site-specific modules would place ``import site''
somewhere near the top of their code.  Because of the automatic
import, this is no longer necessary (but code that does it still
works).

This will append site-specific paths to the module search path.  On
Unix (including Mac OSX), it starts with sys.prefix and
sys.exec_prefix (if different) and appends
lib/python<version>/site-packages as well as lib/site-python.
On other platforms (such as Windows), it tries each of the
prefixes directly, as well as with lib/site-packages appended.  The
resulting directories, if they exist, are appended to sys.path, and
also inspected for path configuration files.

A path configuration file is a file whose name has the form
<package>.pth; its contents are additional directories (one per line)
to be added to sys.path.  Non-existing directories (or
non-directories) are never added to sys.path; no directory is added to
sys.path more than once.  Blank lines and lines beginning with
'#' are skipped. Lines starting with 'import' are executed.

For example, suppose sys.prefix and sys.exec_prefix are set to
/usr/local and there is a directory /usr/local/lib/python2.5/site-packages
with three subdirectories, foo, bar and spam, and two path
configuration files, foo.pth and bar.pth.  Assume foo.pth contains the
following:

  # foo package configuration
  foo
  bar
  bletch

and bar.pth contains:

  # bar package configuration
  bar

Then the following directories are added to sys.path, in this order:

  /usr/local/lib/python2.5/site-packages/bar
  /usr/local/lib/python2.5/site-packages/foo

Note that bletch is omitted because it doesn't exist; bar precedes foo
because bar.pth comes alphabetically before foo.pth; and spam is
omitted because it is not mentioned in either path configuration file.

After these path manipulations, an attempt is made to import a module
named sitecustomize, which can perform arbitrary additional
site-specific customizations.  If this import fails with an
ImportError exception, it is silently ignored.

"""

import sys
import os
import __builtin__

def makepath(*paths):
    dir = os.path.abspath(os.path.join(*paths))
    return dir, os.path.normcase(dir)

def abs__file__():
    """Set all module' __file__ attribute to an absolute path"""
    for m in sys.modules.values():
        if hasattr(m, '__loader__'):
            continue   # don't mess with a PEP 302-supplied __file__
        try:
            m.__file__ = os.path.abspath(m.__file__)
        except AttributeError:
            continue

def removeduppaths():
    """ Remove duplicate entries from sys.path along with making them
    absolute"""
    # This ensures that the initial path provided by the interpreter contains
    # only absolute pathnames, even if we're running from the build directory.
    L = []
    known_paths = set()
    for dir in sys.path:
        # Filter out duplicate paths (on case-insensitive file systems also
        # if they only differ in case); turn relative paths into absolute
        # paths.
        dir, dircase = makepath(dir)
        if not dircase in known_paths:
            L.append(dir)
            known_paths.add(dircase)
    sys.path[:] = L
    return known_paths

# XXX This should not be part of site.py, since it is needed even when
# using the -S option for Python.  See http://www.python.org/sf/586680
def addbuilddir():
    """Append ./build/lib.<platform> in case we're running in the build dir
    (especially for Guido :-)"""
    from distutils.util import get_platform
    s = "build/lib.%s-%.3s" % (get_platform(), sys.version)
    if hasattr(sys, 'gettotalrefcount'):
        s += '-pydebug'
    s = os.path.join(os.path.dirname(sys.path[-1]), s)
    sys.path.append(s)

def _init_pathinfo():
    """Return a set containing all existing directory entries from sys.path"""
    d = set()
    for dir in sys.path:
        try:
            if os.path.isdir(dir):
                dir, dircase = makepath(dir)
                d.add(dircase)
        except TypeError:
            continue
    return d

def addpackage(sitedir, name, known_paths):
    """Process a .pth file within the site-packages directory:
       For each line in the file, either combine it with sitedir to a path
       and add that to known_paths, or execute it if it starts with 'import '.
    """
    if known_paths is None:
        _init_pathinfo()
        reset = 1
    else:
        reset = 0
    fullname = os.path.join(sitedir, name)
    try:
        f = open(fullname, "rU")
    except IOError:
        return
    try:
        for line in f:
            if line.startswith("#"):
                continue
            if line.startswith("import ") or line.startswith("import\t"):
                exec line
                continue
            line = line.rstrip()
            dir, dircase = makepath(sitedir, line)
            if not dircase in known_paths and os.path.exists(dir):
                sys.path.append(dir)
                known_paths.add(dircase)
    finally:
        f.close()
    if reset:
        known_paths = None
    return known_paths

def addsitedir(sitedir, known_paths=None):
    """Add 'sitedir' argument to sys.path if missing and handle .pth files in
    'sitedir'"""
    if known_paths is None:
        known_paths = _init_pathinfo()
        reset = 1
    else:
        reset = 0
    sitedir, sitedircase = makepath(sitedir)
    if not sitedircase in known_paths:
        sys.path.append(sitedir)        # Add path component
    try:
        names = os.listdir(sitedir)
    except os.error:
        return
    names.sort()
    for name in names:
        if name.endswith(os.extsep + "pth"):
            addpackage(sitedir, name, known_paths)
    if reset:
        known_paths = None
    return known_paths

def addsitepackages(known_paths):
    """Add site-packages (and possibly site-python) to sys.path"""
    prefixes = [sys.prefix]
    if sys.exec_prefix != sys.prefix:
        prefixes.append(sys.exec_prefix)
    for prefix in prefixes:
        if prefix:
            if sys.platform in ('os2emx', 'riscos'):
                sitedirs = [os.path.join(prefix, "Lib", "site-packages")]
            elif os.sep == '/':
                sitedirs = [os.path.join(prefix,
                                         "lib",
                                         "python" + sys.version[:3],
                                         "site-packages"),
                            os.path.join(prefix, "lib", "site-python")]
            else:
                sitedirs = [prefix, os.path.join(prefix, "lib", "site-packages")]
            if sys.platform == 'darwin':
                # for framework builds *only* we add the standard Apple
                # locations. Currently only per-user, but /Library and
                # /Network/Library could be added too
                if 'Python.framework' in prefix:
                    home = os.environ.get('HOME')
                    if home:
                        sitedirs.append(
                            os.path.join(home,
                                         'Library',
                                         'Python',
                                         sys.version[:3],
                                         'site-packages'))
            for sitedir in sitedirs:
                if os.path.isdir(sitedir):
                    addsitedir(sitedir, known_paths)
    return None

def setBEGINLIBPATH():
    """The OS/2 EMX port has optional extension modules that do double duty
    as DLLs (and must use the .DLL file extension) for other extensions.
    The library search path needs to be amended so these will be found
    during module import.  Use BEGINLIBPATH so that these are at the start
    of the library search path.

    """
    dllpath = os.path.join(sys.prefix, "Lib", "lib-dynload")
    libpath = os.environ['BEGINLIBPATH'].split(';')
    if libpath[-1]:
        libpath.append(dllpath)
    else:
        libpath[-1] = dllpath
    os.environ['BEGINLIBPATH'] = ';'.join(libpath)

def setquit():
    """Define new built-ins 'quit' and 'exit'.
    These are simply strings that display a hint on how to exit.

    """
    if os.sep == ':':
        eof = 'Cmd-Q'
    elif os.sep == '\\':
        eof = 'Ctrl-Z plus Return'
    else:
        eof = 'Ctrl-D (i.e. EOF)'

    class Quitter(object):
        def __init__(self, name):
            self.name = name
        def __repr__(self):
            return 'Use %s() or %s to exit' % (self.name, eof)
        def __call__(self, code=None):
            # Shells like IDLE catch the SystemExit, but listen when their
            # stdin wrapper is closed.
            try:
                sys.stdin.close()
            except:
                pass
            raise SystemExit(code)
    __builtin__.quit = Quitter('quit')
    __builtin__.exit = Quitter('exit')

class _Printer(object):
    """interactive prompt objects for printing the license text, a list of
    contributors and the copyright notice."""

    MAXLINES = 23

    def __init__(self, name, data, files=(), dirs=()):
        self.__name = name
        self.__data = data
        self.__files = files
        self.__dirs = dirs
        self.__lines = None

    def __setup(self):
        if self.__lines:
            return
        data = None
        for dir in self.__dirs:
            for filename in self.__files:
                filename = os.path.join(dir, filename)
                try:
                    fp = file(filename, "rU")
                    data = fp.read()
                    fp.close()
                    break
                except IOError:
                    pass
            if data:
                break
        if not data:
            data = self.__data
        self.__lines = data.split('\n')
        self.__linecnt = len(self.__lines)

    def __repr__(self):
        self.__setup()
        if len(self.__lines) <= self.MAXLINES:
            return "\n".join(self.__lines)
        else:
            return "Type %s() to see the full %s text" % ((self.__name,)*2)

    def __call__(self):
        self.__setup()
        prompt = 'Hit Return for more, or q (and Return) to quit: '
        lineno = 0
        while 1:
            try:
                for i in range(lineno, lineno + self.MAXLINES):
                    print self.__lines[i]
            except IndexError:
                break
            else:
                lineno += self.MAXLINES
                key = None
                while key is None:
                    key = raw_input(prompt)
                    if key not in ('', 'q'):
                        key = None
                if key == 'q':
                    break

def setcopyright():
    """Set 'copyright' and 'credits' in __builtin__"""
    __builtin__.copyright = _Printer("copyright", sys.copyright)
    if sys.platform[:4] == 'java':
        __builtin__.credits = _Printer(
            "credits",
            "Jython is maintained by the Jython developers (www.jython.org).")
    else:
        __builtin__.credits = _Printer("credits", """\
    Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands
    for supporting Python development.  See www.python.org for more information.""")
    here = os.path.dirname(os.__file__)
    __builtin__.license = _Printer(
        "license", "See http://www.python.org/%.3s/license.html" % sys.version,
        ["LICENSE.txt", "LICENSE"],
        [os.path.join(here, os.pardir), here, os.curdir])

class _Helper(object):
    """Define the built-in 'help'.
    This is a wrapper around pydoc.help (with a twist).

    """

    def __repr__(self):
        return "Type help() for interactive help, " \
               "or help(object) for help about object."
    def __call__(self, *args, **kwds):
        import pydoc
        return pydoc.help(*args, **kwds)

def sethelper():
    __builtin__.help = _Helper()

def aliasmbcs():
    """On Windows, some default encodings are not provided by Python,
    while they are always available as "mbcs" in each locale. Make
    them usable by aliasing to "mbcs" in such a case."""
    if sys.platform == 'win32':
        import locale, codecs
        enc = locale.getdefaultlocale()[1]
        if enc.startswith('cp'):            # "cp***" ?
            try:
                codecs.lookup(enc)
            except LookupError:
                import encodings
                encodings._cache[enc] = encodings._unknown
                encodings.aliases.aliases[enc] = 'mbcs'

def setencoding():
    """Set the string encoding used by the Unicode implementation.  The
    default is 'ascii', but if you're willing to experiment, you can
    change this."""
    encoding = "ascii" # Default value set by _PyUnicode_Init()
    if 0:
        # Enable to support locale aware default string encodings.
        import locale
        loc = locale.getdefaultlocale()
        if loc[1]:
            encoding = loc[1]
    if 0:
        # Enable to switch off string to Unicode coercion and implicit
        # Unicode to string conversion.
        encoding = "undefined"
    if encoding != "ascii":
        # On Non-Unicode builds this will raise an AttributeError...
        sys.setdefaultencoding(encoding) # Needs Python Unicode build !

def execsitecustomize():
    """Run custom site specific code, if available."""
    try:
        import sitecustomize
    except ImportError:
        pass

def main():
    abs__file__()
    paths_in_sys = removeduppaths()
    if (os.name == "posix" and sys.path and
        os.path.basename(sys.path[-1]) == "Modules"):
        addbuilddir()
    paths_in_sys = addsitepackages(paths_in_sys)
    if sys.platform == 'os2emx':
        setBEGINLIBPATH()
    setquit()
    setcopyright()
    sethelper()
    aliasmbcs()
    setencoding()
    execsitecustomize()
    # Remove sys.setdefaultencoding() so that users cannot change the
    # encoding after initialization.  The test for presence is needed when
    # this module is run as a script, because this code is executed twice.
    if hasattr(sys, "setdefaultencoding"):
        del sys.setdefaultencoding

main()

def _test():
    print "sys.path = ["
    for dir in sys.path:
        print "    %r," % (dir,)
    print "]"

if __name__ == '__main__':
    _test()
<|endoftext|>