summaryrefslogtreecommitdiffstats
path: root/fposition.py
blob: 308ceb96b11a0ff6a391b9943054631c46bf1e29 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#/***************************************************************************
#                             fposition.h
#                             -----------
#    begin                : sam nov 2 2002
#    copyright            : (C) 1992-2004 by Fabian Padilla
#    email                : fp@bridgethink.com
# ***************************************************************************/

# /***************************************************************************
#  *                                                                         *
#  *   This program is free software; you can redistribute it and/or modify  *
#  *   it under the terms of the Foundation Public License as published by   *
#  *   bridgethink sarl; either version 2 of the License, or                 *
#  *   (at your option) any later version.                                   *
#  *                                                                         *
#  ***************************************************************************/

#
#  from C++ to python by Jeremy Zurcher <jeremy@asynk.ch>
#


"""/** Define the position ( source OR destination) of the evenData.
  function : maintain the stringHashs for the computer, rooms, port, action.
  Format example : www.revena.com/room1/room2/port:normal

  description : method "setPosition" set all position informations this mean
  - compute
  - room tree
  - port
  - action.
  Then these informations are retrieved with getComputer, getRoom, getPort,
  getAction methods.

  *@author Fabian Padilla
  */"""

__all__ =["Fposition"]

from flist import Flist
from fstringhash import FstringHash

from returncodes import RET_OK
from evenjastrings import ACT_WAIT_NOTHING
from evenjastrings import ACT_NORMAL
from evenjastrings import TXT_NULL

class Fposition(FstringHash):
    """This class keeps informations on  the computer / a list of rooms / a port /an action / and time"""

    def __init__(self):
        FstringHash.__init__(self)    # force constructor
        self.computer = FstringHash()    # /** computer destination or source. */
        self.rooms = Flist()        # /** room destination or source. */
        self.nbLevel = 0
        self.port = FstringHash()    # /** port destination or source. */
        self.action = FstringHash()    # /** action to do at the position (source or destination). */
        self.waitTime = 0        # /** wait time in the action */
        #/** waitTime unit (ex.: y(year), n(month), d(day), h(hour), m(minute), s(seconde) or x(milliseconde))*/
        self.unitWaitTime = ACT_WAIT_NOTHING
        self.positionActived = False

    def __del__(self):
        """I belive this is for no use"""
        self.resetPosition()

    def __str__(self):
        return "\t"+FstringHash.__str__(self)+\
                "Fposition - computer : "+str(self.computer)+\
                " - rooms : "+str(self.rooms)+\
                " - nbLevel : "+str(self.nbLevel)+\
                " - port : "+str(self.port)+\
                " - action : "+str(self.action)+\
                " - waitTime : "+str(self.waitTime)+\
                " - unitWaitTime : "+str(self.unitWaitTime)+\
                " - positionAtived : "+str(self.positionActived)+"\n"

    def resetPosition(self):
        """/** reset position (erase datas) */"""
        if self.positionActived:
            self.computer.setString(TXT_NULL)
            # delete rooms
            for I in range(self.nbLevel):
                self.rooms.remove(0)            # TzurTcH - no self.getRoom()
            self.nbLevel = 0
            self.port.setString(TXT_NULL)
            self.action.setString(TXT_NULL)
            self.waitTime = 0
            self.unitWaitTime = ACT_WAIT_NOTHING
            self.positionActived = False

    def setPosition(self, position):
        """/** Set the source or the destination of an evenData. */"""
        self.resetPosition()
        self.nbLevel = 0
        self.setString(position)

        # search for - computer/rooms<F2>
        buffer = position.split("/")
        if len(buffer) > 1:
            # computer name ?
            ret = None
            try:
                ret = buffer[0].index(".")
            except:
                None
            if ret:
                self.computer.setString(buffer[0])
                buffer = buffer[1:]
            # import rooms
            bufferRooms=""
            for I in buffer[:-1]:
                self.rooms.add( FstringHash())
                if self.nbLevel:
                    bufferRooms += "/"
                bufferRooms += I
                self.rooms.get(self.nbLevel).setString(bufferRooms)
                self.nbLevel += 1
        # search for - port:action
        buffer = buffer[-1].split(":")
        self.port.setString(buffer[0])
        if len(buffer) > 1:
            # search for - action,time
            buffer = buffer[1].split(",")
            self.action.setString(buffer[0])
            if len(buffer) > 1:
                self.waitTime = int(buffer[1][:-1])
                self.unitWaitTime = buffer[1][-1]
            else:
                self.waitTime = 0
                self.unitWaitTime = ACT_WAIT_NOTHING
        else:
            self.action.setString(ACT_NORMAL)
            self.waitTime = 0
            self.unitWaitTime = ACT_WAIT_NOTHING
        self.positionActived = True
        return RET_OK

    def getComputer(self):
        """/** Get the computer name. If the evenData needs to go to another computer. */"""
        return self.computer

    def getRoom(self, level = 0):
        """/** Get the room for destination or source. */"""
        if level < self.nbLevel:
            return self.rooms.get(level)
        else:
            return None

    def getNbLevel(self):
        """/** Number of levels in the rooms routing tree. */"""
        return self.nbLevel

    def getPort(self):
        """/** Get the port of the position (source or destination). This means an evenDoor or a evenBoard. */"""
        return self.port

    def getAction(self):
        """/** Get the action of the position (source or destination). */"""
        return self.action

    def getWaitTime(self):
        """/** Get the amount of waiting time (unity is another get function) */"""
        return self.waitTime

    def getUnitWaitTime(self):
        """/** Get the unity time y,n,d,h,m,s or x */"""
        return self.unitWaitTime

    def copyFrom(self, pos):
        """/** Copy a position From another Fposition class */"""
        self.resetPosition()
        self.computer.copyFrom(pos.getComputer())

        for I in range(pos.getNbLevel()):
            new = FstringHash()
            new.copyFrom(pos.getRoom(I))
            self.rooms.add(new)
        self.nbLevel = pos.getNbLevel()
        self.port.copyFrom(pos.getPort())
        self.action.copyFrom(pos.getAction())
        self.waitTime = pos.getWaitTime()
        self.unitWaitTime = pos.getUnitWaitTime()
        self.positionActived = True



if __name__ == '__main__':

    import unittest
    from evenjastrings import *

    pos1 = Fposition()
    pos2 = Fposition()

    class FpositionTestCase(unittest.TestCase):

        def test1Splitting(self):
            pos1.setPosition( "WWW.TEST.ORG/room1/room2/port")
            self.assertEquals(pos1.getNbLevel(),2)
            self.assertEquals("WWW.TEST.ORG",pos1.getComputer().getString())
            self.assertEquals("room1",pos1.getRoom(0).getString())
            self.assertEquals("room1/room2",pos1.getRoom(1).getString())
            self.assertEquals("port",pos1.getPort().getString())
            self.assertEquals(ACT_NORMAL,pos1.getAction().getString())

            pos1.setPosition("room1/room2/port:destination1Data")
            self.assertEquals(pos1.getNbLevel(),2)
            self.assertEquals(TXT_NULL,pos1.getComputer().getString())
            self.assertEquals("room1",pos1.getRoom(0).getString())
            self.assertEquals("room1/room2",pos1.getRoom(1).getString())
            self.assertEquals("port",pos1.getPort().getString())
            self.assertEquals(ACT_DESTINATION1DATA,pos1.getAction().getString())

            pos1.setPosition( "port:wait,5s")
            self.assertEquals(pos1.getNbLevel(),0)
            self.assertEquals(TXT_NULL,pos1.getComputer().getString())
            self.assertEquals(pos1.getRoom(0),None)
            self.assertEquals("port",pos1.getPort().getString())
            self.assertEquals(ACT_WAIT,pos1.getAction().getString())
            self.assertEquals(pos1.getWaitTime(),5)
            self.assertEquals( pos1.getUnitWaitTime(),ACT_WAIT_SECOND)

            pos1.setPosition( ":wait,5d")
            self.assertEquals(pos1.getNbLevel(),0)
            self.assertEquals(TXT_NULL,pos1.getComputer().getString())
            self.assertEquals(pos1.getRoom(0),None)
            self.assertEquals(TXT_NULL,pos1.getPort().getString())
            self.assertEquals(ACT_WAIT,pos1.getAction().getString())
            self.assertEquals(pos1.getWaitTime(),5)
            self.assertEquals(pos1.getUnitWaitTime(),ACT_WAIT_DAY)

            pos1.setPosition( ":end")
            self.assertEquals(pos1.getNbLevel(),0)
            self.assertEquals(TXT_NULL,pos1.getComputer().getString())
            self.assertEquals(pos1.getRoom(0),None)
            self.assertEquals(TXT_NULL,pos1.getPort().getString())
            self.assertEquals(ACT_END,pos1.getAction().getString())

        def test2Copy(self):
            pos1.setPosition( "WWW.TEST.ORG/room1/room2/port:wait,1d")
            pos2.copyFrom( pos1)
            self.assertEquals(pos1.getComputer().equals( pos2.getComputer()),True)
            self.assertEquals(pos1.getRoom(0).equals(pos2.getRoom(0)),True)
            self.assertEquals(pos1.getRoom( 1).equals( pos2.getRoom( 1)),True)
            self.assertEquals(pos1.getPort().equals( pos2.getPort()),True)
            self.assertEquals(pos1.getAction().equals( pos2.getAction()),True)
            self.assertEquals(pos1.getWaitTime(),pos2.getWaitTime())
            self.assertEquals(pos1.getUnitWaitTime(),pos2.getUnitWaitTime())

    unittest.main()