aboutsummaryrefslogtreecommitdiff
path: root/sgdfi/objects/operation.py
blob: 63858d56510ac5c13290c9c30fc54bc3b095290e (plain)
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
85
86
87
88
89
#!/usr/bin/env python3
#******************************************************************************
# Copyright (C) 2018-2021 Thomas Touhey <thomas@touhey.fr>
# This file is part of the sgdfi project, which is MIT-licensed.
#******************************************************************************
""" Représentation d'un événement technique (opération) sur
	l'intranet SGDF. """

import regex as _re

from .object import (Object as _Object,
	Property as _Property, DateProperty as _DateProperty,
	ObjectProperty as _ObjectProperty, ArrayProperty as _ArrayProperty)
from .adherent import Adherent as _Adherent
from .operationtype import OperationTypeProperty as _OperationTypeProperty
from .structure import Structure as _Structure
from .rallyregistration import RallyRegistration as _RallyRegistration
from .camp import Camp as _Camp
from .place import Place as _Place

__all__ = ['Operation']

class _OperationFields:
	""" Champs de données concernant une opération. """

	def __init__(self, value, sep = ','):
		if type(value) != str:
			raise ValueError("fields should be string")

		pattern = f'([^{sep}\(]*(?P<par>\(([^\(\)]*(?&par)*)*\))*)*'
		reg = _re.compile(pattern, _re.S)

		fields = {}
		lastfield = None
		for k in reg.finditer(value):
			ma = k.group(0)
			if not ma:
				continue

			# From here, we have a valid group.
			# If we have reached the last field already, just append it to
			# the last key. Otherwise, check if it is a new group or not.

			if lastfield and lastkey == lastfield:
				fields[lastkey] += sep + ma
				continue

			g = ma.split(':')
			if len(g) < 2:
				if not lastkey:
					continue

				fields[lastkey] += sep + ma
				continue

			# New group!

			lastkey = g[0].strip()
			fields[lastkey] = ':'.join(g[1:]).lstrip()

		self._fields = fields

	def __repr__(self):
		return f"OperationFields({repr(self._fields)})"

	def __iter__(self):
		return iter(self._fields)

	def __getitem__(self, key):
		return self._fields[key]

class _OperationFieldsProperty(_Property):
	""" Propriété contenant les champs d'une opération. """

	def value(self, value):
		return _OperationFields(value)

class Operation(_Object):
	""" Représentation d'une opération, i.e. un événement ayant eu lieu
		sur l'intranet SGDF. """

	time = _DateProperty()
	type = _OperationTypeProperty()
	author = _ObjectProperty(types = (_Adherent,))
	fields = _OperationFieldsProperty()
	related = _ArrayProperty(types = (_Structure, _Adherent,
		_RallyRegistration, _Camp, _Place))

# End of file.