diff --git a/.github/CODE_OF_CONDUCT.md b/.github/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..ae45a0e --- /dev/null +++ b/.github/CODE_OF_CONDUCT.md @@ -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/) diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md new file mode 100644 index 0000000..f37889c --- /dev/null +++ b/.github/CONTRIBUTING.md @@ -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`. diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md new file mode 100644 index 0000000..bdb82fb --- /dev/null +++ b/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,15 @@ +### I'm submitting a +- [ ] bug report. +- [ ] feature request. + +### Current Behaviour: + + +### Expected Behaviour: + + +### Steps to reproduce: + + +### Do you want to work on this issue? + diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..848f371 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,34 @@ + + + +Fixes # + +### Checklist +- [ ] My branch is up-to-date with upstream/develop branch. +- [ ] Everything works and tested for Python 3.5.2 and above. + +### Description + + +### Change logs + + + + + + + + + + + + + + + + + + + + + diff --git a/README.md b/README.md index 9bef3d2..cf87f32 100644 --- a/README.md +++ b/README.md @@ -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* diff --git a/hydra_python_core/doc_maker.py b/hydra_python_core/doc_maker.py index 71e5449..cc3c3c1 100644 --- a/hydra_python_core/doc_maker.py +++ b/hydra_python_core/doc_maker.py @@ -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 @@ -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)) diff --git a/samples/__init__.py b/samples/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/samples/cli.py b/samples/cli.py new file mode 100644 index 0000000..b7a2957 --- /dev/null +++ b/samples/cli.py @@ -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)) diff --git a/samples/doc_writer_sample.py b/samples/doc_writer_sample.py index c4e54c7..4cf62fc 100644 --- a/samples/doc_writer_sample.py +++ b/samples/doc_writer_sample.py @@ -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 diff --git a/setup.py b/setup.py index e69de29..f9c641e 100644 --- a/setup.py +++ b/setup.py @@ -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 +) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_doc_writer.py b/tests/test_doc_writer.py new file mode 100644 index 0000000..d5f2fcf --- /dev/null +++ b/tests/test_doc_writer.py @@ -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()