forked from altitudenetworks/newversion
-
Notifications
You must be signed in to change notification settings - Fork 2
/
eol_fixer.py
72 lines (56 loc) · 1.48 KB
/
eol_fixer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
Converter between Unix and Windows line endings.
"""
class EOLFixer:
"""
Converter between Unix and Windows line endings.
"""
CRLF = "\r\n"
LF = "\n"
@classmethod
def is_crlf(cls, text: str) -> bool:
"""
Check whether text has `\r\n` characters.
Arguments:
text -- Text to check.
"""
return cls.CRLF in text
@classmethod
def to_lf(cls, text: str) -> str:
"""
Convert `\r\n` to `\n`.
Arguments:
text -- Text to convert.
Returns:
Converted text.
"""
if not cls.is_crlf(text):
return text
return text.replace(cls.CRLF, cls.LF)
@classmethod
def to_crlf(cls, text: str) -> str:
"""
Convert `\n` to `\r\n`.
Arguments:
text -- Text to convert.
Returns:
Converted text.
"""
if cls.is_crlf(text):
return text
return text.replace(cls.LF, cls.CRLF)
@classmethod
def get_line_ending(cls, text: str) -> str:
"""
Get line ending character.
"""
return cls.CRLF if cls.is_crlf(text) else cls.LF
@classmethod
def add_newline(cls, text: str) -> str:
"""
Add newline character to the end if it is missing.
"""
line_ending = cls.get_line_ending(text)
if text.endswith(line_ending):
return text
return f"{text}{line_ending}"