<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

__version__ = '0.1.0'

import BaseHTTPServer
from cStringIO import StringIO
from datetime import datetime, timedelta
import email.message
import gzip
import httplib
import json
from optparse import OptionParser
import random
import re
import socket
import string
import sys
import time
import traceback
import urllib2
import urlparse


DEFAULT_PORT = 13085

class Request(object):
    
    """An HTTP request.
    
    .. attribute:: method
       
       Request method.
    
    .. attribute:: path
       
       The request path, excluding the query string.
    
    .. attribute:: query
       
       A dictionary of parameters parsed from the query string.
       An empty dictionary if none can be parsed.
    
    .. attribute:: headers
       
       An ``rfc822.Message``-like mapping of request headers.
    
    .. attribute:: body
       
       Request entity body as a `str` if there is one, otherwise `None`.
    """
    
    def __init__(self, method, path, query, headers, body):
        self.method = method
        self.path = path
        self.query = query
        self.headers = headers
        self.body = body
    
    def p(self, x):
        if callable(x):
            return x(self)
        else:
            return x


class Response(object):
    
    def __init__(self):
        self.status = httplib.OK
        self.headers = email.message.Message()
        self.body = None
    
    def set_header(self, name, value, **params):
        del self.headers[name]
        self.headers.add_header(name, value, **params)
    
    def add_header(self, name, value, **params):
        self.headers.add_header(name, value, **params)


def make_text(nbytes):
    buf = StringIO()
    written = 0
    words = 'lorem ipsum dolor sit amet, consectetur adipisicing elit'.split()
    while written < nbytes:
        word = random.choice(words)
        buf.write(word)
        written += len(word)
        if random.random() < 0.01:
            buf.write('\n\n')
            written += 2
        else:
            buf.write(' ')
            written += 1
    return buf.getvalue()


def html_escape(s):
    return s.replace('&', '&amp;').replace('"', '&quot;'). \
             replace('<', '&lt;').replace('>', '&gt;')


class Cheat(object):
    
    items = []
    
    @staticmethod
    def entry(args='', comment=''):
        def decorator(func):
            Cheat.items.append((func.__name__, args, comment))
            return func
        return decorator
    
    @staticmethod
    def format():
        code = (
            '<p><code class="example">path(\'/products/*\').delay(1).html().'
            'cookie(\'sessionid\', \'1234\', max_age=3600)</code></p>'
            '<table><tbody>'
        )
        for name, args, comment in Cheat.items:
            code += (
                '<tr><th><code>%s(<span class="args">%s</span>)</code></th>'
                '<td>%s</td></tr>' % (name, html_escape(args), comment)
            )
        code += '</tbody></table>'
        return code


class Rule(object):
    
    url_cache = {}
    
    def __init__(self):
        self._status = None
        
        # Headers are tricky.
        # We just store a sequence of (operation, name, value, params) tuples,
        # a patch of sorts, and then apply this patch.
        # Operation can be "set" (replacing) or "add".
        self._headers = []
        
        self._body = None
        self._processor = None
        self._delay = None
        self._chain = []
        self._final = None
        self._counter = 0
        self._maybe = []
        self._enable_cors = False
        self._allow = None
        self._gzip = False
    
    @Cheat.entry('code')
    def status(self, code):
        """Set response status to `code`."""
        self._status = code
        return self
    
    @Cheat.entry('name, value, **params', 'params are appended as k=v pairs')
    def header(self, name, value, **params):
        """Set response header `name` to `value`.
        
        If `params` are specified, they are appended as k=v pairs.
        For example::
        
            header('Content-Type', 'text/html', charset='utf-8')
        
        produces::
        
            Content-Type: text/html; charset="utf-8"
        """
        self._headers.append(('set', name, value, params))
        return self
    
    @Cheat.entry('name, value, **params', 'added, not replaced')
    def add_header(self, name, value, **params):
        """Same as :meth:`~Rule.header`, but add the header, not replace it.
        
        This can be used to send multiple headers with the same name,
        such as ``Via`` or ``Set-Cookie``
        (but for the latter see :meth:`~Rule.cookie`).
        """
        self._headers.append(('add', name, value, params))
        return self
    
    @Cheat.entry('data')
    def body(self, data):
        """Set response entity body to `data`."""
        self._body = data
        return self
    
    @Cheat.entry('path')
    def body_file(self, path):
        """Set response entity body to the contents of `path`.
        
        The file at `path` is read when the rules are posted."""
        return self.body(open(path).read())
    
    @Cheat.entry('url', 'data from <var>url</var> is cached until Turq exits')
    def body_url(self, url):<commit_msg>Tilde expansion in Rule.body_file<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

__version__ = '0.1.0'

import BaseHTTPServer
from cStringIO import StringIO
from datetime import datetime, timedelta
import email.message
import gzip
import httplib
import json
from optparse import OptionParser
import os.path
import random
import re
import socket
import string
import sys
import time
import traceback
import urllib2
import urlparse


DEFAULT_PORT = 13085

class Request(object):
    
    """An HTTP request.
    
    .. attribute:: method
       
       Request method.
    
    .. attribute:: path
       
       The request path, excluding the query string.
    
    .. attribute:: query
       
       A dictionary of parameters parsed from the query string.
       An empty dictionary if none can be parsed.
    
    .. attribute:: headers
       
       An ``rfc822.Message``-like mapping of request headers.
    
    .. attribute:: body
       
       Request entity body as a `str` if there is one, otherwise `None`.
    """
    
    def __init__(self, method, path, query, headers, body):
        self.method = method
        self.path = path
        self.query = query
        self.headers = headers
        self.body = body
    
    def p(self, x):
        if callable(x):
            return x(self)
        else:
            return x


class Response(object):
    
    def __init__(self):
        self.status = httplib.OK
        self.headers = email.message.Message()
        self.body = None
    
    def set_header(self, name, value, **params):
        del self.headers[name]
        self.headers.add_header(name, value, **params)
    
    def add_header(self, name, value, **params):
        self.headers.add_header(name, value, **params)


def make_text(nbytes):
    buf = StringIO()
    written = 0
    words = 'lorem ipsum dolor sit amet, consectetur adipisicing elit'.split()
    while written < nbytes:
        word = random.choice(words)
        buf.write(word)
        written += len(word)
        if random.random() < 0.01:
            buf.write('\n\n')
            written += 2
        else:
            buf.write(' ')
            written += 1
    return buf.getvalue()


def html_escape(s):
    return s.replace('&', '&amp;').replace('"', '&quot;'). \
             replace('<', '&lt;').replace('>', '&gt;')


class Cheat(object):
    
    items = []
    
    @staticmethod
    def entry(args='', comment=''):
        def decorator(func):
            Cheat.items.append((func.__name__, args, comment))
            return func
        return decorator
    
    @staticmethod
    def format():
        code = (
            '<p><code class="example">path(\'/products/*\').delay(1).html().'
            'cookie(\'sessionid\', \'1234\', max_age=3600)</code></p>'
            '<table><tbody>'
        )
        for name, args, comment in Cheat.items:
            code += (
                '<tr><th><code>%s(<span class="args">%s</span>)</code></th>'
                '<td>%s</td></tr>' % (name, html_escape(args), comment)
            )
        code += '</tbody></table>'
        return code


class Rule(object):
    
    url_cache = {}
    
    def __init__(self):
        self._status = None
        
        # Headers are tricky.
        # We just store a sequence of (operation, name, value, params) tuples,
        # a patch of sorts, and then apply this patch.
        # Operation can be "set" (replacing) or "add".
        self._headers = []
        
        self._body = None
        self._processor = None
        self._delay = None
        self._chain = []
        self._final = None
        self._counter = 0
        self._maybe = []
        self._enable_cors = False
        self._allow = None
        self._gzip = False
    
    @Cheat.entry('code')
    def status(self, code):
        """Set response status to `code`."""
        self._status = code
        return self
    
    @Cheat.entry('name, value, **params', 'params are appended as k=v pairs')
    def header(self, name, value, **params):
        """Set response header `name` to `value`.
        
        If `params` are specified, they are appended as k=v pairs.
        For example::
        
            header('Content-Type', 'text/html', charset='utf-8')
        
        produces::
        
            Content-Type: text/html; charset="utf-8"
        """
        self._headers.append(('set', name, value, params))
        return self
    
    @Cheat.entry('name, value, **params', 'added, not replaced')
    def add_header(self, name, value, **params):
        """Same as :meth:`~Rule.header`, but add the header, not replace it.
        
        This can be used to send multiple headers with the same name,
        such as ``Via`` or ``Set-Cookie``
        (but for the latter see :meth:`~Rule.cookie`).
        """
        self._headers.append(('add', name, value, params))
        return self
    
    @Cheat.entry('data')
    def body(self, data):
        """Set response entity body to `data`."""
        self._body = data
        return self
    
    @Cheat.entry('path')
    def body_file(self, path):
        """Set response entity body to the contents of `path`.
        
        The file at `path` is read when the rules are posted.
        `path` undergoes tilde expansion.
        """
        return self.body(open(os.path.expanduser(path)).read())
    
    @Cheat.entry('url', 'data from <var>url</var> is cached until Turq exits')
    def body_url(self, url):
{'fraction_non_alphanumeric': 0.1287976431596391, 'fraction_numerical': 0.005707972749033327, 'mean_word_length': 1.9486970684039089, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 2, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2900798', 'n_tokens_mistral': 3216, 'n_tokens_neox': 3153, 'n_words': 940}