Skip to content

Commit

Permalink
Merge pull request #12 Complete porting to stand-alone
Browse files Browse the repository at this point in the history
  • Loading branch information
Mec-iS authored Feb 23, 2019
2 parents b0378f4 + c46c778 commit 42920b6
Show file tree
Hide file tree
Showing 12 changed files with 296 additions and 13 deletions.
22 changes: 22 additions & 0 deletions .github/CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Code of Conduct

As contributors and maintainers of this project, and in the interest of fostering an open and welcoming community, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.

We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, religion, or nationality.

Examples of unacceptable behavior by participants include:

* The use of sexualized language or imagery
* Personal attacks
* Trolling or insulting/derogatory comments
* Public or private harassment
* Publishing other's private information, such as physical or electronic addresses, without explicit permission
* Other unethical or unprofessional conduct.

Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. By adopting this Code of Conduct, project maintainers commit themselves to fairly and consistently applying these principles to every aspect of managing this project. Project maintainers who do not follow or enforce the Code of Conduct may be permanently removed from the project team.

This code of conduct applies both within project spaces and in public spaces when an individual is representing the project or its community.

Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.

This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.2.0, available at [http://contributor-covenant.org/version/1/2/0/](http://contributor-covenant.org/version/1/2/0/)
21 changes: 21 additions & 0 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
### Contribution Best Practices

* Read this [how-to about Github workflow here](https://guides.github.com/introduction/flow/) if you are not familiar with.

* Read all the texts related to [contributing for an OS community](https://github.com/HTTP-APIs/hydrus/tree/master/.github).

* Read this [how-to about writing a PR](https://github.com/blog/1943-how-to-write-the-perfect-pull-request) and this [other how-to about writing a issue](https://wiredcraft.com/blog/how-we-write-our-github-issues/)

* **first ask in chat**: if you find a problem, first ask for [help in the chat](https://gitter.im/HTTP-APIs/Lobby), then consider opening a issue.

* **read history**: before opening a PR be sure that all the tests pass successfully. If any is failing for non-related reasons, annotate the test failure in the PR comment.

* **PRs on develop**: any change should be PRed first in `develop`, `master` can only receive merge from develop.

* **testing**: everything should work and be tested for Python 3.5.2 and above.

* **free PR**: no permission is needed to work on the code. Fork `master`, submit a PR and ask for reviewing. PR is the natural place for code comparison and corrections. If many contributors have something ready in a PR, we can consider opening a branch in which different people working on the same part of the application can collaborate.

* **pylint**: code in PRs should be accurately compliant with [PEP-8](https://www.python.org/dev/peps/pep-0008/), checking code with `pylint` is fine.

* **mypy**: every module is and should in future provide type annotations using `mypy`.
15 changes: 15 additions & 0 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
### I'm submitting a
- [ ] bug report.
- [ ] feature request.

### Current Behaviour:
<!-- Describe about the bug -->

### Expected Behaviour:
<!-- Describe what will happen if bug is removed -->

### Steps to reproduce:
<!-- If you can then please provide the steps to reproduce the bug -->

### Do you want to work on this issue?
<!-- yes/no -->
34 changes: 34 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!-- Please create/claim an issue before sending a PR -->
<!-- Add issue number (Eg: fixes #123) -->

Fixes #

### Checklist
- [ ] My branch is up-to-date with upstream/develop branch.
- [ ] Everything works and tested for Python 3.5.2 and above.

### Description
<!-- Describe about what this PR does, previous state and new state of the output -->

### Change logs

<!-- #### Added -->
<!-- Edit these points below to describe the new features added with this PR -->
<!-- - Feature 1 -->
<!-- - Feature 2 -->


<!-- #### Changed -->
<!-- Edit these points below to describe the changes made in existing functionality with this PR -->
<!-- - Change 1 -->
<!-- - Change 1 -->


<!-- #### Fixed -->
<!-- Edit these points below to describe the bug fixes made with this PR -->
<!-- - Bug 1 -->


<!-- #### Removed -->
<!-- Edit these points below to describe the removed features with this PR -->
<!-- - Deprecated feature 1 -->
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
# hydra-python-core
This library provides the core functions to implement Hydra Official Specification in Python.

Currently the library mainly consists of 2 modules `doc_writer` and `doc_maker` which help hydrus generalise a lot of things.

*Porting out from hydrus the hydraspecs directory*
-> `doc_writer` creates a new API Documentation as well as a `HydraDoc` object while,
-> `doc_maker` uses an existing API Documentation to create a `HydraDoc` object c`



### Installation

To install the library:

```bash
pip install git+https://github.com/HTTP-APIs/hydra-python-core.git#egg=hydra_python_core
```

**Note :-** If using hydrus, the library doesn't need to be installed separately as it is already a part of `requirements.txt` for hydrus.



### Usage

To import the modules:

```python
from hydra_python_core import doc_writer
from hydra_python_core import doc_maker
```

*Porting out from hydrus the hydraspecs directory*
17 changes: 6 additions & 11 deletions hydra_python_core/doc_maker.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'''Contsructor to take a Python dict containing an API Documentation and
create a HydraDoc object for it'''
"""Contsructor to take a Python dict containing an API Documentation and
create a HydraDoc object for it
"""
import re
import json
from hydrus.samples.doc_writer_sample import api_doc as sample_document
from hydrus.hydraspec.doc_writer import HydraDoc, HydraClass, HydraClassProp, HydraClassOp
from hydrus.hydraspec.doc_writer import HydraStatus
from hydra_python_core.doc_writer import HydraDoc, HydraClass, HydraClassProp, HydraClassOp
from hydra_python_core.doc_writer import HydraStatus
from typing import Any, Dict, Match, Optional, Tuple, Union


def error_mapping(body: str = None) -> str:
def error_mapping(body: str=None) -> str:
"""Function returns starting error message based on its body type.
:param body: Params type for error message
:return string: Error message for input key
Expand Down Expand Up @@ -357,8 +357,3 @@ def create_status(possible_status: Dict[str, Any]) -> HydraStatus:
status = HydraStatus(result["statusCode"],
result["title"], result["description"])
return status


if __name__ == "__main__":
api_doc = create_doc(sample_document.generate())
print(json.dumps(api_doc.generate(), indent=4, sort_keys=True))
Empty file added samples/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions samples/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import json

if __name__ == "__main__":
api_doc = create_doc(sample_document.generate())
print(json.dumps(api_doc.generate(), indent=4, sort_keys=True))
2 changes: 1 addition & 1 deletion samples/doc_writer_sample.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Sample to create Hydra APIDocumentation using doc_writer."""

from hydrus.hydraspec.doc_writer import HydraDoc, HydraClass, HydraClassProp, HydraClassOp
from hydra_python_core.doc_writer import HydraDoc, HydraClass, HydraClassProp, HydraClassOp
from typing import Any, Dict, Union

# Creating the HydraDoc object, this is the primary class for the Doc
Expand Down
13 changes: 13 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from setuptools import setup, find_packages

setup(
name='hydra_python_core',
version='0.1',
packages=find_packages(),
license='MIT',
description='Core functions for Hydrus',
long_description=open('README.md').read(),
long_description_content_type="text/markdown",
url='https://github.com/HTTP-APIs/hydra-python-core',
zip_safe=False
)
Empty file added tests/__init__.py
Empty file.
152 changes: 152 additions & 0 deletions tests/test_doc_writer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import unittest
from hydra_python_core import doc_writer
from unittest.mock import MagicMock, patch


class TestDocWriter(unittest.TestCase):

# test context class methods
def test_context_with_nothing(self):
"""
Test method to test if correct context is generated when no arguments are passed
"""
context = doc_writer.Context('https://hydrus.com/api')
expected_context = {
"hydra": "http://www.w3.org/ns/hydra/core#",
"property": {
"@type": "@id",
"@id": "hydra:property"
},
"supportedClass": "hydra:supportedClass",
"supportedProperty": "hydra:supportedProperty",
"supportedOperation": "hydra:supportedOperation",
"statusCodes": "hydra:statusCodes",
"label": "rdfs:label",
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"vocab": "https://hydrus.com/api/vocab#",
# "vocab": "localhost/api/vocab#",
"domain": {
"@type": "@id",
"@id": "rdfs:domain"
},
"ApiDocumentation": "hydra:ApiDocumentation",
"range": {
"@type": "@id",
"@id": "rdfs:range"
},
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
"title": "hydra:title",
"expects": {
"@type": "@id",
"@id": "hydra:expects"
},
"returns": {
"@id": "hydra:returns",
"@type": "@id"
},
"readonly": "hydra:readonly",
"writeonly": "hydra:writeonly",
"possibleStatus": "hydra:possibleStatus",
"required": "hydra:required",
"method": "hydra:method",
"statusCode": "hydra:statusCode",
"description": "hydra:description",
"subClassOf": {
"@id": "rdfs:subClassOf",
"@type": "@id"
}
}
self.assertEqual(expected_context, context.generate())

@patch('hydra_python_core.doc_writer.HydraEntryPoint',
spec=doc_writer.HydraEntryPoint)
def test_context_with_entrypoint(self, mock_entry):
"""
Test method to test if correct context is generated when HydraEntryPoint is passed
"""

hydra_entry_point_mock = mock_entry()
hydra_entry_point_mock.base_url = "http://petstore.swagger.io/v2"
hydra_entry_point_mock.entrypoint = "EntryPoint"

context = doc_writer.Context('http://petstore.swagger.io/v2',
entrypoint=hydra_entry_point_mock)

expected_context = {
"EntryPoint": "vocab:EntryPoint",
"vocab": "http://petstore.swagger.io/v2/vocab#"
}
self.assertEqual(expected_context, context.generate())

def test_context_with_class(self):
"""
Test method to test if correct context is generated when HydraClass is passed
"""

mocked_hydra_class = MagicMock()
with patch(
'hydra_python_core.doc_writer.HydraClass',
mocked_hydra_class, spec_set=doc_writer.HydraClass):
mocked_hydra_property = MagicMock()
mocked_hydra_class.id_ = "vocab:Pet"
mocked_hydra_class.title = "Pet"
mocked_hydra_class.desc = "Pet"
with patch('hydra_python_core.doc_writer.HydraClassProp', mocked_hydra_property,
spec_set=doc_writer.HydraClassProp):
mocked_hydra_property.prop = ""
mocked_hydra_property.readonly = "true"
mocked_hydra_property.required = "false"
mocked_hydra_property.title = "id"
mocked_hydra_property.writeonly = "true"

mocked_hydra_class.supportedProperty = [mocked_hydra_property]

context = doc_writer.Context(
'http://petstore.swagger.io/v2',
class_=mocked_hydra_class)

expected_context = {
"vocab": "http://petstore.swagger.io/v2/vocab#",
"hydra": "http://www.w3.org/ns/hydra/core#",
"members": "http://www.w3.org/ns/hydra/core#member",
"object": "http://schema.org/object",
"Pet": "vocab:Pet",
"id": ""
}

self.assertEqual(expected_context, context.generate())

@patch('hydra_python_core.doc_writer.HydraClass', spec=doc_writer.HydraClass)
@patch('hydra_python_core.doc_writer.HydraCollection',
spec=doc_writer.HydraCollection)
def test_context_with_collection(self, hydra_class, hydra_collection):
"""
Test method to test if correct context is generated when HydraCollection is passed
"""
mocked_hydra_class = hydra_class()
mocked_hydra_class.id_ = "vocab:Pet"
mocked_hydra_class.title = "Pet"
mocked_hydra_class.desc = "Pet"

mocked_hydra_collection = hydra_collection()
mocked_hydra_collection.class_ = mocked_hydra_class
mocked_hydra_collection.name = "{}Collection".format(mocked_hydra_class.title)
context = doc_writer.Context(
'http://petstore.swagger.io/v2',
collection=mocked_hydra_collection)
expected_context = {
"vocab": "http://petstore.swagger.io/v2/vocab#",
"hydra": "http://www.w3.org/ns/hydra/core#",
"members": "http://www.w3.org/ns/hydra/core#member",
"PetCollection": "vocab:PetCollection",
"Pet": "vocab:Pet"
}

self.assertEqual(expected_context, context.generate())

if __name__ == '__main__':
unittest.main()

0 comments on commit 42920b6

Please sign in to comment.