Skip to content

Commit

Permalink
Spacy example update
Browse files Browse the repository at this point in the history
  • Loading branch information
Kanushka Gupta authored and Kanushka Gupta committed May 20, 2024
2 parents 0ca66c9 + 00c1fc2 commit 204105c
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 91 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,25 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

### Changed

### Removed


## [0.1.0] - 2024-05-20

### Added

* Added cli for rendering installation instructions for end users.
* Added config section in readme.
* Added installation section in readme.
* Added examples.
* Added many tests.
* Added `raise` jinja macro.
* Generated project with template.
* Added badges.
* Added contributors.


[unreleased]: https://github.com/instructions-d-installation/installation-instruction/compare/v0.1.0...HEAD
[0.1.0]: https://github.com/instructions-d-installation/installation-instruction/releases/tag/v0.1.0
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,43 @@

</div>

## Installation

### [pipx](https://github.com/pypa/pipx)

```
pipx install installation_instruction
```


### pip

```
python -m pip install installation_instruction
```


### installation_instruction

*(Don't try at home.)*
```yaml
name: installation_instruction
type: object
properties:
method:
enum:
- pipx
- pip
----------------------------------
{% if method == "pip" %}
python -m pip
{% else %}
pipx
{% endif %}
install installation_instruction
```


## CLI Usage

```
Expand Down
11 changes: 8 additions & 3 deletions installation_instruction/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,17 @@ def get_command(self, ctx, config_file: str) -> click.Command|None:
click.echo("Config file not found.")
return None

instruction = InstallationInstruction.from_file(config_file)
options = get_flags_and_options(instruction.schema)
try:
instruction = InstallationInstruction.from_file(config_file)
options = get_flags_and_options(instruction.schema)
except Exception as e:
click.echo(str(e))
exit(1)


def callback(**kwargs):
inst = instruction.validate_and_render(kwargs)
print(inst[0])
click.echo(inst[0])
exit(0 if not inst[1] else 1)

return click.Command(
Expand Down
4 changes: 2 additions & 2 deletions installation_instruction/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

def _get_error_message_from_string(string: str) -> str | None:
"""
Parses via regex and returns error message enquoted in "[[ERROR]]". if no error message is found returns `None`.
Parses error message of error given by using jinja macro `RAISE_JINJA_MACRO_STRING`. If no error message is found returns `None`.
:param string: This is the raw input string where an error message might be.
:param string: This is the raw error string where an error message might be.
:type string: str
:return: Error message if found else None.
:rtpye: str or None
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ build-backend = "setuptools.build_meta"
# single source of truth for installation_instruction's version
[project]
name = "installation_instruction"
description = "Add short description here"
description = "Library and CLI for generating installation instructions from json schema and jinja templates."
readme = "README.md"
maintainers = [
{ name = "Adam McKellar", email = "dev@mckellar.eu" },
{ name = "Kanushka Gupta" },
{ name = "Timo Ege", email = "Timoege@online.de" },
]
version = "0.0.1"
version = "0.1.0"
requires-python = ">=3.10"
license = { text = "Apache-2.0" }
classifiers = [
Expand Down
36 changes: 36 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2024 Adam McKellar, Kanushka Gupta, Timo Ege

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import pytest
import os
import yaml
import json


@pytest.fixture
def test_data_flags_options():
file_path = os.path.join(os.path.dirname(__file__), 'data', 'flags_options_example.schema.yml')
with open(file_path,"r") as file:
return yaml.safe_load(file)

@pytest.fixture
def test_data_user_input(request):
file_name = request.param
file_path = os.path.join(os.path.dirname(__file__), 'data', file_name)
with open(file_path,"r") as file:
data = file.read()
test_input = json.loads(data)
return [file_name,test_input]

44 changes: 44 additions & 0 deletions tests/data/flags_options_example.schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
$schema: https://json-schema.org/draft/2020-12/schema
$id: https://github.com/instructions-d-installation/installation-instruction/examples/scikit-learn/scikit-learn-instruction.schema.yml
title: Scikit-learn installation schema
description: This is a Schema to construct installation instructions for the python package scikit-learn by Timo Ege.
type: object
properties:
os:
title: Operating System
description: The operating system in which the package is installed.
enum:
- Windows
- macOS
- Linux
default: Windows

packager:
title: Packager
description: The package manager of your choosing.
enum:
- pip
- conda
default: pip

virtualenv:
title: Use pip virtualenv
description: Choose if you want to use a virtual environment to install the package.
type: boolean
default: false

compute_platform:
title: Compute Platform
description: Should your gpu or your cpu handle the task?
anyOf:
- title: CUDA 11.8
const: cu118
- title: CUDA 12.1
const: cu121
default: cu118

required:
- os
- packager

additionalProperties: false
6 changes: 6 additions & 0 deletions tests/data/pytorch_invalid_data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"build": "preview",
"os": "win",
"package": "piiip",
"compute_platform": "ro60"
}
6 changes: 6 additions & 0 deletions tests/data/pytorch_valid_data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"build": "preview",
"os": "win",
"package": "pip",
"compute_platform": "ro60"
}
5 changes: 5 additions & 0 deletions tests/data/scikit_invalid_data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"os": "Kali",
"packager": "pip",
"virtualenv": False
}
5 changes: 5 additions & 0 deletions tests/data/scikit_valid_data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"os": "Windows",
"packager": "pip",
"virtualenv": False
}
50 changes: 3 additions & 47 deletions tests/test_get_flags_and_options_from_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,53 +17,9 @@
import click
import yaml

def test_get_flags_and_options():
example_schema = '''$schema: https://json-schema.org/draft/2020-12/schema
$id: https://github.com/instructions-d-installation/installation-instruction/examples/scikit-learn/scikit-learn-instruction.schema.yml
title: Scikit-learn installation schema
description: This is a Schema to construct installation instructions for the python package scikit-learn by Timo Ege.
type: object
properties:
os:
title: Operating System
description: The operating system in which the package is installed.
enum:
- Windows
- macOS
- Linux
default: Windows
packager:
title: Packager
description: The package manager of your choosing.
enum:
- pip
- conda
default: pip
virtualenv:
title: Use pip virtualenv
description: Choose if you want to use a virtual environment to install the package.
type: boolean
default: false
compute_platform:
title: Compute Platform
description: Should your gpu or your cpu handle the task?
anyOf:
- title: CUDA 11.8
const: cu118
- title: CUDA 12.1
const: cu121
default: cu118
required:
- os
- packager
additionalProperties: false'''
example = yaml.safe_load(example_schema)
options = get_flags_and_options(example)
def test_get_flags_and_options(test_data_flags_options):
example_schema = test_data_flags_options
options = get_flags_and_options(example_schema)

assert len(options) == 4

Expand Down
60 changes: 23 additions & 37 deletions tests/test_installation_instruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,56 +13,42 @@
# limitations under the License.


from pytest import raises
import pytest

from installation_instruction.installation_instruction import InstallationInstruction

def test_validate_and_render_pytorch():
valid_user_input = {
"build": "preview",
"os": "win",
"package": "pip",
"compute_platform": "ro60"
}
@pytest.mark.parametrize("test_data_user_input",["pytorch_invalid_data.txt","pytorch_valid_data.txt"])
def test_validate_and_render_pytorch(test_data_user_input):

bad_user_input = {
"build": "preview",
"os": "win",
"package": "piiip",
"compute_platform": "ro60"
}

test_data = test_data_user_input
file_name = test_data[0]
user_input = test_data[1]
install = InstallationInstruction.from_file("examples/pytorch/pytorch-instruction.schema.yml.jinja")

good_installation_instruction = install.validate_and_render(valid_user_input)

assert ('Windows does not support ROCm!', True) == good_installation_instruction

with raises(Exception):
install.validate_and_render(bad_user_input)

if "_valid_" in file_name:
good_installation_instruction = install.validate_and_render(user_input)

def test_validate_and_render_scikit():
valid_user_input = {
"os": "Windows",
"packager": "pip",
"virtualenv": False
}
assert ('Windows does not support ROCm!', True) == good_installation_instruction
elif "_invalid_" in file_name:
with pytest.raises(Exception):
install.validate_and_render(user_input)

invalid_user_input = {
"os": "Kali",
"packager": "pip",
"virtualenv": False
}
@pytest.mark.parametrize("test_data_user_input",["scikit_invalid_data.txt","scikit_valid_data.txt"])
def test_validate_and_render_scikit(test_data_user_input):
test_data = test_data_user_input
file_name = test_data[0]
user_input = test_data[1]

install = InstallationInstruction.from_file("examples/scikit-learn/scikit-learn-instruction.schema.yml.jinja")

good_installation_instruction = install.validate_and_render(valid_user_input)
if "_valid_" in file_name:
good_installation_instruction = install.validate_and_render(user_input)

assert ('pip install -U scikit-learn', False) == good_installation_instruction
assert ('pip install -U scikit-learn', False) == good_installation_instruction

with raises(Exception):
install.validate_and_render(invalid_user_input)
elif "_invalid_" in file_name:
with pytest.raises(Exception):
install.validate_and_render(user_input)

def test_validate_and_render_spacy():
valid_user_input = {
Expand Down

0 comments on commit 204105c

Please sign in to comment.