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
|
# Copyright (c) 2011 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import sys
def main():
"""Converts a vertical serialization into a compact, horizontal serialization.
"""
COLUMNS = ['First name', 'Middle name', 'Last name', 'Email', 'Company name',
'Address line 1', 'Address line 2', 'City', 'State', 'ZIP code',
'Country', 'Phone']
if len(sys.argv) != 2:
print "Usage: python flatten.py <path/to/serialized_profiles>"
return
profiles = [COLUMNS]
with open(sys.argv[1], 'r') as serialized_profiles:
profile = []
previous_field_type = ''
for line in serialized_profiles:
# Trim the newline if present.
if line[-1] == '\n':
line = line[:-1]
if line == "---":
if len(profile):
# Reached the end of a profile.
# Save the current profile and prepare to build up the next one.
profiles.append(profile)
profile = []
else:
# Append the current field's value to the current profile.
line_parts = line.split(': ', 1)
field_type = line_parts[0]
field_value = line_parts[1]
if field_type != previous_field_type:
profile.append("'%s'" % field_value)
else:
# This is a non-primary value for a multi-valued field.
profile[-1] += ", '%s'" % field_value
previous_field_type = field_type
if len(profile):
profiles.append(profile)
# Prepare format strings so that we can align the contents of each column.
transposed = zip(*profiles)
column_widths = []
for column in transposed:
widths = [len(value) for value in column]
column_widths.append(max(widths))
column_formats = ["{0:<" + str(width) + "}" for width in column_widths]
for profile in profiles:
profile_format = zip(column_formats, profile)
profile = [format_.format(value) for (format_, value) in profile_format]
print " | ".join(profile)
if __name__ == '__main__':
main()
|