<commit_before>"""
sentry.interfaces
~~~~~~~~~~~~~~~~~

Interfaces provide an abstraction for how structured data should be
validated and rendered.

:copyright: (c) 2010-2012 by the Sentry Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""

import itertools
import urlparse

from django.http import QueryDict

from sentry.web.helpers import render_to_string

# unserialization concept is based on pickle
class _EmptyClass(object):
    pass

def unserialize(klass, data):
    value = _EmptyClass()
    value.__class__ = klass
    value.__setstate__(data)
    return value

def get_context(lineno, context_line, pre_context=None, post_context=None):
    lineno = int(lineno)
    context = []
    start_lineno = lineno - len(pre_context or [])
    if pre_context:
        start_lineno = lineno - len(pre_context)
        at_lineno = start_lineno
        for line in pre_context:
            context.append((at_lineno, line))
            at_lineno += 1
    else:
        start_lineno = lineno
        at_lineno = lineno

    context.append((at_lineno, context_line))
    at_lineno += 1

    if post_context:
        for line in post_context:
            context.append((at_lineno, line))
            at_lineno += 1

    return context

class Interface(object):
    """
    An interface is a structured represntation of data, which may
    render differently than the default ``extra`` metadata in an event.
    """

    score = 0

    def __init__(self, **kwargs):
        self.attrs = kwargs.keys()
        self.__dict__.update(kwargs)

    def __setstate__(self, data):
        kwargs = self.unserialize(data)
        self.attrs = kwargs.keys()
        self.__dict__.update(kwargs)

    def __getstate__(self):
        return self.serialize()

    def unserialize(self, data):
        return data

    def serialize(self):
        return dict((k, self.__dict__[k]) for k in self.attrs)

    def get_hash(self):
        return []

    def to_html(self, event):
        return ''

    def to_string(self, event):
        return ''

    def get_title(self):
        return self.__class__.__name__

    def get_search_context(self, event):
        """
        Returns a dictionary describing the data that should be indexed
        by the search engine. Several fields are accepted:

        - text: a list of text items to index as part of the generic query
        - filters: a map of fields which are used for precise matching
        """
        return {
            # 'text': ['...'],
            # 'filters': {
            #     'field": ['...'],
            # },
        }

class Message(Interface):
    def __init__(self, message, params=()):
        self.message = message
        self.params = params

    def serialize(self):
        return {
            'message': self.message,
            'params': self.params,
        }

    def get_hash(self):
        return [self.message]

    def get_search_context(self, event):
        if isinstance(self.params, (list, tuple)):
            params = list(self.params)
        elif isinstance(self.params, dict):
            params = self.params.values()
        else:
            params = ()
        return {
            'text': [self.message] + params,
        }

class Query(Interface):
    def __init__(self, query, engine=None):
        self.query = query
        self.engine = engine

    def get_hash(self):
        return [self.query]

    def serialize(self):
        return {
            'query': self.query,
            'engine': self.engine,
        }

    def get_search_context(self, event):
        return {
            'text': [self.query],
        }

class Stacktrace(Interface):
    score = 1000

    def __init__(self, frames):
        self.frames = frames
        for frame in frames:
            # ensure we've got the correct required values
            assert 'filename' in frame
            assert 'lineno' in frame
            # assert 'context_line' in frame
            # assert 'function' in frame

    def _shorten(self, value, depth=1):
        if depth > 5:
            return type(value)
        if isinstance(value, dict):
            return dict((k, self._shorten(v, depth + 1)) for k, v in sorted(value.iteritems())[:100 / depth])
        elif isinstance(value, (list, tuple, set, frozenset)):
            return tuple(self._shorten(v, depth + 1) for v in value)[:100 / depth]
        elif isinstance(value, (int, long, float)):
            return value
        elif not value:
            return value
        return value[:100]

    def serialize(self):
        return {
            'frames': self.frames,
        }

    def get_hash(self):
        output = []
        for frame in self.frames:
            if 'module' in frame:
                output.append(frame['module'])
            else:
                output.append(frame['filename'])

            if 'function' in frame:
                output.append(frame['function'])
            else:
                output.append(frame['lineno'])
        return output

    def to_html(self, event):
        frames = []
        for frame in self.frames:
            if 'context_line' in frame:
                context = get_context(frame['lineno'], frame['context_line'], frame.get('pre_context'), frame.get('post_context'))
                start_lineno = context[0][0]
            else:
                context = []
                start_lineno = None

            context_vars = []
            if 'vars' in frame:
                context_vars = self._shorten(frame['vars'])
            else:
                context_vars = []

            frames.append({
                'abs_path': frame.get('abs_path'),
                'filename': frame['filename'],
                'function': frame.get('function'),
                'start_lineno': start_lineno,
                'lineno': frame.get('lineno'),
                'context': context,
                'vars': context_vars,
            })

        return render_to_string('sentry/partial/interfaces/stacktrace.html', {
            'event': event,
            'frames': frames,
            'stacktrace': self.get_traceback(event),
        })

    def to_string(self, event):
        return self.get_stacktrace(event)

    def get_stacktrace(self, event):
        result = [
            'Stacktrace (most recent call last):', '',
        ]
        for frame in self.frames:
            if 'function' in frame:
                result.append('  File "%(filename)s", line %(lineno)s, in %(function)s' % frame)
            else:
                result.append('  File "%(filename)s", line %(lineno)s' % frame)
            if 'context_line' in frame:
                result.append('    %s' % frame['context_line'].strip())
            result.append('')

        return '\n'.join(result)

    def get_traceback(self, event):
        result = [
            event.message, '',
            self.get_stacktrace(event),
        ]

        return '\n'.join(result)

    def get_search_context(self, event):
        return {
            'text': list(itertools.chain(*[[f.get('filename'), f.get('function'), f.get('context_line')] for f in self.frames])),
        }

class Exception(Interface):
    def __init__(self, value, type=None, module=None):
        # A human readable value for the exception
        self.value = value
        # The exception type name (e.g. TypeError)
        self.type = type
        # Optional module of the exception type (e.g. __builtin__)
        self.module = module

    def serialize(self):
        return {
            'type': self.type,
            'value': self.value,
            'module': self.module,
        }

    def get_hash(self):
        output = filter(bool, [self.module, self.type])
        if not output:
            output = [self.value]
        return output

    def to_html(self, event):
        return render_to_string('sentry/partial/interfaces/exception.html', {
            'event': event,
            'exception_value': self.value,
            'exception_type': self.type,
            'exception_module': self.module,
        })

    def get_search_context(self, event):
        return {
            'text': [self.value, self.type, self.module]
        }

class Http(Interface):
    score = 100

    def __init__(self, url, method=None, data=None, query_string=None, cookies=None, headers=None, env=None, **kwargs):
        if data is None:
            data = {}

        if method:
            method = method.upper()

        urlparts = urlparse.urlsplit(url)

        if not query_string:
            # define querystring from url
            query_string = urlparts.query

        elif query_string.startswith('?'):
            # remove '?' prefix
            query_string = query_string[1:]

        self.url = '%s://%s%s' % (urlparts.scheme, urlparts.netloc, urlparts.path)
        self.method = method
        self.data = data
        self.query_string = query_string
        if cookies:
            self.cookies = cookies
        else:
            self.cookies = {}
        # if cookies were [also] included in headers we
        # strip them out
        if headers and 'Cookie' in headers:
            cookies = headers.pop('Cookie')
            if not self.cookies:
                cookies = self.cookies
        self.headers = headers or {}
        self.env = env or {}

    def serialize(self):
        return {
            'url': self.url,
            'method': self.method,
            'data': self.data,
            'query_string': self.query_string,
            'cookies': self.cookies,
            'headers': self.headers,
            'env': self.env,
        }

    def to_string(self, event):
        return render_to_string('sentry/partial/interfaces/http.txt', {
            'event': event,
            'full_url': '?'.join(filter(bool, [self.url, self.query_string])),
            'url': self.url,
            'method': self.method,
            'query_string': self.query_string,
        })

    def to_html(self, event):
        data = self.data
        data_is_dict = False
        if self.headers.get('Content-Type') == 'application/x-www-form-urlencoded':
            try:
                data = QueryDict(data)
            except:
                pass
            else:
                data_is_dict = True

        # It's kind of silly we store this twice
        cookies = self.cookies or self.headers.pop('Cookie', {})
        cookies_is_dict = isinstance(cookies, dict)
        if not cookies_is_dict:
            try:
                cookies = QueryDict(cookies)
            except:
                pass
            else:
                cookies_is_dict = True

        return render_to_string('sentry/partial/interfaces/http.html', {
            'event': event,
            'full_url': '?'.join(filter(bool, [self.url, self.query_string])),
            'url': self.url,
            'method': self.method,
            'data': data,
            'data_is_dict': data_is_dict,
            'query_string': self.query_string,
            'cookies': cookies,
            'cookies_is_dict': cookies_is_dict,
            'headers': self.headers,
            'env': self.env,
        })

    def get_search_context(self, event):
        return {
            'filters': {
                'url': [self.url],
            }
        }

class Template(Interface):
    score = 1001

    def __init__(self, filename, context_line, lineno, pre_context=None, post_context=None,
                 abs_path=None):
        self.abs_path = abs_path
        self.filename = filename
        self.context_line = context_line
        self.lineno = lineno
        self.pre_context = pre_context
        self.post_context = post_context

    def serialize(self):
        return {
            'abs_path': self.abs_path,
            'filename': self.filename,
            'context_line': self.context_line,
            'lineno': self.lineno,
            'pre_context': self.pre_context,
            'post_context': self.post_context,
        }

    def get_hash(self):
        return [self.filename, self.context_line]

    def to_string(self, event):
        result = [
            'Stacktrace (most recent call last):', '',
            self.get_traceback()
        ]

        return '\n'.join(result)

    def to_html(self, event):
        context = get_context(self.lineno, self.context_line, self.pre_context, self.post_context)

        return render_to_string('sentry/partial/interfaces/template.html', {
            'event': event,
            'abs_path': self.abs_path,
            'filename': self.filename,
            'lineno': self.lineno,
            'start_lineno': context[0][0],
            'context': context,
            'template': self.get_traceback(event, context),
        })

    def get_traceback(self, event, context):
        result = [
            event.message, '',
            'File "%s", line %s' % (self.filename, self.lineno), '',
        ]
        result.extend([n[1].strip('\n') for n in context])

        return '\n'.join(result)

    def get_search_context(self, event):
        return {
            'text': [self.abs_path, self.filename, self.context_line],
        }

class User(Interface):
    def __init__(self, is_authenticated, **kwargs):
        self.is_authenticated = is_authenticated
        self.id = kwargs.get('id')
        self.username = kwargs.get('username')
        self.email = kwargs.get('email')

    def serialize(self):
        if self.is_authenticated:
            return {
                'is_authenticated': self.is_authenticated,
                'id': self.id,
                'username': self.username,
                'email': self.email,
            }
        else:
            return {
                'is_authenticated': self.is_authenticated
            }

    def get_hash(self):
        if not self.is_authenticated:
            return []
        else:
            return [self.id, self.username, self.email]

    def to_html(self, event):
        return render_to_string('sentry/partial/interfaces/user.html', {
            'event': event,
            'user_authenticated': self.is_authenticated,
            'user_id': self.id,
            'user_username': self.username,
            'user_email': self.email
        })

    def get_search_context(self, event):
        return {
            'text': [self.id, self.username, self.email]
        }
<commit_msg>Readded METHODS because it's used by the admin UI.  Extended with PATCH<commit_after>"""
sentry.interfaces
~~~~~~~~~~~~~~~~~

Interfaces provide an abstraction for how structured data should be
validated and rendered.

:copyright: (c) 2010-2012 by the Sentry Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""

import itertools
import urlparse

from django.http import QueryDict

from sentry.web.helpers import render_to_string

# unserialization concept is based on pickle
class _EmptyClass(object):
    pass

def unserialize(klass, data):
    value = _EmptyClass()
    value.__class__ = klass
    value.__setstate__(data)
    return value

def get_context(lineno, context_line, pre_context=None, post_context=None):
    lineno = int(lineno)
    context = []
    start_lineno = lineno - len(pre_context or [])
    if pre_context:
        start_lineno = lineno - len(pre_context)
        at_lineno = start_lineno
        for line in pre_context:
            context.append((at_lineno, line))
            at_lineno += 1
    else:
        start_lineno = lineno
        at_lineno = lineno

    context.append((at_lineno, context_line))
    at_lineno += 1

    if post_context:
        for line in post_context:
            context.append((at_lineno, line))
            at_lineno += 1

    return context

class Interface(object):
    """
    An interface is a structured represntation of data, which may
    render differently than the default ``extra`` metadata in an event.
    """

    score = 0

    def __init__(self, **kwargs):
        self.attrs = kwargs.keys()
        self.__dict__.update(kwargs)

    def __setstate__(self, data):
        kwargs = self.unserialize(data)
        self.attrs = kwargs.keys()
        self.__dict__.update(kwargs)

    def __getstate__(self):
        return self.serialize()

    def unserialize(self, data):
        return data

    def serialize(self):
        return dict((k, self.__dict__[k]) for k in self.attrs)

    def get_hash(self):
        return []

    def to_html(self, event):
        return ''

    def to_string(self, event):
        return ''

    def get_title(self):
        return self.__class__.__name__

    def get_search_context(self, event):
        """
        Returns a dictionary describing the data that should be indexed
        by the search engine. Several fields are accepted:

        - text: a list of text items to index as part of the generic query
        - filters: a map of fields which are used for precise matching
        """
        return {
            # 'text': ['...'],
            # 'filters': {
            #     'field": ['...'],
            # },
        }

class Message(Interface):
    def __init__(self, message, params=()):
        self.message = message
        self.params = params

    def serialize(self):
        return {
            'message': self.message,
            'params': self.params,
        }

    def get_hash(self):
        return [self.message]

    def get_search_context(self, event):
        if isinstance(self.params, (list, tuple)):
            params = list(self.params)
        elif isinstance(self.params, dict):
            params = self.params.values()
        else:
            params = ()
        return {
            'text': [self.message] + params,
        }

class Query(Interface):
    def __init__(self, query, engine=None):
        self.query = query
        self.engine = engine

    def get_hash(self):
        return [self.query]

    def serialize(self):
        return {
            'query': self.query,
            'engine': self.engine,
        }

    def get_search_context(self, event):
        return {
            'text': [self.query],
        }

class Stacktrace(Interface):
    score = 1000

    def __init__(self, frames):
        self.frames = frames
        for frame in frames:
            # ensure we've got the correct required values
            assert 'filename' in frame
            assert 'lineno' in frame
            # assert 'context_line' in frame
            # assert 'function' in frame

    def _shorten(self, value, depth=1):
        if depth > 5:
            return type(value)
        if isinstance(value, dict):
            return dict((k, self._shorten(v, depth + 1)) for k, v in sorted(value.iteritems())[:100 / depth])
        elif isinstance(value, (list, tuple, set, frozenset)):
            return tuple(self._shorten(v, depth + 1) for v in value)[:100 / depth]
        elif isinstance(value, (int, long, float)):
            return value
        elif not value:
            return value
        return value[:100]

    def serialize(self):
        return {
            'frames': self.frames,
        }

    def get_hash(self):
        output = []
        for frame in self.frames:
            if 'module' in frame:
                output.append(frame['module'])
            else:
                output.append(frame['filename'])

            if 'function' in frame:
                output.append(frame['function'])
            else:
                output.append(frame['lineno'])
        return output

    def to_html(self, event):
        frames = []
        for frame in self.frames:
            if 'context_line' in frame:
                context = get_context(frame['lineno'], frame['context_line'], frame.get('pre_context'), frame.get('post_context'))
                start_lineno = context[0][0]
            else:
                context = []
                start_lineno = None

            context_vars = []
            if 'vars' in frame:
                context_vars = self._shorten(frame['vars'])
            else:
                context_vars = []

            frames.append({
                'abs_path': frame.get('abs_path'),
                'filename': frame['filename'],
                'function': frame.get('function'),
                'start_lineno': start_lineno,
                'lineno': frame.get('lineno'),
                'context': context,
                'vars': context_vars,
            })

        return render_to_string('sentry/partial/interfaces/stacktrace.html', {
            'event': event,
            'frames': frames,
            'stacktrace': self.get_traceback(event),
        })

    def to_string(self, event):
        return self.get_stacktrace(event)

    def get_stacktrace(self, event):
        result = [
            'Stacktrace (most recent call last):', '',
        ]
        for frame in self.frames:
            if 'function' in frame:
                result.append('  File "%(filename)s", line %(lineno)s, in %(function)s' % frame)
            else:
                result.append('  File "%(filename)s", line %(lineno)s' % frame)
            if 'context_line' in frame:
                result.append('    %s' % frame['context_line'].strip())
            result.append('')

        return '\n'.join(result)

    def get_traceback(self, event):
        result = [
            event.message, '',
            self.get_stacktrace(event),
        ]

        return '\n'.join(result)

    def get_search_context(self, event):
        return {
            'text': list(itertools.chain(*[[f.get('filename'), f.get('function'), f.get('context_line')] for f in self.frames])),
        }

class Exception(Interface):
    def __init__(self, value, type=None, module=None):
        # A human readable value for the exception
        self.value = value
        # The exception type name (e.g. TypeError)
        self.type = type
        # Optional module of the exception type (e.g. __builtin__)
        self.module = module

    def serialize(self):
        return {
            'type': self.type,
            'value': self.value,
            'module': self.module,
        }

    def get_hash(self):
        output = filter(bool, [self.module, self.type])
        if not output:
            output = [self.value]
        return output

    def to_html(self, event):
        return render_to_string('sentry/partial/interfaces/exception.html', {
            'event': event,
            'exception_value': self.value,
            'exception_type': self.type,
            'exception_module': self.module,
        })

    def get_search_context(self, event):
        return {
            'text': [self.value, self.type, self.module]
        }

class Http(Interface):
    score = 100

    # methods as defined by http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html + PATCH
    METHODS = ('GET', 'POST', 'PUT', 'OPTIONS', 'HEAD', 'DELETE', 'TRACE', 'CONNECT', 'PATCH')

    def __init__(self, url, method=None, data=None, query_string=None, cookies=None, headers=None, env=None, **kwargs):
        if data is None:
            data = {}

        if method:
            method = method.upper()

        urlparts = urlparse.urlsplit(url)

        if not query_string:
            # define querystring from url
            query_string = urlparts.query

        elif query_string.startswith('?'):
            # remove '?' prefix
            query_string = query_string[1:]

        self.url = '%s://%s%s' % (urlparts.scheme, urlparts.netloc, urlparts.path)
        self.method = method
        self.data = data
        self.query_string = query_string
        if cookies:
            self.cookies = cookies
        else:
            self.cookies = {}
        # if cookies were [also] included in headers we
        # strip them out
        if headers and 'Cookie' in headers:
            cookies = headers.pop('Cookie')
            if not self.cookies:
                cookies = self.cookies
        self.headers = headers or {}
        self.env = env or {}

    def serialize(self):
        return {
            'url': self.url,
            'method': self.method,
            'data': self.data,
            'query_string': self.query_string,
            'cookies': self.cookies,
            'headers': self.headers,
            'env': self.env,
        }

    def to_string(self, event):
        return render_to_string('sentry/partial/interfaces/http.txt', {
            'event': event,
            'full_url': '?'.join(filter(bool, [self.url, self.query_string])),
            'url': self.url,
            'method': self.method,
            'query_string': self.query_string,
        })

    def to_html(self, event):
        data = self.data
        data_is_dict = False
        if self.headers.get('Content-Type') == 'application/x-www-form-urlencoded':
            try:
                data = QueryDict(data)
            except:
                pass
            else:
                data_is_dict = True

        # It's kind of silly we store this twice
        cookies = self.cookies or self.headers.pop('Cookie', {})
        cookies_is_dict = isinstance(cookies, dict)
        if not cookies_is_dict:
            try:
                cookies = QueryDict(cookies)
            except:
                pass
            else:
                cookies_is_dict = True

        return render_to_string('sentry/partial/interfaces/http.html', {
            'event': event,
            'full_url': '?'.join(filter(bool, [self.url, self.query_string])),
            'url': self.url,
            'method': self.method,
            'data': data,
            'data_is_dict': data_is_dict,
            'query_string': self.query_string,
            'cookies': cookies,
            'cookies_is_dict': cookies_is_dict,
            'headers': self.headers,
            'env': self.env,
        })

    def get_search_context(self, event):
        return {
            'filters': {
                'url': [self.url],
            }
        }

class Template(Interface):
    score = 1001

    def __init__(self, filename, context_line, lineno, pre_context=None, post_context=None,
                 abs_path=None):
        self.abs_path = abs_path
        self.filename = filename
        self.context_line = context_line
        self.lineno = lineno
        self.pre_context = pre_context
        self.post_context = post_context

    def serialize(self):
        return {
            'abs_path': self.abs_path,
            'filename': self.filename,
            'context_line': self.context_line,
            'lineno': self.lineno,
            'pre_context': self.pre_context,
            'post_context': self.post_context,
        }

    def get_hash(self):
        return [self.filename, self.context_line]

    def to_string(self, event):
        result = [
            'Stacktrace (most recent call last):', '',
            self.get_traceback()
        ]

        return '\n'.join(result)

    def to_html(self, event):
        context = get_context(self.lineno, self.context_line, self.pre_context, self.post_context)

        return render_to_string('sentry/partial/interfaces/template.html', {
            'event': event,
            'abs_path': self.abs_path,
            'filename': self.filename,
            'lineno': self.lineno,
            'start_lineno': context[0][0],
            'context': context,
            'template': self.get_traceback(event, context),
        })

    def get_traceback(self, event, context):
        result = [
            event.message, '',
            'File "%s", line %s' % (self.filename, self.lineno), '',
        ]
        result.extend([n[1].strip('\n') for n in context])

        return '\n'.join(result)

    def get_search_context(self, event):
        return {
            'text': [self.abs_path, self.filename, self.context_line],
        }

class User(Interface):
    def __init__(self, is_authenticated, **kwargs):
        self.is_authenticated = is_authenticated
        self.id = kwargs.get('id')
        self.username = kwargs.get('username')
        self.email = kwargs.get('email')

    def serialize(self):
        if self.is_authenticated:
            return {
                'is_authenticated': self.is_authenticated,
                'id': self.id,
                'username': self.username,
                'email': self.email,
            }
        else:
            return {
                'is_authenticated': self.is_authenticated
            }

    def get_hash(self):
        if not self.is_authenticated:
            return []
        else:
            return [self.id, self.username, self.email]

    def to_html(self, event):
        return render_to_string('sentry/partial/interfaces/user.html', {
            'event': event,
            'user_authenticated': self.is_authenticated,
            'user_id': self.id,
            'user_username': self.username,
            'user_email': self.email
        })

    def get_search_context(self, event):
        return {
            'text': [self.id, self.username, self.email]
        }
<|endoftext|>


<commit_before>#!/usr/bin/env python3

import http
import database
import os

database = database.Database()

err = ""
redirect = ""
sql = ""
second = 0
sndkey = ""
show_table = ""
select = ""
addLink = ""
tableList = []
nameList = []

redirect = database.check_cookie(os.environ)

sql = "SHOW tables"
database.cur.execute(sql)

if database.cur.rowcount > 0:
    query = database.cur.fetchall()
    for row in query:
        tableList += row

    for name in tableList:
        selected = ""
        if 'table' in http.post:
            if http.post['table'].value == name:
                selected = """ selected="selected" """
        select += "<option %s>%s</option>" % (selected, name)

if os.environ['REQUEST_METHOD'] == 'POST':

    if 'logout' in http.post:
        print("Set-Cookie: session_id=0; Max-Age=0")
        redirect = """<meta http-equiv="refresh" content="0; url=login.py">"""
    if 'table' in http.post:
        #counts primary keys in a table
        sql = "SHOW index FROM %s WHERE key_name = 'PRIMARY'" % http.post['table'].value
        database.cur.execute(sql)
        if database.cur.rowcount > 1:
           query = database.cur.fetchall()
           row = query[1]
           second = 1
        #retrieves all columns names from a table
        sql = "SHOW columns FROM %s" % http.post['table'].value
        database.cur.execute(sql)

        if database.cur.rowcount > 0:
            query = database.cur.fetchall()
            #stores all column names
            for row in query:
                if row[0].find('pass_hash') >= 0:
                    pass
                elif row[0].find('salt') >= 0:
                    pass
                else:
                    string = [row[0]]
                    nameList += string

            show_table += "

"

            sql = "SELECT"
            for name in nameList:
                #adds column name to sql statment
                sql += " %s," % name
                #adds column name to table header
                show_table += "%s	" % name

            show_table += "Options	"
            show_table += "
"

            #removes last comma
            sql = sql[:-1]
            sql += " FROM %s" % http.post['table'].value

            database.cur.execute(sql)

            #generates table rows and columns
            if database.cur.rowcount > 0:
                query = database.cur.fetchall()
                for row in query:
                    show_table += "
"
                    for column in row:
                        show_table += "%s	" % column

                    if second == 1:
                        sndkey = row[1]

                        M
                    show_table += """<form method="POST" action="edit.py" id="edit"><button form="edit" name="edit" type="submit" value="%s">Edit</button><input type="hidden" name="key" value="%s"><input type="hidden" name="table" value="%s"></form>""" % (row[0], sndkey, http.post['table'].value)
                    if http.post['table'].value == 'member':
                        show_table += """<form method="POST" action="view.py" id="view"><button form="view" name="member_id" type="submit" value="%s">View</button></form>""" % row[0]
                    show_table += "
"

            show_table += "
"

database.close()

print("""Content-Type: text/html\n\n

    <meta name="viewport" content="width=device-width, initial-scale=1">
    %s
    <title>Home</title>

                <form class="logout"action="home.py" method="post">
                    <input type="submit" name="logout" value="logout">

                </form>

Hello There

Search

                <form class="search" action="home.py" method="post">
                    <select name="table">%s</select>
                    <input type="submit" name="search" value="search">
                </form>
                %s

                %s
                """ % (redirect, select, addLink, show_table))
<commit_msg>Fixed typos<commit_after>#!/usr/bin/env python3

import http
import database
import os

database = database.Database()

err = ""
redirect = ""
sql = ""
second = 0
sndkey = ""
show_table = ""
select = ""
addLink = ""
tableList = []
nameList = []

redirect = database.check_cookie(os.environ)

sql = "SHOW tables"
database.cur.execute(sql)

if database.cur.rowcount > 0:
    query = database.cur.fetchall()
    for row in query:
        tableList += row

    for name in tableList:
        selected = ""
        if 'table' in http.post:
            if http.post['table'].value == name:
                selected = """ selected="selected" """
        select += "<option %s>%s</option>" % (selected, name)

if os.environ['REQUEST_METHOD'] == 'POST':

    if 'logout' in http.post:
        print("Set-Cookie: session_id=0; Max-Age=0")
        redirect = """<meta http-equiv="refresh" content="0; url=login.py">"""
    if 'table' in http.post:
        #counts primary keys in a table
        sql = "SHOW index FROM %s WHERE key_name = 'PRIMARY'" % http.post['table'].value
        database.cur.execute(sql)
        if database.cur.rowcount > 1:
           query = database.cur.fetchall()
           row = query[1]
           second = 1
        #retrieves all columns names from a table
        sql = "SHOW columns FROM %s" % http.post['table'].value
        database.cur.execute(sql)

        if database.cur.rowcount > 0:
            query = database.cur.fetchall()
            #stores all column names
            for row in query:
                if row[0].find('pass_hash') >= 0:
                    pass
                elif row[0].find('salt') >= 0:
                    pass
                else:
                    string = [row[0]]
                    nameList += string

            show_table += "

"

            sql = "SELECT"
            for name in nameList:
                #adds column name to sql statment
                sql += " %s," % name
                #adds column name to table header
                show_table += "%s	" % name

            show_table += "Options	"
            show_table += "
"

            #removes last comma
            sql = sql[:-1]
            sql += " FROM %s" % http.post['table'].value

            database.cur.execute(sql)

            #generates table rows and columns
            if database.cur.rowcount > 0:
                query = database.cur.fetchall()
                for row in query:
                    show_table += "
"
                    for column in row:
                        show_table += "%s	" % column

                    if second == 1:
                        sndkey = row[1]

                    show_table += """<form method="POST" action="edit.py" id="edit"><button form="edit" name="edit" type="submit" value="%s">Edit</button><input type="hidden" name="key" value="%s"><input type="hidden" name="table" value="%s"></form>""" % (row[0], sndkey, http.post['table'].value)
                    if http.post['table'].value == 'member':
                        show_table += """<form method="POST" action="view.py" id="view"><button form="view" name="member_id" type="submit" value="%s">View</button></form>""" % row[0]
                    show_table += "
"

            show_table += "
"

database.close()

print("""Content-Type: text/html\n\n

    <meta name="viewport" content="width=device-width, initial-scale=1">
    %s
    <title>Home</title>

                <form class="logout"action="home.py" method="post">
                    <input type="submit" name="logout" value="logout">

                </form>

Hello There

Search

                <form class="search" action="home.py" method="post">
                    <select name="table">%s</select>
                    <input type="submit" name="search" value="search">
                </form>
                %s

                %s
                """ % (redirect, select, addLink, show_table))
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
try:
    from setuptools import setup, find_packages
except ImportError:
    from ez_setup import use_setuptools
    use_setuptools()
    from setuptools import setup, find_packages

from allura.version import __version__

setup(
    name='Allura',
    version=__version__,
    description='',
    author='SourceForge Team',
    author_email='allura@geek.net',
    url='http://sourceforge.net/p/',
    install_requires=[
        "TurboGears2 >= 2.1a1",
        "PasteScript",
        "Babel >= 0.9.4",
        "pymongo >= 1.7",
        "pysolr",
        "repoze.what-quickstart",
        "sqlalchemy-migrate",
        "Markdown >= 2.0.3",
        "Pygments >= 1.1.1",
        "PyYAML >= 3.09",
        "python-openid >= 2.2.4",
        "python-ldap == 2.3.9",
        "python-dateutil >= 1.4.1",
        "WebOb == 0.9.8",
        "WebTest == 1.2",
        "EasyWidgets >= 0.1.1",
        "PIL >= 1.1.7",
        "iso8601",
        "chardet == 1.0.1",
        "feedparser >= 5.0.1",
        "oauth2 == 1.2.0",
        ],
    setup_requires=["PasteScript >= 1.7"],
    paster_plugins=['PasteScript', 'Pylons', 'TurboGears2', 'tg.devtools', 'Ming'],
    packages=find_packages(exclude=['ez_setup']),
    include_package_data=True,
    test_suite='nose.collector',
    tests_require=['WebTest >= 1.2', 'BeautifulSoup', 'pytidylib', 'poster'],
    package_data={'allura': ['i18n/*/LC_MESSAGES/*.mo',
                                 'templates/*/*',
                                 'public/*/*']},
    message_extractors={'allura': [
            ('**.py', 'python', None),
            ('templates/**.mako', 'mako', None),
            ('templates/**.html', 'genshi', None),
            ('public/**', 'ignore', None)]},

    entry_points="""
    [paste.app_factory]
    main = allura.config.middleware:make_app
    task = allura.config.middleware:make_task_app
    tool_test = allura.config.middleware:make_tool_test_app

    [paste.app_install]
    main = pylons.util:PylonsInstaller
    tool_test = pylons.util:PylonsInstaller

    [allura]
    profile = allura.ext.user_profile:UserProfileApp
    admin = allura.ext.admin:AdminApp
    search = allura.ext.search:SearchApp
    home = allura.ext.project_home:ProjectHomeApp

    [allura.auth]
    local = allura.lib.plugin:LocalAuthenticationProvider
    ldap = allura.lib.plugin:LdapAuthenticationProvider

    [allura.user_prefs]
    local = allura.lib.plugin:LocalUserPreferencesProvider

    [allura.project_registration]
    local = allura.lib.plugin:LocalProjectRegistrationProvider

    [allura.theme]
    allura = allura.lib.plugin:ThemeProvider

    [flyway.migrations]
    pyforge = allura.migrations

    [paste.paster_command]
    taskd = allura.command.taskd:TaskdCommand
    task = allura.command.taskd:TaskCommand
    models = allura.command:ShowModelsCommand
    reindex = allura.command:ReindexCommand
    ensure_index = allura.command:EnsureIndexCommand
    script = allura.command:ScriptCommand
    set-tool-access = allura.command:SetToolAccessCommand
    smtp_server=allura.command:SMTPServerCommand
    create-neighborhood = allura.command:CreateNeighborhoodCommand

    [easy_widgets.resources]
    ew_resources=allura.config.resources:register_ew_resources

    [easy_widgets.engines]
    jinja = allura.config.app_cfg:JinjaEngine

    """,
)

<commit_msg>[#1740] Updated project metadata in Allura/setup.py.<commit_after># -*- coding: utf-8 -*-
try:
    from setuptools import setup, find_packages
except ImportError:
    from ez_setup import use_setuptools
    use_setuptools()
    from setuptools import setup, find_packages

from allura.version import __version__

PROJECT_DESCRIPTION='''
Allura is an open source implementation of a software "forge", a web site
that manages source code repositories, bug reports, discussions, mailing
lists, wiki pages, blogs and more for any number of individual projects.
'''
setup(
    name='Allura',
    version=__version__,
    description='Base distribution of the Allura development platform',
    long_description=PROJECT_DESCRIPTION,
    author='SourceForge Team',
    author_email='develop@discussion.allura.p.re.sf.net',
    url='http://sourceforge.net/p/allura',
    keywords='sourceforge allura turbogears pylons jinja2 mongodb rabbitmq',
    license='Apache License, http://www.apache.org/licenses/LICENSE-2.0',
    platforms=[
        'Linux',
        'MacOS X',
        ],
    classifiers=[
        'Development Status :: 4 - Beta',
        'Environment :: Web Environment',
        'Framework :: Pylons',
        'Framework :: TurboGears',
        'Intended Audience :: Developers',
        'Programming Language :: Python :: 2.6',
        'Topic :: Internet :: WWW/HTTP :: WSGI :: Application',
        'License :: OSI Approved :: Apache Software License',
        ],
    install_requires=[
        "TurboGears2 >= 2.1a1",
        "PasteScript",
        "Babel >= 0.9.4",
        "pymongo >= 1.7",
        "pysolr",
        "repoze.what-quickstart",
        "sqlalchemy-migrate",
        "Markdown >= 2.0.3",
        "Pygments >= 1.1.1",
        "PyYAML >= 3.09",
        "python-openid >= 2.2.4",
        "python-ldap == 2.3.9",
        "python-dateutil >= 1.4.1",
        "WebOb == 0.9.8",
        "WebTest == 1.2",
        "EasyWidgets >= 0.1.1",
        "PIL >= 1.1.7",
        "iso8601",
        "chardet == 1.0.1",
        "feedparser >= 5.0.1",
        "oauth2 == 1.2.0",
        ],
    setup_requires=["PasteScript >= 1.7"],
    paster_plugins=['PasteScript', 'Pylons', 'TurboGears2', 'tg.devtools', 'Ming'],
    packages=find_packages(exclude=['ez_setup']),
    include_package_data=True,
    test_suite='nose.collector',
    tests_require=['WebTest >= 1.2', 'BeautifulSoup', 'pytidylib', 'poster'],
    package_data={'allura': ['i18n/*/LC_MESSAGES/*.mo',
                                 'templates/*/*',
                                 'public/*/*']},
    message_extractors={'allura': [
            ('**.py', 'python', None),
            ('templates/**.mako', 'mako', None),
            ('templates/**.html', 'genshi', None),
            ('public/**', 'ignore', None)]},

    entry_points="""
    [paste.app_factory]
    main = allura.config.middleware:make_app
    task = allura.config.middleware:make_task_app
    tool_test = allura.config.middleware:make_tool_test_app

    [paste.app_install]
    main = pylons.util:PylonsInstaller
    tool_test = pylons.util:PylonsInstaller

    [allura]
    profile = allura.ext.user_profile:UserProfileApp
    admin = allura.ext.admin:AdminApp
    search = allura.ext.search:SearchApp
    home = allura.ext.project_home:ProjectHomeApp

    [allura.auth]
    local = allura.lib.plugin:LocalAuthenticationProvider
    ldap = allura.lib.plugin:LdapAuthenticationProvider

    [allura.user_prefs]
    local = allura.lib.plugin:LocalUserPreferencesProvider

    [allura.project_registration]
    local = allura.lib.plugin:LocalProjectRegistrationProvider

    [allura.theme]
    allura = allura.lib.plugin:ThemeProvider

    [flyway.migrations]
    pyforge = allura.migrations

    [paste.paster_command]
    taskd = allura.command.taskd:TaskdCommand
    task = allura.command.taskd:TaskCommand
    models = allura.command:ShowModelsCommand
    reindex = allura.command:ReindexCommand
    ensure_index = allura.command:EnsureIndexCommand
    script = allura.command:ScriptCommand
    set-tool-access = allura.command:SetToolAccessCommand
    smtp_server=allura.command:SMTPServerCommand
    create-neighborhood = allura.command:CreateNeighborhoodCommand

    [easy_widgets.resources]
    ew_resources=allura.config.resources:register_ew_resources

    [easy_widgets.engines]
    jinja = allura.config.app_cfg:JinjaEngine

    """,
)

<|endoftext|>


<commit_before>from flask import Blueprint, jsonify, Response, render_template, url_for, request
from itertools import groupby
from operator import itemgetter
from os.path import join, dirname
import json, os

import requests
import uritemplate
import psycopg2

from . import util

blueprint = Blueprint('Metro-Extracts', __name__)

def load_cities(filename):
    '''
    '''
    with open(filename) as file:
        cities = json.load(file)

    return cities

cities = load_cities(join(dirname(__file__), '..', 'cities.json'))

def apply_blueprint(app, url_prefix):
    '''
    '''
    app.register_blueprint(blueprint, url_prefix=url_prefix)

@blueprint.route('/')
@util.errors_logged
def index():
    ordered_cities = sorted(cities, key=itemgetter('country'))
    metros_tree = list()

    for (country, sub_cities) in groupby(ordered_cities, itemgetter('country')):
        sub_metros = list()

        for sub_city in sorted(sub_cities, key=itemgetter('name')):
            sub_city['href'] = url_for('Metro-Extracts.get_metro', metro_id=sub_city['id'])
            sub_metros.append(sub_city)

        metros_tree.append({'country': country, 'metros': sub_metros})

    return render_template('index.html', metros_tree=metros_tree, util=util)

@blueprint.route('/cities.geojson')
@util.errors_logged
def get_cities_geojson():
    features = list()

    for city in cities:
        x1, y1, x2, y2 = [float(city['bbox'][k])
                          for k in ('left', 'bottom', 'right', 'top')]

        feature = dict(type='Feature', id=city['id'])
        #feature['bbox'] = [x1, y1, x2, y2]
        feature['geometry'] = dict(type='Polygon')
        feature['geometry']['coordinates'] = [[[x1, y1], [x1, y2], [x2, y2], [x2, y1], [x1, y1]]]
        feature['properties'] = dict(name=city['id'], display_name=city['name'])
        feature['properties']['href'] = url_for('Metro-Extracts.get_metro', metro_id=city['id'])
        features.append(feature)

    return jsonify(dict(features=features))

@blueprint.route('/metro/<metro_id>/')
@blueprint.route('/metro/<metro_id>/<wof_id>/<wof_name>/')
@util.errors_logged
def get_metro(metro_id, wof_id=None, wof_name=None):
    with open('cities.json') as file:
        cities = json.load(file)
        metro = {c['id']: c for c in cities}[metro_id]

    return render_template('metro.html', metro=metro, wof_id=wof_id,
                           wof_name=wof_name, util=util)

@blueprint.route('/wof/<id>.geojson')
@util.errors_logged
def wof_geojson(id):
    ''' Proxy requests to http://whosonfirst.mapzen.com/spelunker/id/{id}.geojson
    '''
    template = 'http://whosonfirst.mapzen.com/spelunker/id/{id}.geojson'
    url = uritemplate.expand(template, dict(id=id))
    wof_resp = requests.get(url)

    headers = {key: val for (key, val) in wof_resp.headers.items()
               if key in ('Content-Type', 'Content-Length')}

    return Response(wof_resp.content, headers=headers)
<commit_msg>Displaying basic information about headers and things<commit_after>from flask import Blueprint, jsonify, Response, render_template, url_for, request
from itertools import groupby
from operator import itemgetter
from os.path import join, dirname
import json, os

import requests
import uritemplate
import psycopg2

from . import util

blueprint = Blueprint('Metro-Extracts', __name__)

def load_cities(filename):
    '''
    '''
    with open(filename) as file:
        cities = json.load(file)

    return cities

cities = load_cities(join(dirname(__file__), '..', 'cities.json'))

def apply_blueprint(app, url_prefix):
    '''
    '''
    app.register_blueprint(blueprint, url_prefix=url_prefix)

@blueprint.route('/')
@util.errors_logged
def index():
    ordered_cities = sorted(cities, key=itemgetter('country'))
    metros_tree = list()

    for (country, sub_cities) in groupby(ordered_cities, itemgetter('country')):
        sub_metros = list()

        for sub_city in sorted(sub_cities, key=itemgetter('name')):
            sub_city['href'] = url_for('Metro-Extracts.get_metro', metro_id=sub_city['id'])
            sub_metros.append(sub_city)

        metros_tree.append({'country': country, 'metros': sub_metros})

    return render_template('index.html', metros_tree=metros_tree, util=util)

@blueprint.route('/cities.geojson')
@util.errors_logged
def get_cities_geojson():
    features = list()

    for city in cities:
        x1, y1, x2, y2 = [float(city['bbox'][k])
                          for k in ('left', 'bottom', 'right', 'top')]

        feature = dict(type='Feature', id=city['id'])
        #feature['bbox'] = [x1, y1, x2, y2]
        feature['geometry'] = dict(type='Polygon')
        feature['geometry']['coordinates'] = [[[x1, y1], [x1, y2], [x2, y2], [x2, y1], [x1, y1]]]
        feature['properties'] = dict(name=city['id'], display_name=city['name'])
        feature['properties']['href'] = url_for('Metro-Extracts.get_metro', metro_id=city['id'])
        features.append(feature)

    return jsonify(dict(features=features))

@blueprint.route('/metro/<metro_id>/')
@blueprint.route('/metro/<metro_id>/<wof_id>/<wof_name>/')
@util.errors_logged
def get_metro(metro_id, wof_id=None, wof_name=None):
    with open('cities.json') as file:
        cities = json.load(file)
        metro = {c['id']: c for c in cities}[metro_id]

    return render_template('metro.html', metro=metro, wof_id=wof_id,
                           wof_name=wof_name, util=util)

@blueprint.route('/wof/<id>.geojson')
@util.errors_logged
def wof_geojson(id):
    ''' Proxy requests to http://whosonfirst.mapzen.com/spelunker/id/{id}.geojson
    '''
    template = 'http://whosonfirst.mapzen.com/spelunker/id/{id}.geojson'
    url = uritemplate.expand(template, dict(id=id))
    wof_resp = requests.get(url)

    headers = {key: val for (key, val) in wof_resp.headers.items()
               if key in ('Content-Type', 'Content-Length')}

    return Response(wof_resp.content, headers=headers)

@blueprint.route('/whereami')
@util.errors_logged
def whereami():
    return jsonify(
        host=request.headers.get('Host'),
        cf_fwd_proto=request.headers.get('CloudFront-Forwarded-Proto'),
        x_fwd_proto=request.headers.get('X-Forwarded-Proto')
        )
<|endoftext|>


<commit_before>from django.contrib.admin import site, ModelAdmin, StackedInline

from site_content.settings import ENABLE_BUILTIN_MEDIA
from site_content.models import SitePage, SiteMenu, SiteMenuItem, SiteBlock, SitePageAlias, SitePageRedirect, SitePosition

class SitePageAliasInline(StackedInline):
    model = SitePageAlias
    classes = ('collapse-open',)
    allow_add = True
    extra = 0

class SitePageRedirectInline(StackedInline):
    model = SitePageRedirect
    classes = ('collapse-open',)
    allow_add = True
    extra = 0

class SitePageAdmin(ModelAdmin):
    list_display = ('url', 'content_header', 'sitemenu', 'sitemenu_label', 'sitemenu_weight', 'template', 'site')
    list_filter = ('sitemenu', 'site')
    list_editable = ('sitemenu', 'sitemenu_label', 'sitemenu_weight','template')
    save_on_top = True
    inlines = (SitePageAliasInline,SitePageRedirectInline)
    ordering = ('sitemenu', 'sitemenu_weight')

    fieldsets = (
        (None, {'fields': ('site', 'is_index', 'url', 'title', 'content_header', 'content')}),
        ('Menu', {'fields': ('sitemenu', 'sitemenu_label', 'sitemenu_weight')}),
        ('Meta Tags', {'classes': ('collapse closed',), 'fields': ('meta_description', 'meta_keywords')}),
        ('Advanced', {'classes': ('collapse closed',), 'fields': ('page_class', 'template')})
    )

    if ENABLE_BUILTIN_MEDIA:
        class Media:
            js = ('/static/grappelli/tinymce/jscripts/tiny_mce/tiny_mce.js', '/static/js/lyrical_pageTinyMCEAdmin.js')

    def __unicode__(self):
        return '%s' % 'administration'

site.register(SitePage, SitePageAdmin)

class SiteMenuItemAdmin(ModelAdmin):
    list_display = ('label', 'weight', 'url', 'sitemenu')
    list_filter = ('sitemenu',)
    list_editable = ('weight',)
    ordering = ('weight',)

    def __unicode__(self):
        return '%s' % 'administration'

site.register(SiteMenuItem, SiteMenuItemAdmin)

class SiteMenuAdmin(ModelAdmin):
    list_display = ('label', 'code', 'weight')
    ordering = ('weight',)

    def __unicode__(self):
        return '%s' % 'administration'

site.register(SiteMenu, SiteMenuAdmin)

class SiteBlockAdmin(ModelAdmin):
    save_on_top = True
    list_display = ('code', 'css_class', 'siteposition')
    list_editable = ('css_class', 'siteposition')
    list_filter = ('siteposition',)
    class Media:
        js = ('/static/grappelli/tinymce/jscripts/tiny_mce/tiny_mce.js', '/static/js/lyrical_pageTinyMCEAdmin.js')

    def __unicode__(self):
        return '%s' % 'administration'

site.register(SiteBlock, SiteBlockAdmin)

class SitePageRedirect(ModelAdmin):
    list_display = ('sitepage', 'url')
    save_on_top = True

class SitePositionAdmin(ModelAdmin):
    save_on_top = True
    list_display = ('code', 'weight', 'css_class')
    list_editable = ('weight', 'css_class')

site.register(SitePosition, SitePositionAdmin)<commit_msg>display weight in edit list<commit_after>from django.contrib.admin import site, ModelAdmin, StackedInline

from site_content.settings import ENABLE_BUILTIN_MEDIA
from site_content.models import SitePage, SiteMenu, SiteMenuItem, SiteBlock, SitePageAlias, SitePageRedirect, SitePosition

class SitePageAliasInline(StackedInline):
    model = SitePageAlias
    classes = ('collapse-open',)
    allow_add = True
    extra = 0

class SitePageRedirectInline(StackedInline):
    model = SitePageRedirect
    classes = ('collapse-open',)
    allow_add = True
    extra = 0

class SitePageAdmin(ModelAdmin):
    list_display = ('url', 'content_header', 'sitemenu', 'sitemenu_label', 'sitemenu_weight', 'template', 'site')
    list_filter = ('sitemenu', 'site')
    list_editable = ('sitemenu', 'sitemenu_label', 'sitemenu_weight','template')
    save_on_top = True
    inlines = (SitePageAliasInline,SitePageRedirectInline)
    ordering = ('sitemenu', 'sitemenu_weight')

    fieldsets = (
        (None, {'fields': ('site', 'is_index', 'url', 'title', 'content_header', 'content')}),
        ('Menu', {'fields': ('sitemenu', 'sitemenu_label', 'sitemenu_weight')}),
        ('Meta Tags', {'classes': ('collapse closed',), 'fields': ('meta_description', 'meta_keywords')}),
        ('Advanced', {'classes': ('collapse closed',), 'fields': ('page_class', 'template')})
    )

    if ENABLE_BUILTIN_MEDIA:
        class Media:
            js = ('/static/grappelli/tinymce/jscripts/tiny_mce/tiny_mce.js', '/static/js/lyrical_pageTinyMCEAdmin.js')

    def __unicode__(self):
        return '%s' % 'administration'

site.register(SitePage, SitePageAdmin)

class SiteMenuItemAdmin(ModelAdmin):
    list_display = ('label', 'weight', 'url', 'sitemenu')
    list_filter = ('sitemenu',)
    list_editable = ('weight',)
    ordering = ('weight',)

    def __unicode__(self):
        return '%s' % 'administration'

site.register(SiteMenuItem, SiteMenuItemAdmin)

class SiteMenuAdmin(ModelAdmin):
    list_display = ('label', 'code', 'weight')
    ordering = ('weight',)

    def __unicode__(self):
        return '%s' % 'administration'

site.register(SiteMenu, SiteMenuAdmin)

class SiteBlockAdmin(ModelAdmin):
    save_on_top = True
    list_display = ('code', 'css_class', 'siteposition', 'weight',)
    list_editable = ('css_class', 'siteposition', 'weight',)
    list_filter = ('siteposition',)
    class Media:
        js = ('/static/grappelli/tinymce/jscripts/tiny_mce/tiny_mce.js', '/static/js/lyrical_pageTinyMCEAdmin.js')

    def __unicode__(self):
        return '%s' % 'administration'

site.register(SiteBlock, SiteBlockAdmin)

class SitePageRedirect(ModelAdmin):
    list_display = ('sitepage', 'url')
    save_on_top = True

class SitePositionAdmin(ModelAdmin):
    save_on_top = True
    list_display = ('code', 'weight', 'css_class')
    list_editable = ('weight', 'css_class')

site.register(SitePosition, SitePositionAdmin)<|endoftext|>


<commit_before>#!/usr/bin/env python
#
# zip up the titanium mobile SDKs into suitable distribution formats
#
import os, types, glob, shutil, sys, platform
import zipfile, datetime, subprocess

if platform.system() == 'Darwin':
	import importresolver

cur_dir = os.path.abspath(os.path.dirname(sys._getframe(0).f_code.co_filename))
top_dir = os.path.abspath(os.path.join(os.path.dirname(sys._getframe(0).f_code.co_filename),'..'))
template_dir = os.path.join(top_dir,'support')
all_dir = os.path.abspath(os.path.join(template_dir,'all'))
android_dir = os.path.abspath(os.path.join(template_dir,'android'))
iphone_dir = os.path.abspath(os.path.join(template_dir,'iphone'))
osx_dir = os.path.abspath(os.path.join(template_dir,'osx'))

buildtime = datetime.datetime.now()
ts = buildtime.strftime("%m/%d/%y %H:%M")

# get the githash for the build so we can always pull this build from a specific
# commit
p = subprocess.Popen(["git","show","--abbrev-commit"],stderr=subprocess.PIPE, stdout=subprocess.PIPE)
githash = p.communicate()[0][8:].split('\n')[0]

ignoreExtensions = ['.pbxuser','.perspectivev3','.pyc']
ignoreDirs = ['.DS_Store','.git','.gitignore','libTitanium.a','titanium.jar','build','bridge.txt']

def ignore(file):
	 for f in ignoreDirs:
		if file == f:
			return True
	 return False

def zip_dir(zf,dir,basepath,subs=None,cb=None):
	for root, dirs, files in os.walk(dir):
		for name in ignoreDirs:
			if name in dirs:
				dirs.remove(name)	# don't visit ignored directories
		for file in files:
			e = os.path.splitext(file)
			if len(e)==2 and e[1] in ignoreExtensions: continue
			from_ = os.path.join(root, file)
			to_ = from_.replace(dir, basepath, 1)
			if subs!=None:
				c = open(from_).read()
				for key in subs:
					c = c.replace(key,subs[key])
				if cb!=None:
					c = cb(file,e[1],c)
				zf.writestr(to_,c)
			else:
				zf.write(from_, to_)

def zip_android(zf,basepath):
	android_dist_dir = os.path.join(top_dir, 'dist', 'android')
	android_jar = os.path.join(android_dist_dir, 'titanium.jar')
	zf.write(android_jar, '%s/android/titanium.jar' % basepath)

	titanium_lib_dir = os.path.join(top_dir, 'android', 'titanium', 'lib')
	for thirdparty_jar in os.listdir(titanium_lib_dir):
		if thirdparty_jar == "smalljs.jar": continue
		elif thirdparty_jar == "commons-logging-1.1.1.jar": continue
		jar_path = os.path.join(top_dir, 'android', 'titanium', 'lib', thirdparty_jar)
		zf.write(jar_path, '%s/android/%s' % (basepath, thirdparty_jar))

	# include all module lib dependencies
	modules_dir = os.path.join(top_dir, 'android', 'modules')
	for module_dir in os.listdir(modules_dir):
		module_lib_dir = os.path.join(modules_dir, module_dir, 'lib')
		if os.path.exists(module_lib_dir):
			for thirdparty_jar in os.listdir(module_lib_dir):
				if thirdparty_jar.endswith('.jar'):
					jar_path = os.path.join(module_lib_dir, thirdparty_jar)
					zf.write(jar_path, '%s/android/%s' % (basepath, thirdparty_jar))

	android_module_jars = glob.glob(os.path.join(android_dist_dir, 'titanium-*.jar'))
	for android_module_jar in android_module_jars:
		 jarname = os.path.split(android_module_jar)[1]
		 zf.write(android_module_jar, '%s/android/modules/%s' % (basepath, jarname))

def resolve_source_imports(platform):
	sys.path.append(iphone_dir)
	import run,prereq
	return importresolver.resolve_source_imports(os.path.join(top_dir,platform,'Classes'))

def make_symbol(fn):
	if fn.startswith('TI') and fn!='TITANIUM' and fn!='TI':
		return fn[2:]
	return fn

def zip_iphone_ipad(zf,basepath,platform,version):

#	zf.writestr('%s/iphone/imports.json'%basepath,resolve_source_imports(platform))

	# include our headers such that 3rd party modules can be compiled
	headers_dir=os.path.join(top_dir,'iphone','Classes')
	for f in os.listdir(headers_dir):
		path = os.path.join(headers_dir,f)
		if os.path.isfile(path) and os.path.splitext(f)[1]=='.h':
			 zf.write(path,'%s/iphone/include/%s' % (basepath,f))
		elif os.path.isdir(path):
			for df in os.listdir(path):
				dfpath = os.path.join(headers_dir,f,df)
				if os.path.isfile(dfpath) and os.path.splitext(df)[1]=='.h':
					 zf.write(dfpath,'%s/iphone/include/%s/%s' % (basepath,f,df))

	tp_headers_dir=os.path.join(top_dir,'iphone','headers','TiCore')
	for f in os.listdir(tp_headers_dir):
		if os.path.isfile(os.path.join(tp_headers_dir,f)) and os.path.splitext(f)[1]=='.h':
			 zf.write(os.path.join(tp_headers_dir,f),'%s/iphone/include/TiCore/%s' % (basepath,f))

	subs = {
		"__VERSION__":version,
		"__TIMESTAMP__":ts,
		"__GITHASH__": githash
	}

	# xcode_templates_dir =  os.path.join(top_dir,'iphone','templates','xcode')
	# zip_dir(zf,xcode_templates_dir,basepath+'/iphone/xcode/templates',subs)

	iphone_lib = os.path.join(top_dir,'iphone',platform,'build')

	zip_dir(zf,os.path.join(top_dir,'iphone','Classes'),basepath+'/iphone/Classes',subs)
	zip_dir(zf,os.path.join(top_dir,'iphone','headers'),basepath+'/iphone/headers',subs)
	zip_dir(zf,os.path.join(top_dir,'iphone','iphone'),basepath+'/iphone/iphone',subs)

	ticore_lib = os.path.join(top_dir,'iphone','lib')

	# during 1.3.3, we added a new lib to a folder that had a .gitignore
	# and we need to manually reset this
	if not os.path.exists(os.path.join(ticore_lib,'libtiverify.a')):
		os.system("git checkout iphone/lib")
		if not os.path.exists(os.path.join(ticore_lib,'libtiverify.a')):
			print "[ERROR] missing libtiverify.a!  make sure you checkout iphone/lib or edit your iphone/.gitignore and remove the lib entry"
			sys.exit(1)

	if not os.path.exists(os.path.join(ticore_lib,'libTiCore.a')):
		print "[ERROR] missing libTiCore.a!"
		sys.exit(1)

	zf.write(os.path.join(ticore_lib,'libTiCore.a'),'%s/%s/libTiCore.a'%(basepath,platform))
	zf.write(os.path.join(ticore_lib,'libtiverify.a'),'%s/%s/libtiverify.a'%(basepath,platform))

	zip_dir(zf,osx_dir,basepath)

	modules_dir = os.path.join(top_dir,'iphone','Resources','modules')
	for f in os.listdir(modules_dir):
		if os.path.isdir(os.path.join(modules_dir,f)):
			module_images = os.path.join(modules_dir,f)
			if os.path.exists(module_images):
				module_name = f.replace('Module','').lower()
				zip_dir(zf,module_images,'%s/%s/modules/%s/images' % (basepath,platform,module_name))

def create_platform_zip(platform,dist_dir,osname,version):
	if not os.path.exists(dist_dir):
		os.makedirs(dist_dir)
	basepath = '%s/%s/%s' % (platform,osname,version)
	sdkzip = os.path.join(dist_dir,'%s-%s-%s.zip' % (platform,version,osname))
	zf = zipfile.ZipFile(sdkzip, 'w', zipfile.ZIP_DEFLATED)
	return (zf,basepath)

def zip_mobilesdk(dist_dir,osname,version,android,iphone,ipad):
	zf, basepath = create_platform_zip('mobilesdk',dist_dir,osname,version)

	version_txt = """version=%s
timestamp=%s
githash=%s
""" % (version,ts,githash)

	zf.writestr('%s/version.txt' % basepath,version_txt)

	zip_dir(zf,all_dir,basepath)
	zip_dir(zf,template_dir,basepath)
	if android: zip_android(zf,basepath)
	if (iphone or ipad) and osname == "osx": zip_iphone_ipad(zf,basepath,'iphone',version)
	zf.close()

def zip_it(dist_dir,osname,version,android,iphone,ipad):
	zip_mobilesdk(dist_dir,osname,version,android,iphone,ipad)

class Packager(object):
	def __init__(self):
		self.os_names = { "Windows":"win32", "Linux":"linux", "Darwin":"osx" }

	def build(self,dist_dir,version,android=True,iphone=True,ipad=True):
		zip_it(dist_dir,self.os_names[platform.system()],version,android,iphone,ipad)

	def build_all_platforms(self,dist_dir,version,android=True,iphone=True,ipad=True):
		for os in self.os_names.values():
			zip_it(dist_dir,os,version,android,iphone,ipad)

if __name__ == '__main__':
	Packager().build(os.path.abspath('../dist'), "1.1.0")
<commit_msg>[#1358] We were off-by-one when parsing the build hash<commit_after>#!/usr/bin/env python
#
# zip up the titanium mobile SDKs into suitable distribution formats
#
import os, types, glob, shutil, sys, platform
import zipfile, datetime, subprocess

if platform.system() == 'Darwin':
	import importresolver

cur_dir = os.path.abspath(os.path.dirname(sys._getframe(0).f_code.co_filename))
top_dir = os.path.abspath(os.path.join(os.path.dirname(sys._getframe(0).f_code.co_filename),'..'))
template_dir = os.path.join(top_dir,'support')
all_dir = os.path.abspath(os.path.join(template_dir,'all'))
android_dir = os.path.abspath(os.path.join(template_dir,'android'))
iphone_dir = os.path.abspath(os.path.join(template_dir,'iphone'))
osx_dir = os.path.abspath(os.path.join(template_dir,'osx'))

buildtime = datetime.datetime.now()
ts = buildtime.strftime("%m/%d/%y %H:%M")

# get the githash for the build so we can always pull this build from a specific
# commit
p = subprocess.Popen(["git","show","--abbrev-commit"],stderr=subprocess.PIPE, stdout=subprocess.PIPE)
githash = p.communicate()[0][7:].split('\n')[0].strip()

ignoreExtensions = ['.pbxuser','.perspectivev3','.pyc']
ignoreDirs = ['.DS_Store','.git','.gitignore','libTitanium.a','titanium.jar','build','bridge.txt']

def ignore(file):
	 for f in ignoreDirs:
		if file == f:
			return True
	 return False

def zip_dir(zf,dir,basepath,subs=None,cb=None):
	for root, dirs, files in os.walk(dir):
		for name in ignoreDirs:
			if name in dirs:
				dirs.remove(name)	# don't visit ignored directories
		for file in files:
			e = os.path.splitext(file)
			if len(e)==2 and e[1] in ignoreExtensions: continue
			from_ = os.path.join(root, file)
			to_ = from_.replace(dir, basepath, 1)
			if subs!=None:
				c = open(from_).read()
				for key in subs:
					c = c.replace(key,subs[key])
				if cb!=None:
					c = cb(file,e[1],c)
				zf.writestr(to_,c)
			else:
				zf.write(from_, to_)

def zip_android(zf,basepath):
	android_dist_dir = os.path.join(top_dir, 'dist', 'android')
	android_jar = os.path.join(android_dist_dir, 'titanium.jar')
	zf.write(android_jar, '%s/android/titanium.jar' % basepath)

	titanium_lib_dir = os.path.join(top_dir, 'android', 'titanium', 'lib')
	for thirdparty_jar in os.listdir(titanium_lib_dir):
		if thirdparty_jar == "smalljs.jar": continue
		elif thirdparty_jar == "commons-logging-1.1.1.jar": continue
		jar_path = os.path.join(top_dir, 'android', 'titanium', 'lib', thirdparty_jar)
		zf.write(jar_path, '%s/android/%s' % (basepath, thirdparty_jar))

	# include all module lib dependencies
	modules_dir = os.path.join(top_dir, 'android', 'modules')
	for module_dir in os.listdir(modules_dir):
		module_lib_dir = os.path.join(modules_dir, module_dir, 'lib')
		if os.path.exists(module_lib_dir):
			for thirdparty_jar in os.listdir(module_lib_dir):
				if thirdparty_jar.endswith('.jar'):
					jar_path = os.path.join(module_lib_dir, thirdparty_jar)
					zf.write(jar_path, '%s/android/%s' % (basepath, thirdparty_jar))

	android_module_jars = glob.glob(os.path.join(android_dist_dir, 'titanium-*.jar'))
	for android_module_jar in android_module_jars:
		 jarname = os.path.split(android_module_jar)[1]
		 zf.write(android_module_jar, '%s/android/modules/%s' % (basepath, jarname))

def resolve_source_imports(platform):
	sys.path.append(iphone_dir)
	import run,prereq
	return importresolver.resolve_source_imports(os.path.join(top_dir,platform,'Classes'))

def make_symbol(fn):
	if fn.startswith('TI') and fn!='TITANIUM' and fn!='TI':
		return fn[2:]
	return fn

def zip_iphone_ipad(zf,basepath,platform,version):

#	zf.writestr('%s/iphone/imports.json'%basepath,resolve_source_imports(platform))

	# include our headers such that 3rd party modules can be compiled
	headers_dir=os.path.join(top_dir,'iphone','Classes')
	for f in os.listdir(headers_dir):
		path = os.path.join(headers_dir,f)
		if os.path.isfile(path) and os.path.splitext(f)[1]=='.h':
			 zf.write(path,'%s/iphone/include/%s' % (basepath,f))
		elif os.path.isdir(path):
			for df in os.listdir(path):
				dfpath = os.path.join(headers_dir,f,df)
				if os.path.isfile(dfpath) and os.path.splitext(df)[1]=='.h':
					 zf.write(dfpath,'%s/iphone/include/%s/%s' % (basepath,f,df))

	tp_headers_dir=os.path.join(top_dir,'iphone','headers','TiCore')
	for f in os.listdir(tp_headers_dir):
		if os.path.isfile(os.path.join(tp_headers_dir,f)) and os.path.splitext(f)[1]=='.h':
			 zf.write(os.path.join(tp_headers_dir,f),'%s/iphone/include/TiCore/%s' % (basepath,f))

	subs = {
		"__VERSION__":version,
		"__TIMESTAMP__":ts,
		"__GITHASH__": githash
	}

	# xcode_templates_dir =  os.path.join(top_dir,'iphone','templates','xcode')
	# zip_dir(zf,xcode_templates_dir,basepath+'/iphone/xcode/templates',subs)

	iphone_lib = os.path.join(top_dir,'iphone',platform,'build')

	zip_dir(zf,os.path.join(top_dir,'iphone','Classes'),basepath+'/iphone/Classes',subs)
	zip_dir(zf,os.path.join(top_dir,'iphone','headers'),basepath+'/iphone/headers',subs)
	zip_dir(zf,os.path.join(top_dir,'iphone','iphone'),basepath+'/iphone/iphone',subs)

	ticore_lib = os.path.join(top_dir,'iphone','lib')

	# during 1.3.3, we added a new lib to a folder that had a .gitignore
	# and we need to manually reset this
	if not os.path.exists(os.path.join(ticore_lib,'libtiverify.a')):
		os.system("git checkout iphone/lib")
		if not os.path.exists(os.path.join(ticore_lib,'libtiverify.a')):
			print "[ERROR] missing libtiverify.a!  make sure you checkout iphone/lib or edit your iphone/.gitignore and remove the lib entry"
			sys.exit(1)

	if not os.path.exists(os.path.join(ticore_lib,'libTiCore.a')):
		print "[ERROR] missing libTiCore.a!"
		sys.exit(1)

	zf.write(os.path.join(ticore_lib,'libTiCore.a'),'%s/%s/libTiCore.a'%(basepath,platform))
	zf.write(os.path.join(ticore_lib,'libtiverify.a'),'%s/%s/libtiverify.a'%(basepath,platform))

	zip_dir(zf,osx_dir,basepath)

	modules_dir = os.path.join(top_dir,'iphone','Resources','modules')
	for f in os.listdir(modules_dir):
		if os.path.isdir(os.path.join(modules_dir,f)):
			module_images = os.path.join(modules_dir,f)
			if os.path.exists(module_images):
				module_name = f.replace('Module','').lower()
				zip_dir(zf,module_images,'%s/%s/modules/%s/images' % (basepath,platform,module_name))

def create_platform_zip(platform,dist_dir,osname,version):
	if not os.path.exists(dist_dir):
		os.makedirs(dist_dir)
	basepath = '%s/%s/%s' % (platform,osname,version)
	sdkzip = os.path.join(dist_dir,'%s-%s-%s.zip' % (platform,version,osname))
	zf = zipfile.ZipFile(sdkzip, 'w', zipfile.ZIP_DEFLATED)
	return (zf,basepath)

def zip_mobilesdk(dist_dir,osname,version,android,iphone,ipad):
	zf, basepath = create_platform_zip('mobilesdk',dist_dir,osname,version)

	version_txt = """version=%s
timestamp=%s
githash=%s
""" % (version,ts,githash)

	zf.writestr('%s/version.txt' % basepath,version_txt)

	zip_dir(zf,all_dir,basepath)
	zip_dir(zf,template_dir,basepath)
	if android: zip_android(zf,basepath)
	if (iphone or ipad) and osname == "osx": zip_iphone_ipad(zf,basepath,'iphone',version)
	zf.close()

def zip_it(dist_dir,osname,version,android,iphone,ipad):
	zip_mobilesdk(dist_dir,osname,version,android,iphone,ipad)

class Packager(object):
	def __init__(self):
		self.os_names = { "Windows":"win32", "Linux":"linux", "Darwin":"osx" }

	def build(self,dist_dir,version,android=True,iphone=True,ipad=True):
		zip_it(dist_dir,self.os_names[platform.system()],version,android,iphone,ipad)

	def build_all_platforms(self,dist_dir,version,android=True,iphone=True,ipad=True):
		for os in self.os_names.values():
			zip_it(dist_dir,os,version,android,iphone,ipad)

if __name__ == '__main__':
	Packager().build(os.path.abspath('../dist'), "1.1.0")
<|endoftext|>


<commit_before># Copyright 2009 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Helpful things for user interfaces."""

import datetime
import os
import tempfile
import urllib
import webbrowser

import benchmark
import history_parser
import nameserver_list

__author__ = 'tstromberg@google.com (Thomas Stromberg)'

# Hack to locate the Alexa data
RSRC_DIR = None

def GenerateOutputFilename(extension):
#  output_dir = os.path.join(os.getenv('HOME'), 'Desktop')
  output_dir = tempfile.gettempdir()
  output_base = 'namebench_%s' % datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H%M')
  output_base = output_base.replace(':', '').replace(' ', '_')
  return os.path.join(output_dir, '%s.%s' % (output_base, extension))

class BaseUI(object):
  """Common methods for UI implementations."""

  def UpdateStatus(self, UpdateStatus, count=None, total=None, error=None):
    """Update the little status message on the bottom of the window."""
    if hasattr(self, 'status_callback') and self.status_callback:
      self.status_callback(UpdateStatus, count=count, total=total)
    else:
      print '(UpdateStatus: %s count=%s total=%s, err=%s)' % (UpdateStatus, count, total, error)

  def PrepareBenchmark(self):
    self.UpdateStatus('Building nameserver objects')
    self.nameservers = nameserver_list.NameServers(
        self.primary,
        self.secondary,
        num_servers=self.options.num_servers,
        timeout=self.options.timeout,
        health_timeout=self.options.health_timeout,
        status_callback=self.UpdateStatus
    )

    self.nameservers.cache_dir = tempfile.gettempdir()
    if len(self.nameservers) > 1:
      self.nameservers.thread_count = int(self.options.thread_count)
      self.nameservers.cache_dir = tempfile.gettempdir()

    self.UpdateStatus('Checking nameserver health')
    self.nameservers.CheckHealth()
    self.bmark = benchmark.Benchmark(self.nameservers,
                                     test_count=self.options.test_count,
                                     run_count=self.options.run_count,
                                     status_callback=self.UpdateStatus)
    self.bmark.UpdateStatus = self.UpdateStatus
    self.UpdateStatus('Creating test records using %s' % self.options.select_mode)
    if self.options.data_source:
      hosts = self.hparser.GetParsedSource(self.options.data_source)
      test_data = self.hparser.GenerateTestData(hosts)
      self.bmark.CreateTests(test_data, select_mode=self.options.select_mode)
    else:
      # The Alexa data (by default)
      data_path = os.path.join(self.resource_dir, self.options.data_file)
      self.bmark.CreateTestsFromFile(data_path,
                                     select_mode=self.options.select_mode)
    self.UpdateStatus('Benchmark preparation is complete.')

  def RunBenchmark(self):
    self.UpdateStatus('Running the benchmark')
    self.bmark.Run()
    self.UpdateStatus('Calculating the best nameserver')
    best = self.bmark.BestOverallNameServer()
    self.CreateReports()
    self.DisplayHtmlReport()
    self.UpdateStatus('Complete! %s [%s] is the best.' % (best.name, best.ip))

  def CreateReports(self):
    """Create CSV & HTML reports for the latest run."""
    self.html_path = GenerateOutputFilename('html')
    self.csv_path = GenerateOutputFilename('csv')

    self.UpdateStatus('Saving HTML report')
    f = open(self.html_path, 'w')
    self.bmark.CreateReport(format='html', output_fp=f, config=self.options, csv_path=self.csv_path)
    f.close()

    self.UpdateStatus('Saving query details (CSV)')
    self.bmark.SaveResultsToCsv(self.csv_path)
    self.UpdateStatus('Reports saved.')

  def DisplayHtmlReport(self):
    self.UpdateStatus('Preparing to displayHTML report')
    print self.html_path
    # All this garbage is to make Windows happy
    url = 'file://%s' % urllib.quote(self.html_path.replace('\\', '/')).replace('%3A', ':')
    print url
    self.UpdateStatus('Opening HTML report')
    webbrowser.open(url)

  def DiscoverSources(self):
    """Seek out and create a list of valid data sources."""
    self.UpdateStatus('Searching for usable data sources')
    self.hparser = history_parser.HistoryParser()
    self.sources = self.hparser.GetAvailableHistorySources()
    self.UpdateStatus('All available sources have been parsed.')

  def ParseSourceSelection(self, selection):
    for source in self.sources:
      if history_parser.sourceToTitle(source) == selection:
        src_type = source[0]
        self.UpdateStatus('Parsed source type to %s' % src_type)
        return src_type

<commit_msg>Make ParseSourceSelection case insensitive, show message when nothing matches<commit_after># Copyright 2009 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Helpful things for user interfaces."""

import datetime
import os
import tempfile
import urllib
import webbrowser

import benchmark
import history_parser
import nameserver_list

__author__ = 'tstromberg@google.com (Thomas Stromberg)'

# Hack to locate the Alexa data
RSRC_DIR = None

def GenerateOutputFilename(extension):
#  output_dir = os.path.join(os.getenv('HOME'), 'Desktop')
  output_dir = tempfile.gettempdir()
  output_base = 'namebench_%s' % datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H%M')
  output_base = output_base.replace(':', '').replace(' ', '_')
  return os.path.join(output_dir, '%s.%s' % (output_base, extension))

class BaseUI(object):
  """Common methods for UI implementations."""

  def UpdateStatus(self, UpdateStatus, count=None, total=None, error=None):
    """Update the little status message on the bottom of the window."""
    if hasattr(self, 'status_callback') and self.status_callback:
      self.status_callback(UpdateStatus, count=count, total=total)
    else:
      print '(UpdateStatus: %s count=%s total=%s, err=%s)' % (UpdateStatus, count, total, error)

  def PrepareBenchmark(self):
    self.UpdateStatus('Building nameserver objects')
    self.nameservers = nameserver_list.NameServers(
        self.primary,
        self.secondary,
        num_servers=self.options.num_servers,
        timeout=self.options.timeout,
        health_timeout=self.options.health_timeout,
        status_callback=self.UpdateStatus
    )

    self.nameservers.cache_dir = tempfile.gettempdir()
    if len(self.nameservers) > 1:
      self.nameservers.thread_count = int(self.options.thread_count)
      self.nameservers.cache_dir = tempfile.gettempdir()

    self.UpdateStatus('Checking nameserver health')
    self.nameservers.CheckHealth()
    self.bmark = benchmark.Benchmark(self.nameservers,
                                     test_count=self.options.test_count,
                                     run_count=self.options.run_count,
                                     status_callback=self.UpdateStatus)
    self.bmark.UpdateStatus = self.UpdateStatus
    self.UpdateStatus('Creating test records using %s' % self.options.select_mode)
    if self.options.data_source:
      hosts = self.hparser.GetParsedSource(self.options.data_source)
      test_data = self.hparser.GenerateTestData(hosts)
      self.bmark.CreateTests(test_data, select_mode=self.options.select_mode)
    else:
      # The Alexa data (by default)
      data_path = os.path.join(self.resource_dir, self.options.data_file)
      self.bmark.CreateTestsFromFile(data_path,
                                     select_mode=self.options.select_mode)
    self.UpdateStatus('Benchmark preparation is complete.')

  def RunBenchmark(self):
    self.UpdateStatus('Running the benchmark')
    self.bmark.Run()
    self.UpdateStatus('Calculating the best nameserver')
    best = self.bmark.BestOverallNameServer()
    self.CreateReports()
    self.DisplayHtmlReport()
    self.UpdateStatus('Complete! %s [%s] is the best.' % (best.name, best.ip))

  def CreateReports(self):
    """Create CSV & HTML reports for the latest run."""
    self.html_path = GenerateOutputFilename('html')
    self.csv_path = GenerateOutputFilename('csv')

    self.UpdateStatus('Saving HTML report')
    f = open(self.html_path, 'w')
    self.bmark.CreateReport(format='html', output_fp=f, config=self.options, csv_path=self.csv_path)
    f.close()

    self.UpdateStatus('Saving query details (CSV)')
    self.bmark.SaveResultsToCsv(self.csv_path)
    self.UpdateStatus('Reports saved.')

  def DisplayHtmlReport(self):
    self.UpdateStatus('Preparing to displayHTML report')
    print self.html_path
    # All this garbage is to make Windows happy
    url = 'file://%s' % urllib.quote(self.html_path.replace('\\', '/')).replace('%3A', ':')
    print url
    self.UpdateStatus('Opening HTML report')
    webbrowser.open(url)

  def DiscoverSources(self):
    """Seek out and create a list of valid data sources."""
    self.UpdateStatus('Searching for usable data sources')
    self.hparser = history_parser.HistoryParser()
    self.sources = self.hparser.GetAvailableHistorySources()
    self.UpdateStatus('All available sources have been parsed.')

  def ParseSourceSelection(self, selection):
    self.UpdateStatus('Matching "%s" to %s' % (selection, self.sources))
    for source in self.sources:
      parsed_name = history_parser.sourceToTitle(source)
      if parsed_name.lower() == selection.lower():
        src_type = source[0]
        self.UpdateStatus('Parsed source type to %s' % src_type)
        return src_type
    self.UpdateStatus('Unable to match "%s" to a source type' % selection)
    return None

<|endoftext|>


<commit_before># ######################################################################
# Copyright (c) 2014, Brookhaven Science Associates, Brookhaven        #
# National Laboratory. All rights reserved.                            #
#                                                                      #
# Redistribution and use in source and binary forms, with or without   #
# modification, are permitted provided that the following conditions   #
# are met:                                                             #
#                                                                      #
# * Redistributions of source code must retain the above copyright     #
#   notice, this list of conditions and the following disclaimer.      #
#                                                                      #
# * Redistributions in binary form must reproduce the above copyright  #
#   notice this list of conditions and the following disclaimer in     #
#   the documentation and/or other materials provided with the         #
#   distribution.                                                      #
#                                                                      #
# * Neither the name of the Brookhaven Science Associates, Brookhaven  #
#   National Laboratory nor the names of its contributors may be used  #
#   to endorse or promote products derived from this software without  #
#   specific prior written permission.                                 #
#                                                                      #
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  #
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT    #
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS    #
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE       #
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,           #
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES   #
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR   #
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)   #
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,  #
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OTHERWISE) ARISING   #
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE   #
# POSSIBILITY OF SUCH DAMAGE.                                          #
########################################################################
from __future__ import absolute_import, division, print_function

import logging

from skbeam.io.save_powder_output import logger

logger = logging.getLogger(__name__)

try:
    from .net_cdf_io import load_netCDF
except ImportError:
    def load_netCDF(*args, **kwargs):
        # Die at call time so as not to ruin entire io package.
        raise ImportError("This function requires netCDF4.")

from .binary import read_binary

from .avizo_io import load_amiramesh

from .save_powder_output import save_output

from .gsas_file_reader import gsas_reader

from .save_powder_output import gsas_writer

__all__ = ['load_netCDF', 'read_binary', 'load_amiramesh', 'save_output',
           'gsas_reader', 'gsas_writer']
<commit_msg>MTN: remove spare logger<commit_after># ######################################################################
# Copyright (c) 2014, Brookhaven Science Associates, Brookhaven        #
# National Laboratory. All rights reserved.                            #
#                                                                      #
# Redistribution and use in source and binary forms, with or without   #
# modification, are permitted provided that the following conditions   #
# are met:                                                             #
#                                                                      #
# * Redistributions of source code must retain the above copyright     #
#   notice, this list of conditions and the following disclaimer.      #
#                                                                      #
# * Redistributions in binary form must reproduce the above copyright  #
#   notice this list of conditions and the following disclaimer in     #
#   the documentation and/or other materials provided with the         #
#   distribution.                                                      #
#                                                                      #
# * Neither the name of the Brookhaven Science Associates, Brookhaven  #
#   National Laboratory nor the names of its contributors may be used  #
#   to endorse or promote products derived from this software without  #
#   specific prior written permission.                                 #
#                                                                      #
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  #
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT    #
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS    #
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE       #
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,           #
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES   #
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR   #
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)   #
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,  #
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OTHERWISE) ARISING   #
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE   #
# POSSIBILITY OF SUCH DAMAGE.                                          #
########################################################################
from __future__ import absolute_import, division, print_function

import logging

logger = logging.getLogger(__name__)

try:
    from .net_cdf_io import load_netCDF
except ImportError:
    def load_netCDF(*args, **kwargs):
        # Die at call time so as not to ruin entire io package.
        raise ImportError("This function requires netCDF4.")

from .binary import read_binary

from .avizo_io import load_amiramesh

from .save_powder_output import save_output

from .gsas_file_reader import gsas_reader

from .save_powder_output import gsas_writer

__all__ = ['load_netCDF', 'read_binary', 'load_amiramesh', 'save_output',
           'gsas_reader', 'gsas_writer']
<|endoftext|>