#coding: utf-8
import sys
import re
in_workflow = False
try:
import workflow
in_workflow = True
except:
in_workflow = False
if in_workflow:
table_text = workflow.get_input().strip()
else:
table_text = """|Nome coluna | cd_cop | tp_cop | cd_tit |
|:------------|:------:|:------:|:------:|
|Tipo chave | PK | - | FK |
|Nulo? | | NN | NN |""".strip()
class Table:
table_text = ""
table_divider = r'[ ]{0,3}[\|\-\+\:\.][ \-\+\|\:\.]+?\|[ \-\+\|\:\.]*' # Alignment definitions
table_caption = r'[ ]{0,3}\[.*?\][ \t]*\n?' # Table caption
table_rows = r'(?:[^\n]*?\|[^\n]*?\n)+'
table_row = r'[^\n]*?\|[^\n]*?\n'
first_row = r'[ ]{0,3}\S+.*?\|.*?\n'
def __init__(self, table):
self.table_text = table + '\n'
def aligments(self):
return re.findall(self.table_divider, self.table_text)[0]
def aligments_text(self):
aligments = []
for item in self.aligments().split('|')[1:-1]:
if re.match(r'^:[-]+:$', item.strip()):
aligments.append('center')
elif re.match(r'^[:]?[-]+$', item.strip()):
aligments.append('ljust')
elif re.match(r'^[-]+:$', item.strip()):
aligments.append('rjust')
return aligments
def caption_summary(self):
return re.findall(self.table_caption, self.table_text)
def caption(self):
if len(self.caption_summary()) > 0:
return self.caption_summary()[0].strip()
return ''
def summary(self):
if len(self.caption_summary()) > 1:
return self.caption_summary()[1].strip()
return ''
def table(self):
table = []
lines = re.findall(self.table_rows, self.table_text)[0].strip().split('\n')
for line in lines:
new_line = []
columns = line.strip().split('|')[1:-1]
for column in columns:
new_column = column.strip()
new_line.append(new_column)
table.append(new_line)
return table
def rows(self):
return re.findall(self.table_row, self.table_text)[2:]
def captions(self):
return re.findall(self.first_row, self.table_text)[0].strip()
def biggest_item_len2(self, column):
biggest = -1
for line in self.table():
if len(line[column].strip()) > biggest:
biggest = len(line[column].strip())
if re.match(r'.{1}just', self.aligments_text()[column]):
return biggest + 1
else:
return biggest + 2
def biggest_item_len(self, column):
biggest = -1
item = self.captions().split('|')[1:-1][column]
if len(item.strip()) > biggest:
biggest = len(item.strip())
for line in self.rows():
line = line.strip().split('|')[1:-1]
if len(line[column].strip()) > biggest:
biggest = len(line[column].strip())
if re.match(r'.{1}just', self.aligments_text()[column]):
return biggest + 1
else:
return biggest + 2
def format(self, line, column):
text = self.table()[line][column]
size = self.biggest_item_len(column)
position = self.aligments_text()[column]
if position == 'ljust' and re.search(r'^[-|:]{2,}', text):
return ':' + '-' * (size - 1)
elif position == 'rjust' and re.search(r'^[-|:]{2,}', text):
return '-' * (size - 1) + ':'
elif position == 'center' and re.search(r'^[-|:]{2,}', text):
return ':' + '-' * (size - 2) + ':'
elif position == 'ljust':
return text.ljust(size)
elif position == 'rjust':
return text.rjust(size)
elif position == 'center':
return text.center(size)
def formatted(self):
formatted = ''
for x in xrange(0, len(self.table())):
for y in xrange(0, len(self.table()[0])):
formatted += '|' + self.format(x,y)
formatted += '|\n'
return formatted.strip()
def make_table(table_text):
t = Table(table_text)
output = ''
if t.caption():
output += t.caption() + '\n\n'
output += t.formatted()
if t.summary():
output += '\n\n' + t.summary()
return output
output = make_table(table_text)
if in_workflow:
workflow.set_output(output)
else:
print(output)
There are no comments yet.