Skip to content
This repository has been archived by the owner on Feb 2, 2022. It is now read-only.

Commit

Permalink
Add Formatters to coverage (#133)
Browse files Browse the repository at this point in the history
Bring formatters to 100% test coverage.

Drive-by:
* Remove GSheet formatter
* Remove unused functions and dependencies
  • Loading branch information
rgreinho committed May 26, 2019
1 parent e624268 commit 5bb0601
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 296 deletions.
1 change: 0 additions & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ omit =
scrapd/cli/*
scrapd/config.py
scrapd/core/constant.py
scrapd/core/formatter.py
scrapd/core/version.py
scrapd/main.py
scrapd/tests/*
14 changes: 11 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Refactor the functions parsing the fields to simplify their maintenance.
- Remove LXML as a dependency. [#]
- Refactor the functions parsing the fields to simplify their maintenance and improve the quality of the parsing.
- Remove LXML as a dependency. [#117]
- Dates are stored internaly as date objects instead of strings. The formatting is delegated to the `Formatters`
themselves. [#125]
- Remove GSheet support. [#]

### Fixed

- Fix parsing of fatalities without date of birth. [#125]

## [[1.5.1]] - 2019-04-25

Expand Down Expand Up @@ -131,4 +138,5 @@ This first version allows a user to retrieve traffic fatality repports for a cer
[#85]: https://github.com/scrapd/scrapd/pull/85
[#90]: https://github.com/scrapd/scrapd/pull/90
[#116]: https://github.com/scrapd/scrapd/pull/116
[#]: https://github.com/scrapd/scrapd/pull/
[#117]: https://github.com/scrapd/scrapd/pull/117
[#125]: https://github.com/scrapd/scrapd/pull/125
9 changes: 0 additions & 9 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,6 @@ of the process::
}
]


Export the results to Google Sheets::

scrapd -v \
--from "Feb 1 2019" \
--format gsheets \
--gcredentials creds.json \
--gcontributors "remy.greinhofer@gmail.com:user:writer"

Speed and accuracy
------------------

Expand Down
11 changes: 1 addition & 10 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
aiohttp==3.5.4
anyconfig==0.9.9
click==7.0
dateparser==0.7.1
google-api-python-client==1.7.8
google-auth-httplib2==0.0.3
google-auth-oauthlib==0.2.0
gspread==3.1.0
loguru==0.2.5
pbr==5.2.0
tabulate==0.8.3
pbr>=5.2.0
tenacity==5.0.4

# TODO(remyg): These libraries are deprecated. Use a modern authentication method and remove them.
oauth2client
PyOpenSSL
31 changes: 0 additions & 31 deletions scrapd/cli/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import sys

from loguru import logger
from tabulate import tabulate


class AbstractCommand:
Expand Down Expand Up @@ -40,33 +39,3 @@ def execute(self):
def _execute(self):
"""Define the internal execution of the command."""
raise NotImplementedError

def display_tabular_data(self):
"""
Show the results in a table on the screen.
If not header is defined, only the data is displayed, otherwise, the results will be shown in a table.
"""
# Nothing to display if there is no data.
if not self.data:
return

# Define the table format based on the headers content.
table_format = "simple" if self.headers else "plain"

# Print the results.
print(tabulate(self.data, self.headers, tablefmt=table_format))

def placeholder_dict(self):
"""
Convert the set of placeholders to a dictionary.
Placeholders are a set of key/value pairs. Their format is `key=value`. During the convertion, the `key`
becomes the key of the entry in the dictionary, the `value` becomes the value of the entry.
:param set placeholder: the placeholder to convert
:return dict: a dictionary representing the placeholder set.
"""
placeholders = self.args.get('placeholder', [])
kvp_list = [kvp.split('=', 1) for kvp in placeholders if '=' in kvp]
return dict(kvp_list)
22 changes: 0 additions & 22 deletions scrapd/cli/cli.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
"""Define the top-level cli command."""
import asyncio
import logging
import os
import sys

import click
from loguru import logger

from scrapd import config
from scrapd.cli.base import AbstractCommand
from scrapd.core import apd
from scrapd.core.formatter import Formatter
Expand Down Expand Up @@ -45,11 +43,6 @@ def cli(ctx, format_, from_, gcontributors, gcredentials, pages, to, verbose):
ctx.obj = {**ctx.params}
ctx.auto_envvar_prefix = 'VZ'

# Load defaults from configuration file if any.
cfg_path = os.path.join(click.get_app_dir(APP_NAME), APP_NAME + '.conf')
cfg = cfg_path if os.path.exists(cfg_path) else None
ctx.default_map = config.load(cfg, with_defaults=True, validate=True)

# Configure logger.
INITIAL_LOG_LEVEL = logging.WARNING
LOG_FORMAT_COMPACT = "<level>{message}</level>"
Expand Down Expand Up @@ -81,9 +74,6 @@ class Retrieve(AbstractCommand):

def _execute(self):
"""Define the internal execution of the command."""
# Check the params.
self.check_params()

# Collect the results.
results, _ = asyncio.run(apd.async_retrieve(
self.args['pages'],
Expand All @@ -97,15 +87,3 @@ def _execute(self):
format_ = self.args['format_'].lower()
formatter = Formatter(format_)
formatter.print(results)

def check_params(self):
"""
Raise an exception if a custom parameters is invalid or missing.
This is to avoid collecting all the data then failing due to a missing parameter.
"""
if self.args['format_'].lower() == 'gsheets':
if not self.args.get('gcredentials'):
raise click.ClickException('Google credentials are required.')
if not self.args.get('gcontributors'):
raise click.ClickException('At least 1 contributor is required.')
49 changes: 0 additions & 49 deletions scrapd/config.py

This file was deleted.

30 changes: 4 additions & 26 deletions scrapd/core/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import sys

from scrapd.core.constant import Fields
from scrapd.core.gsheets import GSheets

CSVFIELDS = [
Fields.CRASHES,
Expand Down Expand Up @@ -54,12 +53,15 @@ def _get_formatter(self):
formatter = self.formatters.get(self.format, self)
return formatter()

def print(self, results, **kwargs):
def print(self, results, **kwargs): # pragma: no cover
"""
Print the results with the appropriate formatter.
:param list(dict) results: the results to display.
"""
# Coverage is disabled for this function as the fact that we set output to sys.stdout early messes up
# with capsys in pytest, and prevent it to capture the out correctly.
# https://github.com/pytest-dev/pytest/issues/1132
formatter = self._get_formatter()
formatter.printer(results, **kwargs)

Expand All @@ -69,7 +71,6 @@ def date_serialize(self, obj):
:rtype: str
"""

if isinstance(obj, (datetime.datetime, datetime.date)):
return obj.strftime("%m/%d/%Y")
raise TypeError("Type %s not serializable" % type(obj))
Expand All @@ -91,7 +92,6 @@ def to_json_string(self, results):
:rtype: str
"""

return json.dumps(results, sort_keys=True, indent=2, default=self.date_serialize)


Expand Down Expand Up @@ -140,28 +140,6 @@ def printer(self, results, **kwargs): # noqa: D102
writer.writerows(results)


class GSheetFormatter(Formatter):
"""
Define the GSheet formatter.
Stores the results into a Google Sheets document.
"""

__format_name__ = 'gsheets'

def printer(self, results, **kwargs): # noqa: D102
credentials = kwargs.get('gcredentials')
if not credentials:
raise AttributeError('Google credentials are required.')
contributors = kwargs.get('gcontributors', '').split(',')
if not contributors:
raise AttributeError('At least 1 contributor is required.')
gs = GSheets(credentials, contributors)
gs.authenticate()
gs.create(datetime.datetime.now().strftime('%Y-%m-%d'))
gs.add_csv_data(CSVFIELDS, results)


class CountFormatter(Formatter):
"""
Define the Count formatter.
Expand Down
81 changes: 0 additions & 81 deletions scrapd/core/gsheets.py

This file was deleted.

Loading

0 comments on commit 5bb0601

Please sign in to comment.