-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethod.py
More file actions
84 lines (68 loc) · 2.8 KB
/
method.py
File metadata and controls
84 lines (68 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
###################################################################################################
#
# Caffa
# Copyright (C) Kontur AS
#
# GNU Lesser General Public License Usage
# This library is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This library 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 Lesser General Public License at <<http:#www.gnu.org/licenses/lgpl-2.1.html>>
# for more details.
#
import json
import logging
class Method:
_log = logging.getLogger("caffa-method")
_labelled_arguments = {}
_positional_arguments = {}
def __init__(self, self_object):
self._self_object = self_object
def __call__(self, *args, **kwargs):
from .object import Object
arguments = {}
if len(kwargs.items()) > 0:
arguments["labelledArguments"] = self.__class__._labelled_arguments[
self.__class__.__name__
]
for key, value in kwargs.items():
if isinstance(value, Object):
value = value.to_dict()
arguments["labelledArguments"][key] = value
elif len(args) > 0:
arguments["positionalArguments"] = self.__class__._positional_arguments[
self.__class__.__name__
]
for i, value in enumerate(args):
if isinstance(value, Object):
value = value.to_dict()
arguments["positionalArguments"][i] = value
return self._self_object.execute(self, arguments)
@classmethod
def static_name(cls):
return cls.__name__
def name(self):
return self.__class__.__name__
def make_read_lambda(property_name):
return lambda self: self_self_object.get(property_name)
def make_write_lambda(property_name):
return lambda self, value: self.set(property_name, value)
def create_method_class(name, schema):
def __init__(self, self_object):
return Method.__init__(self, self_object)
newclass = type(name, (Method,), {"__init__": __init__})
newclass._labelled_arguments[name] = {}
newclass._positional_arguments[name] = []
if "labelledArguments" in schema:
for argument_name in schema["labelledArguments"]["properties"]:
newclass._labelled_arguments[name][argument_name] = None
if "positionalArguments" in schema:
for i, entry in enumerate(schema["positionalArguments"]["items"]):
newclass._positional_arguments[name].append(None)
return newclass