<commit_before>#!/usr/bin/env python
# Copyright(C) 2011 by <NAME> <<EMAIL>>

# This program 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.
# 
# This program 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 this program.  If not, see
# <http://www.gnu.org/licenses/agpl.html>.

import sys
import os
import warnings
import optparse
import re
from cgi import escape
import posixpath
import wsgiref.util
import time

import DataStore

# bitcointools -- modified deserialize.py to return raw transaction
import deserialize
import util  # Added functions.
import base58

ABE_APPNAME = "Abe"
ABE_VERSION = '0.3'
ABE_URL = 'https://github.com/jtobey/bitcoin-abe'

COPYRIGHT_YEARS = '2011'
COPYRIGHT = "<NAME>"
COPYRIGHT_URL = "mailto:<EMAIL>"

DONATIONS_BTC = '1PWC7PNHL1SgvZaN7xEtygenKjWobWsCuf'
DONATIONS_NMC = 'NJ3MSELK1cWnqUa6xhF2wUYAnz3RSrWXcK'

DEFAULT_TEMPLATE = """
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <link rel="stylesheet" type="text/css"
     href="%(dotdot)s%(STATIC_PATH)sabe.css" />
    <link rel="shortcut icon" href="%(dotdot)s%(STATIC_PATH)sfavicon.ico" />
    <title>%(title)s</title>
</head>
<body>
    <h1><a href="%(dotdot)schains"><img
     src="%(dotdot)s%(STATIC_PATH)slogo32.png" alt="ABE logo" /></a> %(h1)s
    </h1>
    %(body)s
    <p style="font-size: smaller">
        <span style="font-style: italic">
            <a href="%(ABE_URL)s">%(APPNAME)s</a>
            %(VERSION)s &#169; %(COPYRIGHT_YEARS)s
            <a href="%(COPYRIGHT_URL)s">%(COPYRIGHT)s</a>
        </span>
        %(download)s
        Tips appreciated!
        <a href="%(dotdot)saddress/%(DONATIONS_BTC)s">BTC</a>
        <a href="%(dotdot)saddress/%(DONATIONS_NMC)s">NMC</a>
    </p>
</body>
</html>
"""

# XXX This should probably be a property of chain, or even a query param.
LOG10COIN = 8
COIN = 10 ** LOG10COIN

ADDRESS_RE = re.compile(
    '[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{6,}\\Z')
HEIGHT_RE = re.compile('(?:0|[1-9][0-9]*)\\Z')
HASH_PREFIX_RE = re.compile('[0-9a-fA-F]{6,64}\\Z')

def make_store(args):
    store = DataStore.new(args)
    store.catch_up()
    return store

class NoSuchChainError(Exception):
    """Thrown when a chain lookup fails"""

class PageNotFound(Exception):
    """Thrown when code wants to return 404 Not Found"""

class Redirect(Exception):
    """Thrown when code wants to redirect the request"""

class Streamed(Exception):
    """Thrown when code has written the document to the callable
    returned by start_response."""

class Abe:
    def __init__(abe, store, args):
        abe.store = store
        abe.args = args
        abe.htdocs = os.path.join(os.path.split(__file__)[0],
                                  args.document_root or 'htdocs')
        abe.static_path = '' if args.static_path is None else args.static_path
        abe.template_vars = args.template_vars.copy()
        abe.template_vars['STATIC_PATH'] = (
            abe.template_vars.get('STATIC_PATH', abe.static_path))
        abe.template = flatten(args.template)
        abe.debug = args.debug
        import logging
        abe.log = logging
        abe.log.info('Abe initialized.')
        abe.handlers = {
            "": abe.show_world,
            "chains": abe.show_world,
            "chain": abe.show_chain,
            "block": abe.show_block,
            "tx": abe.show_tx,
            "address": abe.show_address,
            "search": abe.search,
            }
        if args.auto_agpl:
            abe.handlers["download"] = abe.download_srcdir
        else:
            abe.template_vars['download'] = (
                abe.template_vars.get('download', ''))

    def __call__(abe, env, start_response):
        import urlparse

        status = '200 OK'
        page = {
            "title": [escape(ABE_APPNAME), " ", ABE_VERSION],
            "body": [],
            "env": env,
            "params": {},
            "dotdot": "../" * (env['PATH_INFO'].count('/') - 1),
            "start_response": start_response,
            }
        if 'QUERY_STRING' in env:
            page['params'] = urlparse.parse_qs(env['QUERY_STRING'])

        if fix_path_info(env):
            print "fixed path_info"
            return redirect(page)

        # Always be up-to-date, even if we means having to wait for a response!
        # XXX Could use threads, timers, or a cron job.
        abe.store.catch_up()

        obtype = wsgiref.util.shift_path_info(env)
        handler = abe.handlers.get(obtype)
        try:
            if handler is None:
                return abe.serve_static(obtype + env['PATH_INFO'],
                                        start_response)
            handler(page)
        except PageNotFound:
            status = '404 Not Found'
            page["body"] = ['<p class="error">Sorry, ', env['SCRIPT_NAME'],
                            env['PATH_INFO'],
                            ' does not exist on this server.</p>']
        except NoSuchChainError, e:
            page['body'] += [
                '<p class="error">'
                'Sorry, I don\'t know about that chain!</p>\n']
        except Redirect:
            return redirect(page)
        except Streamed:
            return ''
        except:
            abe.store.rollback()
            raise

        abe.store.rollback()  # Close imlicitly opened transaction.

        start_response(status, [('Content-type', 'application/xhtml+xml'),
                                ('Cache-Control', 'max-age=30')])

        tvars = abe.template_vars.copy()
        tvars['dotdot'] = page['dotdot']
        tvars['title'] = flatten(page['title'])
        tvars['h1'] = flatten(page.get('h1') or page['title'])
        tvars['body'] = flatten(page['body'])
        if abe.args.auto_agpl:
            tvars['download'] = (
                ' <a href="' + page['dotdot'] + 'download">Source</a>')<commit_msg>Don't scan for new blocks on static requests.<commit_after>#!/usr/bin/env python
# Copyright(C) 2011 by <NAME> <<EMAIL>>

# This program 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.
# 
# This program 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 this program.  If not, see
# <http://www.gnu.org/licenses/agpl.html>.

import sys
import os
import warnings
import optparse
import re
from cgi import escape
import posixpath
import wsgiref.util
import time

import DataStore

# bitcointools -- modified deserialize.py to return raw transaction
import deserialize
import util  # Added functions.
import base58

ABE_APPNAME = "Abe"
ABE_VERSION = '0.3'
ABE_URL = 'https://github.com/jtobey/bitcoin-abe'

COPYRIGHT_YEARS = '2011'
COPYRIGHT = "<NAME>"
COPYRIGHT_URL = "mailto:<EMAIL>"

DONATIONS_BTC = '1PWC7PNHL1SgvZaN7xEtygenKjWobWsCuf'
DONATIONS_NMC = 'NJ3MSELK1cWnqUa6xhF2wUYAnz3RSrWXcK'

DEFAULT_TEMPLATE = """
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <link rel="stylesheet" type="text/css"
     href="%(dotdot)s%(STATIC_PATH)sabe.css" />
    <link rel="shortcut icon" href="%(dotdot)s%(STATIC_PATH)sfavicon.ico" />
    <title>%(title)s</title>
</head>
<body>
    <h1><a href="%(dotdot)schains"><img
     src="%(dotdot)s%(STATIC_PATH)slogo32.png" alt="ABE logo" /></a> %(h1)s
    </h1>
    %(body)s
    <p style="font-size: smaller">
        <span style="font-style: italic">
            <a href="%(ABE_URL)s">%(APPNAME)s</a>
            %(VERSION)s &#169; %(COPYRIGHT_YEARS)s
            <a href="%(COPYRIGHT_URL)s">%(COPYRIGHT)s</a>
        </span>
        %(download)s
        Tips appreciated!
        <a href="%(dotdot)saddress/%(DONATIONS_BTC)s">BTC</a>
        <a href="%(dotdot)saddress/%(DONATIONS_NMC)s">NMC</a>
    </p>
</body>
</html>
"""

# XXX This should probably be a property of chain, or even a query param.
LOG10COIN = 8
COIN = 10 ** LOG10COIN

ADDRESS_RE = re.compile(
    '[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{6,}\\Z')
HEIGHT_RE = re.compile('(?:0|[1-9][0-9]*)\\Z')
HASH_PREFIX_RE = re.compile('[0-9a-fA-F]{6,64}\\Z')

def make_store(args):
    store = DataStore.new(args)
    store.catch_up()
    return store

class NoSuchChainError(Exception):
    """Thrown when a chain lookup fails"""

class PageNotFound(Exception):
    """Thrown when code wants to return 404 Not Found"""

class Redirect(Exception):
    """Thrown when code wants to redirect the request"""

class Streamed(Exception):
    """Thrown when code has written the document to the callable
    returned by start_response."""

class Abe:
    def __init__(abe, store, args):
        abe.store = store
        abe.args = args
        abe.htdocs = os.path.join(os.path.split(__file__)[0],
                                  args.document_root or 'htdocs')
        abe.static_path = '' if args.static_path is None else args.static_path
        abe.template_vars = args.template_vars.copy()
        abe.template_vars['STATIC_PATH'] = (
            abe.template_vars.get('STATIC_PATH', abe.static_path))
        abe.template = flatten(args.template)
        abe.debug = args.debug
        import logging
        abe.log = logging
        abe.log.info('Abe initialized.')
        abe.handlers = {
            "": abe.show_world,
            "chains": abe.show_world,
            "chain": abe.show_chain,
            "block": abe.show_block,
            "tx": abe.show_tx,
            "address": abe.show_address,
            "search": abe.search,
            }
        if args.auto_agpl:
            abe.handlers["download"] = abe.download_srcdir
        else:
            abe.template_vars['download'] = (
                abe.template_vars.get('download', ''))

    def __call__(abe, env, start_response):
        import urlparse

        status = '200 OK'
        page = {
            "title": [escape(ABE_APPNAME), " ", ABE_VERSION],
            "body": [],
            "env": env,
            "params": {},
            "dotdot": "../" * (env['PATH_INFO'].count('/') - 1),
            "start_response": start_response,
            }
        if 'QUERY_STRING' in env:
            page['params'] = urlparse.parse_qs(env['QUERY_STRING'])

        if fix_path_info(env):
            print "fixed path_info"
            return redirect(page)

        obtype = wsgiref.util.shift_path_info(env)
        handler = abe.handlers.get(obtype)
        try:
            if handler is None:
                return abe.serve_static(obtype + env['PATH_INFO'],
                                        start_response)

            # Always be up-to-date, even if we means having to wait
            # for a response!  XXX Could use threads, timers, or a
            # cron job.
            abe.store.catch_up()

            handler(page)
        except PageNotFound:
            status = '404 Not Found'
            page["body"] = ['<p class="error">Sorry, ', env['SCRIPT_NAME'],
                            env['PATH_INFO'],
                            ' does not exist on this server.</p>']
        except NoSuchChainError, e:
            page['body'] += [
                '<p class="error">'
                'Sorry, I don\'t know about that chain!</p>\n']
        except Redirect:
            return redirect(page)
        except Streamed:
            return ''
        except:
            abe.store.rollback()
            raise

        abe.store.rollback()  # Close imlicitly opened transaction.

        start_response(status, [('Content-type', 'application/xhtml+xml'),
                                ('Cache-Control', 'max-age=30')])

        tvars = abe.template_vars.copy()
        tvars['dotdot'] = page['dotdot']
        tvars['title'] = flatten(page['title'])
        tvars['h1'] = flatten(page.get('h1') or page['title'])
        tvars['body'] = flatten(page['body'])
        if abe.args.auto_agpl:
            tvars['download'] = (
                ' <a href="' + page['dotdot'] + 'download">Source</a>')
{'fraction_non_alphanumeric': 0.1349467418546366, 'fraction_numerical': 0.01206140350877193, 'mean_word_length': 2.4196572040707016, 'pattern_counts': {'":': 26, '<': 81, '<?xml version=': 0, '>': 81, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2504534', 'n_tokens_mistral': 4271, 'n_tokens_neox': 4024, 'n_words': 982}