77# MIT License: (C) 2018 Dídac Coll
88from typing import Optional
99
10+
1011class MarkDownFile (object ):
1112 """MarkDownFile class creates a new file of MarkDown extension.
1213
@@ -15,49 +16,56 @@ class MarkDownFile(object):
1516 - Rewrite a file with new data.
1617 - Write at the end of the file."""
1718
18- def __init__ (self , name = '' , dirname : Optional [str ] = None ):
19+ def __init__ (self , name = "" , dirname : Optional [str ] = None ):
1920 """Creates a markdown file, if name is not empty.
2021 :param str name: provide the file name or a path joinly with the file name. For example: `./my-path/my-file.md`.
21- :param str dirname: use dirname if you want to provide the path separately from the name.
22+ :param str dirname: use dirname if you want to provide the path separately from the name.
2223 If not you can specify the path directly on the name."""
2324 if name :
2425 self .dirname = dirname
2526 self .file_name = self ._get_file_name (name , dirname )
26- self .file = open (f' { self .file_name } ' , 'w+' , encoding = ' UTF-8' )
27+ self .file = open (f" { self .file_name } " , "w+" , encoding = " UTF-8" )
2728 self .file .close ()
2829
29- def _get_file_name (self , name : str , dirname : Optional [str ] = None ) -> str :
30- if dirname :
31- return f'{ dirname } /{ name } ' if name .endswith ('.md' ) else f'{ dirname } /{ name } .md'
32-
33- return name if name .endswith ('.md' ) else f'{ name } .md'
30+ def _get_file_name (self , name : str , dirname : Optional [str ] = None ) -> str :
31+ if dirname :
32+ return (
33+ f"{ dirname } /{ name } " if name .endswith (".md" ) else f"{ dirname } /{ name } .md"
34+ )
35+
36+ return name if name .endswith (".md" ) else f"{ name } .md"
3437
3538 def rewrite_all_file (self , data : str ):
3639 """Rewrite all the data of a Markdown file by ``data``.
3740
38- :param str data: is a string containing all the data that is written in the markdown file."""
39- with open (f'{ self .file_name } ' , 'w' , encoding = 'utf-8' ) as self .file :
41+ :param str data: is a string containing all the data that is written in the markdown file.
42+ """
43+ with open (f"{ self .file_name } " , "w" , encoding = "utf-8" ) as self .file :
4044 self .file .write (data )
4145
4246 def append_end (self , data : str ):
4347 """Write at the last position of a Markdown file.
4448
45- :param str data: is a string containing all the data that is written in the markdown file."""
46- with open (f'{ self .file_name } ' , 'a' , encoding = 'utf-8' ) as self .file :
49+ :param str data: is a string containing all the data that is written in the markdown file.
50+ """
51+ with open (f"{ self .file_name } " , "a" , encoding = "utf-8" ) as self .file :
4752 self .file .write (data )
4853
4954 def append_after_second_line (self , data : str ):
5055 """Write after the file's first line.
5156
52- :param str data: is a string containing all the data that is written in the markdown file."""
53- with open (f'{ self .file_name } ' , 'r+' , encoding = 'utf-8' ) as self .file :
57+ :param str data: is a string containing all the data that is written in the markdown file.
58+ """
59+ with open (f"{ self .file_name } " , "r+" , encoding = "utf-8" ) as self .file :
5460 file_data = self .file .read () # Save all the file's content
5561 self .file .seek (0 , 0 ) # Place file pointer at the beginning
5662 first_line = self .file .readline () # Read the first line
5763 second_line = self .file .readline () # Read the second line
58- self .file .seek (len (first_line + second_line ), 0 ) # Place file pointer at the end of the first line
64+ self .file .seek (
65+ len (first_line + second_line ), 0
66+ ) # Place file pointer at the end of the first line
5967 self .file .write (data ) # Write data
60- self .file .write (' \n ' + file_data [len (first_line + second_line ):])
68+ self .file .write (" \n " + file_data [len (first_line + second_line ):])
6169
6270 @staticmethod
6371 def read_file (file_name : str ) -> str :
@@ -68,15 +76,15 @@ def read_file(file_name: str) -> str:
6876 :return: return all file's data.
6977 :rtype: str"""
7078
71- if file_name .find (' .md' ) == - 1 :
72- file_name += ' .md'
79+ if file_name .find (" .md" ) == - 1 :
80+ file_name += " .md"
7381
74- with open (file_name , 'r' , encoding = ' utf-8' ) as file :
82+ with open (file_name , "r" , encoding = " utf-8" ) as file :
7583 file_data = file .read ()
7684
7785 return file_data
7886
7987
80- if __name__ == ' __main__' :
81- new_file = MarkDownFile (' Example' )
88+ if __name__ == " __main__" :
89+ new_file = MarkDownFile (" Example" )
8290 new_file .rewrite_all_file (data = "# Some Text Example" )
0 commit comments