Initial import
[samba] / source / python / samba / printerdata.py
1 #!/usr/bin/env python
2
3 #
4 # A python module that maps printerdata to a dictionary.  We define
5 # two classes.  The printerdata class maps to Get/Set/Enum/DeletePrinterData
6 # and the printerdata_ex class maps to Get/Set/Enum/DeletePrinterDataEx
7 #
8
9 #
10 # TODO:
11 #
12 #   - Implement __delitem__
13 #
14
15 from samba import spoolss
16
17 class printerdata:
18     def __init__(self, host, creds = {}, access = 0x02000000):
19         # For read access, use MAXIMUM_ALLOWED_ACCESS = 0x02000000
20         # For write access, use PRINTER_ACCESS_ADMINISTER = 0x00000004
21         self.hnd = spoolss.openprinter(host, creds = creds, access = access)
22
23     def keys(self):
24         return self.hnd.enumprinterdata().keys()
25
26     def __getitem__(self, key):
27         return self.hnd.getprinterdata(key)['data']
28
29     def __setitem__(self, key, value):
30         # Store as REG_BINARY for now
31         self.hnd.setprinterdata({"key": "", "value": key, "type": 3,
32                                  "data": value})
33         
34 class printerdata_ex:
35     def __init__(self, host, creds = {}, access = 0x02000000):
36         # For read access, use MAXIMUM_ALLOWED_ACCESS = 0x02000000
37         # For write access, use PRINTER_ACCESS_ADMINISTER = 0x00000004
38         self.host = host
39         self.top_level_keys = ["PrinterDriverData", "DsSpooler", "DsDriver",
40                                "DsUser"]
41         self.creds = creds
42         self.access = access
43
44     def keys(self):
45         return self.top_level_keys
46
47     def has_key(self, key):
48         for k in self.top_level_keys:
49             if k == key:
50                 return 1
51         return 0
52
53     class printerdata_ex_subkey:
54         def __init__(self, host, key, creds, access):
55             self.hnd = spoolss.openprinter(host, creds, access)
56             self.key = key
57
58         def keys(self):
59             return self.hnd.enumprinterdataex(self.key).keys()
60
61         def __getitem__(self, key):
62             return self.hnd.getprinterdataex(self.key, key)['data']
63
64     def __getitem__(self, key):
65         return self.printerdata_ex_subkey(
66             self.host, key, self.creds, self.access)