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

###########################################################
# © 2011 Daniel 'grindhold' Brendle and Team
#
# This file is part of Skarphed.
#
# Skarphed 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.
#
# Skarphed 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 Skarphed. 
# If not, see http://www.gnu.org/licenses/.
###########################################################


from data.Generic import ObjectStore, GenericSkarphedObject
from data.Instance import Instance, InstanceType
import net.HTTPRpc

from Users import Users
from Modules import Modules
from Roles import Roles
from Sites import Sites
from Template import Template
from Operation import OperationManager, OperationDaemon
from View import Views

import gobject
from threading import Thread
import base64
import random
import os
from time import sleep

from common.errors import RepositoryException
from common.enums import ActivityType

#from glue.threads import KillableThread

class AbstractInstaller(GenericSkarphedObject):
    class InstallThread(Thread):
        def __init__(self, installer):
            Thread.__init__(self)
            self.installer = installer

        def run(self):
            self.installer.execute_installation()


    def __init__(self,data,server):
        GenericSkarphedObject.__init__(self)
        self.server=server
        self.data  = data
        self.domain = ""
        self.installationId = int(random.random()*1000)
        self.BUILDPATH = os.path.join("/","tmp","scvinst"+str(self.installationId))
        self.installThread = self.InstallThread(self)
        self.status = 0

    def execute_installation(self):
        pass #To be implemented in realization

    def addInstanceToServer(self):
        if self.domain != "":
            self.server.createInstance(InstanceType("skarphed","Skarphed"), "http://"+self.domain, "root", "root")
        else:
            self.server.createInstance(InstanceType("skarphed","Skarphed"), "http://"+self.server.getIp(), "root", "root")
        self.destroy()

    def getName(self):
        return "Skarphed Installer"

    def getStatus(self):
        return self.status

    def install(self):
        self.installThread.start()

class AbstractDestroyer(GenericSkarphedObject):
    class DestroyThread(Thread):
        def __init__(self, destroyer):
            Thread.__init__(self)
            self.destroyer = destroyer

        def run(self):
            self.destroyer.execute_destruction()

    def __init__(self, instanceid, instance):
        GenericSkarphedObject.__init__(self)
        self.instanceid = instanceid
        self.instance = instance
        self.status = 0
        self.destroyThread = self.DestroyThread(self)

    def execute_destruction(self):
        pass

    def removeInstanceFromServer(self):
        server = self.instance.getServer()
        server.removeInstance(self.instance)
        self.destroy()

    def getName(self):
        return "Skarphed Destroyer"

    def getStatus(self):
        return self.status

    def takedown(self):
        self.destroyThread.start()

class PokeThread(Thread):
    """
    This thread is repsonsible for poking the skarphed-core
    to get information about whether the skarphed-core has
    new information to display. For example when another
    user that operates on that server made some changes
    """
    POKEYNESS_MAX = 10
    POKEYNESS_MIN = 1
    def __init__(self, skarphed):
        Thread.__init__(self)
        self.pokeyness = 1.0
        self.last_amount = 0
        self.skarphed = skarphed
        self.lock = False
        if PokeThread.POKEYNESS_MAX < PokeThread.POKEYNESS_MIN:
            raise Exception("Pokeyness MAX mustbe greater or equal MIN")

    
    def run(self):
        def poke_callback(data):
            amount = data["amount"]
            activity_types = data["activity_types"]
            delta = self.last_amount-amount
            self.pokeyness -= 0.2
            if self.pokeyness < PokeThread.POKEYNESS_MIN:
                self.pokeyness = PokeThread.POKEYNESS_MIN
            if delta > 2:
                self.pokeyness += 2
                if self.pokeyness > PokeThread.POKEYNESS_MAX:
                    self.pokeyness = PokeThread.POKEYNESS_MAX


            to_execute = []

            if ActivityType.USER in activity_types:
                to_execute.append(self.skarphed.getUsers().refresh)
                
            if ActivityType.MODULE in activity_types:
                to_execute.append(self.skarphed.getModules().refresh)

            if ActivityType.WIDGET in activity_types:
                for module in self.skarphed.getModules().getAllModules():
                    to_execute.append(module.loadWidgets)

            if ActivityType.ROLE in activity_types:
                to_execute.append(self.skarphed.getRoles().refresh)

            if ActivityType.REPOSITORY in activity_types:
                to_execute.append(self.skarphed.getRepository)

            if ActivityType.PERMISSION in activity_types:
                if self.skarphed.getRoles().refresh not in to_execute:
                    to_execute.append(self.skarphed.getRoles().refresh)


            if ActivityType.MENU in activity_types:
                for site in self.skarphed.getSites().getSites():
                    to_execute.append(site.loadMenus)

            if ActivityType.VIEW in activity_types:
                views = self.skarphed.getViews().getViews()
                for view in views:
                    if view.isFullyLoaded():
                        to_execute.append(view.loadFull)
                to_execute.append(views.refresh)

            if ActivityType.REPOSITORY in activity_types:
                pass

            if ActivityType.TEMPLATE in activity_types:
                to_execute.append(self.skarphed.getTemplate().load)

            for method in to_execute:
                gobject.idle_add(method)

            self.lock = False

        while True:
            if self.skarphed.getApplication().quitrequest:
                break
            sleep(1/self.pokeyness*3)
            self.skarphed.doRPCCall(poke_callback, "poke")
            self.lock = True
            while self.lock:
                sleep(0.2)<commit_msg>fixed method callback method name<commit_after>#!/usr/bin/python
#-*- coding: utf-8 -*-

###########################################################
# © 2011 Daniel 'grindhold' Brendle and Team
#
# This file is part of Skarphed.
#
# Skarphed 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.
#
# Skarphed 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 Skarphed. 
# If not, see http://www.gnu.org/licenses/.
###########################################################


from data.Generic import ObjectStore, GenericSkarphedObject
from data.Instance import Instance, InstanceType
import net.HTTPRpc

from Users import Users
from Modules import Modules
from Roles import Roles
from Sites import Sites
from Template import Template
from Operation import OperationManager, OperationDaemon
from View import Views

import gobject
from threading import Thread
import base64
import random
import os
from time import sleep

from common.errors import RepositoryException
from common.enums import ActivityType

#from glue.threads import KillableThread

class AbstractInstaller(GenericSkarphedObject):
    class InstallThread(Thread):
        def __init__(self, installer):
            Thread.__init__(self)
            self.installer = installer

        def run(self):
            self.installer.execute_installation()


    def __init__(self,data,server):
        GenericSkarphedObject.__init__(self)
        self.server=server
        self.data  = data
        self.domain = ""
        self.installationId = int(random.random()*1000)
        self.BUILDPATH = os.path.join("/","tmp","scvinst"+str(self.installationId))
        self.installThread = self.InstallThread(self)
        self.status = 0

    def execute_installation(self):
        pass #To be implemented in realization

    def addInstanceToServer(self):
        if self.domain != "":
            self.server.createInstance(InstanceType("skarphed","Skarphed"), "http://"+self.domain, "root", "root")
        else:
            self.server.createInstance(InstanceType("skarphed","Skarphed"), "http://"+self.server.getIp(), "root", "root")
        self.destroy()

    def getName(self):
        return "Skarphed Installer"

    def getStatus(self):
        return self.status

    def install(self):
        self.installThread.start()

class AbstractDestroyer(GenericSkarphedObject):
    class DestroyThread(Thread):
        def __init__(self, destroyer):
            Thread.__init__(self)
            self.destroyer = destroyer

        def run(self):
            self.destroyer.execute_destruction()

    def __init__(self, instanceid, instance):
        GenericSkarphedObject.__init__(self)
        self.instanceid = instanceid
        self.instance = instance
        self.status = 0
        self.destroyThread = self.DestroyThread(self)

    def execute_destruction(self):
        pass

    def removeInstanceFromServer(self):
        server = self.instance.getServer()
        server.removeInstance(self.instance)
        self.destroy()

    def getName(self):
        return "Skarphed Destroyer"

    def getStatus(self):
        return self.status

    def takedown(self):
        self.destroyThread.start()

class PokeThread(Thread):
    """
    This thread is repsonsible for poking the skarphed-core
    to get information about whether the skarphed-core has
    new information to display. For example when another
    user that operates on that server made some changes
    """
    POKEYNESS_MAX = 10
    POKEYNESS_MIN = 1
    def __init__(self, skarphed):
        Thread.__init__(self)
        self.pokeyness = 1.0
        self.last_amount = 0
        self.skarphed = skarphed
        self.lock = False
        if PokeThread.POKEYNESS_MAX < PokeThread.POKEYNESS_MIN:
            raise Exception("Pokeyness MAX mustbe greater or equal MIN")

    
    def run(self):
        def poke_callback(data):
            amount = data["amount"]
            activity_types = data["activity_types"]
            delta = self.last_amount-amount
            self.pokeyness -= 0.2
            if self.pokeyness < PokeThread.POKEYNESS_MIN:
                self.pokeyness = PokeThread.POKEYNESS_MIN
            if delta > 2:
                self.pokeyness += 2
                if self.pokeyness > PokeThread.POKEYNESS_MAX:
                    self.pokeyness = PokeThread.POKEYNESS_MAX


            to_execute = []

            if ActivityType.USER in activity_types:
                to_execute.append(self.skarphed.getUsers().refresh)
                
            if ActivityType.MODULE in activity_types:
                to_execute.append(self.skarphed.getModules().refresh)

            if ActivityType.WIDGET in activity_types:
                for module in self.skarphed.getModules().getAllModules():
                    to_execute.append(module.loadWidgets)

            if ActivityType.ROLE in activity_types:
                to_execute.append(self.skarphed.getRoles().refresh)

            if ActivityType.REPOSITORY in activity_types:
                to_execute.append(self.skarphed.getRepository)

            if ActivityType.PERMISSION in activity_types:
                if self.skarphed.getRoles().refresh not in to_execute:
                    to_execute.append(self.skarphed.getRoles().refresh)


            if ActivityType.MENU in activity_types:
                for site in self.skarphed.getSites().getSites():
                    to_execute.append(site.loadMenus)

            if ActivityType.VIEW in activity_types:
                views = self.skarphed.getViews().getViews()
                for view in views:
                    if view.isFullyLoaded():
                        to_execute.append(view.loadFull)
                to_execute.append(self.skarphed.getViews().refresh)

            if ActivityType.REPOSITORY in activity_types:
                pass

            if ActivityType.TEMPLATE in activity_types:
                to_execute.append(self.skarphed.getTemplate().load)

            for method in to_execute:
                gobject.idle_add(method)

            self.lock = False

        while True:
            if self.skarphed.getApplication().quitrequest:
                break
            sleep(1/self.pokeyness*3)
            self.skarphed.doRPCCall(poke_callback, "poke")
            self.lock = True
            while self.lock:
                sleep(0.2)
{'fraction_non_alphanumeric': 0.09618354290756673, 'fraction_numerical': 0.0041179498492536215, 'mean_word_length': 2.4025519139354516, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7230202', 'n_tokens_mistral': 3776, 'n_tokens_neox': 3604, 'n_words': 953}