-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from courage-tci/dev
0.7.0
- Loading branch information
Showing
6 changed files
with
113 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from gekkota.constants import Config, StrGen | ||
from gekkota.utils import Utils | ||
from .core import Statement | ||
|
||
|
||
class Comment(Statement): | ||
""" | ||
text (str): comment text | ||
use_string (bool): render a string (triple-quote) instead of #-based comment | ||
""" | ||
|
||
def __init__(self, text: str, use_string: bool = False): | ||
self.lines = text.split("\n") | ||
self.use_string = use_string | ||
|
||
def render_hashed(self, config: Config) -> StrGen: | ||
for line in self.lines: | ||
yield "#" | ||
yield " " | ||
yield line | ||
yield "\n" | ||
|
||
def escape_string(self, s: str): | ||
return repr(s)[1:-1].replace('"""', '\\"\\"\\"') | ||
|
||
def render_string_contents(self, config: Config) -> StrGen: | ||
return Utils.separated_str( | ||
["\n"], | ||
(self.escape_string(s) for s in self.lines), | ||
config, | ||
) | ||
|
||
def render_string(self, config: Config) -> StrGen: | ||
yield '"""' | ||
|
||
if len(self.lines) > 1: | ||
yield "\n" | ||
yield from Utils.add_tab(self.render_string_contents(config), config) | ||
yield "\n" | ||
|
||
elif self.lines: | ||
yield self.escape_string(self.lines[0]) | ||
|
||
yield '"""' | ||
yield "\n" | ||
|
||
def render(self, config: Config) -> StrGen: | ||
if self.use_string: | ||
return self.render_string(config) | ||
return self.render_hashed(config) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from gekkota import Comment | ||
|
||
|
||
class TestClass: | ||
def test_hashed(self): | ||
assert str(Comment("test")) == "# test\n" | ||
assert str(Comment("test\notherline")) == "# test\n# otherline\n" | ||
|
||
def test_string(self): | ||
assert str(Comment("test", use_string=True)) == '"""test"""\n' | ||
assert ( | ||
str(Comment("test\ntest2", use_string=True)) | ||
== '"""\n test\n test2\n"""\n' | ||
) |