Skip to content

Commit

Permalink
Merge pull request #15 from courage-tci/dev
Browse files Browse the repository at this point in the history
0.7.0
  • Loading branch information
evtn committed May 2, 2024
2 parents 1685e9a + a4a109f commit f508f7f
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 45 deletions.
47 changes: 19 additions & 28 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
name: Test that everything works
name: Test

on:
workflow_dispatch:
push:
branches:
- pub
paths:
- "**.py"
- "**.yml"
pull_request:
paths:
- "**.py"

- "**.yml"
jobs:
check_types:
runs-on: ubuntu-latest
name: Check Types
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]

steps:
- name: git-checkout
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.9
python-version: ${{ matrix.python-version }}

- name: Install Poetry
uses: abatilo/actions-poetry@v2

- run: poetry install --with dev
- run: poetry run pyright
- run: poetry run pyright gekkota/*.py

run-tests:
runs-on: ubuntu-latest
Expand All @@ -35,7 +42,6 @@ jobs:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]

name: Run Tests

steps:
- name: git-checkout
uses: actions/checkout@v3
Expand All @@ -50,30 +56,15 @@ jobs:

- run: poetry install --with dev

- name: Test
run: poetry run coverage run -m pytest test/

update-coverage:
runs-on: ubuntu-latest
name: Update Coverage

steps:
- name: git-checkout
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4

- name: Install Poetry
uses: abatilo/actions-poetry@v2

- run: poetry install --with dev

- name: Test
run: poetry run coverage run --include "gekkota/*" -m pytest test/

- name: Coveralls update
- name: Convert coverage data to XML
run: poetry run coverage xml

run: poetry run coveralls --service=github
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Coveralls Update
uses: coverallsapp/github-action@v2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
flag-name: ${{ matrix.python-version }}
coverage_format: cobertura
2 changes: 2 additions & 0 deletions gekkota/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from .constants import StrGen as StrGen, Config as Config

from .comments import Comment as Comment

from .core import Renderable as Renderable, Statement as Statement

from .expression import Expression as Expression, Parens as Parens
Expand Down
51 changes: 51 additions & 0 deletions gekkota/comments.py
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)
42 changes: 26 additions & 16 deletions gekkota/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Sequence
from typing import Sequence, Iterable


class Utils:
Expand All @@ -18,28 +18,38 @@ def add_tab(generator: StrGen, config: Config) -> StrGen:

@staticmethod
def separated(
separator: Sequence[str], renderables: Sequence[Renderable], config: Config
separator: Iterable[str], renderables: Iterable[Renderable], config: Config
) -> StrGen:
if not len(renderables):
yield ""
return
yield from renderables[0].render(config)
for renderable in renderables[1:]:
yield from separator
counter = 0

for renderable in renderables:
if counter:
yield from separator

yield from renderable.render(config)

@staticmethod
def separated_str(separator: Sequence[str], strings: Sequence[str], config: Config):
if not len(strings):
counter += 1

if not counter:
yield ""
return
yield strings[0]
for renderable in strings[1:]:
yield from separator

@staticmethod
def separated_str(separator: Iterable[str], strings: Iterable[str], config: Config):
counter = 0

for renderable in strings:
if counter:
yield from separator

yield renderable

counter += 1

if not counter:
yield ""

@staticmethod
def comma_separated(renderables: Sequence[Renderable], config: Config) -> StrGen:
def comma_separated(renderables: Iterable[Renderable], config: Config) -> StrGen:
yield from Utils.separated(", ", renderables, config)

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "gekkota"
version = "0.6.0"
version = "0.7.0"
description = "Python code-generation for Python"
authors = ["Dmitry Gritsenko <k01419q45@ya.ru>"]
license = "MIT"
Expand Down
14 changes: 14 additions & 0 deletions test/test_comment.py
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'
)

0 comments on commit f508f7f

Please sign in to comment.