Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proposal for real Twine API #361

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
Twine's Proposed Programmatic API
=================================

Over the years, people have longed for an API so other Python tooling can take
advantage of how Twine interacts with PyPI and other package servers. It's
been a long road to get here, but here's what I'm proposing our API would look
like:

.. code-block:: python

import os

from twine import api as twine


configuration = twine.Settings(
repository_url='https://upload.pypi.org/legacy/',
username=os.environ['TWINE_USERNAME'],
password=os.environ['TWINE_PASSWORD'],
)

repository = configuration.create_repository()

packages = [twine.FileUploader(filename, repository, configuration)
for filename in os.listdir(path='./dist')]
twine.upload(packages)


The design comes from the knowledge that there are already a lot of knobs to
tweak in Twine from the command-line and many of the users of the API will
expect similar abilities. The easiest way to manage all that configuration
would appear to be through a single object that understands how to validate
it - the ``Settings`` object. Part of the design of this object would force
users to pass all configuration via keyword arguments so there are no implicit
positional arguments. It would also handle parsing the user's ``~/.pypirc``
and attempting to retrieve the username and password. Finally, it would
provide a short-cut for producing the ``Repository`` object that is already
available as a semi-public API.

Next, we'll want to provide some way of generating the files to be uploaded.
Above we have a notion of a ``FileUploader`` object that would create the
semi-public ``PackageFile`` object and incorporate the configuration and then
handle the interaction between a repository and the package file. An
alternative might be a ``DirectoryUploader`` or a Builder object that produces
a ``PackageFile``. For example, the Builder object might behave like:

.. code-block:: python

import os

from twine import api as twine

configuration = twine.Settings(...)
repository = configuration.create_repository()

builder = twine.UploadBuilder(
).add_repository(
repository
).add_configuration(
configuration
)
packages = [
builder.add_filename(filename).finalize()
for filename in os.listdir(path='./dist')
]
twine.upload(packages)


There's also room here for a Settings builder object if one is desired.
Something like:

.. code-block:: python

from twine import api as twine

settings = twine.SettingsBuilder().authenticate_with(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Builder pattern doesn't look typical for Python apps. I'd say that we're more used to having context managers and we expect object initializer (__init__ method) to exist with fully initialized and validated instance.

username=os.environ['TWINE_USERNAME'],
password=os.environ['TWINE_PASSWORD'],
).upload_to(
'https://upload.pypi.org/legacy'
).enable_signatures(
sign_with='gpg',
sign_as='my-identity',
).finalize()


We're also open to other designs as things move along. I (Ian) plan to split
this work up into the following:

1. Create the ``Settings`` object and replace our argparse parsing/options
usage in core Twine with it. (Dogfooding our API is important.) The
settings object:

* should have a ``from_argparse`` classmethod that can do the right thing

* should be able to be instantiated from a builder if desirable

* provides validation of arguments, e.g., passing both ``--sign`` and
``--identity``

* creates other objects that may be necessary for normal operations

2. Next we'll create the object(s) to manage configuration files on disk they
will be used by the Settings object in lieu of the existing functionality
and will

* parse config files

* discern correct config data to return

* prints warnings based on configuration, e.g., detecting LEGACY_PYPI

* retrieves username and password if unset

3. Next we'll work on the Uploader object(s) which will:

* handle the work of coordinating required objects to perform the upload

* handle logic of skipping an upload

* handle logic of checking the status code

* and will require:

- Repository

- PackageFile

- Settings

At some point we'll need to work on the Repository object as well to remove
the use of ProgressBar without losing that functionality. An API shouldn't
force this on its users.
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Twine user documentation

contributing
changelog
api
Python Packaging User Guide <https://packaging.python.org/tutorials/distributing-packages/>

* :ref:`search`