Add forgotten fields, add json dump

pull/2/head
Raphaël Vinot 2016-10-13 17:23:10 +02:00
parent aec809fac8
commit 734927c3d2
6 changed files with 88 additions and 25 deletions

View File

@ -1 +1 @@
from .api import Taxonomies
from .api import Taxonomies, EncodeTaxonomies

View File

@ -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

View File

@ -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()

View File

@ -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'])

View File

@ -8,7 +8,7 @@
<form class="form-inline" action="search" method=post>
{{ form.csrf_token }}
{{ form.query(size=20) }}
<input type="submit" value="Search">
{{ form.submit }}
</form>
</br>
<div>

View File

@ -19,6 +19,7 @@
<thead>
<tr>
<th>Description</th>
<th>Expanded</th>
<th>Predicate</th>
{% if taxonomy.has_entries() %}
<th>Values</th>
@ -31,6 +32,7 @@
<tfoot>
<tr>
<th>Description</th>
<th>Expanded</th>
<th>Predicate</th>
{% if taxonomy.has_entries() %}
<th>Values</th>
@ -44,6 +46,7 @@
{% for p in taxonomy.predicates.values() %}
<tr>
<td>{% if p.description %}{{ p.description }}{% endif %}</td>
<td>{% if p.expanded %}{{ p.expanded }}{% endif %}</td>
<td>{{ p.predicate }}</td>
{% if p.entries %}
<td>
@ -51,6 +54,7 @@
<thead>
<tr>
<th>Value</th>
<th>Numerical Value</th>
<th>Expanded</th>
<th>Description</th>
<th>Colour</th>
@ -61,6 +65,7 @@
{% for e in p.entries.values() %}
<tr>
<td>{{ e.value }}</td>
<td>{% if e.numerical_value %}{{ e.numerical_value }}{% endif %}</td>
<td>{% if e.expanded %}{{ e.expanded }}{% endif %}</td>
<td>{% if e.description %}{{ e.description }}{% endif %}</td>
<td>{% if p.colour %}<div class='colorbox' background={{ p.colour }}></div>{% endif %}</td>