libs redesign
This commit is contained in:
		
							
								
								
									
										235
									
								
								trunk/opal.py
									
									
									
									
									
								
							
							
						
						
									
										235
									
								
								trunk/opal.py
									
									
									
									
									
								
							@@ -1,229 +1,26 @@
 | 
			
		||||
#!/usr/bin/env python
 | 
			
		||||
#-------------------------------------------------------------------------------
 | 
			
		||||
# Name:         opal.py
 | 
			
		||||
# Purpose:
 | 
			
		||||
#
 | 
			
		||||
# Author:       Anton Vakhrushev
 | 
			
		||||
#
 | 
			
		||||
# Created:      14.03.2012
 | 
			
		||||
# Copyright:    (c) Anton Vakhrushev 2012
 | 
			
		||||
# Licence:      LGPL
 | 
			
		||||
#-------------------------------------------------------------------------------
 | 
			
		||||
#!/usr/bin/env python#!/usr/bin/env python
 | 
			
		||||
#! coding: utf-8
 | 
			
		||||
 | 
			
		||||
import subprocess
 | 
			
		||||
import json
 | 
			
		||||
import os, sys
 | 
			
		||||
import datetime
 | 
			
		||||
 | 
			
		||||
#-------------------------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
def GenerateId(data):
 | 
			
		||||
    import hashlib
 | 
			
		||||
    title  = data['title']
 | 
			
		||||
    author = data['author']
 | 
			
		||||
    id = hashlib.md5(title + author).hexdigest()
 | 
			
		||||
    return id
 | 
			
		||||
 | 
			
		||||
#-------------------------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
class LocalServer:
 | 
			
		||||
    """
 | 
			
		||||
    """
 | 
			
		||||
    def __init__(self):
 | 
			
		||||
        self.max_run = 2
 | 
			
		||||
        self.cur_run = 0
 | 
			
		||||
 | 
			
		||||
        self.task_descrs = []
 | 
			
		||||
        self.task_queue = []
 | 
			
		||||
        self.log = None
 | 
			
		||||
 | 
			
		||||
        self.Init()
 | 
			
		||||
 | 
			
		||||
    def Init(self):
 | 
			
		||||
        self.log = open('log.txt', 'w')
 | 
			
		||||
        self.WriteToLog('local server initialized')
 | 
			
		||||
 | 
			
		||||
    def Close(self):
 | 
			
		||||
        self.WriteToLog('local server closed\n')
 | 
			
		||||
 | 
			
		||||
    def __del__(self):
 | 
			
		||||
        self.Close()
 | 
			
		||||
 | 
			
		||||
    def WriteToLog(self, msg):
 | 
			
		||||
        tm = str(datetime.datetime.now())
 | 
			
		||||
        msg = tm + '  ' + msg
 | 
			
		||||
        self.log.write(msg + '\n')
 | 
			
		||||
        print msg
 | 
			
		||||
 | 
			
		||||
    def Start(self):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    def Stop(self):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    def TestTaskData(self, data):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    def LoadTasksDescriptions(self, source = 'tasks.conf'):
 | 
			
		||||
        """
 | 
			
		||||
        """
 | 
			
		||||
        self.task_descrs = []
 | 
			
		||||
 | 
			
		||||
        self.WriteToLog('tasks interrogation starts')
 | 
			
		||||
        for line in open(source, 'r'):
 | 
			
		||||
            try:
 | 
			
		||||
                # нормализуем указанный путь
 | 
			
		||||
                line = os.path.normpath(line)
 | 
			
		||||
                # считываем данные через shell (важно для скриптовых языков)
 | 
			
		||||
                textdata = subprocess.check_output([line, '-i'], shell = True)
 | 
			
		||||
                # загружаем данные описания задачи
 | 
			
		||||
                data = json.loads(textdata)
 | 
			
		||||
                # провряем их на корректность
 | 
			
		||||
                self.TestTaskData(data)
 | 
			
		||||
                # пакуем все в объект-описание задачи
 | 
			
		||||
                task_descr = TaskDescription(self, line, data)
 | 
			
		||||
                # добавляем в список описаний
 | 
			
		||||
                self.task_descrs.append(task_descr)
 | 
			
		||||
                self.WriteToLog('Task from "{}" asked'.format(line))
 | 
			
		||||
            except IOError, e:
 | 
			
		||||
                self.WriteToLog('file "{}" not found'.format(line))
 | 
			
		||||
            except subprocess.CalledProcessError, e:
 | 
			
		||||
                self.WriteToLog('file "{}" not opened, error {} (msg: {})'.format(line, e, e.output))
 | 
			
		||||
            except ValueError, e:
 | 
			
		||||
                self.WriteToLog('file "{}" not opened, error "{}")'.format(line, e))
 | 
			
		||||
 | 
			
		||||
    def GetTasksDescriptions(self):
 | 
			
		||||
        """
 | 
			
		||||
        Return list with task descriptions
 | 
			
		||||
        """
 | 
			
		||||
        return self.task_descrs
 | 
			
		||||
 | 
			
		||||
    def GetTaskCount(self):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    def GetTask(self, index):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    def AddTask(self, task):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
#-------------------------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
class TaskDescription:
 | 
			
		||||
    """
 | 
			
		||||
    Description of the task. Task runs on server.
 | 
			
		||||
    """
 | 
			
		||||
    def __init__(self, server, execpath, data):
 | 
			
		||||
        """
 | 
			
		||||
        ``server`` is owner of task process
 | 
			
		||||
 | 
			
		||||
        ``execpath`` - path to task executable
 | 
			
		||||
 | 
			
		||||
        ``data`` is parsed data presentation about models, methods
 | 
			
		||||
        and meta information
 | 
			
		||||
        """
 | 
			
		||||
        self.server     = server
 | 
			
		||||
        self.execpath   = execpath
 | 
			
		||||
        self.data       = data
 | 
			
		||||
        self.models     = []
 | 
			
		||||
        for label, data in self.data['models'].iteritems():
 | 
			
		||||
            self.models.append(ModelDescription(self, label, data))
 | 
			
		||||
 | 
			
		||||
    def GetModelsDescriptions(self):
 | 
			
		||||
        return self.models
 | 
			
		||||
 | 
			
		||||
#-------------------------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
class Parameter:
 | 
			
		||||
    def __init__(self, paramdescr):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
def DoDataParametrization(objectdata):
 | 
			
		||||
    data = objectdata['data']
 | 
			
		||||
    for label in data:
 | 
			
		||||
        par = Parameter(data[label])
 | 
			
		||||
        data[label] = par
 | 
			
		||||
 | 
			
		||||
class ObjectDescription:
 | 
			
		||||
    def __init__(self, parentdescr, label, data):
 | 
			
		||||
        self.parentdescr = parentdescr
 | 
			
		||||
        self.label       = label
 | 
			
		||||
        self.data        = data
 | 
			
		||||
        DoDataParametrization(self.data)
 | 
			
		||||
 | 
			
		||||
    def GetLabel(self):
 | 
			
		||||
        return self.label
 | 
			
		||||
 | 
			
		||||
    def GetTitle(self):
 | 
			
		||||
        return self.data.get('title', self.label)
 | 
			
		||||
 | 
			
		||||
    def GetAuthor(self):
 | 
			
		||||
        return self.data.get('author', 'Unknown')
 | 
			
		||||
 | 
			
		||||
    def GetId(self):
 | 
			
		||||
        return None
 | 
			
		||||
 | 
			
		||||
class ModelDescription(ObjectDescription):
 | 
			
		||||
    def __init__(self, taskdescr, label, data):
 | 
			
		||||
        ObjectDescription.__init__(self, taskdescr, label, data)
 | 
			
		||||
        self.methods = []
 | 
			
		||||
        for label, data in self.data['methods'].iteritems():
 | 
			
		||||
            self.methods.append(MethodDescription(self, label, data))
 | 
			
		||||
 | 
			
		||||
    def GetMethodsDescriptions(self):
 | 
			
		||||
        return self.methods
 | 
			
		||||
 | 
			
		||||
class MethodDescription(ObjectDescription):
 | 
			
		||||
    def __init__(self, modeldescr, label, data):
 | 
			
		||||
        ObjectDescription.__init__(self, modeldescr, label, data)
 | 
			
		||||
 | 
			
		||||
#-------------------------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
class ObjectDefinition:
 | 
			
		||||
    def __init__(self, objectdescr):
 | 
			
		||||
        self.descr = objectdescr
 | 
			
		||||
        self.params = {}
 | 
			
		||||
 | 
			
		||||
    def GetParameter(self, label):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    def SetParameter(self, label, value):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
class ModelDefinition(ObjectDefinition):
 | 
			
		||||
    def __init__(self, modeldescr):
 | 
			
		||||
        ObjectDefinition(self, modeldescr)
 | 
			
		||||
 | 
			
		||||
class MethodDefinition(ObjectDefinition):
 | 
			
		||||
    def __init__(self, methoddescr):
 | 
			
		||||
        ObjectDefinition(self, methoddescr)
 | 
			
		||||
        self.taskjob = None
 | 
			
		||||
 | 
			
		||||
#-------------------------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
class Taskjob:
 | 
			
		||||
    def __init__(self, data):
 | 
			
		||||
        self.data = data
 | 
			
		||||
 | 
			
		||||
    def Start(self):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    def Stop(self):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    def Pause(self):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    def Status(self):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
#-------------------------------------------------------------------------------
 | 
			
		||||
import server
 | 
			
		||||
import task
 | 
			
		||||
 | 
			
		||||
def main():
 | 
			
		||||
    import pprint
 | 
			
		||||
    s = LocalServer()
 | 
			
		||||
    s = server.LocalServer()
 | 
			
		||||
    s.LoadTasksDescriptions()
 | 
			
		||||
    ds = s.GetTasksDescriptions()
 | 
			
		||||
    ms = ds[0].GetModelsDescriptions()
 | 
			
		||||
    for m in ms:
 | 
			
		||||
        print m.GetTitle()
 | 
			
		||||
        print m.GetLabel()
 | 
			
		||||
        print m.GetAuthor()
 | 
			
		||||
        print pprint.pformat(m.data, indent = 2)
 | 
			
		||||
    ds = s.GetTasksDescriptions()[0]
 | 
			
		||||
 | 
			
		||||
    mds = ms[0]
 | 
			
		||||
    mdf = 0
 | 
			
		||||
    pprint.pprint(ds.data)
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    main()
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										132
									
								
								trunk/server.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										132
									
								
								trunk/server.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,132 @@
 | 
			
		||||
#-------------------------------------------------------------------------------
 | 
			
		||||
# Name:         server.py
 | 
			
		||||
# Purpose:
 | 
			
		||||
#
 | 
			
		||||
# Author:       Anton Vakhrushev
 | 
			
		||||
#
 | 
			
		||||
# Created:      14.03.2012
 | 
			
		||||
# Copyright:    (c) Anton Vakhrushev 2012
 | 
			
		||||
# Licence:      LGPL
 | 
			
		||||
#-------------------------------------------------------------------------------
 | 
			
		||||
#!/usr/bin/env python
 | 
			
		||||
# -*- coding: UTF-8 -*-
 | 
			
		||||
 | 
			
		||||
import subprocess
 | 
			
		||||
import json
 | 
			
		||||
import os, sys
 | 
			
		||||
import datetime
 | 
			
		||||
 | 
			
		||||
import task
 | 
			
		||||
 | 
			
		||||
def GenerateId(data):
 | 
			
		||||
    import hashlib
 | 
			
		||||
    title  = data['title']
 | 
			
		||||
    author = data['author']
 | 
			
		||||
    id = hashlib.md5(title + author).hexdigest()
 | 
			
		||||
    return id
 | 
			
		||||
 | 
			
		||||
class LocalServer:
 | 
			
		||||
    """
 | 
			
		||||
    """
 | 
			
		||||
    def __init__(self):
 | 
			
		||||
        self.max_run = 2
 | 
			
		||||
        self.cur_run = 0
 | 
			
		||||
 | 
			
		||||
        self.task_descrs = []
 | 
			
		||||
        self.task_queue = []
 | 
			
		||||
        self.log = None
 | 
			
		||||
 | 
			
		||||
        self.Init()
 | 
			
		||||
 | 
			
		||||
    def Init(self):
 | 
			
		||||
        self.log = open('log.txt', 'w')
 | 
			
		||||
        self.WriteToLog('local server initialized')
 | 
			
		||||
 | 
			
		||||
    def Close(self):
 | 
			
		||||
        self.WriteToLog('local server closed\n')
 | 
			
		||||
 | 
			
		||||
    def __del__(self):
 | 
			
		||||
        self.Close()
 | 
			
		||||
 | 
			
		||||
    def WriteToLog(self, msg):
 | 
			
		||||
        tm = str(datetime.datetime.now())
 | 
			
		||||
        msg = tm + '  ' + msg
 | 
			
		||||
        self.log.write(msg + '\n')
 | 
			
		||||
        print msg
 | 
			
		||||
 | 
			
		||||
    def Start(self):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    def Stop(self):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    def TestTaskData(self, data):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    def LoadTasksDescriptions(self, source = 'tasks.conf'):
 | 
			
		||||
        """
 | 
			
		||||
        """
 | 
			
		||||
        self.task_descrs = []
 | 
			
		||||
 | 
			
		||||
        self.WriteToLog('tasks interrogation starts')
 | 
			
		||||
        for line in open(source, 'r'):
 | 
			
		||||
            try:
 | 
			
		||||
                # нормализуем указанный путь
 | 
			
		||||
                line = os.path.normpath(line)
 | 
			
		||||
                # считываем данные через shell (важно для скриптовых языков)
 | 
			
		||||
                textdata = subprocess.check_output([line, '-i'], shell = True)
 | 
			
		||||
                # загружаем данные описания задачи
 | 
			
		||||
                data = json.loads(textdata)
 | 
			
		||||
                # провряем их на корректность
 | 
			
		||||
                self.TestTaskData(data)
 | 
			
		||||
                # пакуем все в объект-описание задачи
 | 
			
		||||
                task_descr = task.TaskDescription(self, line, data)
 | 
			
		||||
                # добавляем в список описаний
 | 
			
		||||
                self.task_descrs.append(task_descr)
 | 
			
		||||
                self.WriteToLog('Task from "{}" asked'.format(line))
 | 
			
		||||
            except IOError, e:
 | 
			
		||||
                self.WriteToLog('file "{}" not found'.format(line))
 | 
			
		||||
            except subprocess.CalledProcessError, e:
 | 
			
		||||
                self.WriteToLog('file "{}" not opened, error {} (msg: {})'.format(line, e, e.output))
 | 
			
		||||
            except ValueError, e:
 | 
			
		||||
                self.WriteToLog('file "{}" not opened, error "{}")'.format(line, e))
 | 
			
		||||
 | 
			
		||||
    def GetTasksDescriptions(self):
 | 
			
		||||
        """
 | 
			
		||||
        Return list with task descriptions
 | 
			
		||||
        """
 | 
			
		||||
        return self.task_descrs
 | 
			
		||||
 | 
			
		||||
    def GetTaskCount(self):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    def GetTask(self, index):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    def AddTask(self, task):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
#-------------------------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
class Taskjob:
 | 
			
		||||
    def __init__(self, data):
 | 
			
		||||
        self.data = data
 | 
			
		||||
 | 
			
		||||
    def Start(self):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    def Stop(self):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    def Pause(self):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    def Status(self):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def main():
 | 
			
		||||
    pass
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    main()
 | 
			
		||||
							
								
								
									
										87
									
								
								trunk/task.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										87
									
								
								trunk/task.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,87 @@
 | 
			
		||||
#-------------------------------------------------------------------------------
 | 
			
		||||
# Name:         task.py
 | 
			
		||||
# Purpose:
 | 
			
		||||
#
 | 
			
		||||
# Author:       Anton Vakhrushev
 | 
			
		||||
#
 | 
			
		||||
# Created:      14.03.2012
 | 
			
		||||
# Copyright:    (c) Anton Vakhrushev 2012
 | 
			
		||||
# Licence:      LGPL
 | 
			
		||||
#-------------------------------------------------------------------------------
 | 
			
		||||
#!/usr/bin/env python
 | 
			
		||||
# -*- coding: UTF-8 -*-
 | 
			
		||||
 | 
			
		||||
class TaskDescription:
 | 
			
		||||
    """
 | 
			
		||||
    Description of the task. Task runs on server.
 | 
			
		||||
    """
 | 
			
		||||
    def __init__(self, server, execpath, data):
 | 
			
		||||
        """
 | 
			
		||||
        ``server`` is owner of task process
 | 
			
		||||
 | 
			
		||||
        ``execpath`` - path to task executable
 | 
			
		||||
 | 
			
		||||
        ``data`` is parsed data presentation about models, methods
 | 
			
		||||
        and meta information
 | 
			
		||||
        """
 | 
			
		||||
        self.server     = server
 | 
			
		||||
        self.execpath   = execpath
 | 
			
		||||
        self.data       = data
 | 
			
		||||
        self.models     = []
 | 
			
		||||
        for label, data in self.data['models'].iteritems():
 | 
			
		||||
            self.models.append(DataDescription(self, label, data))
 | 
			
		||||
 | 
			
		||||
    def GetModelsDescriptions(self):
 | 
			
		||||
        return self.models
 | 
			
		||||
 | 
			
		||||
#-------------------------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
class Parameter:
 | 
			
		||||
    def __init__(self, paramdescr):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
def DoDataParametrization(objectdata):
 | 
			
		||||
    data = objectdata['data']
 | 
			
		||||
    for label in data:
 | 
			
		||||
        par = Parameter(data[label])
 | 
			
		||||
        data[label] = par
 | 
			
		||||
 | 
			
		||||
class DataDescription:
 | 
			
		||||
    def __init__(self, parentdescr, label, data):
 | 
			
		||||
        self.parentdescr = parentdescr
 | 
			
		||||
        self.label       = label
 | 
			
		||||
        self.data        = data
 | 
			
		||||
 | 
			
		||||
    def GetLabel(self):
 | 
			
		||||
        return self.label
 | 
			
		||||
 | 
			
		||||
    def GetTitle(self):
 | 
			
		||||
        return self.data.get('title', self.label)
 | 
			
		||||
 | 
			
		||||
    def GetAuthor(self):
 | 
			
		||||
        return self.data.get('author', 'Unknown')
 | 
			
		||||
 | 
			
		||||
    def GetId(self):
 | 
			
		||||
        return None
 | 
			
		||||
 | 
			
		||||
#-------------------------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
class DataDefinition:
 | 
			
		||||
    def __init__(self, objectdescr):
 | 
			
		||||
        self.descr = objectdescr
 | 
			
		||||
        self.params = {}
 | 
			
		||||
        self.taskjob = None
 | 
			
		||||
 | 
			
		||||
    def GetParameter(self, label):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    def SetParameter(self, label, value):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
#-------------------------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
def main():
 | 
			
		||||
    pass
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    main()
 | 
			
		||||
@@ -6,11 +6,11 @@
 | 
			
		||||
 | 
			
		||||
        "simpleexample": {
 | 
			
		||||
 | 
			
		||||
            "title": "Simple example model",
 | 
			
		||||
            "title": "Simple model for example",
 | 
			
		||||
            "author": "Anton Vakhrushev",
 | 
			
		||||
            "date": "2012-03-08",
 | 
			
		||||
 | 
			
		||||
            "data": {
 | 
			
		||||
            "params": {
 | 
			
		||||
 | 
			
		||||
                "x": {
 | 
			
		||||
                    "type":     "int",
 | 
			
		||||
@@ -20,24 +20,14 @@
 | 
			
		||||
                "u": {
 | 
			
		||||
                    "type":     "double",
 | 
			
		||||
                    "default":  3.14
 | 
			
		||||
                }
 | 
			
		||||
                },
 | 
			
		||||
 | 
			
		||||
            "methods": {
 | 
			
		||||
            
 | 
			
		||||
                "default": {
 | 
			
		||||
                    "title": "Default method",
 | 
			
		||||
                    "author": "Anton Vakhrushev",
 | 
			
		||||
                    "data": {
 | 
			
		||||
                        "p": {
 | 
			
		||||
                "n": {
 | 
			
		||||
                    "type":     "int",
 | 
			
		||||
                            "default":  20
 | 
			
		||||
                    "default":  1000
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
            
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user