|
1 |
| -#!/usr/bin/env python |
2 |
| -# -*- coding: utf-8 -*- |
| 1 | +from __future__ import print_function, unicode_literals |
3 | 2 |
|
4 |
| -from __future__ import absolute_import, print_function, unicode_literals |
5 |
| - |
6 |
| -from .exceptions import (SuppressedError, |
7 |
| - TooManyValues) |
8 |
| -from .messages import (not_enough_values, |
9 |
| - overwrite_message, |
10 |
| - no_more_rows) |
11 |
| - |
12 |
| - |
13 |
| -# Todo: Add remove_column and remove row |
14 |
| - |
15 |
| - |
16 |
| -class Table: |
17 |
| - """docstring for Table. |
18 |
| - This is the main class. It adds rows and columns, with data |
19 |
| -
|
20 |
| - Usage: |
21 |
| -
|
22 |
| - >>> t = Table('name') |
23 |
| -
|
24 |
| - >>> t.all_columns('column_name') |
25 |
| -
|
26 |
| - >>> t.add_row([value, value, value, ...]) |
27 |
| -
|
28 |
| - >>> table_code = t.table #gets table code |
29 |
| -
|
30 |
| - """ |
31 |
| - |
32 |
| - def __init__(self, name: str, debug: bool = True): # creates self variables |
33 |
| - """ |
34 |
| - The initiator of the Table() class. Creates all the initial self values |
35 |
| -
|
36 |
| - :type name: str |
37 |
| - :type debug: bool |
38 |
| - :param name: The name of the first column |
39 |
| - :param debug: Do you want to enable debug function? |
40 |
| - """ |
41 |
| - super(Table, self).__init__() # idk |
42 |
| - self.to_debug = debug # debug? |
43 |
| - self.rows = 0 # rows |
44 |
| - self.columns = 1 # columns |
45 |
| - self.table = '''|{}|'''.format(str(name)) |
46 |
| - self.finalized = False |
47 |
| - if self.to_debug: |
48 |
| - self.functions = [] |
49 |
| - self.finalized_run = False |
50 |
| - |
51 |
| - def debug(self, print_d=True): |
52 |
| - """ |
53 |
| -
|
54 |
| - :raise: SuppressedError |
55 |
| - :type print_d: bool |
56 |
| - :param print_d: Print the debug or return as string |
57 |
| - :return: The debug value |
58 |
| - :rtype: str |
59 |
| - """ |
60 |
| - global debugmessage |
61 |
| - try: |
62 |
| - debugmessage = '''Printing debug information: |
63 |
| - Rows: {rows} |
64 |
| - Columns: {cols} |
65 |
| - Finalized?: {fin} |
66 |
| - Table Content: {table} |
67 |
| - Functions: {funcs}'''.format(rows=str(self.rows), |
68 |
| - cols=str(self.columns), |
69 |
| - fin=str(self.finalized), |
70 |
| - table=self.table, |
71 |
| - funcs=self.functions) |
72 |
| - if print_d: |
73 |
| - print(debugmessage) |
74 |
| - else: |
75 |
| - return debugmessage |
76 |
| - except NameError: |
77 |
| - pass |
78 |
| - except Exception as e: |
79 |
| - raise SuppressedError(type(e).__name__, str(e), debugmessage) |
80 |
| - |
81 |
| - def add_column(self, name: str, all_cols: bool = False): |
82 |
| - """ |
83 |
| - Adds a column to the table. Must be used before adding rows. |
84 |
| -
|
85 |
| - :type all_cols: bool |
86 |
| - :type name: str |
87 |
| - :param name: The name of the columns |
88 |
| - :param all_cols: Determines if all_columns() called add_column() |
89 |
| - :return: Nothing |
90 |
| - :rtype: None |
91 |
| - :raise: SuppressedError |
92 |
| - """ |
93 |
| - self.columns += 1 |
94 |
| - self.table += '{}|'.format(str(name)) |
95 |
| - try: |
96 |
| - if all_cols: |
97 |
| - return {'function': 'add_column', 'data': [str(name)]} |
98 |
| - else: |
99 |
| - self.functions.append({'function': 'add_column', 'data': [str(name)]}) |
100 |
| - except NameError: |
101 |
| - pass |
102 |
| - except Exception as e: |
103 |
| - raise SuppressedError(type(e).__name__, str(e), self.debug(print_d=False)) |
104 |
| - |
105 |
| - def all_columns(self, *args): |
106 |
| - """ |
107 |
| - Adds all columns, as many as you want |
108 |
| -
|
109 |
| - :type args: str |
110 |
| - :param args: The names of every column. Can be a list |
111 |
| - :return: Nothing |
112 |
| - :rtype: None |
113 |
| - :raise: SuppressedError |
114 |
| - """ |
115 |
| - if isinstance(args[0], list): |
116 |
| - self.all_columns_with_list(args[0]) |
117 |
| - else: |
118 |
| - try: |
119 |
| - all_col_data = {'function': 'all_columns', 'data': []} |
120 |
| - for value in args: |
121 |
| - all_col_data['data'].append(self.add_column(str(value), all_cols=True)) |
122 |
| - self.functions.append(all_col_data) |
123 |
| - except Exception as e: |
124 |
| - raise SuppressedError(type(e).__name__, str(e), self.debug(print_d=False)) |
125 |
| - |
126 |
| - def all_columns_with_list(self, list): |
127 |
| - """ |
128 |
| -
|
129 |
| - :param list: list |
130 |
| - :return: None |
131 |
| - """ |
132 |
| - try: |
133 |
| - all_col_data = {'function': 'all_columns', 'data': []} |
134 |
| - for value in list: |
135 |
| - all_col_data['data'].append(self.add_column(str(value), all_cols=True)) |
136 |
| - self.functions.append(all_col_data) |
137 |
| - except Exception as e: |
138 |
| - raise SuppressedError(type(e).__name__, str(e), self.debug(print_d=False)) |
139 |
| - |
140 |
| - def finalize_cols(self): |
141 |
| - """ |
142 |
| - Finalizes columns. Can be called manually, but usually called by the first add_row() argument. |
143 |
| -
|
144 |
| - :return: Nothing |
145 |
| - :rtype: None |
146 |
| - :raise: SuppressedError |
147 |
| - """ |
148 |
| - try: |
149 |
| - finalizer = '\n|' |
150 |
| - for i in range(self.columns): |
151 |
| - finalizer += '---|' |
152 |
| - self.table += finalizer |
153 |
| - self.functions.append({'function': 'finalize_cols', 'data': finalizer}) |
154 |
| - except Exception as e: |
155 |
| - raise SuppressedError(type(e).__name__, str(e), self.debug(print_d=False)) |
156 |
| - |
157 |
| - def add_row(self, *args, show_warning_message: bool = True): |
158 |
| - """ |
159 |
| - Adds rows, one for each arg. A row may be in string or list form. If too little arguments are in the list, |
160 |
| - then the rest will be blanked. If there are too many errors, an exception will be raised. |
161 |
| -
|
162 |
| - :type args: str |
163 |
| - :type show_warning_message: bool |
164 |
| - :param show_warning_message: Shows warning messages if there is an exception. |
165 |
| - :param args: The values for the row. |
166 |
| - :return: Nothing |
167 |
| - :rtype: None |
168 |
| - :raises: SuppressedError, TooManyValues |
169 |
| - """ |
170 |
| - if isinstance(args[0], list): |
171 |
| - self.add_row_with_list(args[0], show_warning_message=show_warning_message) |
172 |
| - else: |
173 |
| - try: |
174 |
| - if self.finalized_run: |
175 |
| - self.finalized_run = False |
176 |
| - if not self.finalized: |
177 |
| - self.finalize_cols() |
178 |
| - self.finalized_run = True |
179 |
| - self.finalized = True |
180 |
| - add_row_data = {'function': 'add_row', |
181 |
| - 'data': {'finalized_run': self.finalized_run, |
182 |
| - 'show_warning_message': show_warning_message, |
183 |
| - 'values': []}} |
184 |
| - self.rows += 1 |
185 |
| - row = '|' |
186 |
| - rows_made = 0 |
187 |
| - for i in range(int(len(args))): |
188 |
| - row += '{}|'.format(str(args[i])) |
189 |
| - rows_made += 1 |
190 |
| - add_row_data['data']['values'].append(args[i]) |
191 |
| - if self.columns > rows_made: |
192 |
| - if show_warning_message: |
193 |
| - print(not_enough_values(rows_made, self.columns)) |
194 |
| - add_row_data['data']['message_shown'] = True |
195 |
| - for i in range(int(self.columns - rows_made)): |
196 |
| - row += ' |' |
197 |
| - # noinspection PyTypeChecker |
198 |
| - add_row_data['data']['values'].append('{} blank values added'.format(str(self.columns - rows_made))) |
199 |
| - elif self.columns < rows_made: |
200 |
| - raise TooManyValues(self.columns) |
201 |
| - self.table += '\n{}'.format(row) |
202 |
| - self.functions.append(add_row_data) |
203 |
| - except TooManyValues: |
204 |
| - raise |
205 |
| - except Exception as e: |
206 |
| - raise SuppressedError(type(e).__name__, str(e), self.debug(print_d=False)) |
207 |
| - |
208 |
| - def add_row_with_list(self, li: list, show_warning_message: bool = True): |
209 |
| - """ |
210 |
| - Adds a row based on a list. |
211 |
| -
|
212 |
| - :type show_warning_message: bool |
213 |
| - :type li: list |
214 |
| - :param show_warning_message: Shows the debug message if there is an exception. |
215 |
| - :param li: The list to be used to add values |
216 |
| - :return: Nothing |
217 |
| - :rtype: None |
218 |
| - :raise: SuppressedError |
219 |
| - """ |
220 |
| - try: |
221 |
| - if self.finalized_run: |
222 |
| - self.finalized_run = False |
223 |
| - if not self.finalized: |
224 |
| - self.finalize_cols() |
225 |
| - self.finalized_run = True |
226 |
| - self.finalized = True |
227 |
| - add_row_data = {'function': 'add_row', |
228 |
| - 'data': {'finalized_run': self.finalized_run, |
229 |
| - 'show_warning_message': show_warning_message, |
230 |
| - 'values': []}} |
231 |
| - self.rows += 1 |
232 |
| - row = '|' |
233 |
| - rows_made = 0 |
234 |
| - for i in range(int(len(li))): |
235 |
| - row += '{}|'.format(str(li[i])) |
236 |
| - rows_made += 1 |
237 |
| - add_row_data['data']['values'].append(li[i]) |
238 |
| - if self.columns > rows_made: |
239 |
| - if show_warning_message: |
240 |
| - print(not_enough_values(rows_made, self.columns)) |
241 |
| - add_row_data['data']['message_shown'] = True |
242 |
| - for i in range(int(self.columns - rows_made)): |
243 |
| - row += ' |' |
244 |
| - # noinspection PyTypeChecker |
245 |
| - add_row_data['data']['values'].append('{} blank values added'.format(str(self.columns - rows_made))) |
246 |
| - elif self.columns < rows_made: |
247 |
| - raise TooManyValues(self.columns) |
248 |
| - self.table += '\n{}'.format(row) |
249 |
| - self.functions.append(add_row_data) |
250 |
| - except TooManyValues: |
251 |
| - raise |
252 |
| - except Exception as e: |
253 |
| - raise SuppressedError(type(e).__name__, str(e), self.debug(print_d=False)) |
254 |
| - |
255 |
| - def remove_row(self): |
256 |
| - lines = self.table.split('\n') |
257 |
| - last_line = lines[len(lines)] |
258 |
| - new_table = '' |
259 |
| - if '|---|' not in last_line: |
260 |
| - lines.remove(lines[len(lines)]) |
261 |
| - for line in lines: |
262 |
| - new_table += line + '\n' |
263 |
| - else: |
264 |
| - print(no_more_rows) |
265 |
| - |
266 |
| - def export_table_to_file(self, filename: str = 'markdowntable', extension: str = 'txt', mode: str = 'w'): |
267 |
| - """ |
268 |
| -
|
269 |
| - :type mode: str |
270 |
| - :type extension: str |
271 |
| - :type filename: str |
272 |
| - :param filename: The filename to use |
273 |
| - :param extension: The extension to use |
274 |
| - :param mode: The mode to write in, usually write and read |
275 |
| - :return: Nothing |
276 |
| - :rtype: None |
277 |
| - :raise: SuppressedError |
278 |
| - """ |
279 |
| - try: |
280 |
| - with open('{fname}.{ext}'.format(fname=str(filename), ext=str(extension)), str(mode)) as file: |
281 |
| - file.write(self.table) |
282 |
| - self.functions.append({'function': 'export_table_to_file', |
283 |
| - 'data': {'filename': filename, 'extension': extension, 'writemode': mode}}) |
284 |
| - except Exception as e: |
285 |
| - raise SuppressedError(type(e).__name__, str(e), self.debug(print_d=False)) |
| 3 | +import markdowntable.errors |
| 4 | +from markdowntable.row import Row, Column |
| 5 | +from markdowntable.table import Table |
0 commit comments