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: ...@@ -25,7 +25,7 @@ class bcolors:
def markOption(word, pattern): def markOption(word, pattern):
p = re.compile(pattern) p = re.compile(pattern)
cp = bcolors.UNDERLINE + bcolors.OKGREEN + pattern + bcolors.ENDC cp = bcolors.UNDERLINE + bcolors.OKGREEN + pattern + bcolors.ENDC
return p.sub(cp, word) return p.sub(cp, word, count=1)
@staticmethod @staticmethod
def mark(word): def mark(word):
...@@ -152,13 +152,15 @@ def prompt(species_file): ...@@ -152,13 +152,15 @@ def prompt(species_file):
def findSpecies(): def findSpecies():
needle = input("Plaza id or part of a species name> ") needle = input("Plaza id or part of a species name> ")
hits = plaza.find_species(needle) hits = plaza.find_species(needle, all=False)
nr = 1 nr = 1
ordered = [] ordered = []
for h in hits: for hit in hits:
print(bcolors.markOption("\t{}. {}:\t{}".format(nr, h, hits[h]), str(nr))) for version in hits[hit]:
ordered.append(h) print(bcolors.markOption("\t{}. {}:\t{}\t({}; {})"
nr+=1 .format(nr, hit, version[0], version[1], version[2]), str(nr)))
ordered.append(hit)
nr += 1
if len(hits) > 0: if len(hits) > 0:
selection = input("Add species to list> ") selection = input("Add species to list> ")
if selection: if selection:
...@@ -166,12 +168,10 @@ def prompt(species_file): ...@@ -166,12 +168,10 @@ def prompt(species_file):
cedalionId = input("Cedalion ID ({})> ".format(selected)) cedalionId = input("Cedalion ID ({})> ".format(selected))
if not cedalionId: if not cedalionId:
cedalionId = selected cedalionId = selected
entries.append(SpeciesEntry(cedalionId, longName=hits[selected], plazaId=selected)) entries.append(SpeciesEntry(cedalionId, longName=hits[selected][0][0], plazaId=selected))
else: else:
print(bcolors.warning("No Plaza entries match your search...")) print(bcolors.warning("No Plaza entries match your search..."))
print() print()
def removeSpecies(): def removeSpecies():
speciesNr = int(input("Delete species number> ")) -1 speciesNr = int(input("Delete species number> ")) -1
......
...@@ -36,19 +36,31 @@ def get_ftp_url(species, data_type): ...@@ -36,19 +36,31 @@ def get_ftp_url(species, data_type):
return None return None
def find_species(needle): def find_species(needle, all=True):
''' '''
Find species by (part of) a full name or Plaza id. Find species by (part of) a full name or Plaza id.
:param needle: search string matched against Plaza id and species common name. :param needle: search string matched against Plaza id and
:returns: dict with search hits {plaza_id: common_name} 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() needle = needle.lower()
j = get_plaza_data() j = get_plaza_data()
hits = {} hits = {}
for entry in j: for entry in j:
if entry['species'].lower() == needle or needle in entry['common_name'].lower(): 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 return hits
...@@ -89,8 +101,9 @@ def find(needle): ...@@ -89,8 +101,9 @@ def find(needle):
NEEDLE: a Plaza ID or (part of) a full species name. NEEDLE: a Plaza ID or (part of) a full species name.
''' '''
hits = find_species(needle) hits = find_species(needle)
for h in hits: for hit in hits:
print("{}\t{}".format(h, hits[h])) for version in hits[hit]:
print("{}\t{}\t({}; {})".format(hit, version[0], version[1], version[2]))
@cli.command() @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