Skip to content

Commit 24ddf17

Browse files
author
James Foster
committed
Refactor files and make a useful README.md.
1 parent 3e0e5e3 commit 24ddf17

File tree

5 files changed

+310
-146
lines changed

5 files changed

+310
-146
lines changed

GciClasses.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
2+
Definitions of basic classes and types for the GCI Library
33
"""
44

55
from ctypes import *

GciDefault.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
Login information for tests
3+
Edit GciLogin.py (a copy of GciDefault.py that is not included in source code control)
4+
"""
5+
6+
gem_host='localhost'
7+
stone='gs64stone'
8+
gs_user='DataCurator'
9+
gs_password='swordfish'
10+
netldi='netldi'
11+
host_user=''
12+
host_password=''

GciLibrary.py

Lines changed: 63 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,27 @@
11
"""
2-
>>> # See $GEMSTONE/include/gcits.hf for details
3-
4-
>>> gci = GciLibrary()
5-
>>> isinstance(gci, GciLibrary)
6-
True
7-
8-
>>> gci.version()
9-
'3.4.3 build gss64_3_4_x_branch-45183'
10-
11-
>>> gci.oopToChar(16668)
12-
65
13-
14-
>>> gci.oopToChar(16667)
15-
-1
16-
17-
>>> gci.oopToChar(24860) # $a
18-
97
19-
20-
>>> gci.charToOop(65)
21-
16668
22-
23-
>>> gci.charToOop(97) # $a
24-
24860
25-
26-
>>> gci.charToOop(1114111)
27-
285212444
28-
29-
>>> gci.charToOop(1114112)
30-
1
31-
32-
>>> gci.doubleToSmallDouble(1.0)
33-
9151314442816847878
34-
35-
>>> gci.doubleToSmallDouble(1e40)
36-
1
37-
38-
>>> gci.I32ToOop(0)
39-
2
40-
41-
>>> gci.I32ToOop(55)
42-
442
43-
"""
44-
45-
"""
46-
>>> try:
47-
... gci.login(netldi='gs64ldi99', stone='gs64stone4')
48-
... except GciException as ex:
49-
... ex.number() # invalid NetLDI
50-
4147
51-
52-
>>> try:
53-
... gci.login(netldi='gs64ldi', stone='gs64stone44')
54-
... except GciException as ex:
55-
... ex.number() # invalid stone
56-
4065
57-
58-
>>> try:
59-
... gci.login(netldi='gs64ldi', stone='gs64stone', gs_user='fred')
60-
... except GciException as ex:
61-
... ex.number() # invalid user/password
62-
4051
63-
64-
>>> session = gci.login(netldi='gs64ldi', stone='gs64stone')
65-
>>> isinstance(session, int) # successful login
66-
True
67-
68-
>>> gci.is_session_valid(session)
69-
True
70-
71-
>>> gci.logout(session)
72-
73-
>>> gci.is_session_valid(session)
74-
False
75-
76-
>>> try:
77-
... gci.logout(session)
78-
... except GciException as ex:
79-
... ex.number() # invalid session
80-
4100
81-
2+
GciLibrary provides the public API for access to GemStone
823
"""
834

5+
import os
846
import platform
857
from GciClasses import *
868

879
class GciLibrary:
8810

89-
def __init__(self, version='3.4.3', directory=''):
90-
system = platform.system() # 'Darwin', 'Linux', or 'Windows'
91-
size = sizeof(c_voidp) # 4 (32-bit) or 8 (64-bit)
92-
print(system, size, flush=True)
93-
suffixes = {
94-
4: {
95-
'Darwin': '-32.dynlib',
96-
'Linux': '-32.so',
97-
'Windows': '-32.dll'
98-
},
99-
8: {
100-
'Darwin': '-64.dynlib',
101-
'Linux': '-64.so',
102-
'Windows': '-64.dll'
103-
}
104-
}
105-
suffix = suffixes.get(size, 'Invalid size').get(system, 'Invalid system')
106-
path = directory + 'libgcits-' + version + suffix
107-
print(suffix, path, flush=True)
108-
self.library = CDLL(path)
11+
def __init__(self, version='3.4.3', directory=os.getcwd()):
12+
self._initLibrary(version, directory)
13+
14+
self.gciI32ToOop = self.library.GciI32ToOop
15+
self.gciI32ToOop.restype = c_int32
16+
self.gciI32ToOop.argtypes = [OopType]
17+
18+
self.gciTsCharToOop = self.library.GciTsCharToOop
19+
self.gciTsCharToOop.restype = OopType
20+
self.gciTsCharToOop.argtypes = [c_uint]
21+
22+
self.gciTsDoubleToSmallDouble = self.library.GciTsDoubleToSmallDouble
23+
self.gciTsDoubleToSmallDouble.restype = OopType
24+
self.gciTsDoubleToSmallDouble.argtypes = [c_double]
10925

11026
self.gciTsLogin = self.library.GciTsLogin
11127
self.gciTsLogin.restype = GciSession
@@ -125,6 +41,14 @@ def __init__(self, version='3.4.3', directory=''):
12541
self.gciTsLogout.restype = c_bool
12642
self.gciTsLogout.argtypes = [GciSession, POINTER(GciErrSType)]
12743

44+
self.gciTsOopIsSpecial = self.library.GciTsOopIsSpecial
45+
self.gciTsOopIsSpecial.restype = c_bool
46+
self.gciTsOopIsSpecial.argtypes = [OopType]
47+
48+
self.gciTsOopToChar = self.library.GciTsOopToChar
49+
self.gciTsOopToChar.restype = c_int
50+
self.gciTsOopToChar.argtypes = [OopType]
51+
12852
self.gciTsSessionIsRemote = self.library.GciTsSessionIsRemote
12953
self.gciTsSessionIsRemote.restype = c_int
13054
self.gciTsSessionIsRemote.argtypes = [GciSession]
@@ -133,49 +57,39 @@ def __init__(self, version='3.4.3', directory=''):
13357
self.gciTsVersion.restype = c_int
13458
self.gciTsVersion.argtypes = [c_char_p, c_size_t]
13559

136-
self.gciTsOopToChar = self.library.GciTsOopToChar
137-
self.gciTsOopToChar.restype = c_int
138-
self.gciTsOopToChar.argtypes = [OopType]
139-
140-
self.gciTsCharToOop = self.library.GciTsCharToOop
141-
self.gciTsCharToOop.restype = OopType
142-
self.gciTsCharToOop.argtypes = [c_uint]
143-
144-
self.gciTsDoubleToSmallDouble = self.library.GciTsDoubleToSmallDouble
145-
self.gciTsDoubleToSmallDouble.restype = OopType
146-
self.gciTsDoubleToSmallDouble.argtypes = [c_double]
147-
148-
self.gciI32ToOop = self.library.GciI32ToOop
149-
self.gciI32ToOop.restype = c_int32
150-
self.gciI32ToOop.argtypes = [OopType]
151-
152-
self.gciTsOopIsSpecial = self.library.GciTsOopIsSpecial
153-
self.gciTsOopIsSpecial.restype = c_bool
154-
self.gciTsOopIsSpecial.argtypes = [OopType]
155-
156-
def oopIsSpecial(self, oop) -> c_bool:
157-
result = self.gciTsOopIsSpecial(oop)
158-
return result
60+
def _initLibrary(self, version, directory):
61+
system = platform.system() # 'Darwin', 'Linux', or 'Windows'
62+
size = sizeof(c_voidp) # 4 (32-bit) or 8 (64-bit)
63+
suffixes = {
64+
4: {
65+
'Darwin': '32.dylib',
66+
'Linux': '32.so',
67+
'Windows': '32.dll'
68+
},
69+
8: {
70+
'Darwin': '64.dylib',
71+
'Linux': '64.so',
72+
'Windows': '64.dll'
73+
}
74+
}
75+
suffix = suffixes.get(size, 'Invalid size').get(system, 'Invalid system')
76+
path = os.path.join(directory, 'libgcits-' + version + '-' + suffix)
77+
self.library = CDLL(path)
15978

160-
def I32ToOop(self, arg) -> c_int32:
161-
result = self.gciI32ToOop(arg)
79+
def charToOop(self, ch) -> OopType:
80+
result = self.gciTsCharToOop(ch)
81+
# should check for 1 (OOP_ILLEGAL)
16282
return result
163-
83+
16484
def doubleToSmallDouble(self, aFloat) -> c_double:
16585
result = self.gciTsDoubleToSmallDouble(aFloat)
16686
# should check for 1 (OOP_ILLEGAL)
16787
return result
16888

169-
def charToOop(self, ch) -> OopType:
170-
result = self.gciTsCharToOop(ch)
171-
# should check for 1 (OOP_ILLEGAL)
172-
return result
173-
174-
def oopToChar(self, oop) -> c_int:
175-
result = self.gciTsOopToChar(oop)
176-
# should check for -1
89+
def I32ToOop(self, arg) -> c_int32:
90+
result = self.gciI32ToOop(arg)
17791
return result
178-
92+
17993
def is_session_valid(self, session) -> bool:
18094
return self.gciTsSessionIsRemote(session) == 1
18195

@@ -200,9 +114,12 @@ def login(self,
200114
error = GciErrSType()
201115
session = self.gciTsLogin(
202116
stone_nrs.encode('ascii'),
203-
host_user, host_password, False,
117+
host_user,
118+
host_password,
119+
False,
204120
gem_nrs.encode('ascii'),
205-
gs_user.encode('ascii'), gs_password.encode('ascii'),
121+
gs_user.encode('ascii'),
122+
gs_password.encode('ascii'),
206123
0, 0, byref(error))
207124
if session is None:
208125
raise GciException(error)
@@ -214,13 +131,17 @@ def logout(self, session) -> None:
214131
raise GciException(error)
215132
return None
216133

134+
def oopIsSpecial(self, oop) -> c_bool:
135+
result = self.gciTsOopIsSpecial(oop)
136+
return result
137+
138+
def oopToChar(self, oop) -> c_int:
139+
result = self.gciTsOopToChar(oop)
140+
# should check for -1
141+
return result
142+
217143
def version(self) -> str:
218144
buf = create_string_buffer(256)
219145
code = self.gciTsVersion(buf, sizeof(buf))
220146
assert code == 3
221147
return buf.value.decode('ascii')
222-
223-
224-
if __name__ == '__main__':
225-
import doctest
226-
doctest.testmod()

GciTests.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
"""
2+
>>> # See gcits.hf for details
3+
4+
>>> gci = GciLibrary()
5+
>>> isinstance(gci, GciLibrary)
6+
True
7+
8+
>>> gci.version()
9+
'3.4.3 build gss64_3_4_x_branch-45183'
10+
11+
>>> gci.oopToChar(16668)
12+
65
13+
14+
>>> gci.oopToChar(16667)
15+
-1
16+
17+
>>> gci.oopToChar(24860) # $a
18+
97
19+
20+
>>> gci.charToOop(65)
21+
16668
22+
23+
>>> gci.charToOop(97) # $a
24+
24860
25+
26+
>>> gci.charToOop(1114111)
27+
285212444
28+
29+
>>> gci.charToOop(1114112)
30+
1
31+
32+
>>> gci.doubleToSmallDouble(1.0)
33+
9151314442816847878
34+
35+
>>> gci.doubleToSmallDouble(1e40)
36+
1
37+
38+
>>> gci.I32ToOop(0)
39+
2
40+
41+
>>> gci.I32ToOop(55)
42+
442
43+
44+
>>> try:
45+
... gci.login(netldi='badldi', stone='badstone')
46+
... except GciException as ex:
47+
... ex.number() # invalid NetLDI
48+
4147
49+
50+
>>> try:
51+
... gci.login(netldi=netldi, stone='badstone')
52+
... except GciException as ex:
53+
... ex.number() # invalid stone
54+
4065
55+
56+
>>> try:
57+
... gci.login(netldi=netldi, stone=stone, gs_user='badUser')
58+
... except GciException as ex:
59+
... ex.number() # invalid user/password
60+
4051
61+
62+
>>> session = gci.login(netldi=netldi, stone=stone, gs_user=gs_user, gs_password=gs_password)
63+
>>> isinstance(session, int) # successful login
64+
True
65+
66+
>>> gci.is_session_valid(session)
67+
True
68+
69+
>>> gci.logout(session)
70+
71+
>>> gci.is_session_valid(session)
72+
False
73+
74+
>>> try:
75+
... gci.logout(session)
76+
... except GciException as ex:
77+
... ex.number() # invalid session
78+
4100
79+
80+
"""
81+
82+
if __name__ == '__main__':
83+
import os
84+
if (not os.path.isfile('GciLogin.py')):
85+
import shutil
86+
shutil.copy2('GciDefault.py', 'GciLogin.py')
87+
from GciLibrary import GciLibrary, GciException
88+
from GciLogin import *
89+
import doctest
90+
print('Start tests')
91+
doctest.testmod()
92+
print('End tests')

0 commit comments

Comments
 (0)