From 05cf18c2674ae3249e52cd598434f428bc64a15a Mon Sep 17 00:00:00 2001 From: Danny Colligan Date: Wed, 2 Nov 2016 11:14:12 -0400 Subject: [PATCH] Hierarchical namespace --- .gitignore | 2 +- .travis.yml | 4 +- client_dev.py | 2 +- docs/api.rst | 2 +- docs/conf.py | 2 +- ga4gh/__init__.py | 8 ++ {ga4gh_client => ga4gh/client}/__init__.py | 3 + {ga4gh_client => ga4gh/client}/cli.py | 12 +- {ga4gh_client => ga4gh/client}/client.py | 6 +- {ga4gh_client => ga4gh/client}/exceptions.py | 0 {ga4gh_client => ga4gh/client}/protocol.py | 46 +++---- ga4gh_client/_protocol_version.py | 2 - requirements.txt | 4 +- scripts/process_schemas.py | 130 ------------------- setup.py | 7 +- tests/unit/test_cli.py | 7 +- tests/unit/test_client.py | 6 +- 17 files changed, 62 insertions(+), 181 deletions(-) create mode 100644 ga4gh/__init__.py rename {ga4gh_client => ga4gh/client}/__init__.py (65%) rename {ga4gh_client => ga4gh/client}/cli.py (99%) rename {ga4gh_client => ga4gh/client}/client.py (99%) rename {ga4gh_client => ga4gh/client}/exceptions.py (100%) rename {ga4gh_client => ga4gh/client}/protocol.py (87%) delete mode 100644 ga4gh_client/_protocol_version.py delete mode 100644 scripts/process_schemas.py diff --git a/.gitignore b/.gitignore index 0de3b15..5e0c190 100644 --- a/.gitignore +++ b/.gitignore @@ -12,7 +12,7 @@ scripts/auth.yml *.bak *.db-shm *.db-wal -ga4gh_client/_version.py +_version.py tests/data/registry.db # Below based off of https://gist.github.com/octocat/9257657 diff --git a/.travis.yml b/.travis.yml index a6c599d..296b492 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,9 +21,9 @@ before_script: # run_tests.py runs everything under the script: tag so only put commands # under it that we want to run (and want to be able to run) as local tests script: - - flake8 client_dev.py setup.py tests ga4gh_client scripts + - flake8 client_dev.py setup.py tests ga4gh scripts - nosetests tests - --with-coverage --cover-package ga4gh_client + --with-coverage --cover-package ga4gh.client --cover-inclusive --cover-min-percentage=50 --cover-branches --cover-erase - make clean -C docs diff --git a/client_dev.py b/client_dev.py index 5df7f94..8a5f590 100644 --- a/client_dev.py +++ b/client_dev.py @@ -1,7 +1,7 @@ """ Simple shim for running the client program during development. """ -import ga4gh_client.cli as cli +import ga4gh.client.cli as cli if __name__ == "__main__": cli.client_main() diff --git a/docs/api.rst b/docs/api.rst index 2e0a8b2..00191f8 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -29,7 +29,7 @@ Client API .. todo:: Add overview documentation for the client API. -.. autoclass:: ga4gh_client.client.HttpClient +.. autoclass:: ga4gh.client.client.HttpClient :members: get_reference_set, get_reference, get_dataset, get_variant_set, get_variant, get_read_group_set, get_read_group, diff --git a/docs/conf.py b/docs/conf.py index c21f96b..a790383 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -19,7 +19,7 @@ import os import sys sys.path.insert(0, os.path.abspath('..')) -import ga4gh_client +import ga4gh.client # -- General configuration ------------------------------------------------ diff --git a/ga4gh/__init__.py b/ga4gh/__init__.py new file mode 100644 index 0000000..0208db4 --- /dev/null +++ b/ga4gh/__init__.py @@ -0,0 +1,8 @@ +""" +Client for servers implementing the GA4GH api +""" +from __future__ import division +from __future__ import print_function +from __future__ import unicode_literals + +__import__('pkg_resources').declare_namespace(__name__) diff --git a/ga4gh_client/__init__.py b/ga4gh/client/__init__.py similarity index 65% rename from ga4gh_client/__init__.py rename to ga4gh/client/__init__.py index 39fbd56..68f7399 100644 --- a/ga4gh_client/__init__.py +++ b/ga4gh/client/__init__.py @@ -1,6 +1,9 @@ """ Reference implementation of the GA4GH client """ +# Don't include future imports here; we don't want to export them as +# part of the package + __version__ = "undefined" try: from . import _version diff --git a/ga4gh_client/cli.py b/ga4gh/client/cli.py similarity index 99% rename from ga4gh_client/cli.py rename to ga4gh/client/cli.py index a5387c7..894b28b 100644 --- a/ga4gh_client/cli.py +++ b/ga4gh/client/cli.py @@ -9,12 +9,12 @@ import logging import requests -import ga4gh_client -import ga4gh_client.client as client -import ga4gh_client.exceptions as exceptions -import ga4gh_client.protocol as protocol +import ga4gh.client +import ga4gh.client.client as client +import ga4gh.client.exceptions as exceptions +import ga4gh.client.protocol as protocol -import ga4gh_common.cli as cli +import ga4gh.common.cli as cli ############### @@ -36,7 +36,7 @@ def addVersionArgument(parser): versionString = ( "GA4GH Client Version {}\n" "(Protocol Version {})".format( - ga4gh_client.__version__, protocol.version)) + ga4gh.client.__version__, protocol.version)) parser.add_argument( "--version", version=versionString, action="version") diff --git a/ga4gh_client/client.py b/ga4gh/client/client.py similarity index 99% rename from ga4gh_client/client.py rename to ga4gh/client/client.py index 38126ba..729e49e 100644 --- a/ga4gh_client/client.py +++ b/ga4gh/client/client.py @@ -9,10 +9,10 @@ import posixpath import logging -import ga4gh_client.protocol as protocol -import ga4gh_client.exceptions as exceptions +import ga4gh.client.protocol as protocol +import ga4gh.client.exceptions as exceptions -import ga4gh_schemas.pb as pb +import ga4gh.schemas.pb as pb class AbstractClient(object): diff --git a/ga4gh_client/exceptions.py b/ga4gh/client/exceptions.py similarity index 100% rename from ga4gh_client/exceptions.py rename to ga4gh/client/exceptions.py diff --git a/ga4gh_client/protocol.py b/ga4gh/client/protocol.py similarity index 87% rename from ga4gh_client/protocol.py rename to ga4gh/client/protocol.py index 75901de..7ab1ea8 100644 --- a/ga4gh_client/protocol.py +++ b/ga4gh/client/protocol.py @@ -14,29 +14,29 @@ import google.protobuf.message as message import google.protobuf.struct_pb2 as struct_pb2 -import ga4gh_schemas.pb as pb - -from ga4gh_schemas._protocol_version import version # noqa -from ga4gh_schemas.ga4gh.common_pb2 import * # noqa -from ga4gh_schemas.ga4gh.assay_metadata_pb2 import * # noqa -from ga4gh_schemas.ga4gh.metadata_pb2 import * # noqa -from ga4gh_schemas.ga4gh.metadata_service_pb2 import * # noqa -from ga4gh_schemas.ga4gh.read_service_pb2 import * # noqa -from ga4gh_schemas.ga4gh.reads_pb2 import * # noqa -from ga4gh_schemas.ga4gh.reference_service_pb2 import * # noqa -from ga4gh_schemas.ga4gh.references_pb2 import * # noqa -from ga4gh_schemas.ga4gh.variant_service_pb2 import * # noqa -from ga4gh_schemas.ga4gh.variants_pb2 import * # noqa -from ga4gh_schemas.ga4gh.allele_annotations_pb2 import * # noqa -from ga4gh_schemas.ga4gh.allele_annotation_service_pb2 import * # noqa -from ga4gh_schemas.ga4gh.sequence_annotations_pb2 import * # noqa -from ga4gh_schemas.ga4gh.sequence_annotation_service_pb2 import * # noqa -from ga4gh_schemas.ga4gh.bio_metadata_pb2 import * # noqa -from ga4gh_schemas.ga4gh.bio_metadata_service_pb2 import * # noqa -from ga4gh_schemas.ga4gh.genotype_phenotype_pb2 import * # noqa -from ga4gh_schemas.ga4gh.genotype_phenotype_service_pb2 import * # noqa -from ga4gh_schemas.ga4gh.rna_quantification_pb2 import * # noqa -from ga4gh_schemas.ga4gh.rna_quantification_service_pb2 import * # noqa +import ga4gh.schemas.pb as pb + +from ga4gh.schemas._protocol_version import version # noqa +from ga4gh.schemas.ga4gh.common_pb2 import * # noqa +from ga4gh.schemas.ga4gh.assay_metadata_pb2 import * # noqa +from ga4gh.schemas.ga4gh.metadata_pb2 import * # noqa +from ga4gh.schemas.ga4gh.metadata_service_pb2 import * # noqa +from ga4gh.schemas.ga4gh.read_service_pb2 import * # noqa +from ga4gh.schemas.ga4gh.reads_pb2 import * # noqa +from ga4gh.schemas.ga4gh.reference_service_pb2 import * # noqa +from ga4gh.schemas.ga4gh.references_pb2 import * # noqa +from ga4gh.schemas.ga4gh.variant_service_pb2 import * # noqa +from ga4gh.schemas.ga4gh.variants_pb2 import * # noqa +from ga4gh.schemas.ga4gh.allele_annotations_pb2 import * # noqa +from ga4gh.schemas.ga4gh.allele_annotation_service_pb2 import * # noqa +from ga4gh.schemas.ga4gh.sequence_annotations_pb2 import * # noqa +from ga4gh.schemas.ga4gh.sequence_annotation_service_pb2 import * # noqa +from ga4gh.schemas.ga4gh.bio_metadata_pb2 import * # noqa +from ga4gh.schemas.ga4gh.bio_metadata_service_pb2 import * # noqa +from ga4gh.schemas.ga4gh.genotype_phenotype_pb2 import * # noqa +from ga4gh.schemas.ga4gh.genotype_phenotype_service_pb2 import * # noqa +from ga4gh.schemas.ga4gh.rna_quantification_pb2 import * # noqa +from ga4gh.schemas.ga4gh.rna_quantification_service_pb2 import * # noqa # A map of response objects to the name of the attribute used to diff --git a/ga4gh_client/_protocol_version.py b/ga4gh_client/_protocol_version.py deleted file mode 100644 index 4b8f68f..0000000 --- a/ga4gh_client/_protocol_version.py +++ /dev/null @@ -1,2 +0,0 @@ -# File generated by scripts/process_schemas.py; do not edit -version = '0.6.0.a8' diff --git a/requirements.txt b/requirements.txt index 1fe6185..f5bd66a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,7 +3,7 @@ # # These requirements are read directly into setup.py, so specify them # in the order that they should be in in setup.py -ga4gh_common==0.0.4 -ga4gh_schemas==0.0.7 +ga4gh_common==0.0.5 +ga4gh_schemas==0.0.8 requests protobuf==3.0.0b3 diff --git a/scripts/process_schemas.py b/scripts/process_schemas.py deleted file mode 100644 index 3240666..0000000 --- a/scripts/process_schemas.py +++ /dev/null @@ -1,130 +0,0 @@ -""" -A script to generate the schemas for the GA4GH protocol. These are generated -from a copy of the Protocol Buffers schema and use it to generate -the Python class definitions. These are also stored in revision -control to aid Travis building. -""" -from __future__ import division -from __future__ import print_function -from __future__ import unicode_literals - -import os -import os.path -import subprocess -import fnmatch -import re -import argparse - -import ga4gh_common.utils as utils - - -class ProtobufGenerator(object): - - def __init__(self, version): - self.version = version - - def _assertSchemasExist(self, schemas): - if not os.path.exists(schemas): - raise Exception( - "Can't find schemas folder. " + - "Thought it would be at {}".format( - os.path.realpath(schemas))) - - def _assertProtoDirectoryExists(self, src): - if not os.path.exists(src): - msg = "Can't find source proto directory {}".format( - os.path.realpath(src)) - raise Exception(msg) - - def _find_in_path(self, cmd): - PATH = os.environ.get("PATH", os.defpath).split(os.pathsep) - for x in PATH: - possible = os.path.join(x, cmd) - if os.path.exists(possible): - return possible - return None - - # From http://stackoverflow.com/a/1714190/320546 - def _version_compare(self, version1, version2): - def normalize(v): - return [int(x) for x in re.sub(r'(\.0+)*$', '', v).split(".")] - return cmp(normalize(version1), normalize(version2)) - - def _getProtoc(self, server): - protocs = [ - os.path.realpath(x) for x in - "{}/protobuf/src/protoc".format(server), - self._find_in_path("protoc") - if x is not None] - protoc = None - for c in protocs: - if not os.path.exists(c): - continue - output = subprocess.check_output([c, "--version"]).strip() - try: - (lib, version) = output.split(" ") - if lib != "libprotoc": - raise Exception("lib didn't match 'libprotoc'") - if self._version_compare("3.0.0", version) > 0: - raise Exception("version < 3.0.0") - protoc = c - break - except Exception: - utils.log( - "Not using {path} because it returned " + - "'{version}' rather than \"libprotoc \", where " + - " >= 3.0.0").format(path=c, format=output) - - if protoc is None: - raise Exception("Can't find a good protoc. Tried {}".format( - protocs)) - utils.log("Using protoc: '{}'".format(protoc)) - return protoc - - def _writePythonFiles(self, src, protoc, server): - protos = [] - for root, dirs, files in os.walk(src): - protos.extend([ - os.path.join(root, f) - for f in fnmatch.filter(files, "*.proto")]) - if len(protos) == 0: - raise Exception("Didn't find any proto files in ".format(src)) - cmd = "{protoc} -I {src} --python_out={server} {proto_files}".format( - protoc=protoc, src=src, - server=server, proto_files=" ".join(protos)) - utils.runCommand(cmd) - - def _writeVersionFile(self): - with open("ga4gh_client/_protocol_version.py", "w") as version_file: - version_file.write( - "# File generated by scripts/process_schemas.py; " - "do not edit\n") - version_file.write("version = '{}'\n".format(self.version)) - - def run(self, args): - script_path = os.path.dirname(os.path.realpath(__file__)) - server = os.path.realpath(os.path.join(script_path, "..")) - schemas = os.path.realpath(args.schemapath) - self._assertSchemasExist(schemas) - schemasSubdir = "src/main/proto" - src = os.path.realpath(os.path.join(schemas, schemasSubdir)) - self._assertProtoDirectoryExists(src) - protoc = self._getProtoc(server) - self._writePythonFiles(src, protoc, server) - self._writeVersionFile() - - -def main(): - parser = argparse.ArgumentParser( - description="Script to process GA4GH Protocol buffer schemas") - parser.add_argument( - "version", help="Version number of the schema we're compiling") - parser.add_argument( - "schemapath", - help="Path to schemas.") - args = parser.parse_args() - pb = ProtobufGenerator(args.version) - pb.run(args) - -if __name__ == "__main__": - main() diff --git a/setup.py b/setup.py index 093733d..7a904b4 100644 --- a/setup.py +++ b/setup.py @@ -27,12 +27,13 @@ # END BOILERPLATE name="ga4gh_client", description="A client for the GA4GH reference server", - packages=["ga4gh_client"], + packages=["ga4gh", "ga4gh.client"], + namespace_packages=["ga4gh"], url="https://github.com/ga4gh/ga4gh-client", - use_scm_version={"write_to": "ga4gh_client/_version.py"}, + use_scm_version={"write_to": "ga4gh/client/_version.py"}, entry_points={ 'console_scripts': [ - 'ga4gh_client=ga4gh_client.cli:client_main', + 'ga4gh_client=ga4gh.client.cli:client_main', ] }, # BEGIN BOILERPLATE diff --git a/tests/unit/test_cli.py b/tests/unit/test_cli.py index 61ad050..7a7391a 100644 --- a/tests/unit/test_cli.py +++ b/tests/unit/test_cli.py @@ -9,11 +9,12 @@ import mock import unittest -import ga4gh_client.cli as cli_client -import ga4gh_client.protocol as protocol import google.protobuf.descriptor as descriptor import google.protobuf.internal.python_message as python_message -import ga4gh_common.utils as utils + +import ga4gh.client.cli as cli_client +import ga4gh.client.protocol as protocol +import ga4gh.common.utils as utils class TestClientArguments(unittest.TestCase): diff --git a/tests/unit/test_client.py b/tests/unit/test_client.py index f599efb..7d4fb10 100644 --- a/tests/unit/test_client.py +++ b/tests/unit/test_client.py @@ -9,9 +9,9 @@ import mock -import ga4gh_client.protocol as protocol -import ga4gh_client.client as client -import ga4gh_client.exceptions as exceptions +import ga4gh.client.protocol as protocol +import ga4gh.client.client as client +import ga4gh.client.exceptions as exceptions class TestSearchMethodsCallRunRequest(unittest.TestCase):