Skip to content

Commit

Permalink
GH-46: Add flake8 config
Browse files Browse the repository at this point in the history
- Format mdutils source files.
- Format mdutils tests files.
  • Loading branch information
didix21 committed Mar 12, 2023
1 parent f4562f9 commit ea587c9
Show file tree
Hide file tree
Showing 23 changed files with 1,092 additions and 640 deletions.
6 changes: 6 additions & 0 deletions .flake8
@@ -0,0 +1,6 @@
[flake8]
max-line-length = 120
exclude =
.git,
build,
dist
3 changes: 2 additions & 1 deletion .vscode/settings.json
Expand Up @@ -3,5 +3,6 @@
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
"python.testing.pytestEnabled": true,
"python.formatting.provider": "black"
}
6 changes: 5 additions & 1 deletion codecov.yml
Expand Up @@ -3,4 +3,8 @@ coverage:
project:
default:
target: 80% # the required coverage value
threshold: 1%
threshold: 5%
patch:
default:
target: 80%
threshold: 5%
50 changes: 29 additions & 21 deletions mdutils/fileutils/fileutils.py
Expand Up @@ -7,6 +7,7 @@
# MIT License: (C) 2018 Dídac Coll
from typing import Optional


class MarkDownFile(object):
"""MarkDownFile class creates a new file of MarkDown extension.
Expand All @@ -15,49 +16,56 @@ class MarkDownFile(object):
- Rewrite a file with new data.
- Write at the end of the file."""

def __init__(self, name='', dirname: Optional[str] = None):
def __init__(self, name="", dirname: Optional[str] = None):
"""Creates a markdown file, if name is not empty.
:param str name: provide the file name or a path joinly with the file name. For example: `./my-path/my-file.md`.
:param str dirname: use dirname if you want to provide the path separately from the name.
:param str dirname: use dirname if you want to provide the path separately from the name.
If not you can specify the path directly on the name."""
if name:
self.dirname = dirname
self.file_name = self._get_file_name(name, dirname)
self.file = open(f'{self.file_name}', 'w+', encoding='UTF-8')
self.file = open(f"{self.file_name}", "w+", encoding="UTF-8")
self.file.close()

def _get_file_name(self, name: str, dirname: Optional[str] = None ) -> str:
if dirname:
return f'{dirname}/{name}' if name.endswith('.md') else f'{dirname}/{name}.md'

return name if name.endswith('.md') else f'{name}.md'
def _get_file_name(self, name: str, dirname: Optional[str] = None) -> str:
if dirname:
return (
f"{dirname}/{name}" if name.endswith(".md") else f"{dirname}/{name}.md"
)

return name if name.endswith(".md") else f"{name}.md"

def rewrite_all_file(self, data: str):
"""Rewrite all the data of a Markdown file by ``data``.
:param str data: is a string containing all the data that is written in the markdown file."""
with open(f'{self.file_name}', 'w', encoding='utf-8') as self.file:
:param str data: is a string containing all the data that is written in the markdown file.
"""
with open(f"{self.file_name}", "w", encoding="utf-8") as self.file:
self.file.write(data)

def append_end(self, data: str):
"""Write at the last position of a Markdown file.
:param str data: is a string containing all the data that is written in the markdown file."""
with open(f'{self.file_name}', 'a', encoding='utf-8') as self.file:
:param str data: is a string containing all the data that is written in the markdown file.
"""
with open(f"{self.file_name}", "a", encoding="utf-8") as self.file:
self.file.write(data)

def append_after_second_line(self, data: str):
"""Write after the file's first line.
:param str data: is a string containing all the data that is written in the markdown file."""
with open(f'{self.file_name}', 'r+', encoding='utf-8') as self.file:
:param str data: is a string containing all the data that is written in the markdown file.
"""
with open(f"{self.file_name}", "r+", encoding="utf-8") as self.file:
file_data = self.file.read() # Save all the file's content
self.file.seek(0, 0) # Place file pointer at the beginning
first_line = self.file.readline() # Read the first line
second_line = self.file.readline() # Read the second line
self.file.seek(len(first_line + second_line), 0) # Place file pointer at the end of the first line
self.file.seek(
len(first_line + second_line), 0
) # Place file pointer at the end of the first line
self.file.write(data) # Write data
self.file.write('\n' + file_data[len(first_line + second_line):])
self.file.write("\n" + file_data[len(first_line + second_line):])

@staticmethod
def read_file(file_name: str) -> str:
Expand All @@ -68,15 +76,15 @@ def read_file(file_name: str) -> str:
:return: return all file's data.
:rtype: str"""

if file_name.find('.md') == -1:
file_name += '.md'
if file_name.find(".md") == -1:
file_name += ".md"

with open(file_name, 'r', encoding='utf-8') as file:
with open(file_name, "r", encoding="utf-8") as file:
file_data = file.read()

return file_data


if __name__ == '__main__':
new_file = MarkDownFile('Example')
if __name__ == "__main__":
new_file = MarkDownFile("Example")
new_file.rewrite_all_file(data="# Some Text Example")

0 comments on commit ea587c9

Please sign in to comment.