diff --git a/pytaxonomies/__init__.py b/pytaxonomies/__init__.py index 9ee35ce..0a57f42 100644 --- a/pytaxonomies/__init__.py +++ b/pytaxonomies/__init__.py @@ -1 +1 @@ -from .api import Taxonomies +from .api import Taxonomies, EncodeTaxonomies diff --git a/pytaxonomies/api.py b/pytaxonomies/api.py index 95a9afb..aa8d7e8 100644 --- a/pytaxonomies/api.py +++ b/pytaxonomies/api.py @@ -5,6 +5,7 @@ import json import os import collections import re +from json import JSONEncoder try: import requests @@ -13,15 +14,22 @@ except ImportError: HAS_REQUESTS = False +class EncodeTaxonomies(JSONEncoder): + def default(self, obj): + try: + return obj._json() + except AttributeError: + return JSONEncoder.default(self, obj) + + class Entry(): - def __init__(self, value, expanded, colour, description): + def __init__(self, value, expanded, colour, description, numerical_value): self.value = value self.expanded = expanded self.colour = colour - self.description = None - if description: - self.description = description + self.description = description + self.numerical_value = numerical_value def __str__(self): return self.value @@ -29,20 +37,19 @@ class Entry(): class Predicate(collections.Mapping): - def __init__(self, predicate, description, colour, entries): + def __init__(self, predicate, expanded, description, colour, entries): self.predicate = predicate - self.description = None + self.expanded = expanded + self.description = description self.colour = colour - if description: - self.description = description - self.entries = {} - if entries: - self.__init_entries(entries) + self.__init_entries(entries) def __init_entries(self, entries): - for e in entries: - self.entries[e['value']] = Entry(e['value'], e['expanded'], - e.get('colour'), e.get('description')) + self.entries = {} + if entries: + for e in entries: + self.entries[e['value']] = Entry(e['value'], e['expanded'], e.get('colour'), + e.get('description'), e.get('numerical_value')) def __str__(self): return self.predicate @@ -77,9 +84,52 @@ class Taxonomy(collections.Mapping): entries[v['predicate']] = [] entries[v['predicate']] += v['entry'] for p in self.taxonomy['predicates']: - self.predicates[p['value']] = Predicate(p['value'], p.get('expanded'), + self.predicates[p['value']] = Predicate(p['value'], p.get('expanded'), p.get('description'), p.get('colour'), entries.get(p['value'])) + def _json_predicates(self): + predicates_to_return = [] + values_to_return = [] + for predicate in self.predicates.values(): + temp_predicate = {'value': predicate.predicate} + if predicate.expanded: + temp_predicate['expanded'] = predicate.expanded + if predicate.description: + temp_predicate['description'] = predicate.description + if predicate.colour: + temp_predicate['colour'] = predicate.colour + predicates_to_return.append(temp_predicate) + + if predicate.entries: + temp_entries = {'entry': [], 'predicate': predicate.predicate} + for entry in predicate.entries.values(): + temp_entry = {'value': entry.value} + if entry.expanded: + temp_entry['expanded'] = entry.expanded + if entry.numerical_value: + temp_entry['numerical_value'] = entry.numerical_value + if entry.colour: + temp_entry['colour'] = entry.colour + if entry.description: + temp_entry['description'] = entry.description + temp_entries['entry'].append(temp_entry) + values_to_return.append(temp_entries) + + return predicates_to_return, values_to_return + + def _json(self): + to_return = {'namespace': self.name, 'description': self.description, 'version': self.version} + if self.expanded: + to_return['expanded'] = self.expanded + if self.refs: + to_return['refs'] = self.refs + p, v = self._json_predicates() + if p: + to_return['predicates'] = p + if v: + to_return['values'] = v + return to_return + def has_entries(self): if self.predicates.values() and list(self.predicates.values())[0].entries: return True diff --git a/tests/tests.py b/tests/tests.py index f628be0..f5fb961 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -1,8 +1,9 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +import json import unittest -from pytaxonomies import Taxonomies +from pytaxonomies import Taxonomies, EncodeTaxonomies import pytaxonomies.api @@ -35,12 +36,13 @@ class TestPyTaxonomies(unittest.TestCase): self.taxonomies.search('phish', expanded=True) def test_print_classes(self): - tax = list(self.taxonomies.values())[0] - print(tax) - pred = list(tax.values())[0] - print(pred) - entry = list(pred.values())[0] - print(entry) + for t in self.taxonomies.values(): + if not t.has_entries(): + continue + tax = list(t.values())[0] + print(tax) + pred = list(tax.values())[0] + print(pred) def test_amountEntries(self): list(self.taxonomies.values())[0].amount_entries() @@ -58,5 +60,10 @@ class TestPyTaxonomies(unittest.TestCase): mt = tax.make_machinetag(p) self.taxonomies.revert_machinetag(mt) + def test_json(self): + for t in self.taxonomies: + json.dumps(t, cls=EncodeTaxonomies) + + if __name__ == "__main__": unittest.main() diff --git a/website/__init__.py b/website/__init__.py index fba75d4..bdf190d 100644 --- a/website/__init__.py +++ b/website/__init__.py @@ -6,7 +6,7 @@ from flask_bootstrap import Bootstrap from flask_nav import Nav from flask_nav.elements import Navbar, View from flask_wtf import FlaskForm -from wtforms import StringField +from wtforms import StringField, SubmitField from wtforms.validators import DataRequired from pytaxonomies import Taxonomies @@ -35,6 +35,7 @@ t = Taxonomies() class SearchForm(FlaskForm): query = StringField('Query', validators=[DataRequired()]) + submit = SubmitField('Search') @app.route('/', methods=['GET']) diff --git a/website/templates/search.html b/website/templates/search.html index dbebd2d..347c9cc 100644 --- a/website/templates/search.html +++ b/website/templates/search.html @@ -8,7 +8,7 @@
{{ form.csrf_token }} {{ form.query(size=20) }} - + {{ form.submit }}

diff --git a/website/templates/taxonomy.html b/website/templates/taxonomy.html index 649eebd..047306f 100644 --- a/website/templates/taxonomy.html +++ b/website/templates/taxonomy.html @@ -19,6 +19,7 @@ Description + Expanded Predicate {% if taxonomy.has_entries() %} Values @@ -31,6 +32,7 @@ Description + Expanded Predicate {% if taxonomy.has_entries() %} Values @@ -44,6 +46,7 @@ {% for p in taxonomy.predicates.values() %} {% if p.description %}{{ p.description }}{% endif %} + {% if p.expanded %}{{ p.expanded }}{% endif %} {{ p.predicate }} {% if p.entries %} @@ -51,6 +54,7 @@ Value + Numerical Value Expanded Description Colour @@ -61,6 +65,7 @@ {% for e in p.entries.values() %} {{ e.value }} + {% if e.numerical_value %}{{ e.numerical_value }}{% endif %} {% if e.expanded %}{{ e.expanded }}{% endif %} {% if e.description %}{{ e.description }}{% endif %} {% if p.colour %}
{% endif %}