<commit_before>import re, threading, os.path, copy, cPickle

from operator import attrgetter
from itertools import count

from pony.auth import get_ticket
from pony.templating import Html, StrHtml, htmljoin, htmltag
from pony.web import get_request

class FormCancel(Exception):
    pass

class Form(object):
    def __init__(self, method='POST', secure=None, **attrs):
        self._cleared = False
        self._error_text = None
        self._request = get_request()
        self.attrs = dict((name.lower(), str(value))
                          for name, value in attrs.iteritems())
        if 'name' not in attrs: self.attrs['name'] = self.__class__.__name__
        self.fields = []
        self.hidden_fields = []
        self.submit_fields = []
        self._secure = False
        self._set_method(method)
        self._set_secure(secure)
        self._f = Hidden(self.attrs.get('name', ''))

        fields = []
        for name, x in self.__class__.__dict__.items():
            if isinstance(x, HtmlField):
                x.name = name
                fields.append(x)
        fields.sort(key=attrgetter('_id_'))
        for field in fields: setattr(self, field.name, copy.copy(field))

        self.validate()        
    @classmethod
    def _handle_http_request(cls):
        request = get_request()
        request.form_processed = None
        form = cls()
        if not form.is_valid: return
        try: form.on_submit()
        except FormCancel: request.form_processed = False
        else: request.form_processed = True
    def on_submit(self):
        pass
    def clear(self):
        self._cleared = True
        self.is_submitted = False
    def _set_method(self, method):
        method = method.upper()
        if method not in ('GET', 'POST'): raise TypeError(
            'Invalid form method: %s (must be GET or POST)' % method)
        if method == 'GET' and self._secure:
            raise TypeError('GET form cannot be secure')
        self._method = method
        self._update_status()
    method = property(attrgetter('_method'), _set_method)
    def _set_secure(self, secure):
        if self._method == 'GET':
            if secure: raise TypeError('GET form cannot be secure')
            self._secure = False
        elif self.method == 'POST': self._secure = secure or secure is None
        else: assert False
        if self._secure: self._t = Ticket()
        elif hasattr(self, '_t'): del self._t
        self._update_status()
    secure = property(attrgetter('_secure'), _set_secure)
    def _update_status(self):
        self.is_submitted = False
        request = self._request
        if self._cleared or request.form_processed: return
        if request.form_processed is not None \
           and request.submitted_form != self.attrs.get('name'): return<commit_msg>Fixed bug in static forms<commit_after>import re, threading, os.path, copy, cPickle

from operator import attrgetter
from itertools import count

from pony.auth import get_ticket
from pony.templating import Html, StrHtml, htmljoin, htmltag
from pony.web import get_request

class FormCanceled(Exception):
    pass

class Form(object):
    def __init__(self, method='POST', secure=None, **attrs):
        self._cleared = False
        self._error_text = None
        self._request = get_request()
        self.attrs = dict((name.lower(), str(value))
                          for name, value in attrs.iteritems())
        if 'name' not in attrs: self.attrs['name'] = self.__class__.__name__
        self.fields = []
        self.hidden_fields = []
        self.submit_fields = []
        self._secure = False
        self._set_method(method)
        self._set_secure(secure)
        self._f = Hidden(self.attrs.get('name', ''))

        fields = []
        for name, x in self.__class__.__dict__.items():
            if isinstance(x, HtmlField):
                x.name = name
                fields.append(x)
        fields.sort(key=attrgetter('_id_'))
        for field in fields: setattr(self, field.name, copy.copy(field))

        self.validate()        
    @classmethod
    def _handle_http_request(cls):
        request = get_request()
        request.form_processed = None
        form = cls()
        if not form.is_valid:
            request.form_processed = False
            return
        try: form.on_submit()
        except FormCanceled: request.form_processed = False
        else: request.form_processed = True
    def on_submit(self):
        raise FormCanceled
    def clear(self):
        self._cleared = True
        self.is_submitted = False
    def _set_method(self, method):
        method = method.upper()
        if method not in ('GET', 'POST'): raise TypeError(
            'Invalid form method: %s (must be GET or POST)' % method)
        if method == 'GET' and self._secure:
            raise TypeError('GET form cannot be secure')
        self._method = method
        self._update_status()
    method = property(attrgetter('_method'), _set_method)
    def _set_secure(self, secure):
        if self._method == 'GET':
            if secure: raise TypeError('GET form cannot be secure')
            self._secure = False
        elif self.method == 'POST': self._secure = secure or secure is None
        else: assert False
        if self._secure: self._t = Ticket()
        elif hasattr(self, '_t'): del self._t
        self._update_status()
    secure = property(attrgetter('_secure'), _set_secure)
    def _update_status(self):
        self.is_submitted = False
        request = self._request
        if self._cleared or request.form_processed: return
        if request.form_processed is not None \
           and request.submitted_form != self.attrs.get('name'): return
{'fraction_non_alphanumeric': 0.1027433164424253, 'fraction_numerical': 0.0, 'mean_word_length': 2.331781140861467, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4174157', 'n_tokens_mistral': 1626, 'n_tokens_neox': 1603, 'n_words': 454}