marrow/uri

Missing scheme

prochac opened this issue · 3 comments

Can't register another scheme without pull request
example: amqp:// and amqps://
really painful

You absolutely can, reference, using entry_points plugin discovery. Edited to add: though a PR to add such to the core library would be appreciated for HTTP-like ("URL") URI in common usage, especially if reference to the specification defining it can be provided. (Docstrings are great, and on GitHub creating a fork and issuing a PR from viewing a file you want changed is essentially three clicks and some typing.) Apologies for all the edits, apparently I'm retentive.

Thank, I learned more about Python. I found the schemas in the setup.py but didn't know how to edit during runtime.

… but didn't know how to edit during runtime.

Ah! That's generally not the best way to do this, as it can represent an "import time side-effect" (ref: common mistakes; same content from a possibly non-Medium-powered, but rather ugly site).

However, AL/№ 36: practicality beats purity. It's possible, but requires modifying an object within the URI package dynamically at runtime. This is the code that would provide the desired change:

from uri.scheme import URLScheme
from uri.part.scheme import SchemePart

SchemePart.registry['amqp'] = URLScheme('amqp')
SchemePart.registry['amqps'] = URLScheme('amqps')

The seeming duplication is due to the fact the generic URLScheme formatter needs to know what it is representing, and the registry itself needs to be able to look up this formatter by string name. Using entry_points plugins this is automated away. (The registry here is a cache or short-circuit, saving the need for repeated iterative querying of Python package metadata to go looking for plugins and instantiation of such on every use. Essentially a pool of singletons.)