Skip to content
This repository was archived by the owner on Dec 21, 2023. It is now read-only.

Commit a8ba143

Browse files
author
PokestarFan
committed
Add changes
1 parent 3a333f8 commit a8ba143

File tree

5 files changed

+191
-66
lines changed

5 files changed

+191
-66
lines changed

Diff for: .gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,6 @@ setupcommands.bat
112112

113113
#.anything
114114
.*
115+
116+
#logs
117+
logs*

Diff for: markdowntable/__init__.py

+124-58
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,156 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4-
from __future__ import print_function, unicode_literals
5-
from .exceptions import SupressedError
4+
from __future__ import absolute_import, print_function, unicode_literals
65

7-
class Table():
6+
import io
7+
8+
from .exceptions import (SuppressedError,
9+
TooManyValues,
10+
DoNotOverwrite)
11+
12+
13+
class Table:
814
"""docstring for Table.
915
This is the main class. It adds rows and columns, with data
1016
"""
11-
def __init__(self, name, debug = True):
17+
18+
def __init__(self, name, debug=True):
1219
super(Table, self).__init__()
13-
self.debug = debug
20+
self.todebug = debug
1421
self.rows = 0
1522
self.columns = 1
1623
self.table = '''|{}|'''.format(str(name))
1724
self.finalized = False
18-
if self.debug:
25+
if self.todebug:
1926
self.functions = []
27+
self.finalized_run = False
2028

21-
def debug(self):
29+
def debug(self, print_d=True):
30+
"""
31+
:rtype: str
32+
"""
33+
global dmessage
2234
try:
23-
print('''Printing debug information:
35+
dmessage = '''Printing debug information:
2436
Rows: {rows}
2537
Columns: {cols}
2638
Finalized?: {fin}
2739
Table Content: {table}
28-
Functions: {funcs}'''.format(rows = str(self.rows),
29-
cols = str(self.columns),
30-
fin = str(self.finalized),
31-
table = self.table,
32-
funcs = self.functions))
33-
except(NameError):
34-
pass
35-
except Exception as e:
40+
Functions: {funcs}'''.format(rows=str(self.rows),
41+
cols=str(self.columns),
42+
fin=str(self.finalized),
43+
table=self.table,
44+
funcs=self.functions)
45+
if print_d:
46+
print(dmessage)
47+
else:
48+
return dmessage
49+
except NameError:
50+
pass
51+
except Exception as e:
52+
raise SuppressedError(type(e).__name__, str(e), dmessage)
3653

37-
def add_column(self, name, all_cols = False):
54+
def add_column(self, name, all_cols=False):
3855
self.columns += 1
3956
self.table += '{}|'.format(str(name))
4057
try:
4158
if all_cols:
42-
return {'function':'add_column', 'data': [str(name)]}
59+
return {'function': 'add_column', 'data': [str(name)]}
4360
else:
44-
self.functions.append({'function':'add_column', 'data': [str(name)]})
45-
except(NameError):
61+
self.functions.append({'function': 'add_column', 'data': [str(name)]})
62+
except NameError:
4663
pass
64+
except Exception as e:
65+
raise SuppressedError(type(e).__name__, str(e), self.debug(print_d=False))
4766

4867
def all_columns(self, *args):
49-
all_col_data = {'function':'all_columns', 'data': []}
50-
for value in args:
51-
all_col_data['data'].append(self.add_column(str(value), all_cols = True))
68+
try:
69+
all_col_data = {'function': 'all_columns', 'data': []}
70+
for value in args:
71+
all_col_data['data'].append(self.add_column(str(value), all_cols=True))
72+
self.functions.append(all_col_data)
73+
except Exception as e:
74+
raise SuppressedError(type(e).__name__, str(e), self.debug(print_d=False))
5275

5376
def finalize_cols(self):
54-
finalizer = '\n|'
55-
for i in range(self.columns):
56-
finalizer += '---|'
57-
self.table += finalizer
58-
59-
def add_row(self,show_warning_message = True, *args):
60-
if not self.finalized:
61-
self.finalize_cols()
62-
self.finalized = True
63-
self.rows += 1
64-
row = '|'
65-
rows_made = 0
66-
for i in range(int(len(args))):
67-
row += '{}|'.format(str(args[i]))
68-
rows_made += 1
69-
if self.columns > rows_made:
70-
if show_warning_message:
71-
print('You did not enter in enough values. You entered in {} values out of {} values. The values that you did not enter in will be filled in with a blank space. You can stop this message from appearing by adding the argument show_warning_message = False'.format(str(rows_made), str(self.columns)))
72-
for i in range(int(self.columns-rows_made)):
73-
row += ' |'
74-
elif self.columns < rows_made:
75-
raise AssertionError('You entered in too many row values. Please only enter {} row names.'.format(str(self.columns)))
76-
self.table += '\n{}'.format(row)
77+
try:
78+
finalizer = '\n|'
79+
for i in range(self.columns):
80+
finalizer += '---|'
81+
self.table += finalizer
82+
self.functions.append({'function': 'finalize_cols', 'data': finalizer})
83+
except Exception as e:
84+
raise SuppressedError(type(e).__name__, str(e), self.debug(print_d=False))
7785

86+
def add_row(self, show_warning_message=True, *args):
87+
try:
88+
if self.finalized_run:
89+
self.finalized_run = False
90+
if not self.finalized:
91+
self.finalize_cols()
92+
self.finalized_run = True
93+
self.finalized = True
94+
add_row_data = {'function': 'add_row',
95+
'data': {'finalized_run': self.finalized_run,
96+
'show_warning_message': show_warning_message,
97+
'values': []}}
98+
self.rows += 1
99+
row = '|'
100+
rows_made = 0
101+
for i in range(int(len(args))):
102+
row += '{}|'.format(str(args[i]))
103+
rows_made += 1
104+
add_row_data['data']['values'].append(args[i])
105+
if self.columns > rows_made:
106+
if show_warning_message:
107+
print(
108+
"You did not enter in enough values. You entered in {} values out of {} values. The values "
109+
"that you did not enter in will be filled in with a blank space. You can stop this message "
110+
"from appearing by adding the argument show_warning_message = False".format(
111+
str(rows_made), str(self.columns)))
112+
add_row_data['data']['message_shown'] = True
113+
for i in range(int(self.columns - rows_made)):
114+
row += ' |'
115+
add_row_data['data']['values'].append('{} blank values added'.format(str(int(self.columns) - int(
116+
rows_made))))
117+
elif self.columns < rows_made:
118+
raise TooManyValues(self.columns)
119+
self.table += '\n{}'.format(row)
120+
self.functions.append(add_row_data)
121+
except TooManyValues:
122+
raise
123+
except Exception as e:
124+
raise SuppressedError(type(e).__name__, str(e), self.debug(print_d=False))
78125

79-
def export_table_to_file(self, filename = 'markdowntable', extension='txt', mode = 'w+', overwrite_warning = True):
80-
with open('{fname}.{ext}'.format(fname = str(filename), ext = str(extension)),str(mode)) as file:
81-
try:
82-
contents = file.read()
83-
if mode == 'w' or mode == 'w+':
84-
mode_check = True
85-
if len(contents) > 0 and overwrite_warning and mode_check:
86-
print('This file already contains content. Do you want to overwrite the contents of the file? You can add the argument mode = a[+] to not overwrite.')
87-
except(io.UnsupportedOperation):
88-
pass
89-
file.write(table)
90-
return True
126+
def export_table_to_file(self, filename='markdowntable', extension='txt', mode='w+', overwrite_warning=True):
127+
global mode_check, message_displayed, fileread
128+
try:
129+
with open('{fname}.{ext}'.format(fname=str(filename), ext=str(extension)), str(mode)) as file:
130+
try:
131+
contents = file.read()
132+
print(contents)
133+
if mode == 'w' or mode == 'w+':
134+
mode_check = True
135+
if len(contents) > 0 and overwrite_warning and mode_check:
136+
print(
137+
'This file already contains content. Do you want to overwrite the contents of the file? '
138+
'You can add the argument mode = a[+] to not overwrite.')
139+
true = True
140+
false = False
141+
overwrite = input('Write True or False:')
142+
if overwrite:
143+
raise DoNotOverwrite
144+
message_displayed = True
145+
fileread = True
146+
except io.UnsupportedOperation:
147+
pass
148+
except DoNotOverwrite:
149+
pass
150+
else:
151+
file.write(self.table)
152+
self.functions.append({'function': 'export_table_to_file',
153+
'data': {'filename': filename, 'extension': extension, 'writemode': mode,
154+
'overwrite_warning': overwrite_warning, 'file read': fileread}})
155+
except Exception as e:
156+
raise SuppressedError(type(e).__name__, str(e), self.debug(print_d=False))

Diff for: markdowntable/exceptions.py

+27-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
11
from __future__ import unicode_literals
22

3-
class SupressedError(Exception):
4-
"""docstring for SupressedError."""
5-
def __init__(self, error, message):
6-
super(SupressedError, self).__init__()
3+
4+
class MarkdownTableException(Exception):
5+
def __init__(self, debug):
6+
Exception.__init__(self,
7+
'There is an error with your Markdown Table. Printing debug information. \n {debug}'.format(
8+
debug=debug))
9+
10+
11+
class SuppressedError(MarkdownTableException):
12+
"""docstring for SuppressedError."""
13+
14+
def __init__(self, error, message, debug):
15+
"""
16+
:rtype: None
17+
"""
718
Exception.__init__(self, '''An error has occured. Printing error. information:
819
Error Name: {n}
9-
Error Message: {m}'''.format(n = error,
10-
m = message))
20+
Error Message: {m}
21+
Debug Info: {d}'''.format(n=error, m=message, d=debug))
22+
23+
24+
class TooManyValues(MarkdownTableException):
25+
def __init__(self, columns):
26+
Exception.__init__(self,
27+
'You entered in too many row values. Please only enter {} row names.'.format(str(columns)))
28+
29+
30+
class DoNotOverwrite(MarkdownTableException):
31+
pass

Diff for: quicktest.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import logging
2+
3+
from pokestarfansloggingsetup import setup_logger
4+
5+
logger = setup_logger('quicktest', loglevel=logging.DEBUG)
6+
7+
8+
def quicktest(): # this is so it can be imported.
9+
try:
10+
import markdowntable
11+
exble = markdowntable.Table('ex 1')
12+
logging.info('Run create table')
13+
exble.all_columns(1, 2, 3, 4, 5, 6, 7, 8, 9)
14+
logging.info('Run all columns')
15+
exble.add_row(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
16+
logging.info('Run add row with 10/11 values')
17+
exble.add_row(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
18+
logging.info('Run add row with 11/11 values')
19+
try:
20+
exble.add_row(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
21+
except markdowntable.exceptions.TooManyValues:
22+
logging.info('TooManyValues exception found, sucess', exc_info=True)
23+
exble.export_table_to_file(filename='test 1')
24+
logging.info('Run export table to file')
25+
exble.export_table_to_file(filename='test 2', mode='w')
26+
logging.info('Run export table to file with mode w')
27+
exble.export_table_to_file(filename='test 3', mode='a')
28+
logging.info('Run export table to file with mode a')
29+
logging.debug(exble.debug(print_d=False))
30+
except Exception as e:
31+
logging.warning('Error!', exc_info=True)
32+
33+
34+
if __name__ == '__main__':
35+
quicktest()

Diff for: setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33

44
setup(name='markdowntable',
5-
version='4.0.1.3',
5+
version='5.0.1.1',
66
description='Easy way to make markdown code for tables',
77
url='https://github.com/PokestarFan/Python-Markdown-Table',
88
author='PokestarFan',
99
author_email='[email protected]',
1010
license='MIT',
1111
packages=['markdowntable'],
12-
zip_safe=True)
12+
zip_safe=True)

0 commit comments

Comments
 (0)