Giter Site home page Giter Site logo

grimen / python-connection-uri Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 26 KB

A robust connection URI parser/stringifier - for Python.

Home Page: https://pypi.org/project/connection-uri

License: MIT License

Makefile 4.50% Python 95.50%
connection-uri connection-url database-uri database-url server-uri server-url connection database server uri

python-connection-uri's Introduction

connection-uri PyPI version Build Status Coverage Status

A robust connection URI parser/stringifier - for Python.

Introduction

This connection URI library was implemented in lack of robust Python alternatives. This one supports absolute/relative multi-host connection URIs with smart fallbacks.

Install

Install using pip:

$ pip install connection-uri

Use

Very basic example:

import connection_uri

basic_connection_uri = 'localhost:3000/namespace/foo/'
basic_connection_options = connection_uri.unpack(basic_connection_uri)

print('\nconnection_uri.unpack({0})\n\n{1}\n'.format(basic_connection_uri, basic_connection_options))
#
# {
#     'protocol': 'http',
#     'auth': None,
#
#     'endpoint': 'localhost:3000',
#     'endpoints': ['localhost:3000'],
#
#     'host': 'localhost',
#     'hosts': ['localhost'],
#
#     'port': 3000,
#     'ports': [3000],

#     'path': '/namespace/foo/',
#     'query': {},
#
#     'credentials': {
#         'username': None,
#         'password': None,
#     },
#     'key': 'namespace/foo',
#     'namespace': 'namespace/foo',
#
#     'url': 'http://localhost:3000/namespace/foo/',
#     'urls': [
#         'http://localhost:3000/namespace/foo/'
#     ],
# }
#

basic_connection_uri = connection_uri.pack(basic_connection_options)

print('\nconnection_uri.pack({0})\n\n{1}\n'.format(basic_connection_options, basic_connection_uri))
#
# 'http://localhost:3000/namespace/foo/'
#

print('---')

complex_multihost_uri = 'foo://m+4.gTe~5e^(:m+4.gTe~5e^(@ds143144-a0.mlab.com:43144,ds143144-a1.mlab.com:43145/bar-baz'
complex_multihost_options = connection_uri.unpack(complex_multihost_uri)

print('\nconnection_uri.unpack({0})\n\n{1}\n'.format(complex_multihost_uri, complex_multihost_options))
#
# {
#     'protocol': 'foo',
#     'auth': 'm+4.gTe~5e^(:m+4.gTe~5e^(',
#
#     'host': 'ds143144-a0.mlab.com',
#     'port': 43144,
#
#     'endpoint': 'ds143144-a0.mlab.com:43144',
#     'endpoints': ['ds143144-a0.mlab.com:43144', 'ds143144-a1.mlab.com:43145'],
#
#     'host': 'ds143144-a0.mlab.com',
#     'hosts': ['ds143144-a0.mlab.com', 'ds143144-a1.mlab.com'],
#
#     'port': 43144,
#     'ports': [43144, 43145],
#
#     'path': '/bar-baz',
#     'query': {},
#
#     'credentials': {
#         'username': 'm+4.gTe~5e^(',
#         'password': 'm+4.gTe~5e^(',
#     },
#     'key': 'bar-baz',
#     'namespace': 'bar-baz',
#
#     'url': 'foo://m+4.gTe~5e^(:m+4.gTe~5e^(@ds143144-a0.mlab.com:43144,ds143144-a1.mlab.com:43145/bar-baz',
#     'urls': [
#         'foo://m+4.gTe~5e^(:m+4.gTe~5e^(@ds143144-a0.mlab.com:43144/bar-baz',
#         'foo://m+4.gTe~5e^(:m+4.gTe~5e^(@ds143144-a1.mlab.com:43145/bar-baz'
#     ],
# }
#

complex_multihost_uri = connection_uri.pack(complex_multihost_options)

print('\nconnection_uri.pack({0})\n\n{1}\n'.format(complex_multihost_options, complex_multihost_uri))
#
# 'foo://m+4.gTe~5e^(:m+4.gTe~5e^(@ds143144-a0.mlab.com:43144,ds143144-a1.mlab.com:43145/bar-baz'
#

# NOTE: see tests for more advanced examples, e.g. the library handles absolute and relative URIs, etc.

Test

Clone down source code:

$ make install

Run colorful tests, with only native environment (dependency sandboxing up to you):

$ make test

Run less colorful tests, with multi-environment (using tox):

$ make test-tox

About

This project was mainly initiated - in lack of solid existing alternatives - to be used at our work at Markable.ai to have common code conventions between various programming environments where Python (research, CV, AI) is heavily used.

License

Released under the MIT license.

python-connection-uri's People

Contributors

grimen avatar

Stargazers

 avatar

Watchers

 avatar  avatar

python-connection-uri's Issues

BUG: handle `IndexError: list index out of range` for invalid URIs

======================================================================
ERROR: test___str__ (__main__.TestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/grimen/.pyenv/versions/3.8.0/lib/python3.8/site-packages/connection_uri/uri.py", line 336, in deserialize
    'ports': [ports[endpoint_index]],
IndexError: list index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "markable_utils/tests/data/storages2/test_mongodb.py", line 1009, in test___str__
    reset(storage)
  File "markable_utils/tests/data/storages2/test_mongodb.py", line 85, in reset
    raise error
  File "markable_utils/tests/data/storages2/test_mongodb.py", line 60, in reset
    client = connect(storage)
  File "markable_utils/tests/data/storages2/test_mongodb.py", line 47, in connect
    raise error
  File "markable_utils/tests/data/storages2/test_mongodb.py", line 36, in connect
    uri_object = connection_uri.unpack(storage.uri)
  File "/Users/grimen/.pyenv/versions/3.8.0/lib/python3.8/site-packages/connection_uri/uri.py", line 418, in unpack
    return uri.unpack(*args, **kwargs)
  File "/Users/grimen/.pyenv/versions/3.8.0/lib/python3.8/site-packages/connection_uri/uri.py", line 364, in unpack
    return self.deserialize(*args, **kwargs)
  File "/Users/grimen/.pyenv/versions/3.8.0/lib/python3.8/site-packages/connection_uri/uri.py", line 356, in deserialize
    raise URIError(error, details = {
connection_uri.uri.URIError: list index out of range - {'data': 'mongodb://localhost:27017{path.sep}{db_key}'}

----------------------------------------------------------------------

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.