directives.py 8.99 KB
Newer Older
1
2
3
4
5
from docutils.parsers.rst import directives, Directive
from docutils.parsers.rst.directives.parts import Contents
from docutils.statemachine import StringList
from docutils import io, nodes

6
7
from . import json

8
###############################################################################
9
# Directive's options converter
10

11
def csv_list(items_type=str):
12
13
    """ Comma separated values as a list. """

14
15
16
    return lambda s: [items_type(value.strip()) for value in s.split(',')]

def multiline_list(items_type=str):
17
    """ List formatted as a multi-line string """
18
19

    return lambda s: list(map(items_type, s.splitlines()))
20
21
22
23
24


###############################################################################
# Nodes

25
26
class region(nodes.General, nodes.Element):
    """ a region """
Roland Denis's avatar
Roland Denis committed
27

28

29
30
31
class meso_list(nodes.General, nodes.Element):
    """ mesocenters list """

32

Roland Denis's avatar
Roland Denis committed
33
34
35
36
class meso(nodes.General, nodes.Element):
    """ mesocenter description """


37
38
39
40
41
42
43
44
class cluster_list(nodes.General, nodes.Element):
    """ clusters list """


class cluster(nodes.General, nodes.Element):
    """ cluster description """


Fabrice Roy's avatar
Fabrice Roy committed
45
46
47
48
49
50
51
52
class cluster_storage_list(nodes.General, nodes.Element):
    """ storage list associated to clusters """


class cluster_storage(nodes.General, nodes.Element):
    """ storage associated to cluster description """


53
54
55
56
57
58
59
60
class storage_list(nodes.General, nodes.Element):
    """ storage list """


class storage(nodes.General, nodes.Element):
    """ storage description """


61
62
63
64
65
66
67
class node_type_list(nodes.General, nodes.Element):
    """ node list """


class node_type(nodes.General, nodes.Element):
    """ node description """

68
69
70
###############################################################################
# Directives

71
class Region(Directive):
72
73
    required_arguments = 1
    optional_arguments = 0
74
75
76
77
78
79
80
    final_argument_whitespace = True
    option_spec = {}
    has_content = True

    def run(self):
        node = region()
        node['name'] = self.arguments[0]
81

82
83
84
85
86
        self.state.nested_parse(self.content, self.content_offset, node)

        return [node]


87
88
class MesoList(Directive):
    required_arguments = 0
89
    optional_arguments = 0
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
    option_spec = {
        'json_url': directives.uri,
    }
    has_content = True

    def run(self):
        ml = meso_list()
        if self.content:
            if 'json_url' in self.options:
                raise self.error(
                    '"%s" directive may not both specify a json url '
                    'and have content.' % self.name)
            self.state.nested_parse(self.content, self.content_offset, ml)
        elif 'json_url' in self.options:
            source = self.options['json_url']
105
            content = json.json_mesolist_to_rst(source).split('\n')
106
107
108
109
110
111
            self.state.nested_parse(StringList(content), len(content), ml)
        else:
            self.assert_has_content()

        return [ml]

Roland Denis's avatar
Roland Denis committed
112
113

class Meso(Directive):
114
115
    required_arguments = 1
    optional_arguments = 0
Roland Denis's avatar
Roland Denis committed
116
    final_argument_whitespace = True
117
118
    option_spec = { # Option name in lowercase!
        'institutesname': multiline_list(str),
Roland Denis's avatar
Roland Denis committed
119
        'url': directives.uri,
120
        'financersname': multiline_list(str),
Roland Denis's avatar
Roland Denis committed
121
        'location': str,
122
        'gpscoordinates': multiline_list(float),
Roland Denis's avatar
Roland Denis committed
123
124
        'contactname': str,
        'contactaddress': str,
125
126
127
128
        'mesocorenumber': int,
        'mesostoragesize': float,
        'mesogpunumber': int,
        'mesoramsize': int,
129
130
        'distributedinfra': multiline_list(str),
        'servicename': multiline_list(str),
Roland Denis's avatar
Roland Denis committed
131
        'etptnumber': float,
132
        'accesspolicy': multiline_list(str),
133
        'fulldescription': str,
Roland Denis's avatar
Roland Denis committed
134
135
136
137
138
139
140
141
142
143
    }
    has_content = True

    def run(self):
        node = meso()

        node['name'] = self.arguments[0]
        for key in self.option_spec:
            node[key] = self.options.get(key, None)

144
145
        # FIXME: nested parse of fulldescription (e.g. for url formatting)

Roland Denis's avatar
Roland Denis committed
146
147
        self.state.nested_parse(self.content, self.content_offset, node)

Roland Denis's avatar
Roland Denis committed
148
149
150
151
152
        if 'fulldescription' in node:
          if node['fulldescription'] is not None:
            phrase = node[ 'fulldescription'].split(".")
            node['shortdescription'] = phrase[0] + '.'
            if len(phrase) > 1:
Fabrice Roy's avatar
Fabrice Roy committed
153
154
              node['fulldescription'] = ''
              for p in phrase[1:]:
Roland Denis's avatar
Roland Denis committed
155
156
                if p.strip():
                  node['fulldescription'] = node['fulldescription'] + p + '.'
Fabrice Roy's avatar
Fabrice Roy committed
157

Roland Denis's avatar
Roland Denis committed
158
159
160
        return [node]


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
class ClusterList(Directive):
    required_arguments = 0
    optional_arguments = 0
    final_argument_whitespace = True
    option_spec = {}
    has_content = True

    def run(self):
        node = cluster_list()
        if self.content:
            self.state.nested_parse(self.content, self.content_offset, node)

        return [node]


class Cluster(Directive):
    required_arguments = 0
    optional_arguments = 1
    final_argument_whitespace = True
    option_spec = { # Option name in lowercase!
        'clustercorenumber': int,
182
183
184
        'clustergpunumber': int,
        'clusternodenumber' : int,
        'clusterramsize': int,
185
186
187
        'networktype': str,
        'networkbandwidth': float,
        'networktopology': str,
Fabrice Roy's avatar
Fabrice Roy committed
188
        'jobschedulername': str,
189
        'vendorname': multiline_list(str),
190
191
192
193
194
195
196
197
198
199
200
201
202
203
    }
    has_content = True

    def run(self):
        node = cluster()

        node['name'] = self.arguments[0] if self.arguments else ''
        for key in self.option_spec:
            node[key] = self.options.get(key, None)

        self.state.nested_parse(self.content, self.content_offset, node)

        return [node]

Roland Denis's avatar
Roland Denis committed
204

Fabrice Roy's avatar
Fabrice Roy committed
205
206
207
208
209
210
211
212
213
214
215
216
217
218
class ClusterStorageList(Directive):
    required_arguments = 0
    optional_arguments = 0
    final_argument_whitespace = True
    option_spec = {}
    has_content = True

    def run(self):
        node = cluster_storage_list()
        if self.content:
            self.state.nested_parse(self.content, self.content_offset, node)

        return [node]

Roland Denis's avatar
Roland Denis committed
219

Fabrice Roy's avatar
Fabrice Roy committed
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
class ClusterStorage(Directive):
    required_arguments = 0
    optional_arguments = 1
    final_argument_whitespace = True
    option_spec = { # Option name in lowercase!
        'typename': str,
        'name': str,
        'filesystemtype': str,
        'size': float,
    }
    has_content = True

    def run(self):
        node = cluster_storage()

        node['name'] = self.arguments[0] if self.arguments else ''
        for key in self.option_spec:
            node[key] = self.options.get(key, None)

        self.state.nested_parse(self.content, self.content_offset, node)

        return [node]


244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
class StorageList(Directive):
    required_arguments = 0
    optional_arguments = 0
    final_argument_whitespace = True
    option_spec = {}
    has_content = True

    def run(self):
        node = storage_list()
        if self.content:
            self.state.nested_parse(self.content, self.content_offset, node)

        return [node]


class Storage(Directive):
    required_arguments = 0
    optional_arguments = 1
    final_argument_whitespace = True
    option_spec = { # Option name in lowercase!
        'typename': str,
        'name': str,
        'filesystemtype': str,
        'size': float,
    }
    has_content = True

    def run(self):
        node = storage()

        node['name'] = self.arguments[0] if self.arguments else ''
        for key in self.option_spec:
            node[key] = self.options.get(key, None)

        self.state.nested_parse(self.content, self.content_offset, node)

        return [node]

class NodeTypeList(Directive):
    required_arguments = 0
    optional_arguments = 0
    final_argument_whitespace = True
    option_spec = {}
    has_content = True

    def run(self):
        node = node_type_list()
        if self.content:
            self.state.nested_parse(self.content, self.content_offset, node)

        return [node]


class NodeType(Directive):
    required_arguments = 0
    optional_arguments = 1
    final_argument_whitespace = True
    option_spec = { # Option name in lowercase!
        'gputype': str,
303
        'nodegpunumber': int,
304
        'cputype': str,
305
306
307
308
        'cpucorenumber': int,
        'nodecpunumber': int,
        'noderamsize': float,
        'nodestoragesize': float,
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
        'nodenumber': int,
    }
    has_content = True

    def run(self):
        node = node_type()

        node['name'] = self.arguments[0] if self.arguments else ''
        for key in self.option_spec:
            node[key] = self.options.get(key, None)

        self.state.nested_parse(self.content, self.content_offset, node)

        return [node]

324

325
326
327
328
def register():
    directives.register_directive('region', Region)
    directives.register_directive('meso_list', MesoList)
    directives.register_directive('meso', Meso)
329
330
    directives.register_directive('cluster_list', ClusterList)
    directives.register_directive('cluster', Cluster)
Fabrice Roy's avatar
Fabrice Roy committed
331
332
    directives.register_directive('cluster_storage_list', ClusterStorageList)
    directives.register_directive('cluster_storage', ClusterStorage)
333
334
335
336
    directives.register_directive('storage_list', StorageList)
    directives.register_directive('storage', Storage)
    directives.register_directive('node_type_list', NodeTypeList)
    directives.register_directive('node_type', NodeType)
337