-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnoteslib.py
434 lines (350 loc) · 13.6 KB
/
noteslib.py
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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Python classes for manipulating Lotus Notes/Domino objects via COM"""
################################################
# Copyright (c) 2001-2008 Robert I. Follek ([email protected])
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
################################################
"""
noteslib.py is a library of Python classes for manipulating Lotus
Notes/Domino objects via COM.
The noteslib classes correspond to the standard LotusScript classes; they
support all the standard properties and methods. The noteslib classes have
additional methods and ease-of-use features. See the documentation for the
individual classes.
Classes available so far:
Session
Database
ACL
ACLEntry
"""
__version__ = "2.1.1"
import win32com.client
# -------------------------------------------------------------------
class NotesLibError(Exception): pass
class SessionError(NotesLibError): pass
class DatabaseError(NotesLibError): pass
# -------------------------------------------------------------------
class Session:
r"""
The Session class creates an COM connection to Notes. It supports all
the properties and methods of the LotusScript NotesSession class, using
the same syntax.
To create a Session object:
s = noteslib.Session(password)
or
s = noteslib.Session()
The password is optional; if you don't provide it, Notes will prompt you
for a password.
Example:
>>> import noteslib
>>> s = noteslib.Session("password")
>>> s.NotesBuildVersion
166
>>> s.GetEnvironmentString("Directory", -1)
'd:\\notes5.8\\Data'
>>>
Session is a singleton - multiple Session variables share one Session
object. You can instantiate Sessions as needed without a performance
penalty, and you only have to establish a password once. Example:
>>> a = noteslib.Session(password)
>>> id(a)
8429868
>>> b = noteslib.Session()
>>> id(b)
8429868
"""
################################################
# SINGLETON - Implementation Details
#
# 1) The __call__ method in the Session class ensures that function-style
# calls of a Session instance return the instance.
#
# 2) The line "Session = Session()" that immediately follows the Session class definition
# creates an instance of the Session class and rebinds the name "Session" to it.
#
# With these pieces in place, any assignment like "s = Session()" returns the same
# Session instance. This gives us the singleton we want.
#
# The attempt to connect to Notes is in Session.__call__ rather than Session.__init__
# so that we don't try to connect when the "Session = Session()" line executes.
# Otherwise, "import noteslib" might try to connect, fail, and raise an exception.
################################################
__CONNECT_ERROR = r"""
Error connecting to Notes via COM.
"""
def __init__(self):
self.__handle = None
def __connectToNotes(self, password=None):
"""Connect to Notes via COM."""
try:
self.__handle = win32com.client.Dispatch("Lotus.NotesSession")
if password:
self.__handle.Initialize(password)
else:
self.__handle.Initialize()
except:
raise SessionError(self.__CONNECT_ERROR)
def __call__(self, password=None):
"""Executes when an instance is invoked as a function. Singleton support."""
if not self.__handle:
self.__connectToNotes(password)
return self
def __getattr__(self, name):
"""Delegate to the Notes object to support all properties and methods."""
return getattr(self.__handle, name)
Session = Session() # Singleton support.
# end class Session
# -------------------------------------------------------------------
class Database:
r"""
The Database class creates an COM connection to a Notes database. It
supports all the properties and methods of the LotusScript NotesDatabase
class, using the same syntax.
You don't have to create a Session first. A Database object creates its own
Session automatically.
To create a Database object:
db = noteslib.Database(server, database_file, password)
or
db = noteslib.Database(server, database_file)
Example:
>>> import noteslib
>>> db = noteslib.Database("NYNotes1", "ACLTest.nsf", "password")
>>> db.Created
pywintypes.datetime(2001, 6, 30, 11, 12, 40, tzinfo=TimeZoneInfo('GMT Standard Time', True))
Multiple Database objects created for the same database are unique objects,
but they share the same handle to the underlying NotesDatabase object.
You can instantiate Database objects as needed without a performance
penalty. Example:
>>> a = noteslib.Database("NYNotes1", "ACLTest.nsf", "password")
>>> id(a)
15281724
>>> id(a._Database__handle)
15286172
>>> b = noteslib.Database("NYNotes1", "ACLTest.nsf")
>>> id(b)
15270044
>>> id(b._Database__handle)
15286172
a and b are different objects, but they share the same internal
NotesDatabase object via the __handle variable.
"""
__DB_ERROR = r"""
Error connecting to %s %s.
Double-check the server and database file names, and make sure you have
read access to the database.
"""
__handleCache = {}
def __init__(self, server, dbFile, password=None):
"""Set the db handle, either from cache or via the COM connection."""
cacheKey = ( server.lower(), dbFile.lower() )
cachedHandle = self.__handleCache.get(cacheKey)
if cachedHandle:
self.__handle = cachedHandle
else:
try:
s = Session(password)
self.__handle = s.GetDatabase(server, dbFile)
if self.__handle.IsOpen: # Make sure everything's okay.
self.__handleCache[cacheKey] = self.__handle # Cache the handle
except:
raise DatabaseError(self.__DB_ERROR % (server, dbFile))
def __getattr__(self, name):
"""Delegate to the Notes object to support all properties and methods."""
return getattr(self.__handle, name)
# end class Database
# -------------------------------------------------------------------
class ACL:
r"""
The ACL class encapsulates a Notes database ACL. It supports all the
properties and methods of the LotusScript NotesACL class, using the same
syntax.
Additional features:
* You can print an ACL object. It knows how to format itself reasonably.
* getAllEntries() method - Returns the ACL contents as a list of ACLEntry
objects, sorted by Name.
You don't have to create Session or Database objects first. An ACL object
creates its own Session and Database objects automatically.
To create an ACL object:
acl = noteslib.ACL(server, database_file, password)
or
acl = noteslib.ACL(server, database_file)
Example:
>>> import noteslib
>>> acl = noteslib.ACL("NYNotes1", "ACLTest.nsf", "password")
>>> for entry in acl.getAllEntries():
... print (entry.getName())
...
-Default-
Alice Author
Anonymous
bob
Dave Depositor
Donna Designer
LocalDomainServers
OtherDomainServers
Randy Reader
"""
def __init__(self, server, dbFile, password=None):
"""Set the ACL handle, and retrieve the ACL entries."""
self.__entries = []
db = Database(server, dbFile, password)
self.__handle = db.ACL
nextEntry = self.__handle.GetFirstEntry()
while nextEntry:
self.__entries.append( ACLEntry(nextEntry) )
nextEntry = self.__handle.GetNextEntry(nextEntry)
self.__entries.sort()
def getAllEntries(self):
"""Returns a list of noteslib ACLEntry objects, sorted by Name."""
return self.__entries
def __getattr__(self, name):
"""Delegate to the Notes object to support all properties and methods."""
return getattr(self.__handle, name)
def __str__(self):
"""For printing"""
s = ""
for entry in self.getAllEntries():
s += "%s\n" % entry
return s
# end class ACL
# -------------------------------------------------------------------
class ACLEntry:
r"""
The ACLEntry class encapsulates a Notes database ACL entry. It supports
all the properties and methods of the LotusScript NotesACLEntry class,
using the same syntax.
Additional features:
* You can print an ACLEntry object. It knows how to format itself reasonably.
* getName() method - Returns the entry name.
* getLevel() method - Returns the entry level.
* getRoles() method - Returns a list of entry roles, sorted alphabetically.
* getFlags() method - Returns a list of the ACLEntry flags, translated to
strings.
These methods avoid the obvious names, e.g. getName() instead of name(),
to avoid conflict with the existing NotesACLEntry properties.
Normally, you won't create an ACLEntry object directly. Instead, you can
retrieve a list of ACLEntry objects from an ACL object, via its
getAllEntries() method.
Example:
>>> import noteslib
>>> acl = noteslib.ACL("NYNotes1", "ACLTest.nsf", "password")
>>> print (acl.getAllEntries()[3])
Name : bob
Level: Manager
Role : [Role1]
Role : [Role2]
Role : [Role3]
Flag : Create Documents
Flag : Delete Documents
Flag : Create Personal Agents
Flag : Create Personal Folders/Views
Flag : Create Shared Folders/Views
Flag : Create LotusScript/Java Agent
Flag : Read Public Documents
Flag : Write Public Documents
"""
__LEVELS = ["No Access", "Depositor", "Reader", "Author", "Editor", "Designer", "Manager"]
def __init__(self, notesACLEntry):
"""The parameter is a LotusScript NotesACLEntry object."""
self.__handle = notesACLEntry
self.__name = notesACLEntry.Name
self.__level = self.__LEVELS[notesACLEntry.Level]
self.__loadRoles(notesACLEntry)
self.__loadFlags(notesACLEntry)
def getName(self):
"""Returns the ACLEntry Name."""
return self.__name
def getLevel(self):
"""Returns the ACLEntry Level, translated to a string."""
return self.__level
def getFlags(self):
"""Returns a list of the ACLEntry flags, translated to strings."""
return self.__flags
def getRoles(self):
"""Returns a list of the ACLEntry roles, sorted alphabetically."""
return self.__roles
def __loadFlags(self, notesACLEntry):
"""Translate the entry's flags into a list of strings."""
self.__flags = []
if notesACLEntry.CanCreateDocuments:
self.__flags.append("Create Documents")
if notesACLEntry.CanDeleteDocuments:
self.__flags.append("Delete Documents")
if notesACLEntry.CanCreatePersonalAgent:
self.__flags.append("Create Personal Agents")
if notesACLEntry.CanCreatePersonalFolder:
self.__flags.append("Create Personal Folders/Views")
if notesACLEntry.CanCreateSharedFolder:
self.__flags.append("Create Shared Folders/Views")
if notesACLEntry.CanCreateLSOrJavaAgent:
self.__flags.append("Create LotusScript/Java Agent")
if notesACLEntry.IsPublicReader:
self.__flags.append("Read Public Documents")
if notesACLEntry.IsPublicWriter:
self.__flags.append("Write Public Documents")
def __loadRoles(self, notesACLEntry):
"""Load the entry's roles into a sorted list."""
roles = notesACLEntry.Roles
if roles:
self.__roles = sorted(roles)
else:
self.__roles = []
def __lt__(self, other):
"""For sorting: compare on name."""
return self.__name.lower() < other.__name.lower()
def __getattr__(self, name):
"""Delegate to the Notes object to support all properties and methods."""
return getattr(self.__handle, name)
def __str__(self):
"""For printing"""
s = "Name : %s\nLevel: %s\n" % ( self.getName(), self.getLevel() )
if self.getRoles():
for role in self.getRoles():
s += "Role : %s\n" % role
else:
s += "Role : No roles\n"
if self.getFlags():
for flag in self.getFlags():
s += "Flag : %s\n" % flag
else:
s += "Flag : No flags\n"
return s
# end class ACLEntry
# -------------------------------------------------------------------
def test():
testServer = ""
testDB = "cache.ndk"
print("Testing Session")
s = Session()
print(s.CommonUserName)
print("Testing Database")
db = Database(testServer, testDB)
print(db.Created)
print("Testing ACL")
acl = ACL(testServer, testDB)
print(acl.Roles)
print("Testing the full ACL print")
print(acl)
if __name__ == "__main__":
test()