-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ok, let's just duplicate the code then
- Loading branch information
1 parent
7b6bb5a
commit de5fbcc
Showing
1 changed file
with
41 additions
and
1 deletion.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/usr/bin/env python3 | ||
import sys | ||
import ldif | ||
from collections import OrderedDict | ||
|
||
|
||
def kcmp(item): | ||
(key, v) = item | ||
parts = key.split(',')[::-1] | ||
new_key = ','.join(parts) | ||
return (new_key, v) | ||
|
||
|
||
def freeze(o): | ||
if isinstance(o, dict): | ||
return OrderedDict({k: freeze(v) for k, v in sorted(o.items(), key=kcmp)}.items()) | ||
if isinstance(o, list): | ||
return sorted([freeze(v) for v in o]) | ||
return o.decode('utf-8') | ||
|
||
|
||
def my_print(o, depth): | ||
if isinstance(o, OrderedDict): | ||
for k, v in o.items(): | ||
my_print(k, depth) | ||
my_print(v, depth + 2) | ||
elif isinstance(o, list): | ||
for v in o: | ||
my_print(v, depth) | ||
else: | ||
print(f"{' ' * depth}{o}") | ||
|
||
|
||
ldifparser = ldif.LDIFRecordList(sys.stdin) | ||
ldifparser.parse() | ||
|
||
data = {k: v for k, v in ldifparser.all_records} | ||
f = freeze(data) | ||
|
||
# print(json.dumps(f, indent=2)) | ||
my_print(f, 0) |