<commit_before>import json
import traceback

import cherrypy

import tangelo

class TangeloStream(object):
    def __init__(self):
        self.streams = {}

    @cherrypy.expose
    def default(self, key=None):
        # Construct a result object.
        result = {}

        # Perform the requested action: GET means to either return a list of
        # stream keys, or to run the keyed stream one step; DELETE means to
        # delete a keyed stream from the registry.
        method = cherrypy.request.method
        if method == "GET":
            if key is None:
                result = self.streams.keys()
            elif key not in self.streams:
                result["error"] = "Unknown key"
            else:
                # Grab the stream in preparation for running it.
                stream = self.streams[key]

                # Attempt to run the stream via its next() method - if this yields a
                # result, then continue; if the next() method raises StopIteration,
                # then there are no more results to retrieve; if any other exception
                # is raised, this is treated as an error.
                try:
                    result["data"] = stream.next()
                    result["finished"] = False
                except StopIteration:
                    result["finished"] = True
                    del self.streams[key]
                except:
                    del self.streams[key]
                    raise cherrypy.HTTPError("501 Error in Python Service",
                            "Caught exception while executing stream service keyed by %s:<br><pre>%s</pre>" % (key, traceback.format_exc()))

        elif method == "DELETE":
            if key is None:
                result["error"] = "No key specified"
            elif key not in self.streams:
                result["error"] = "Unknown key"
            else:
                del self.streams[key]
                result["data"] = "OK"

        # JSON-serialize the result and return it.
        try:
            return json.dumps(result)
        except TypeError:
            raise cherrypy.HTTPError("501 Bad Response from Python Service",
                    "The stream keyed by %s returned a non JSON-seriazable result: %s" % (key, result["data"]))

    def add(self, stream):
        # Generate a key corresponding to this object.
        key = tangelo.util.generate_key(self.streams)

        # Log the object in the streaming table.
        self.streams[key] = stream

        # Create an object describing the logging of the generator object.
        result = {"stream_key": key}

        # Serialize it to JSON.
        return json.dumps(result)
<commit_msg>Disallowing all methods besides GET and DELETE.<commit_after>import json
import traceback

import cherrypy

import tangelo

class TangeloStream(object):
    def __init__(self):
        self.streams = {}

    @cherrypy.expose
    def default(self, key=None):
        # Construct a result object.
        result = {}

        # Perform the requested action: GET means to either return a list of
        # stream keys, or to run the keyed stream one step; DELETE means to
        # delete a keyed stream from the registry.
        method = cherrypy.request.method
        if method == "GET":
            if key is None:
                result = self.streams.keys()
            elif key not in self.streams:
                result["error"] = "Unknown key"
            else:
                # Grab the stream in preparation for running it.
                stream = self.streams[key]

                # Attempt to run the stream via its next() method - if this yields a
                # result, then continue; if the next() method raises StopIteration,
                # then there are no more results to retrieve; if any other exception
                # is raised, this is treated as an error.
                try:
                    result["data"] = stream.next()
                    result["finished"] = False
                except StopIteration:
                    result["finished"] = True
                    del self.streams[key]
                except:
                    del self.streams[key]
                    raise cherrypy.HTTPError("501 Error in Python Service",
                            "Caught exception while executing stream service keyed by %s:<br><pre>%s</pre>" % (key, traceback.format_exc()))

        elif method == "DELETE":
            if key is None:
                result["error"] = "No key specified"
            elif key not in self.streams:
                result["error"] = "Unknown key"
            else:
                del self.streams[key]
                result["data"] = "OK"

        else:
            # All other methods are illegal.
            raise cherrypy.HTTPError(405)

        # JSON-serialize the result and return it.
        try:
            return json.dumps(result)
        except TypeError:
            raise cherrypy.HTTPError("501 Bad Response from Python Service",
                    "The stream keyed by %s returned a non JSON-seriazable result: %s" % (key, result["data"]))

    def add(self, stream):
        # Generate a key corresponding to this object.
        key = tangelo.util.generate_key(self.streams)

        # Log the object in the streaming table.
        self.streams[key] = stream

        # Create an object describing the logging of the generator object.
        result = {"stream_key": key}

        # Serialize it to JSON.
        return json.dumps(result)

{'fraction_non_alphanumeric': 0.08098845598845598, 'fraction_numerical': 0.0027056277056277055, 'mean_word_length': 1.6633045148895294, 'pattern_counts': {'":': 6, '<': 9, '<?xml version=': 0, '>': 9, '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': '5184032', 'n_tokens_mistral': 1432, 'n_tokens_neox': 1353, 'n_words': 518}