PEP8: fix E302: expected 2 blank lines, found 1
[samba.git] / python / samba / ms_display_specifiers.py
1 # Create DisplaySpecifiers LDIF (as a string) from the documents provided by
2 # Microsoft under the WSPP.
3 #
4 # Copyright (C) Andrew Kroeger <andrew@id10ts.net> 2009
5 #
6 # Based on ms_schema.py
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 from __future__ import print_function
21
22 import re
23
24
25 def __read_folded_line(f, buffer):
26     """Read a line from an LDIF file, unfolding it"""
27     line = buffer
28
29     while True:
30         l = f.readline()
31
32         if l[:1] == " ":
33             # continued line
34
35             # cannot fold an empty line
36             assert(line != "" and line != "\n")
37
38             # preserves '\n '
39             line = line + l
40         else:
41             # non-continued line
42             if line == "":
43                 line = l
44
45                 if l == "":
46                     # eof, definitely won't be folded
47                     break
48             else:
49                 # marks end of a folded line
50                 # line contains the now unfolded line
51                 # buffer contains the start of the next possibly folded line
52                 buffer = l
53                 break
54
55     return (line, buffer)
56
57 # Only compile regexp once.
58 # Will not match options after the attribute type.
59 attr_type_re = re.compile("^([A-Za-z][A-Za-z0-9-]*):")
60
61
62 def __read_raw_entries(f):
63     """Read an LDIF entry, only unfolding lines"""
64
65     buffer = ""
66
67     while True:
68         entry = []
69
70         while True:
71             (l, buffer) = __read_folded_line(f, buffer)
72
73             if l[:1] == "#":
74                 continue
75
76             if l == "\n" or l == "":
77                 break
78
79             m = attr_type_re.match(l)
80
81             if m:
82                 if l[-1:] == "\n":
83                     l = l[:-1]
84
85                 entry.append(l)
86             else:
87                 print("Invalid line: %s" % l, end=' ', file=sys.stderr)
88                 sys.exit(1)
89
90         if len(entry):
91             yield entry
92
93         if l == "":
94             break
95
96
97 def fix_dn(dn):
98     """Fix a string DN to use ${CONFIGDN}"""
99
100     if dn.find("<Configuration NC Distinguished Name>") != -1:
101         dn = dn.replace("\n ", "")
102         return dn.replace("<Configuration NC Distinguished Name>", "${CONFIGDN}")
103     else:
104         return dn
105
106
107 def __write_ldif_one(entry):
108     """Write out entry as LDIF"""
109     out = []
110
111     for l in entry:
112         if l[2] == 0:
113             out.append("%s: %s" % (l[0], l[1]))
114         else:
115             # This is a base64-encoded value
116             out.append("%s:: %s" % (l[0], l[1]))
117
118     return "\n".join(out)
119
120
121 def __transform_entry(entry):
122     """Perform required transformations to the Microsoft-provided LDIF"""
123
124     temp_entry = []
125
126     for l in entry:
127         t = []
128
129         if l.find("::") != -1:
130             # This is a base64-encoded value
131             t = l.split(":: ", 1)
132             t.append(1)
133         else:
134             t = l.split(": ", 1)
135             t.append(0)
136
137         key = t[0].lower()
138
139         if key == "changetype":
140             continue
141
142         if key == "distinguishedname":
143             continue
144
145         if key == "instancetype":
146             continue
147
148         if key == "name":
149             continue
150
151         if key == "cn":
152             continue
153
154         if key == "objectcategory":
155             continue
156
157         if key == "showinadvancedviewonly":
158             value = t[1].upper().lstrip().rstrip()
159             if value == "TRUE":
160                 # Remove showInAdvancedViewOnly attribute if it is set to the
161                 # default value of TRUE
162                 continue
163
164         t[1] = fix_dn(t[1])
165
166         temp_entry.append(t)
167
168     entry = temp_entry
169
170     return entry
171
172
173 def read_ms_ldif(filename):
174     """Read and transform Microsoft-provided LDIF file."""
175
176     out = []
177
178     f = open(filename, "rU")
179     for entry in __read_raw_entries(f):
180         out.append(__write_ldif_one(__transform_entry(entry)))
181
182     return "\n\n".join(out) + "\n\n"
183
184 if __name__ == '__main__':
185     import sys
186
187     try:
188         display_specifiers_file = sys.argv[1]
189     except IndexError:
190         print("Usage: %s display-specifiers-ldif-file.txt" % (sys.argv[0]), file=sys.stderr)
191         sys.exit(1)
192
193     print(read_ms_ldif(display_specifiers_file))
194