Skip to content

Commit

Permalink
tools: create new project
Browse files Browse the repository at this point in the history
  • Loading branch information
SamDanielThangarajan committed Sep 2, 2024
1 parent 1af5c30 commit 8350e0c
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[run]
omit =
src/nasdaq_protocols/tools/*


1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dynamic = ["version"]
[project.scripts]
nasdaq-ouch-codegen="nasdaq_protocols.ouch.codegen:generate"
nasdaq-itch-codegen="nasdaq_protocols.itch.codegen:generate"
nasdaq-protocols-create-new-project="nasdaq_protocols.tools.new_project:create"


[tool.setuptools_scm]
Expand Down
Empty file.
96 changes: 96 additions & 0 deletions src/nasdaq_protocols/tools/new_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
from pathlib import Path
import os

import click
from nasdaq_protocols.tools.toml_template import TOML_TEMPLATE


__all__ = [
'create'
]
SUPPORTED_PROTOCOLS = ['ouch', 'itch']


@click.command()
@click.option(
'--name', '-n', required=True,
help='Project name'
)
@click.option(
'--target-dir', '-d', type=click.Path(), required=True,
help='Target directory to create the project structure'
)
@click.option(
'--application', '-a', multiple=True, required=True,
help=f'''include applications in this project.
format appname:protocol
e.g. -a oe:ouch -a md:itch -a fix_oe:fix
supported protocols: {SUPPORTED_PROTOCOLS}
'''
)
def create(name, target_dir, application):
try:
_validate_applications(application)
except Exception as e:
raise click.ClickException(str(e))

click.echo(f'Creating project {name} in {target_dir}')
src_name = name.replace('-', '_')
target_dir = Path(target_dir) / Path(name)
pyproject_toml = target_dir / Path('pyproject.toml')
build_sh = target_dir / Path('build.sh')

apps = []
for app_proto in application:
app_name, proto_name = app_proto.split(':')
click.echo(f'Creating application {app_name} with protocol {proto_name}')
app_dir = target_dir / Path('src') / Path(src_name) / Path(app_name)
app_xml = app_dir / Path(f'{app_name}.xml')
app_dir.mkdir(parents=True, exist_ok=True)
Path(app_xml).touch()
apps.append((app_name, Path('src') / Path(src_name) / Path(app_name), proto_name))
click.echo(f'Created application directory: {app_dir}')

_write_build_sh(build_sh, apps, src_name)
_write_pyproject(pyproject_toml, name, src_name)
click.echo(f'Created project {name} in {target_dir}')


def _write_pyproject(output_path, project_name, project_src_name):
context = {
'project_name': project_name,
'project_src_name': project_src_name
}
with open(output_path, 'a', encoding='utf-8') as op:
code_as_string = TOML_TEMPLATE.format(**context)
op.write(code_as_string)


def _write_build_sh(output_path, apps, project_src_name):
with open(output_path, 'w', encoding='utf-8') as op:
for app_info in apps:
app_name, app_dir, proto_name = app_info
spec_xml = app_dir / Path(f'{app_name}.xml')
cmd = f'nasdaq-generate-{proto_name}-classes {spec_xml} {app_dir} {app_name} {project_src_name}.{app_name}'
op.write(f'{cmd}\n')
op.write('')
os.chmod(output_path, 0o755)


def _validate_applications(applications):
apps = {}
for app_proto in applications:
if ':' not in app_proto:
raise ValueError(f'Invalid application protocol: {app_proto}, format = appname:protocol')

app_name, proto_name = app_proto.split(':')
if app_name in apps:
raise ValueError(f'Application {app_name} already exists')

if proto_name not in SUPPORTED_PROTOCOLS:
raise ValueError(f'Unsupported protocol: {proto_name} for application: {app_name},'
f' Supported protocols: {SUPPORTED_PROTOCOLS}')

apps[app_name] = proto_name
57 changes: 57 additions & 0 deletions src/nasdaq_protocols/tools/toml_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# This has to be made as a mustache template instead
TOML_TEMPLATE = '''
[build-system]
requires = ["setuptools>=64", "setuptools_scm[toml]>=8"]
build-backend = "setuptools.build_meta"
[project]
name = "{project_name}"
description = "Nasdaq protocols python implementation ({project_name})"
#
# LICENSE - 1
# ADD license here
#
# VERSION - 1
# Comment the following line if you want to use the dynamic versioning based on git tags
version = "0.0.1"
#
# DEPENDENCIES
# add your dependencies to the below list
dependencies = [
'nasdaq-protocols>=0.0.1',
]
#
# VERSION 2
# uncomment the following line if you want to specify the version of this package to be based on git tag
#dynamic = ["version"]
#
# SCRIPTS
# uncomment the following line if you want to specify the scripts of the package
# [project.scripts]
# Add project scripts here
[tool.setuptools_scm]
[tool.setuptools.packages.find]
where = ["src"] # list of folders that contain the packages (["."] by default)
include = ["{{project_src_name}}*"] # package names should match these glob patterns (["*"] by default)
namespaces = false # to disable scanning PEP 420 namespaces (true by default)
#
# DATA FILES
# uncomment the following line if you want to package the asn1 files.
#[tool.setuptools.package-data]
#{project_src_name} = ["*.asn1"]
[options]
zip_safe = true
include_package_data = true
'''

0 comments on commit 8350e0c

Please sign in to comment.