Skip to content

Commit

Permalink
[AdminTL#79] Doc Generator: support 5 level
Browse files Browse the repository at this point in the history
  • Loading branch information
mathben committed Apr 4, 2018
1 parent 0207090 commit 3be7580
Showing 1 changed file with 108 additions and 9 deletions.
117 changes: 108 additions & 9 deletions src/web/py_class/doc_generator_google_drive_spreadsheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,13 @@ def generate_manual(self):
return {"error": str_error}

# Validate the header
expected_header_row = ["Title H1", "Title H1 HTML", "Description H1", "Bullet Description H1",
"Second Bullet Description H1",
"Title H2", "Title H2 HTML", "Description H2", "Bullet Description H2",
"Second Bullet Description H2"]
expected_header_row = [
"Title H1", "Title H1 HTML", "Description H1", "Bullet Description H1", "Second Bullet Description H1",
"Title H2", "Title H2 HTML", "Description H2", "Bullet Description H2", "Second Bullet Description H2",
"Title H3", "Title H3 HTML", "Description H3", "Bullet Description H3", "Second Bullet Description H3",
"Title H4", "Title H4 HTML", "Description H4", "Bullet Description H4", "Second Bullet Description H4",
"Title H5", "Title H5 HTML", "Description H5", "Bullet Description H5", "Second Bullet Description H5"
]
header_row = manual_sheet.row_values(1)
if expected_header_row != header_row:
str_error = "Header of sheet is %s, and expected is %s" % (header_row, expected_header_row)
Expand Down Expand Up @@ -179,26 +182,42 @@ def _parse_manual_doc(self, all_values):
"""
lst_manual_section = []
line_number = 1
first_section = None
second_section = None
third_section = None
fourth_section = None

for row in all_values[1:]:
line_number += 1
is_first_section = bool(row[0]) or bool(row[1]) or bool(row[2]) or bool(row[3]) or bool(row[4])
is_second_section = bool(row[5]) or bool(row[6]) or bool(row[7]) or bool(row[8]) or bool(row[9])
is_third_section = bool(row[10]) or bool(row[11]) or bool(row[12]) or bool(row[13]) or bool(row[14])
is_fourth_section = bool(row[15]) or bool(row[16]) or bool(row[17]) or bool(row[18]) or bool(row[19])
is_fifth_section = bool(row[20]) or bool(row[21]) or bool(row[22]) or bool(row[23]) or bool(row[24])

# Check error
# TODO validate cannot has more than 1 section at same time
if is_first_section and is_second_section:
error = {"error": "L.%s: Cannot contain H1 and H2 on same line." % line_number}
return error
sum_section = sum(
(is_first_section, is_second_section, is_third_section, is_fourth_section, is_fifth_section))

if not is_first_section and not is_second_section:
if sum_section == 0:
# Ignore empty line
continue

if sum_section > 1:
error = {"error": "L.%s: Cannot contain more than 1 section at time. "
"H1: %s, H2: %s, H3: %s, H4: %s, H5: %s." % (
line_number, is_first_section, is_second_section, is_third_section,
is_fourth_section,
is_fifth_section)}
return error

if is_first_section:
status = self._extract_section(0, row, line_number, lst_manual_section)

elif is_second_section:
second_section = None
third_section = None
fourth_section = None
first_section = lst_manual_section[-1]

# Get section from last section
Expand All @@ -210,6 +229,86 @@ def _parse_manual_doc(self, all_values):

self._extract_section(1, row, line_number, lst_section)

elif is_third_section:
third_section = None
fourth_section = None
if not first_section:
error = {"error": "L.%s: Missing section H1 to insert section H3." % line_number}
return error

lst_section = first_section.get("section")
if not lst_section:
error = {"error": "L.%s: Missing section H2 to insert section H3." % line_number}
return error

second_section = lst_section[-1]

# Get section from last section
if "section" in second_section:
lst_section = second_section.get("section")
else:
lst_section = []
second_section["section"] = lst_section

self._extract_section(2, row, line_number, lst_section)

elif is_fourth_section:
fourth_section = None
if not first_section:
error = {"error": "L.%s: Missing section H1 to insert section H4." % line_number}
return error

if not second_section:
error = {"error": "L.%s: Missing section H2 to insert section H4." % line_number}
return error

# Create third_section
lst_section = second_section.get("section")
if not lst_section:
error = {"error": "L.%s: Missing section H3 to insert section H4." % line_number}
return error

third_section = lst_section[-1]

# Get section from last section
if "section" in third_section:
lst_section = third_section.get("section")
else:
lst_section = []
third_section["section"] = lst_section

self._extract_section(3, row, line_number, lst_section)

elif is_fifth_section:
if not first_section:
error = {"error": "L.%s: Missing section H1 to insert section H5." % line_number}
return error

if not second_section:
error = {"error": "L.%s: Missing section H2 to insert section H5." % line_number}
return error

if not third_section:
error = {"error": "L.%s: Missing section H3 to insert section H5." % line_number}
return error

# Create third_section
lst_section = third_section.get("section")
if not lst_section:
error = {"error": "L.%s: Missing section H4 to insert section H5." % line_number}
return error

fourth_section = lst_section[-1]

# Get section from last section
if "section" in fourth_section:
lst_section = fourth_section.get("section")
else:
lst_section = []
fourth_section["section"] = lst_section

self._extract_section(4, row, line_number, lst_section)

# If error, return it
if type(status) is dict and "error" in status:
return status
Expand Down

0 comments on commit 3be7580

Please sign in to comment.