Skip to content

Commit

Permalink
Ability to limit the scope of init command (#870)
Browse files Browse the repository at this point in the history
  • Loading branch information
droserasprout authored Oct 20, 2023
1 parent 3e6cee8 commit 02a26cd
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 11 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog], and this project adheres to [Semantic Versioning].

## Unreleased
## [Unreleased]

### Added

- cli: Relative paths to be initialized now can be passed to the `init` command as arguments.
- tezos.tzkt.token_balances: Added new index.

### Fixed

- tezos.tzkt.token_transfers: Fixed token_id handler in token transfers index.
- tezos.tzkt.token_transfers: Fixed filtering transfers by token_id.

## [7.0.2] - 2023-10-10

Expand Down
20 changes: 18 additions & 2 deletions src/dipdup/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,20 @@ async def run(ctx: click.Context) -> None:
@cli.command()
@click.option('--force', '-f', is_flag=True, help='Overwrite existing types and ABIs.')
@click.option('--base', '-b', is_flag=True, help='Include template base: pyproject.toml, Dockerfile, etc.')
@click.argument(
'include',
type=str,
nargs=-1,
metavar='PATH',
)
@click.pass_context
@_cli_wrapper
async def init(ctx: click.Context, force: bool, base: bool) -> None:
async def init(
ctx: click.Context,
force: bool,
base: bool,
include: list[str],
) -> None:
"""Generate project tree, typeclasses and callback stubs.
This command is idempotent, meaning it won't overwrite previously generated files unless asked explicitly.
Expand All @@ -299,7 +310,12 @@ async def init(ctx: click.Context, force: bool, base: bool) -> None:

config: DipDupConfig = ctx.obj.config
dipdup = DipDup(config)
await dipdup.init(force, base)

await dipdup.init(
force=force,
base=base or bool(include),
include=set(include),
)


@cli.command()
Expand Down
11 changes: 9 additions & 2 deletions src/dipdup/codegen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ def __init__(
config: DipDupConfig,
package: DipDupPackage,
datasources: dict[str, Datasource[Any]],
include: set[str] | None = None,
) -> None:
self._config = config
self._package = package
self._datasources = datasources
self._include = include or set()
self._logger = _logger

async def init(
Expand All @@ -52,9 +54,14 @@ async def init(
self._package.create()

replay = self._package.replay
if base and replay:
if base:
if not replay:
raise FrameworkException('`--base` option passed but `configs/replay.yaml` file is missing')
_logger.info('Recreating base template with replay.yaml')
render_base(replay, force)
render_base(replay, force, self._include)

if self._include:
force = any(str(path).startswith('types') for path in self._include)

await self.generate_abi()
await self.generate_schemas(force)
Expand Down
2 changes: 2 additions & 0 deletions src/dipdup/codegen/tezos_tzkt.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,13 @@ def __init__(
config: DipDupConfig,
package: DipDupPackage,
datasources: dict[str, Datasource[Any]],
include: set[str] | None = None,
) -> None:
super().__init__(
config=config,
package=package,
datasources=datasources,
include=include,
)
self._schemas: dict[str, dict[str, dict[str, Any]]] = {}

Expand Down
15 changes: 13 additions & 2 deletions src/dipdup/dipdup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from tortoise.exceptions import OperationalError

from dipdup import env
from dipdup.codegen import CodeGenerator
from dipdup.codegen import generate_environments
from dipdup.config import DipDupConfig
from dipdup.config import IndexTemplateConfig
Expand Down Expand Up @@ -502,6 +503,7 @@ async def init(
self,
force: bool = False,
base: bool = False,
include: set[str] | None = None,
) -> None:
"""Create new or update existing dipdup project"""
from dipdup.codegen.evm_subsquid import SubsquidCodeGenerator
Expand All @@ -515,8 +517,17 @@ async def init(

package = DipDupPackage(self._config.package_path)

for codegen_cls in (TzktCodeGenerator, SubsquidCodeGenerator):
codegen = codegen_cls(self._config, package, self._datasources)
codegen_classes: tuple[type[CodeGenerator], ...] = (
TzktCodeGenerator,
SubsquidCodeGenerator,
)
for codegen_cls in codegen_classes:
codegen = codegen_cls(
config=self._config,
package=package,
datasources=self._datasources,
include=include,
)
await codegen.init(
force=force,
base=base,
Expand Down
23 changes: 20 additions & 3 deletions src/dipdup/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,17 @@ def render_project(
def render_base(
answers: Answers,
force: bool = False,
include: set[str] | None = None,
) -> None:
"""Render base from template"""
# NOTE: Common base
_render_templates(answers, Path('base'), force, refresh=True)
_render_templates(
answers=answers,
path=Path('base'),
force=force,
include=include,
exists=True,
)

_render(
answers,
Expand All @@ -276,15 +283,25 @@ def render_base(
)


def _render_templates(answers: Answers, path: Path, force: bool = False, refresh: bool = False) -> None:
def _render_templates(
answers: Answers,
path: Path,
force: bool = False,
include: set[str] | None = None,
exists: bool = False,
) -> None:
from jinja2 import Template

project_path = Path(__file__).parent / 'projects' / path
project_paths = project_path.glob('**/*.j2')

for path in project_paths:
template_path = path.relative_to(Path(__file__).parent)
output_base = get_package_path(answers['package']) if refresh else Path(answers['package'])

if include and not any(str(path).startswith(i) for i in include):
continue

output_base = get_package_path(answers['package']) if exists else Path(answers['package'])
output_path = Path(
output_base,
*path.relative_to(project_path).parts,
Expand Down

0 comments on commit 02a26cd

Please sign in to comment.