Fixed issue #33.

This commit is contained in:
Franco Masotti 2021-11-29 12:20:03 +01:00
parent 060403cc93
commit 9b1a795e67
Signed by: frnmst
GPG Key ID: 24116ED85666780A
4 changed files with 28 additions and 11 deletions

View File

@ -34,4 +34,4 @@ twine = ">=3,<4"
pre-commit = ">=2,<3"
[packages]
fpyutils = ">=2,<3"
fpyutils = ">=2.1,<3"

View File

@ -21,6 +21,7 @@
"""The main file."""
import copy
import os
import re
import sys
@ -38,16 +39,19 @@ from .exceptions import (CannotTreatUnicodeString, GithubEmptyLinkLabel,
def write_string_on_file_between_markers(filename: str, string: str,
marker: str):
marker: str, newline_string: str = common_defaults['newline string']):
r"""Write the table of contents on a single file.
:parameter filename: the file that needs to be read or modified.
:parameter string: the string that will be written on the file.
:parameter marker: a marker that will identify the start
and the end of the string.
:parameter newline_string: the new line separator.
Defaults to ``os.linesep``.
:type filenames: str
:type string: str
:type marker: str
:type newline_string: str
:returns: None
:rtype: None
:raises: StdinIsNotAFileToBeWritten or an fpyutils exception
@ -74,11 +78,12 @@ def write_string_on_file_between_markers(filename: str, string: str,
final_string,
marker_line_positions[1],
filename,
append=False)
append=False,
newline_string=newline_string)
def write_strings_on_files_between_markers(filenames: list, strings: list,
marker: str):
marker: str, newline_string: str = common_defaults['newline string']):
r"""Write the table of contents on multiple files.
:parameter filenames: the files that needs to be read or modified.
@ -86,9 +91,12 @@ def write_strings_on_files_between_markers(filenames: list, strings: list,
string is associated with one file.
:parameter marker: a marker that will identify the start
and the end of the string.
:parameter newline_string: the new line separator.
Defaults to ``os.linesep``.
:type filenames: list
:type string: list
:type strings: list
:type marker: str
:type newline_string: str
:returns: None
:rtype: None
:raises: an fpyutils exception or a built-in exception.
@ -106,7 +114,7 @@ def write_strings_on_files_between_markers(filenames: list, strings: list,
file_id = 0
for f in filenames:
write_string_on_file_between_markers(f, strings[file_id], marker)
write_string_on_file_between_markers(f, strings[file_id], marker, newline_string)
file_id += 1
@ -143,8 +151,8 @@ def build_toc(filename: str,
Defaults to ``0```.
:parameter constant_ordered_list: use a single integer
as list marker. This sets ordered to ``True``.
:parameter newline_string: the set of characters used to
go to a new line.
:parameter newline_string: the newline separator.
Defaults to ``os.linesep``.
:type filename: str
:type ordered: bool
:type no_links: bool
@ -336,6 +344,8 @@ def build_multiple_tocs(filenames: list,
Defaults to ``-``.
:parameter constant_ordered_list: use a single integer
as list marker. This sets ordered to ``True``.
:parameter newline_string: the newline separator.
Defaults to ``os.linesep``.
:type filenames: list
:type ordered: bool
:type no_links: bool
@ -345,6 +355,7 @@ def build_multiple_tocs(filenames: list,
:type list_marker: str
:type skip_lines: int
:type constant_ordered_list: bool
:type newline_string: str
:returns: toc_struct, the corresponding table of contents for each input
file.
:rtype: list

View File

@ -77,7 +77,8 @@ class CliToApi():
write_strings_on_files_between_markers(
filenames=args.filename,
strings=toc_struct,
marker=args.toc_marker)
marker=args.toc_marker,
newline_string=newline_string)
else:
for toc in toc_struct:
print(toc, end='')
@ -294,7 +295,12 @@ class CliInterface():
metavar='NEWLINE_STRING',
type=str,
default=common_defaults['newline string'],
help='the string used to separate the lines of the TOC. Use quotes to delimit the string. Defaults to ' + repr(common_defaults['newline string'])
help=('the string used to separate the lines of the TOC. \
Use quotes to delimit the string. \
If you output in place all the newlines of the \
input file will be replaced with this value. \
Defaults to ' +
repr(common_defaults['newline string']))
)
parser.add_argument(
'-p',

View File

@ -123,7 +123,7 @@ class TestApi(unittest.TestCase):
for the unit tests.
"""
with self.assertRaises(exceptions.StdinIsNotAFileToBeWritten):
api.write_string_on_file_between_markers('-', LINE, LINE)
api.write_string_on_file_between_markers('-', LINE, LINE, newline_string='\n')
@unittest.skip("empty test")
def test_write_strings_on_files_between_markers(self):