Skip to content

Commit d3ba9d2

Browse files
committed
Implement support for "BibTeX" file cleanup.
1 parent 7051083 commit d3ba9d2

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

BIBLIOGRAPHY.bib

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@misc{Abecassis2011,
2-
author = {Abecassis, Felix},
3-
title = {{OpenCV - Rotation (Deskewing)}},
4-
url = {http://felix.abecassis.me/2011/10/opencv-rotation-deskewing/},
5-
urldate = {2018-10-27},
6-
year = {2011}
2+
author = {Abecassis, Felix},
3+
title = {{OpenCV - Rotation (Deskewing)}},
4+
url = {http://felix.abecassis.me/2011/10/opencv-rotation-deskewing/},
5+
urldate = {2018-10-27},
6+
year = 2011,
77
}

setup.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import os
1111
import re
12-
12+
import sys
1313
from setuptools import setup
1414
from setuptools import find_packages
1515

@@ -45,6 +45,11 @@
4545
DEVELOPMENT_REQUIREMENTS = DOCS_REQUIREMENTS + TESTS_REQUIREMENTS + [
4646
'invoke', 'restructuredtext_lint', 'twine', 'yapf'
4747
]
48+
if sys.version_info[:2] >= (3, 2):
49+
DEVELOPMENT_REQUIREMENTS += [
50+
'biblib @ git+ssh://git@github.comcolour-science/biblib'
51+
'@v0.1.0#egg=biblib'
52+
]
4853

4954

5055
def get_version():

tasks.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
from __future__ import unicode_literals
88

9+
import sys
10+
if sys.version_info[:2] >= (3, 2):
11+
import biblib.bib
912
import fnmatch
1013
import os
1114
import re
@@ -22,9 +25,10 @@
2225
__status__ = 'Production'
2326

2427
__all__ = [
25-
'APPLICATION_NAME', 'PYTHON_PACKAGE_NAME', 'PYPI_PACKAGE_NAME', 'clean',
26-
'formatting', 'tests', 'quality', 'examples', 'docs', 'todo', 'preflight',
27-
'build', 'virtualise', 'tag', 'release', 'sha256'
28+
'APPLICATION_NAME', 'PYTHON_PACKAGE_NAME', 'PYPI_PACKAGE_NAME',
29+
'BIBLIOGRAPHY_NAME', 'clean', 'formatting', 'tests', 'quality', 'examples',
30+
'docs', 'todo', 'preflight', 'build', 'virtualise', 'tag', 'release',
31+
'sha256'
2832
]
2933

3034
APPLICATION_NAME = colour_checker_detection.__application_name__
@@ -33,6 +37,8 @@
3337

3438
PYPI_PACKAGE_NAME = 'colour-checker-detection'
3539

40+
BIBLIOGRAPHY_NAME = 'BIBLIOGRAPHY.bib'
41+
3642

3743
@task
3844
def clean(ctx, docs=True, bytecode=False):
@@ -69,7 +75,7 @@ def clean(ctx, docs=True, bytecode=False):
6975

7076

7177
@task
72-
def formatting(ctx, yapf=False, asciify=True):
78+
def formatting(ctx, yapf=False, asciify=True, bibtex=True):
7379
"""
7480
Formats the codebase with *Yapf* and converts unicode characters to ASCII.
7581
@@ -81,6 +87,8 @@ def formatting(ctx, yapf=False, asciify=True):
8187
Whether to format the codebase with *Yapf*.
8288
asciify : bool, optional
8389
Whether to convert unicode characters to ASCII.
90+
bibtex : bool, optional
91+
Whether to cleanup the *BibTeX* file.
8492
8593
Returns
8694
-------
@@ -97,6 +105,26 @@ def formatting(ctx, yapf=False, asciify=True):
97105
with ctx.cd('utilities'):
98106
ctx.run('./unicode_to_ascii.py')
99107

108+
if bibtex and sys.version_info[:2] >= (3, 2):
109+
message_box('Cleaning up "BibTeX" file...')
110+
bibtex_path = BIBLIOGRAPHY_NAME
111+
with open(bibtex_path) as bibtex_file:
112+
bibtex = biblib.bib.Parser().parse(
113+
bibtex_file.read()).get_entries()
114+
115+
for entry in sorted(bibtex.values(), key=lambda x: x.key):
116+
try:
117+
del entry['file']
118+
except KeyError:
119+
pass
120+
for key, value in entry.items():
121+
entry[key] = re.sub('(?<!\\\\)\\&', '\\&', value)
122+
123+
with open(bibtex_path, 'w') as bibtex_file:
124+
for entry in bibtex.values():
125+
bibtex_file.write(entry.to_bib())
126+
bibtex_file.write('\n')
127+
100128

101129
@task
102130
def tests(ctx, nose=True):

0 commit comments

Comments
 (0)