Commit 84f68594 authored by Thomas Van Parys's avatar Thomas Van Parys
Browse files

Adapt plaza api and launcher to 'year' entry in Plaza (#56)

parent 50ea9045
......@@ -25,7 +25,7 @@ class bcolors:
def markOption(word, pattern):
p = re.compile(pattern)
cp = bcolors.UNDERLINE + bcolors.OKGREEN + pattern + bcolors.ENDC
return p.sub(cp, word)
return p.sub(cp, word, count=1)
@staticmethod
def mark(word):
......@@ -152,13 +152,15 @@ def prompt(species_file):
def findSpecies():
needle = input("Plaza id or part of a species name> ")
hits = plaza.find_species(needle)
hits = plaza.find_species(needle, all=False)
nr = 1
ordered = []
for h in hits:
print(bcolors.markOption("\t{}. {}:\t{}".format(nr, h, hits[h]), str(nr)))
ordered.append(h)
nr+=1
for hit in hits:
for version in hits[hit]:
print(bcolors.markOption("\t{}. {}:\t{}\t({}; {})"
.format(nr, hit, version[0], version[1], version[2]), str(nr)))
ordered.append(hit)
nr += 1
if len(hits) > 0:
selection = input("Add species to list> ")
if selection:
......@@ -166,12 +168,10 @@ def prompt(species_file):
cedalionId = input("Cedalion ID ({})> ".format(selected))
if not cedalionId:
cedalionId = selected
entries.append(SpeciesEntry(cedalionId, longName=hits[selected], plazaId=selected))
entries.append(SpeciesEntry(cedalionId, longName=hits[selected][0][0], plazaId=selected))
else:
print(bcolors.warning("No Plaza entries match your search..."))
print()
def removeSpecies():
speciesNr = int(input("Delete species number> ")) -1
......
......@@ -36,19 +36,31 @@ def get_ftp_url(species, data_type):
return None
def find_species(needle):
def find_species(needle, all=True):
'''
Find species by (part of) a full name or Plaza id.
:param needle: search string matched against Plaza id and species common name.
:returns: dict with search hits {plaza_id: common_name}
:param needle: search string matched against Plaza id and
species common name.
:param all: get all versions. When False, only return latest version
:returns: dict with search hits {plaza_id: hit list}
search hits are tuples: (common_name, version, year)
'''
needle = needle.lower()
j = get_plaza_data()
hits = {}
for entry in j:
if entry['species'].lower() == needle or needle in entry['common_name'].lower():
hits[entry['species']] = entry['common_name']
new_entry = (entry['common_name'], entry['version'], entry['year'])
if entry['species'] not in hits:
hits[entry['species']] = [new_entry]
else:
if all:
hits[entry['species']].append(new_entry)
else:
if int(entry['year']) > int(hits[entry['species']][0][2]):
hits[entry['species']] = [new_entry]
return hits
......@@ -89,8 +101,9 @@ def find(needle):
NEEDLE: a Plaza ID or (part of) a full species name.
'''
hits = find_species(needle)
for h in hits:
print("{}\t{}".format(h, hits[h]))
for hit in hits:
for version in hits[hit]:
print("{}\t{}\t({}; {})".format(hit, version[0], version[1], version[2]))
@cli.command()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment