mirror of https://github.com/MISP/PyTaxonomies
Add forgotten fields, add json dump
parent
aec809fac8
commit
734927c3d2
|
@ -1 +1 @@
|
||||||
from .api import Taxonomies
|
from .api import Taxonomies, EncodeTaxonomies
|
||||||
|
|
|
@ -5,6 +5,7 @@ import json
|
||||||
import os
|
import os
|
||||||
import collections
|
import collections
|
||||||
import re
|
import re
|
||||||
|
from json import JSONEncoder
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import requests
|
import requests
|
||||||
|
@ -13,15 +14,22 @@ except ImportError:
|
||||||
HAS_REQUESTS = False
|
HAS_REQUESTS = False
|
||||||
|
|
||||||
|
|
||||||
|
class EncodeTaxonomies(JSONEncoder):
|
||||||
|
def default(self, obj):
|
||||||
|
try:
|
||||||
|
return obj._json()
|
||||||
|
except AttributeError:
|
||||||
|
return JSONEncoder.default(self, obj)
|
||||||
|
|
||||||
|
|
||||||
class Entry():
|
class Entry():
|
||||||
|
|
||||||
def __init__(self, value, expanded, colour, description):
|
def __init__(self, value, expanded, colour, description, numerical_value):
|
||||||
self.value = value
|
self.value = value
|
||||||
self.expanded = expanded
|
self.expanded = expanded
|
||||||
self.colour = colour
|
self.colour = colour
|
||||||
self.description = None
|
self.description = description
|
||||||
if description:
|
self.numerical_value = numerical_value
|
||||||
self.description = description
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.value
|
return self.value
|
||||||
|
@ -29,20 +37,19 @@ class Entry():
|
||||||
|
|
||||||
class Predicate(collections.Mapping):
|
class Predicate(collections.Mapping):
|
||||||
|
|
||||||
def __init__(self, predicate, description, colour, entries):
|
def __init__(self, predicate, expanded, description, colour, entries):
|
||||||
self.predicate = predicate
|
self.predicate = predicate
|
||||||
self.description = None
|
self.expanded = expanded
|
||||||
|
self.description = description
|
||||||
self.colour = colour
|
self.colour = colour
|
||||||
if description:
|
self.__init_entries(entries)
|
||||||
self.description = description
|
|
||||||
self.entries = {}
|
|
||||||
if entries:
|
|
||||||
self.__init_entries(entries)
|
|
||||||
|
|
||||||
def __init_entries(self, entries):
|
def __init_entries(self, entries):
|
||||||
for e in entries:
|
self.entries = {}
|
||||||
self.entries[e['value']] = Entry(e['value'], e['expanded'],
|
if entries:
|
||||||
e.get('colour'), e.get('description'))
|
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):
|
def __str__(self):
|
||||||
return self.predicate
|
return self.predicate
|
||||||
|
@ -77,9 +84,52 @@ class Taxonomy(collections.Mapping):
|
||||||
entries[v['predicate']] = []
|
entries[v['predicate']] = []
|
||||||
entries[v['predicate']] += v['entry']
|
entries[v['predicate']] += v['entry']
|
||||||
for p in self.taxonomy['predicates']:
|
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']))
|
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):
|
def has_entries(self):
|
||||||
if self.predicates.values() and list(self.predicates.values())[0].entries:
|
if self.predicates.values() and list(self.predicates.values())[0].entries:
|
||||||
return True
|
return True
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
import json
|
||||||
import unittest
|
import unittest
|
||||||
from pytaxonomies import Taxonomies
|
from pytaxonomies import Taxonomies, EncodeTaxonomies
|
||||||
import pytaxonomies.api
|
import pytaxonomies.api
|
||||||
|
|
||||||
|
|
||||||
|
@ -35,12 +36,13 @@ class TestPyTaxonomies(unittest.TestCase):
|
||||||
self.taxonomies.search('phish', expanded=True)
|
self.taxonomies.search('phish', expanded=True)
|
||||||
|
|
||||||
def test_print_classes(self):
|
def test_print_classes(self):
|
||||||
tax = list(self.taxonomies.values())[0]
|
for t in self.taxonomies.values():
|
||||||
print(tax)
|
if not t.has_entries():
|
||||||
pred = list(tax.values())[0]
|
continue
|
||||||
print(pred)
|
tax = list(t.values())[0]
|
||||||
entry = list(pred.values())[0]
|
print(tax)
|
||||||
print(entry)
|
pred = list(tax.values())[0]
|
||||||
|
print(pred)
|
||||||
|
|
||||||
def test_amountEntries(self):
|
def test_amountEntries(self):
|
||||||
list(self.taxonomies.values())[0].amount_entries()
|
list(self.taxonomies.values())[0].amount_entries()
|
||||||
|
@ -58,5 +60,10 @@ class TestPyTaxonomies(unittest.TestCase):
|
||||||
mt = tax.make_machinetag(p)
|
mt = tax.make_machinetag(p)
|
||||||
self.taxonomies.revert_machinetag(mt)
|
self.taxonomies.revert_machinetag(mt)
|
||||||
|
|
||||||
|
def test_json(self):
|
||||||
|
for t in self.taxonomies:
|
||||||
|
json.dumps(t, cls=EncodeTaxonomies)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
@ -6,7 +6,7 @@ from flask_bootstrap import Bootstrap
|
||||||
from flask_nav import Nav
|
from flask_nav import Nav
|
||||||
from flask_nav.elements import Navbar, View
|
from flask_nav.elements import Navbar, View
|
||||||
from flask_wtf import FlaskForm
|
from flask_wtf import FlaskForm
|
||||||
from wtforms import StringField
|
from wtforms import StringField, SubmitField
|
||||||
from wtforms.validators import DataRequired
|
from wtforms.validators import DataRequired
|
||||||
from pytaxonomies import Taxonomies
|
from pytaxonomies import Taxonomies
|
||||||
|
|
||||||
|
@ -35,6 +35,7 @@ t = Taxonomies()
|
||||||
|
|
||||||
class SearchForm(FlaskForm):
|
class SearchForm(FlaskForm):
|
||||||
query = StringField('Query', validators=[DataRequired()])
|
query = StringField('Query', validators=[DataRequired()])
|
||||||
|
submit = SubmitField('Search')
|
||||||
|
|
||||||
|
|
||||||
@app.route('/', methods=['GET'])
|
@app.route('/', methods=['GET'])
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
<form class="form-inline" action="search" method=post>
|
<form class="form-inline" action="search" method=post>
|
||||||
{{ form.csrf_token }}
|
{{ form.csrf_token }}
|
||||||
{{ form.query(size=20) }}
|
{{ form.query(size=20) }}
|
||||||
<input type="submit" value="Search">
|
{{ form.submit }}
|
||||||
</form>
|
</form>
|
||||||
</br>
|
</br>
|
||||||
<div>
|
<div>
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Description</th>
|
<th>Description</th>
|
||||||
|
<th>Expanded</th>
|
||||||
<th>Predicate</th>
|
<th>Predicate</th>
|
||||||
{% if taxonomy.has_entries() %}
|
{% if taxonomy.has_entries() %}
|
||||||
<th>Values</th>
|
<th>Values</th>
|
||||||
|
@ -31,6 +32,7 @@
|
||||||
<tfoot>
|
<tfoot>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Description</th>
|
<th>Description</th>
|
||||||
|
<th>Expanded</th>
|
||||||
<th>Predicate</th>
|
<th>Predicate</th>
|
||||||
{% if taxonomy.has_entries() %}
|
{% if taxonomy.has_entries() %}
|
||||||
<th>Values</th>
|
<th>Values</th>
|
||||||
|
@ -44,6 +46,7 @@
|
||||||
{% for p in taxonomy.predicates.values() %}
|
{% for p in taxonomy.predicates.values() %}
|
||||||
<tr>
|
<tr>
|
||||||
<td>{% if p.description %}{{ p.description }}{% endif %}</td>
|
<td>{% if p.description %}{{ p.description }}{% endif %}</td>
|
||||||
|
<td>{% if p.expanded %}{{ p.expanded }}{% endif %}</td>
|
||||||
<td>{{ p.predicate }}</td>
|
<td>{{ p.predicate }}</td>
|
||||||
{% if p.entries %}
|
{% if p.entries %}
|
||||||
<td>
|
<td>
|
||||||
|
@ -51,6 +54,7 @@
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Value</th>
|
<th>Value</th>
|
||||||
|
<th>Numerical Value</th>
|
||||||
<th>Expanded</th>
|
<th>Expanded</th>
|
||||||
<th>Description</th>
|
<th>Description</th>
|
||||||
<th>Colour</th>
|
<th>Colour</th>
|
||||||
|
@ -61,6 +65,7 @@
|
||||||
{% for e in p.entries.values() %}
|
{% for e in p.entries.values() %}
|
||||||
<tr>
|
<tr>
|
||||||
<td>{{ e.value }}</td>
|
<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.expanded %}{{ e.expanded }}{% endif %}</td>
|
||||||
<td>{% if e.description %}{{ e.description }}{% endif %}</td>
|
<td>{% if e.description %}{{ e.description }}{% endif %}</td>
|
||||||
<td>{% if p.colour %}<div class='colorbox' background={{ p.colour }}></div>{% endif %}</td>
|
<td>{% if p.colour %}<div class='colorbox' background={{ p.colour }}></div>{% endif %}</td>
|
||||||
|
|
Loading…
Reference in New Issue