<commit_before>'''
Created on 9.8.2013

@author: <NAME>
'''
from abc import abstractmethod
import json
import gearman

class Task:
    @abstractmethod
    def get_name(self):
        pass
    name = property(get_name)

    @abstractmethod
    def get_data(self):
        pass
    data = property(get_data)

class RawTask(Task):
    def __init__(self, name, data):
        self._name = name
        self._data = data

    def get_name(self):
        return self._name
    name = property(get_name)

    def get_data(self):
        return self._data
    data = property(get_data)

class TaskResult:
    @abstractmethod
    def get_task(self):
        pass
    task = property(get_task)

    @abstractmethod
    def get_result(self):
        pass
    result = property(get_result)

class RawTaskResult(TaskResult):
    def __init__(self, task, result):
        assert isinstance(task, Task)
        self._task = task
        self._result = result

    def get_task(self):
        return self._task

    def get_result(self, result):
        return self._result

class TaskDecorator:
    def __init__(self, task):
        self._task = task

class JsonTask(Task, TaskDecorator):
    def get_data(self):
        data = self._task.get_data(self)
        return json.dumps(data)

class SecuredTask(Task, TaskDecorator):
    def get_data(self):
        return {
            "data": self._task.data,
            "security": {
                "random_token": "",
                "hashed_token": ""
            }
        }

class TaskResultDecorator:
    def __init__(self, task_result):
        assert isinstance(task_result, TaskResult)
        self._task_result = task_result

class JsonTaskResult(TaskResult, TaskResultDecorator):
    def get_result(self):
        result = self._task_result.get_result()
        return json.loads(result)

class Service:
    @abstractmethod
    def _inner_call(self, data):
        pass

    def call(self, task, decorators = []):
        task = self.apply_task_decorators(task, decorators)
        data = task.data
        name = task.name
        result = self._inner_call(name, data)
        task_result = RawTaskResult(task, result)
        return self.apply_task_result_decorators(task_result, decorators)

    def apply_task_decorators(self, task, decorators):
        for d in decorators:
            if TaskDecorator in d.__bases__:
                task = d(task)

        return task

    def apply_task_result_decorators(self, task_result, decorators):
        for d in decorators:
            if TaskResultDecorator in d.__bases__:
                task_result = d(task_result)

        return task_result

class GearmanService(Service):
    def __init__(self, gearman_config, security_config):
        self._gearman_config = gearman_config
        self._security_config = security_config
        self._gearman_client = gearman.client.GearmanClient(self._gearman_config['HOST'])
        self._blocking_status = True

    def set_blocking(self, blocking_status):
        self._blocking_status = blocking_status

    def _inner_call(self, name, data):
        if data is None:
            data = "null"
        elif not (isinstance(data, unicode) or isinstance(data, str)):
            data = str(data)

        completed_job_request = self._gearman_client.submit_job(
            self._gearman_config['TASK_NAME'],
            json.dumps({
                'path': name,
                'data': data
            }),
            background = not self._blocking_status
        )

        if self._blocking_status:
            return completed_job_request.result

class WebService(Service):
    def __init__(self, web_config, security_config):
        self._web_config = web_config
        self._security_config = security_config
<commit_msg>Novy klient pre WS.<commit_after>'''
Created on 9.8.2013

@author: <NAME>
'''
from abc import abstractmethod
import hashlib
import json
import random
import urllib2

import gearman


def _random_string(length, allowed_characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'):
    return ''.join(random.choice(allowed_characters) for _ in range(length))

class Task:
    @abstractmethod
    def get_name(self):
        pass
    name = property(get_name)

    @abstractmethod
    def get_data(self):
        pass
    data = property(get_data)

class RawTask(Task):
    def __init__(self, name, data):
        self._name = name
        self._data = data

    def get_name(self):
        return self._name
    name = property(get_name)

    def get_data(self):
        return self._data
    data = property(get_data)

class TaskResult:
    @abstractmethod
    def get_task(self):
        pass
    task = property(get_task)

    @abstractmethod
    def get_result(self):
        pass
    result = property(get_result)

class RawTaskResult(TaskResult):
    def __init__(self, task, result):
        assert isinstance(task, Task)
        self._task = task
        self._result = result

    def get_task(self):
        return self._task

    def get_result(self):
        return self._result

class TaskDecorator(Task):
    def __init__(self, task):
        self._task = task

    def get_name(self):
        return self._task.get_name()

    def get_data(self):
        return self._task.get_data()

class JsonTask(Task, TaskDecorator):
    def get_name(self):
        return TaskDecorator.get_name(self)

    def get_data(self):
        data = self._task.get_data()
        return json.dumps(data)

class SecuredTask(Task, TaskDecorator):
    def get_name(self):
        return TaskDecorator.get_name(self)

    def set_asl(self, asl):
        self._asl = asl

    def get_data(self):
        random_token = _random_string(16)
        return {
            "data": self._task.get_data(),
            "security": {
                "random_token": random_token,
                "hashed_token": hashlib.sha1(random_token + self._asl.get_secure_token()).hexdigest().upper()
            }
        }

class TaskResultDecorator:
    def __init__(self, task_result):
        assert isinstance(task_result, TaskResult)
        self._task_result = task_result

class JsonTaskResult(TaskResult, TaskResultDecorator):
    def get_result(self):
        result = self._task_result.get_result()
        return json.loads(result)

class ErrorTaskResult(TaskResult, TaskResultDecorator):
    def get_complete_result(self):
        result = self._task_result.get_result()
        return result

    def get_result(self):
        result = self._task_result.get_result()
        return json.loads(result['data'])

    def is_error(self):
        result = self._task_result.get_result()
        return True if 'error'in result else False

    def get_error(self):
        return self._task_result.get_result()['error']

class Service:
    @abstractmethod
    def _inner_call(self, data):
        pass

    def call(self, task, decorators = []):
        task = self.apply_task_decorators(task, decorators)

        data = task.get_data()
        name = task.get_name()

        result = self._inner_call(name, data)
        task_result = RawTaskResult(task, result)
        return self.apply_task_result_decorators(task_result, decorators)

    def apply_task_decorators(self, task, decorators):
        for d in decorators:
            if TaskDecorator in d.__bases__:
                task = d(task)
                if hasattr(task, 'set_asl'):
                    task.set_asl(self)

        return task

    def apply_task_result_decorators(self, task_result, decorators):
        for d in decorators:
            if TaskResultDecorator in d.__bases__:
                task_result = d(task_result)

        return task_result

    def get_secure_token(self):
        return self._security_config['SECURITY_TOKEN']

class GearmanService(Service):
    def __init__(self, gearman_config, security_config):
        self._gearman_config = gearman_config
        self._security_config = security_config
        self._gearman_client = gearman.client.GearmanClient(self._gearman_config['HOST'])
        self._blocking_status = True

    def set_blocking(self, blocking_status):
        self._blocking_status = blocking_status

    def _inner_call(self, name, data):
        if data is None:
            data = "null"
        elif not (isinstance(data, unicode) or isinstance(data, str)):
            data = str(data)

        completed_job_request = self._gearman_client.submit_job(
            self._gearman_config['TASK_NAME'],
            json.dumps({
                'path': name,
                'data': data
            }),
            background = not self._blocking_status
        )

        if self._blocking_status:
            return completed_job_request.result

class WebService(Service):
    def __init__(self, web_config, security_config):
        self._web_config = web_config
        self._security_config = security_config
        self._service_layer_url = self._web_config['SERVICE_LAYER_URL']

    def get_service_layer_url(self):
        return self._service_layer_url

    def _inner_call(self, name, data):
        if data is None:
            data = "null"
        elif not (isinstance(data, unicode) or isinstance(data, str)):
            data = str(data)

        req = urllib2.Request(self._service_layer_url + name, data, {'Content-Type': 'application/json'})
        f = urllib2.urlopen(req)
        returned_data = f.read()
        f.close()
        return returned_data

{'fraction_non_alphanumeric': 0.085897697167312, 'fraction_numerical': 0.0028530670470756064, 'mean_word_length': 2.2243758212877793, 'pattern_counts': {'":': 8, '<': 5, '<?xml version=': 0, '>': 5, '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': '3714739', 'n_tokens_mistral': 3058, 'n_tokens_neox': 2793, 'n_words': 627}