Add debug logging, closes #5
This commit is contained in:
parent
62b902492b
commit
22bf2d1246
|
@ -50,17 +50,18 @@ Base arguments
|
|||
.. code::
|
||||
|
||||
$ twtxt-registry
|
||||
[--version]
|
||||
[--help]
|
||||
[-V|--version]
|
||||
[-h|--help]
|
||||
[-k|-insecure]
|
||||
[-f [raw|json|pretty]]
|
||||
[-v|--verbose]
|
||||
REGISTRY_URL
|
||||
COMMAND
|
||||
[subcommand args]
|
||||
|
||||
``--version``
|
||||
``-V`` / ``--version``
|
||||
Output the CLI's version number and exit.
|
||||
``--help``
|
||||
``-h`` / ``--help``
|
||||
Output the main help text and exit.
|
||||
``-k`` / ``--insecure``
|
||||
Disable SSL certificate checks; first added for the `twtxt demo registry`_
|
||||
|
@ -68,6 +69,8 @@ Base arguments
|
|||
``-f`` / ``--format`` with one of ``raw``, ``json`` or ``pretty``
|
||||
Use a specific :class:`Formatter <twtxt_registry_client.output.Formatter>`
|
||||
class to output the HTTP responses.
|
||||
``-v`` / ``--verbose``
|
||||
Enable debug logging to ``stderr``.
|
||||
``REGISTRY_URL``
|
||||
Base URL to a twtxt registry's API.
|
||||
|
||||
|
@ -88,11 +91,11 @@ Registration
|
|||
$ twtxt-registry
|
||||
[...base arguments...]
|
||||
register
|
||||
[--help]
|
||||
[-h|--help]
|
||||
[-n|--nickname [NICK]]
|
||||
[-u|--url [URL]]
|
||||
|
||||
``--help``
|
||||
``-h`` / ``--help``
|
||||
Output the subcommand help text and exit.
|
||||
``-n [NICK]`` / ``--nickname [NICK]``
|
||||
Set a custom nickname. If omitted, the CLI will try to read it from the
|
||||
|
@ -115,10 +118,10 @@ List users
|
|||
$ twtxt-registry
|
||||
[...base arguments...]
|
||||
users
|
||||
[--help]
|
||||
[-h|--help]
|
||||
[-q|--query [TEXT]]
|
||||
|
||||
``--help``
|
||||
``-h`` / ``--help``
|
||||
Output the subcommand help text and exit.
|
||||
``-q [TEXT]`` / ``--query [TEXT]``
|
||||
Optionally filter users by a query.
|
||||
|
@ -137,10 +140,10 @@ List tweets
|
|||
$ twtxt-registry
|
||||
[...base arguments...]
|
||||
tweets
|
||||
[--help]
|
||||
[-h|--help]
|
||||
[-q|--query [TEXT]]
|
||||
|
||||
``--help``
|
||||
``-h`` / ``--help``
|
||||
Output the subcommand help text and exit.
|
||||
``-q [TEXT]`` / ``--query [TEXT]``
|
||||
Optionally filter tweets by a query.
|
||||
|
@ -159,10 +162,10 @@ List tweets by mention
|
|||
$ twtxt-registry
|
||||
[...base arguments...]
|
||||
mentions
|
||||
[--help]
|
||||
[-h|--help]
|
||||
[NAME_OR_URL]
|
||||
|
||||
``--help``
|
||||
``-h`` / ``--help``
|
||||
Output the subcommand help text and exit.
|
||||
``NAME_OR_URL``
|
||||
Name or URL of a user to list mentions to.
|
||||
|
@ -186,10 +189,10 @@ List tweets by tag
|
|||
$ twtxt-registry
|
||||
[...base arguments...]
|
||||
tag
|
||||
[--help]
|
||||
[-h|--help]
|
||||
NAME
|
||||
|
||||
``--help``
|
||||
``-h`` / ``--help``
|
||||
Output the subcommand help text and exit.
|
||||
``NAME``
|
||||
Tag to list tweets for.
|
||||
|
|
|
@ -5,6 +5,9 @@ from requests.exceptions import HTTPError
|
|||
from twtxt.config import Config
|
||||
from twtxt_registry_client import RegistryClient, output
|
||||
import click
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@click.group(name='twtxt-registry')
|
||||
|
@ -21,19 +24,31 @@ import click
|
|||
default='pretty',
|
||||
help='Change the output format.',
|
||||
)
|
||||
@click.option(
|
||||
'-v', '--verbose',
|
||||
is_flag=True,
|
||||
help='Output logs to stderr for debugging purposes.',
|
||||
)
|
||||
@click.pass_context
|
||||
def cli(ctx, registry_url, insecure, format):
|
||||
def cli(ctx, registry_url, insecure, format, verbose):
|
||||
"""
|
||||
Command-line client for the twtxt registry API.
|
||||
|
||||
Takes a mandatory registry URL argument, as the base API URL
|
||||
(ex. https://registry.twtxt.org/api)
|
||||
"""
|
||||
if verbose:
|
||||
logging.basicConfig(
|
||||
format='%(asctime)s [%(levelname)s/%(name)s] %(message)s',
|
||||
level=logging.DEBUG,
|
||||
)
|
||||
|
||||
ctx.obj = Namespace()
|
||||
|
||||
try:
|
||||
ctx.obj.conf = Config.discover()
|
||||
except ValueError:
|
||||
except ValueError as e:
|
||||
logger.debug('Could not load the twtxt configuration: {!s}'.format(e))
|
||||
ctx.obj.conf = Namespace()
|
||||
|
||||
scheme, netloc, path, query, fragment = urlsplit(registry_url)
|
||||
|
@ -42,9 +57,14 @@ def cli(ctx, registry_url, insecure, format):
|
|||
if not netloc and path:
|
||||
netloc, _, path = path.partition('/')
|
||||
registry_url = urlunsplit((scheme, netloc, path, query, fragment))
|
||||
|
||||
logger.debug('Using registry URL {}'.format(registry_url))
|
||||
if insecure:
|
||||
logger.debug('SSL certificate checks are disabled')
|
||||
ctx.obj.client = RegistryClient(registry_url, insecure=insecure)
|
||||
|
||||
ctx.obj.formatter = output.registry[format]()
|
||||
logger.debug('Using formatter {!r}'.format(ctx.obj.formatter))
|
||||
|
||||
|
||||
@cli.command()
|
||||
|
@ -66,14 +86,19 @@ def register(ctx, nickname, url):
|
|||
Register a user on a registry.
|
||||
"""
|
||||
if not nickname or not url:
|
||||
logger.debug(
|
||||
'Nickname or URL were omitted; trying to deduce from '
|
||||
'the twtxt configuration'
|
||||
)
|
||||
if not ctx.obj.conf:
|
||||
raise click.UsageError(
|
||||
'Nickname or URL were omitted from the command-line, but they'
|
||||
'Nickname or URL were omitted from the command-line, but they '
|
||||
'could not be deduced from the twtxt config.',
|
||||
ctx=ctx,
|
||||
)
|
||||
nickname = nickname or ctx.obj.conf.nick
|
||||
url = url or ctx.obj.conf.twturl
|
||||
logger.debug('Using nick {!r} and URL {!r}'.format(nickname, url))
|
||||
|
||||
click.echo(ctx.obj.formatter.format_response(
|
||||
ctx.obj.client.register(nickname, url, raise_exc=False)
|
||||
|
@ -127,21 +152,37 @@ def mentions(ctx, name_or_url):
|
|||
mentions.
|
||||
"""
|
||||
if name_or_url:
|
||||
logger.debug('Parsing the name/URL argument')
|
||||
scheme = urlsplit(name_or_url).scheme
|
||||
if ctx.obj.conf and not scheme: # it could be a nick
|
||||
logger.debug(
|
||||
'Argument could be a nickname; '
|
||||
'trying to deduce from the twtxt configuration'
|
||||
)
|
||||
source = ctx.obj.conf.get_source_by_nick(name_or_url)
|
||||
if source:
|
||||
logger.debug('Found source {!r} with URL {!r}'.format(
|
||||
source.nick, source.url))
|
||||
url = source.url
|
||||
else:
|
||||
logger.debug(
|
||||
'No source found from twtxt configuration; '
|
||||
'assuming argument is a URL'
|
||||
)
|
||||
url = url or name_or_url # Fallback
|
||||
elif not ctx.obj.conf:
|
||||
raise click.UsageError(
|
||||
'URL was omitted from the command-line, but it could not '
|
||||
'be deduced from the twtxt config.',
|
||||
ctx=ctx,
|
||||
)
|
||||
else:
|
||||
logger.debug(
|
||||
'URL was omitted; trying to deduce from the twtxt configuration')
|
||||
if not ctx.obj.conf:
|
||||
raise click.UsageError(
|
||||
'URL was omitted from the command-line, but it could not '
|
||||
'be deduced from the twtxt config.',
|
||||
ctx=ctx,
|
||||
)
|
||||
url = ctx.obj.conf.twturl
|
||||
|
||||
logger.debug('Using URL {}'.format(url))
|
||||
|
||||
try:
|
||||
click.echo(ctx.obj.formatter.format_tweets(
|
||||
ctx.obj.client.list_mentions(url)
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
import logging
|
||||
import urllib
|
||||
import click
|
||||
import requests
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class RegistryClient(object):
|
||||
"""
|
||||
|
@ -24,17 +27,23 @@ class RegistryClient(object):
|
|||
|
||||
from twtxt_registry_client import __version__
|
||||
if disclose_identity or disclose_identity is None:
|
||||
logger.debug('Looking up identity disclosure configuration')
|
||||
config = click.get_current_context().obj.conf
|
||||
disclose_identity = config.get('disclose_identity', False)
|
||||
|
||||
if disclose_identity:
|
||||
logger.debug('disclose_identity is enabled')
|
||||
user_agent = 'twtxt-registry/{} (+{}; @{})'.format(
|
||||
__version__,
|
||||
config.twturl,
|
||||
config.nick,
|
||||
)
|
||||
else:
|
||||
logger.debug(
|
||||
'Configuration not found or disclose_identity is disabled')
|
||||
user_agent = 'twtxt-registry/{}'.format(__version__)
|
||||
|
||||
logger.debug('Using user agent {!r}'.format(user_agent))
|
||||
self.session.headers['User-Agent'] = user_agent
|
||||
|
||||
def request(self, method, endpoint,
|
||||
|
@ -61,11 +70,15 @@ class RegistryClient(object):
|
|||
When ``raise_exc`` is ``True`` and the response has
|
||||
an HTTP 4xx or 5xx status code.
|
||||
"""
|
||||
# Ignore parameters with None values
|
||||
params = {k: v for k, v in params.items() if v}
|
||||
resp = method(
|
||||
'/'.join([self.registry_url, format, endpoint]),
|
||||
# Ignore parameters with None values
|
||||
params={k: v for k, v in params.items() if v},
|
||||
params=params,
|
||||
)
|
||||
logger.debug('{} request to {}'.format(resp.request.method, resp.url))
|
||||
logger.debug('HTTP {} {}'.format(resp.status_code, resp.reason))
|
||||
logger.debug('Response body: {!r}'.format(resp.text))
|
||||
if raise_exc:
|
||||
resp.raise_for_status()
|
||||
return resp
|
||||
|
|
|
@ -5,9 +5,12 @@ from twtxt.mentions import format_mentions
|
|||
from twtxt.parser import parse_iso8601
|
||||
import click
|
||||
import json
|
||||
import logging
|
||||
import humanize
|
||||
import textwrap
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class FormatterRegistry(ClassRegistry):
|
||||
"""
|
||||
|
@ -285,9 +288,15 @@ class PrettyFormatter(Formatter, key='pretty'):
|
|||
return self.format_response(resp)
|
||||
|
||||
# Try to determine the configured character limit and time display
|
||||
logger.debug(
|
||||
'Trying to load pretty printing options from twtxt configuration')
|
||||
conf = click.get_current_context().obj.conf
|
||||
abs_time = conf.get('use_abs_time', False)
|
||||
limit = conf.get('character_limit')
|
||||
logger.debug(
|
||||
'Using {} time'.format('absolute' if abs_time else 'relative'))
|
||||
logger.debug('Character limit: {!r}'.format(limit))
|
||||
|
||||
# Prevent AttributeErrors when using twtxt.helper.format_mentions
|
||||
conf.setdefault('twturl', None)
|
||||
conf.setdefault('following', [])
|
||||
|
|
Reference in New Issue