Skip to content
New issue

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

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

Already on GitHub? # to your account

Enforce yapf for lint. #21

Merged
merged 1 commit into from
Jun 29, 2020
Merged
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
6 changes: 6 additions & 0 deletions .yapfignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Virtual environments
venv
**/venv

# Automatically generated nox files
.nox
7 changes: 7 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ use GitHub pull requests for this purpose. Consult
[GitHub Help](https://help.github.com/articles/about-pull-requests/) for more
information on using pull requests.

Please run the nox tests before sending your code for review. You can do that
with:
```sh
pip install .[dev]
nox
```

## Community Guidelines

This project follows [Google's Open Source Community
Expand Down
24 changes: 15 additions & 9 deletions build_golden_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import pybadges


def generate_images(source_json_path, target_directory):
os.makedirs(target_directory, exist_ok=True)
with open(source_json_path) as f:
Expand All @@ -34,15 +35,20 @@ def generate_images(source_json_path, target_directory):


def main():
parser = argparse.ArgumentParser(description='generate a github-style badge given some text and colors')

parser.add_argument('--source-path',
default=pkg_resources.resource_filename(__name__, 'tests/test-badges.json'),
help='the text to show on the left-hand-side of the badge')

parser.add_argument('--destination-dir',
default=pkg_resources.resource_filename(__name__, 'tests/golden-images'),
help='the text to show on the left-hand-side of the badge')
parser = argparse.ArgumentParser(
description='generate a github-style badge given some text and colors')

parser.add_argument(
'--source-path',
default=pkg_resources.resource_filename(__name__,
'tests/test-badges.json'),
help='the text to show on the left-hand-side of the badge')

parser.add_argument(
'--destination-dir',
default=pkg_resources.resource_filename(__name__,
'tests/golden-images'),
help='the text to show on the left-hand-side of the badge')
args = parser.parse_args()
generate_images(args.source_path, args.destination_dir)

Expand Down
28 changes: 9 additions & 19 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,14 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Nox config for running lint and unit tests."""

import nox


def _run_tests(session):
session.run(
'py.test',
'--quiet',
'tests',
'server-example',
*session.posargs
)
session.run('py.test', '--quiet', 'tests', 'server-example',
*session.posargs)


@nox.session
Expand All @@ -33,10 +27,8 @@ def lint(session):
Returns a failure if flake8 finds linting errors or sufficiently
serious code quality issues.
"""
session.install('flake8')
session.run('flake8', 'pybadges')
session.run('flake8', 'tests')
session.run('flake8', 'server-example')
session.install('yapf')
session.run('python3', '-m', 'yapf', '--diff', '-r', '.')


@nox.session
Expand All @@ -48,8 +40,9 @@ def unit(session):


@nox.session(python=['3.4', '3.5', '3.6', '3.7', '3.8'])
@nox.parametrize('install',
['Jinja2==2.9.0', 'Pillow==5.0.0', 'requests==2.9.0', 'xmldiff==2.4'])
@nox.parametrize(
'install',
['Jinja2==2.9.0', 'Pillow==5.0.0', 'requests==2.9.0', 'xmldiff==2.4'])
def compatibility(session, install):
"""Run the unit test suite with each support library and Python version."""

Expand All @@ -64,8 +57,5 @@ def type_check(session):
"""Run type checking using pytype."""
session.install('-e', '.[dev]')
session.install('pytype')
session.run(
'pytype',
'--python-version=3.6',
'--disable=pyi-error',
'pybadges')
session.run('pytype', '--python-version=3.6', '--disable=pyi-error',
'pybadges')
35 changes: 19 additions & 16 deletions pybadges/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Creates a github-style badge as a SVG image.

This package seeks to generate semantically-identical output to the JavaScript
Expand Down Expand Up @@ -44,7 +43,6 @@
from pybadges import precalculated_text_measurer
from pybadges.version import __version__


_JINJA2_ENVIRONMENT = jinja2.Environment(
trim_blocks=True,
lstrip_blocks=True,
Expand Down Expand Up @@ -77,6 +75,7 @@ def _remove_blanks(node):
elif x.nodeType == minidom.Node.ELEMENT_NODE:
_remove_blanks(x)


def _embed_image(url: str) -> str:
parsed_url = urllib.parse.urlparse(url)

Expand All @@ -90,8 +89,8 @@ def _embed_image(url: str) -> str:
raise ValueError('no "Content-Type" header')
content_type, image_type = content_type.split('/')
if content_type != 'image':
raise ValueError('expected an image, got "{0}"'.format(
content_type))
raise ValueError(
'expected an image, got "{0}"'.format(content_type))
image_data = r.content
elif parsed_url.scheme:
raise ValueError('unsupported scheme "{0}"'.format(parsed_url.scheme))
Expand All @@ -113,16 +112,21 @@ def _embed_image(url: str) -> str:
return 'data:image/{};base64,{}'.format(image_type, encoded_image)


def badge(left_text: str, right_text: str, left_link: Optional[str] = None,
right_link: Optional[str] = None,
whole_link: Optional[str] = None, logo: Optional[str] = None,
left_color: str = '#555', right_color: str = '#007ec6',
measurer: Optional[text_measurer.TextMeasurer] = None,
embed_logo: bool = False,
whole_title: Optional[str] = None,
left_title: Optional[str] = None,
right_title: Optional[str] = None,
) -> str:
def badge(
left_text: str,
right_text: str,
left_link: Optional[str] = None,
right_link: Optional[str] = None,
whole_link: Optional[str] = None,
logo: Optional[str] = None,
left_color: str = '#555',
right_color: str = '#007ec6',
measurer: Optional[text_measurer.TextMeasurer] = None,
embed_logo: bool = False,
whole_title: Optional[str] = None,
left_title: Optional[str] = None,
right_title: Optional[str] = None,
) -> str:
"""Creates a github-style badge as an SVG image.

>>> badge(left_text='coverage', right_text='23%', right_color='red')
Expand Down Expand Up @@ -172,8 +176,7 @@ def badge(left_text: str, right_text: str, left_link: Optional[str] = None,
"""
if measurer is None:
measurer = (
precalculated_text_measurer.PrecalculatedTextMeasurer
.default())
precalculated_text_measurer.PrecalculatedTextMeasurer.default())

if (left_link or right_link) and whole_link:
raise ValueError(
Expand Down
64 changes: 31 additions & 33 deletions pybadges/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Output a github-style badge as an SVG image given some text and colors.

For more information, run:
Expand Down Expand Up @@ -40,20 +39,19 @@ def main():
'--right-text',
default='APACHE',
help='the text to show on the right-hand-side of the badge')
parser.add_argument(
'--whole-link',
default=None,
help='the url to redirect to when the badge is clicked')
parser.add_argument('--whole-link',
default=None,
help='the url to redirect to when the badge is clicked')
parser.add_argument(
'--left-link',
default=None,
help='the url to redirect to when the left-hand of the badge is ' +
'clicked')
'clicked')
parser.add_argument(
'--right-link',
default=None,
help='the url to redirect to when the right-hand of the badge is ' +
'clicked')
'clicked')
parser.add_argument(
'--left-color',
default='#555',
Expand All @@ -73,68 +71,68 @@ def main():
const='yes',
default='no',
help='if the logo is specified then include the image data directly in '
'the badge (this will prevent a URL fetch and may work around the '
'fact that some browsers do not fetch external image references); '
'only works if --logo is a HTTP/HTTPS URI or a file path')
parser.add_argument(
'--browser',
action='store_true',
default=False,
help='display the badge in a browser tab')
'the badge (this will prevent a URL fetch and may work around the '
'fact that some browsers do not fetch external image references); '
'only works if --logo is a HTTP/HTTPS URI or a file path')
parser.add_argument('--browser',
action='store_true',
default=False,
help='display the badge in a browser tab')
parser.add_argument(
'--use-pil-text-measurer',
action='store_true',
default=False,
help='use the PilMeasurer to measure the length of text (kerning may '
'be more precise for non-Western languages. ' +
'--deja-vu-sans-path must also be set.')
'be more precise for non-Western languages. ' +
'--deja-vu-sans-path must also be set.')
parser.add_argument(
'--deja-vu-sans-path',
default=None,
help='the path to the ttf font file containing DejaVu Sans. If not ' +
'present on your system, you can download it from ' +
'https://www.fontsquirrel.com/fonts/dejavu-sans')
'present on your system, you can download it from ' +
'https://www.fontsquirrel.com/fonts/dejavu-sans')
parser.add_argument(
'--whole-title',
default=None,
help='the title to associate with the entire badge. See '
'https://developer.mozilla.org/en-US/docs/Web/SVG/Element/title')
'https://developer.mozilla.org/en-US/docs/Web/SVG/Element/title')
parser.add_argument(
'--left-title',
default=None,
help='the title to associate with the left part of the badge. See '
'https://developer.mozilla.org/en-US/docs/Web/SVG/Element/title')
'https://developer.mozilla.org/en-US/docs/Web/SVG/Element/title')
parser.add_argument(
'--right-title',
default=None,
help='the title to associate with the right part of the badge. See '
'https://developer.mozilla.org/en-US/docs/Web/SVG/Element/title')
'https://developer.mozilla.org/en-US/docs/Web/SVG/Element/title')
parser.add_argument(
'-v', '--version',
'-v',
'--version',
action='version',
version='%(prog)s {version}'.format(version=__version__))
args = parser.parse_args()

if (args.left_link or args.right_link) and args.whole_link:
print(
'argument --whole-link: cannot be set with ' +
'--left-link or --right-link',
file=sys.stderr)
print('argument --whole-link: cannot be set with ' +
'--left-link or --right-link',
file=sys.stderr)
sys.exit(1)

measurer = None
if args.use_pil_text_measurer:
if args.deja_vu_sans_path is None:
print(
'argument --use-pil-text-measurer: must also set ' +
'--deja-vu-sans-path',
file=sys.stderr)
print('argument --use-pil-text-measurer: must also set ' +
'--deja-vu-sans-path',
file=sys.stderr)
sys.exit(1)
from pybadges import pil_text_measurer
measurer = pil_text_measurer.PilMeasurer(args.deja_vu_sans_path)

badge = pybadges.badge(left_text=args.left_text, right_text=args.right_text,
left_link=args.left_link, right_link=args.right_link,
badge = pybadges.badge(left_text=args.left_text,
right_text=args.right_text,
left_link=args.left_link,
right_link=args.right_link,
whole_link=args.whole_link,
left_color=args.left_color,
right_color=args.right_color,
Expand Down
1 change: 0 additions & 1 deletion pybadges/pil_text_measurer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Measure the width, in pixels, of a string rendered using DejaVu Sans 110pt.

Uses a PIL/Pillow to determine the string length.
Expand Down
Loading