Skip to content

Commit

Permalink
docs: different semi-dynamic take on completion badge (fixes #1)
Browse files Browse the repository at this point in the history
  • Loading branch information
swistakm committed Nov 3, 2016
1 parent 69dc8fb commit 874830c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
1 change: 0 additions & 1 deletion .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ build: off

build_script:
- "ci\\appveyor_build.cmd %PYTHON%/python.exe -m pip wheel . -w dist"
- python.exe ci/completion.py imgui/cimgui.pxd dist/completion.svg

on_success:
- if [%APPVEYOR_REPO_BRANCH%]==[master] (
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ clean:
.PHONY: build
build: bootstrap
python setup.py develop
python ci/completion.py imgui/cimgui.pxd README.md


.PHONY: rebuild
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
| ------------- | -------------- |
| [![Build status](https://ci.appveyor.com/api/projects/status/s7pud6on7dww89iv?svg=true)](https://ci.appveyor.com/project/swistakm/pyimgui) | [![Build Status](https://travis-ci.org/swistakm/pyimgui.svg?branch=master)](https://travis-ci.org/swistakm/pyimgui)

[![completion](https://img.shields.io/badge/completion-46%25%20%28144%20of%20307%29-blue.svg)]

# pyimgui,

Expand Down
48 changes: 31 additions & 17 deletions ci/completion.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
# -*- coding: utf-8 -*-
from inspect import cleandoc
import sys
import re
import fileinput

try:
from urllib2 import urlopen, Request
from urllib import quote
from urllib2 import HTTPError
except ImportError:
from urllib.parse import quote
from urllib.request import urlopen, Request
from urllib.error import HTTPError


BASE_URL = 'https://img.shields.io/badge/completion-%s-blue.svg'
BADGE_TEMPLATE = "[![completion](%s)]"
ALL_RE = re.compile(r'(?!(^\s*$)|(^\s*#))')
DONE_RE = re.compile(r'(?!(^\s*$)|(^\s*#)).*✓')
BADGE_RE = re.compile(r'\[!\[completion\]\(.*\)\]\s*$')


if __name__ == "__main__":
Expand All @@ -24,26 +23,41 @@
pxd_file_name, out_file_name = sys.argv[1:]
else:
pxd_file_name, out_file_name = None, None
exit("Usage: python %s PXD_FILE [OUT_FILE]" % __file__)
exit(cleandoc(
"""Usage: python %s PXD_FILE [README]
Estimate completion status and print result.
Note: if README argument is provided it will
try to update it with completion badge looking
for existing markdown badge markup.
""" % __file__
))

with open(pxd_file_name) as pyx_file:
lines = pyx_file.readlines()

all_count = len(list(filter(ALL_RE.match, lines)))
done_count = len(list(filter(DONE_RE.match, lines)))
result = "%d %% (%s of %s)" % (
result = "%d%% (%s of %s)" % (
float(done_count)/all_count * 100,
done_count, all_count
)

badge_url = BASE_URL % quote(result)
badge_md = BADGE_TEMPLATE % badge_url

if out_file_name:
with open(out_file_name, 'w') as out_file:
request = Request(
BASE_URL % quote(result),
# note: Shields.io does not allow Python-urllib/* user agent
headers={"User-Agent": "Spoofed"}
)
response = urlopen(request)
out_file.write(response.read())

print(result)
output = fileinput.input(files=(out_file_name,), inplace=True)
try:
for line in output:
if BADGE_RE.match(line):
sys.stdout.write(badge_md + "\n")
else:
sys.stdout.write(line)

finally:
fileinput.close()

print("Estimated: %s" % result)
print("Badge: %s" % badge_md)

0 comments on commit 874830c

Please sign in to comment.