WPTaxoListBuilder python script, as of 2020-02-28.
Given a json file, attempts to build wikitables for a family of species (subfamily and tribes/clades optional)
Only tested with a few animal families in Carnivora; bugs to be expected and bug reports welcome.
Not packaged for easy reuse; needs python3 to run and expects your system to have Pydash module installed.
USAGE: py WPTaxoListBuilder.py <file.json>
import codecs
import json
import sys
from pydash import _
###############
#
# WP_TAXO_LIST_BUILDER - builds wiki templates for a animal family list page
# from a source json file
#
# USAGE: py WPTaxoListBuilder.py <file.json>
#
###############
def main():
input_file = sys.argv[1]
with codecs.open(input_file, 'r', encoding='utf-8') as taxo_file:
taxo = json.load(taxo_file)
for subfamily in taxo:
print_subfamily_header(subfamily)
for lineage in subfamily['Lineages']:
if 'Name' in lineage:
print_lineage_header(lineage)
for genus in lineage['Genuses']:
print_genus_table_head(genus)
for species in genus['Species']:
print_species_row(species)
print_genus_table_close()
### PRINTERS ###
def print_subfamily_header(subfamily):
print()
print('===Subfamily ' + subfamily['Name'] + '===')
print('
')
def print_lineage_header(lineage):
type = (lineage['Type'] + ' ') if 'Type' in lineage else
print ()
if type == 'lineage ':
print('====' + lineage['Name'] + ' lineage====')
else:
print('====' + type + lineage['Name'] + '====')
def print_genus_table_head(genus):
english_count = english_num(get_count(genus,'Species'))
genus_title = wikilink(genus['Name'])
if 'Extinct' in genus:
genus_title += '†'
authority_name = wikilink(get(genus, 'Namer'))
print('
Genus ' + genus_title + ' – ' + authority_name + ', ' + get(genus, 'Founded') + ' – ' + english_count + ' species
Common name
Scientific name and subspecies
Range
Size and ecology
IUCN status and estimated population')
def print_species_row(species):
species_title = wikilink(species['Name'])
if 'Extinct' in species:
species_title += '†'
authority_name = wikilink(get(species, 'Namer'))
iucn_ref = '[1]'
if get(species, 'IUCNCat') == 'NE':
iucn_ref =
imageSize = species['ImageSizeOverride'] if 'ImageSizeOverride' in species else '180'
rangeSize = species['RangeSizeOverride'] if 'RangeSizeOverride' in species else '180'
population = species['Population'] if get(species, 'Population') != else 'Unknown'
direction_map = {
'up': '',
'down': '',
'stable': '',
'unknown': '',
:
}
print('{{Species table/row')
print('|name=' + species_title + ' |binomial=' + species['Latin'])
print('|image=' + get(species, 'Image') + ' |image-size=' + imageSize + 'px |image-alt=' + get(species, 'ImageAlt'))
print('|authority-name=' + authority_name + ' |authority-year=' + get(species, 'Founded'))
if get_count(species, 'Subspecies') > 0:
print_subspecies(species['Subspecies'])
print('|range=' + get(species, 'RangeAlt') + ' |range-image=' + get(species, 'Range') + ' |range-image-size=' + rangeSize + 'px')
print('|size=' + get(species, 'Size'))
print('|habitat=' + get(species, 'Habitat') + iucn_ref)
print('|hunting=' + get(species, 'Hunting') + iucn_ref)
print('|iucn-status=' + get(species, 'IUCNCat') + ' |population=' + population)
print('|direction=' + direction_map[get(species, 'PopDirection')] + iucn_ref)
print('}}')
def print_subspecies(subspecies):
expand = 'yes' if len(subspecies) <= 6 else
print('|subspecies=')
def print_genus_table_close():
print('
')
### UTIL ###
def get(d, k):
return d[k] if k in d else
def get_count(d, k):
return len(d[k]) if k in d else 0
def english_num(count):
words = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50']
if count < len(words):
return words[count]
else:
return 'many'
def wikilink(link):
return '' + link + ''
if __name__ == '__main__':
main()