Skip to content

Commit

Permalink
feat ✨: Allow to create requests projects. (#37)
Browse files Browse the repository at this point in the history
* Bump version: 0.2.3 → 0.2.4
* Set DOCKER_DEFAULT_PYTHON_VERSION to 3.9
* Proofread text and format code
* Add type flag to estela init
* Update projects framework in the API when initializing projects

---------

Co-authored-by: mgonnav <mateo@emegona.com>
  • Loading branch information
joaquingx and mgonnav committed Jul 12, 2023
1 parent 09db075 commit b6090f0
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.2.3
current_version = 0.2.4
commit = True
tag = True
tag_name = {new_version}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<h1 align="center"> estela CLI </h1>

[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![version](https://img.shields.io/badge/version-0.2.3-blue)](https://github.com/bitmakerla/estela-cli)
[![version](https://img.shields.io/badge/version-0.2.4-blue)](https://github.com/bitmakerla/estela-cli)
[![python-version](https://img.shields.io/badge/python-v3.10-orange)](https://www.python.org)


Expand Down
2 changes: 1 addition & 1 deletion estela_cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__all__ = ["__version__"]

__version__ = "0.2.3"
__version__ = "0.2.4"
5 changes: 2 additions & 3 deletions estela_cli/create/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ def estela_command(name):
response = estela_client.create_project(name)
click.echo("project/{} created.".format(name))
click.echo(
"Hint: Run 'estela init {}' to activate this project".format(
response["pid"]
)
f"Hint: Use 'estela init {response['pid']}' to initialize a Scrapy project.\n"
f"Hint: Use 'estela init {response['pid']} -p requests' to initialize a Requests project."
)
except Exception as ex:
raise click.ClickException(str(ex))
9 changes: 9 additions & 0 deletions estela_cli/estela_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ def create_project(self, name):
response = self.post(endpoint, data=data)
self.check_status(response, 201)
return response.json()

def update_project(self, pid, **kwargs):
endpoint = "projects/{}".format(pid)
data = {
**kwargs,
}
response = self.put(endpoint, data=data)
self.check_status(response, 200)
return response.json()

def delete_project(self, pid):
endpoint = "projects/{}".format(pid)
Expand Down
39 changes: 28 additions & 11 deletions estela_cli/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from estela_cli.templates import (
DATA_DIR,
DOCKER_DEFAULT_ENTRYPOINT,
DOCKER_REQUESTS_ENTRYPOINT,
DOCKERFILE,
DOCKERFILE_NAME,
ESTELA_YAML,
Expand All @@ -20,11 +21,11 @@
ESTELA_DIR,
)

ALLOWED_PLATFORMS = ["scrapy", "requests"]
SHORT_HELP = "Initialize estela project for existing web scraping project"

SHORT_HELP = "Initialize estela project for existing scrapy project"


def gen_estela_yaml(estela_client, pid=None):
def gen_estela_yaml(estela_client, entrypoint_path, pid=None):
estela_yaml_path = get_estela_yaml_path()

if os.path.exists(estela_yaml_path):
Expand All @@ -46,7 +47,7 @@ def gen_estela_yaml(estela_client, pid=None):
"project_data_path": DATA_DIR,
"python_version": DOCKER_DEFAULT_PYTHON_VERSION,
"requirements_path": DOCKER_DEFAULT_REQUIREMENTS,
"entrypoint": DOCKER_DEFAULT_ENTRYPOINT,
"entrypoint": entrypoint_path,
}

result = template.substitute(values)
Expand All @@ -56,7 +57,7 @@ def gen_estela_yaml(estela_client, pid=None):
click.echo("{} file created successfully.".format(ESTELA_YAML_NAME))


def gen_dockerfile(requirements_path):
def gen_dockerfile(requirements_path, entrypoint_path):
dockerfile_path = get_estela_dockerfile_path()

if os.path.exists(dockerfile_path):
Expand All @@ -74,7 +75,7 @@ def gen_dockerfile(requirements_path):
values = {
"python_version": DOCKER_DEFAULT_PYTHON_VERSION,
"requirements_path": requirements_path,
"entrypoint": DOCKER_DEFAULT_ENTRYPOINT,
"entrypoint": entrypoint_path,
}
result = template.substitute(values)

Expand All @@ -85,22 +86,38 @@ def gen_dockerfile(requirements_path):

@click.command(short_help=SHORT_HELP)
@click.argument("pid", required=True)
@click.option(
"-p",
"--platform",
type=click.Choice(ALLOWED_PLATFORMS, case_sensitive=False),
default="scrapy",
help="Platform to use, it can be 'scrapy' or 'requests'",
show_default=True,
)
@click.option(
"-r",
"--requirements",
default=DOCKER_DEFAULT_REQUIREMENTS,
help="Relative path to requirements inside your project",
show_default=True,
)
def estela_command(pid, requirements):
def estela_command(pid, platform, requirements):
"""Initialize estela project
PID is the project's pid
"""

platform_map = {
"scrapy": DOCKER_DEFAULT_ENTRYPOINT,
"requests": DOCKER_REQUESTS_ENTRYPOINT,
}
if not os.path.exists(ESTELA_DIR):
os.makedirs(ESTELA_DIR)

estela_client = login()
gen_estela_yaml(estela_client, pid)
gen_dockerfile(requirements)
try:
response = estela_client.update_project(pid, framework=platform.upper(), action="update")
except Exception as e:
raise click.ClickException("Could not update framework project: %s" % str(e))
finally:
click.echo(f"{pid} is initialized as a {platform.capitalize()} project.")
gen_estela_yaml(estela_client, platform_map[platform], pid)
gen_dockerfile(requirements, platform_map[platform])
4 changes: 3 additions & 1 deletion estela_cli/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@

DOCKER_APP_DIR = "/usr/src/app"

DOCKER_DEFAULT_PYTHON_VERSION = "3.6"
DOCKER_DEFAULT_PYTHON_VERSION = "3.9"

DOCKER_DEFAULT_REQUIREMENTS = "requirements.txt"

DOCKER_DEFAULT_ENTRYPOINT = "git+https://github.com/bitmakerla/estela-entrypoint.git"

DOCKER_REQUESTS_ENTRYPOINT = "git+https://github.com/bitmakerla/estela-requests-entrypoint"

DOCKERFILE_NAME = "Dockerfile-estela"

DOCKERFILE = """\
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="estela",
version="0.2.3",
version="0.2.4",
description="Estela Command Line Interface",
packages=find_packages(),
entry_points={
Expand Down

0 comments on commit b6090f0

Please sign in to comment.