Skip to content
Open
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
4 changes: 2 additions & 2 deletions selfcites/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PHONY: test
.PHONY: test

test: sample_completed.bib

sample_completed.bib: selfcites.py sample.bib source.bib
@python3 selfcites.py sample.bib --source source.bib --suffix _completed
@python3 selfcites.py sample.bib --source source.bib --suffix _completed
2 changes: 1 addition & 1 deletion selfcites/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ the present working one, you should specify their paths, e.g.:
python3 ../resources/scripts/selfcites.py myarticle/mybibliography.bib
```

### Cecking several BibTeX files
### Checking several BibTeX files

You can specify several files, or all BibTeX files at a given location:

Expand Down
7 changes: 7 additions & 0 deletions selfcites/sample.bib
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
% Encoding: UTF-8

@online{johnson:2017,
title = {Compositionality},
author = {Johnson, Michael},
date = {2017-12-14},
url = {http://www.iep.utm.edu/composit/},
urldate = {2017-12-14}
}

@book{vanbenthem:1986,
address = {Dordrecht},
Expand Down
13 changes: 12 additions & 1 deletion selfcites/sample_completed.bib
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
@comment{% Encoding: UTF-8}

@comment{jabref-meta: databaseType:bibtex;}

@book{aloni-etal:2010,
address = {Berlin},
author = {Aloni, Maria and Bastiaanse, Harald and
Expand Down Expand Up @@ -217,6 +221,14 @@ @article{jaskowski:1934
year = {1934}
}

@online{johnson:2017,
author = {Johnson, Michael},
date = {2017-12-14},
title = {Compositionality},
url = {http://www.iep.utm.edu/composit/},
urldate = {2017-12-14}
}

@book{klemke_ed:1970,
address = {Urbana, Illinois},
booktitle = {Essays on Russell},
Expand Down Expand Up @@ -563,4 +575,3 @@ @book{vonheusinger-etal:2011
Language Meaning. Volume 2},
year = {2011}
}

65 changes: 38 additions & 27 deletions selfcites/selfcites.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
Dialectica open access initiative self-citing bibliography file check, (c) Thomas Hodgson 2021
MIT License

The script takes paths as arguments. The assumption is that these are biblatex files.
It looks for biblatex cite commands, and compares these to the biblatex entries in the files.
The script takes paths as arguments. The assumption is that these are bibtex files.
It looks for bibtex cite commands, and compares these to the bibtex entries in the files.
The following will be printed, unless the --quiet option is set:

- the keys used in citation commands (if any)
- the missing keys that are not entries (if any)
- the entries in the file

If the option --source argument is given, the script tries to use bibtexparser to make an extended
biblatex file with missing entries added from the argument to --source.
bibtex file with missing entries added from the argument to --source.
The new file will have the value of the optional argument --suffix added to the name of the original
file.
The default value of --suffix is '_extended'.
Expand Down Expand Up @@ -45,44 +45,51 @@
)
group = parser.add_mutually_exclusive_group()
group.add_argument(
"--verbose", "-v",
"--verbose",
"-v",
help="Print information about what the script does. Double for full verbosity.",
action="count",
default=1,
dest="verbose"
dest="verbose",
)
group.add_argument(
"--quiet", "-q",
"--quiet",
"-q",
help="Do not print information about what the script does",
action="store_const",
const=0,
dest="verbose"
dest="verbose",
)
parser.add_argument(
"bibliographies",
help="The bibliography files to check",
nargs="*",
)
parser.add_argument(
"--source", "-s",
"--source",
"-s",
help="A bibliography file to get missing entries from",
)
parser.add_argument(
"--suffix", "-x",
"--suffix",
"-x",
help="Add this suffix to new bibliography files",
default="_extended",
)
parser.add_argument(
"--directory", "-d",
help="Look in this directory for biblatex files",
"--directory",
"-d",
help="Look in this directory for bibtex files",
)
parser.add_argument(
"--extension", "-e",
"--extension",
"-e",
help="The extension for bibliography files",
default="bib",
)
parser.add_argument(
"--recursive", "-r",
"--recursive",
"-r",
help="Look recursively in subdirectories of the directory specified by --directory",
action="store_true",
)
Expand All @@ -91,8 +98,14 @@
# if a --source option is provided, we need bibtexparser
if args.source:
import bibtexparser
from bibtexparser.bparser import BibTexParser
from bibtexparser.bwriter import BibTexWriter
from bibtexparser.bibdatabase import BibDatabase

source_parser = BibTexParser(ignore_nonstandard_types=False)

# Use bibtexparser to get a database from the file
with open(args.source, "r") as in_file:
source_database = bibtexparser.load(in_file, source_parser)

# Print script information message
if args.verbose >= 1:
Expand All @@ -102,7 +115,7 @@
sep="\n",
)

# Make a list of biblatex files to run on
# Make a list of bibtex files to run on
# Take the positional arguments
bibliographies = args.bibliographies
# Add what is in the specified directory
Expand Down Expand Up @@ -195,20 +208,16 @@

# Try to get the missing entries from the source bibliography
if args.source and missing:
# Use bibtexparser to get a dictionary from the file
with open(args.source, "r") as in_file:
source_database = bibtexparser.load(in_file)
# Use bibtexparser to get a dictionary from the file
# Use bibtexparser to get a database from the file
with open(current_file, "r") as in_file:
current_database = bibtexparser.load(in_file)
current_parser = BibTexParser(ignore_nonstandard_types=False)
current_database = bibtexparser.load(in_file, current_parser)
# A list of the missing entries, represented as dictionaries
missing_entries = [
entry for entry in source_database.entries if entry["ID"] in missing
]
# Create a new database, and add the entries from the current database
# as well as the missing entries
extended_database = BibDatabase()
extended_database.entries = current_database.entries + missing_entries
# Add the missing entries to the current database
current_database.entries += missing_entries

# Write to a file
writer = BibTexWriter()
Expand All @@ -217,15 +226,17 @@
root, ext = os.path.splitext(tail)
new_file = os.path.join(head, root + args.suffix + ext)
with open(new_file, "w") as out_file:
out_file.write(writer.write(extended_database))
out_file.write(writer.write(current_database))
if args.verbose >= 1:
# Print a message about what was written
print(
"I looked for any missing entries in the source bibliography: '{}'.".format(args.source),
"I looked for any missing entries in the source bibliography: '{}'.".format(
args.source
),
"I wrote an extended bibliography file: '{}'.".format(new_file),
sep="\n",
)

except FileNotFoundError:
if args.verbose >=1:
if args.verbose >= 1:
print("I couldn't find {}.".format(current_file))
8 changes: 8 additions & 0 deletions selfcites/source.bib
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
% Encoding: UTF-8

@online{johnson:2017,
title = {Compositionality},
author = {Johnson, Michael},
date = {2017-12-14},
url = {http://www.iep.utm.edu/composit/},
urldate = {2017-12-14}
}

@book{geach_pt:1980,
address = {Ithaca, New York},
author = {Geach, Peter Thomas},
Expand Down