### Flask Application Setup Source: https://github.com/misp/pytaxonomies/blob/main/website/REQUIREMENTS.txt Basic setup for a Flask application, demonstrating the integration of Flask-Bootstrap for UI enhancements. ```python from flask import Flask from flask_bootstrap import Bootstrap app = Flask(__name__) Bootstrap(app) @app.route('/') def index(): return '

Hello, PyTaxonomies!

' if __name__ == '__main__': app.run(debug=True) ``` -------------------------------- ### Install PyTaxonomies Source: https://github.com/misp/pytaxonomies/blob/main/README.md Installs the PyTaxonomies library using pip. This is the primary method for adding the library to your Python environment. ```bash pip install pytaxonomies ``` -------------------------------- ### Initialize and Get Taxonomies Version Source: https://github.com/misp/pytaxonomies/blob/main/README.md Demonstrates how to initialize the Taxonomies object and access its version, license, and description attributes. This is a fundamental step for using the library. ```python from pytaxonomies import Taxonomies taxonomies = Taxonomies() print(taxonomies.version) print(taxonomies.license) print(taxonomies.description) ``` -------------------------------- ### Navigation Bar with Flask-Nav Source: https://github.com/misp/pytaxonomies/blob/main/website/REQUIREMENTS.txt Example of creating a navigation bar using Flask-Nav, allowing for easy management of site navigation elements. ```python from flask import Flask from flask_nav import Nav from flask_nav.elements import Navbar, View app = Flask(__name__) nav = Nav() nnav = Navbar('My Navbar', View('Home', 'index'), View('About', 'about')) nav.register_element(nnav) nnav.init_app(app) @app.route('/') def index(): return '

Home Page

' @app.route('/about') def about(): return '

About Page

' if __name__ == '__main__': app.run(debug=True) ``` -------------------------------- ### Get All Machine Tags Source: https://github.com/misp/pytaxonomies/blob/main/README.md Retrieves and displays all machine tags for all imported taxonomies. This provides a comprehensive overview of the structured data available. ```python taxonomies = Taxonomies() print(taxonomies.all_machinetags()) ``` -------------------------------- ### Get Machine Tags for a Specific Taxonomy Source: https://github.com/misp/pytaxonomies/blob/main/README.md Fetches and displays the machine tags associated with a particular taxonomy, using 'circl' as an example. This helps in understanding the specific tags relevant to that category. ```python taxonomies = Taxonomies() print(taxonomies.get('circl').machinetags()) ``` -------------------------------- ### Access Nested Taxonomy Predicates Source: https://github.com/misp/pytaxonomies/blob/main/README.md Demonstrates how to access nested predicates within a taxonomy, using 'enisa' and 'physical-attack' as an example. It also shows how to get the raw value, expanded name, and description of a specific predicate. ```python taxonomies = Taxonomies() physical_attacks = taxonomies.get('enisa').get('physical-attack') print(list(physical_attacks)) vandalism_predicate = physical_attacks.get('vandalism') print(vandalism_predicate.value) print(vandalism_predicate.expanded) print(vandalism_predicate.description) ``` -------------------------------- ### Edit Existing Taxonomy Source: https://github.com/misp/pytaxonomies/blob/main/notebooks/create_n_edit.ipynb Illustrates how to load an existing taxonomy and modify its properties. This example shows how to access a specific taxonomy by name and update one of its numerical values. ```python from pytaxonomies import Taxonomies from pytaxonomies import Taxonomy, Predicate, Entry taxonomies = Taxonomies() edited_taxonomy = taxonomies["false-positive"] edited_taxonomy.predicates['risk'].entries['low'].numerical_value = 20 ``` -------------------------------- ### Get Expanded Machine Tags for a Specific Taxonomy Source: https://github.com/misp/pytaxonomies/blob/main/README.md Retrieves and displays the expanded machine tags for a specific taxonomy, 'circl' in this case. Expanded tags provide more human-readable versions of the machine tags. ```python taxonomies = Taxonomies() print(taxonomies.get('circl').machinetags_expanded()) ``` -------------------------------- ### Create New Taxonomy Source: https://github.com/misp/pytaxonomies/blob/main/notebooks/create_n_edit.ipynb Demonstrates how to create a new taxonomy object, define its properties, and add predicates with entries. This involves initializing Taxonomy, Predicate, and Entry objects and assigning values to their attributes. ```python from pytaxonomies import Taxonomy, Predicate, Entry new_taxonomy = Taxonomy() new_taxonomy.name = "false-positive" new_taxonomy.description = "This taxonomy aims to ballpark the expected amount of false positives." new_taxonomy.version = 1 new_taxonomy.expanded = "False positive" risk_predicate = Predicate() risk_predicate.predicate = 'risk' risk_predicate.expanded = 'Risk' risk_predicate.description = 'Risk of having false positives in the tagged value.' low = Entry() low.value = 'low' low.expanded = 'Low' low.description = 'The risk of having false positives in the tagged value is low.' low.numerical_value = 25 medium = Entry() medium.value = 'medium' medium.expanded = 'Medium' medium.description = 'The risk of having false positives in the tagged value is medium.' medium.numerical_value = 50 high = Entry() high.value = 'high' high.expanded = 'High' high.description = 'The risk of having false positives in the tagged value is high.' high.numerical_value = 75 risk_predicate.entries = {} risk_predicate.entries['low'] = low risk_predicate.entries['medium'] = medium risk_predicate.entries['high'] = high new_taxonomy.predicates = {} new_taxonomy.predicates['risk'] = risk_predicate ``` -------------------------------- ### Form Handling with Flask-WTF Source: https://github.com/misp/pytaxonomies/blob/main/website/REQUIREMENTS.txt Demonstrates how to create and handle forms using Flask-WTF, including CSRF protection and form validation. ```python from flask import Flask, render_template, request from flask_wtf import FlaskForm from wtforms import StringField, SubmitField from wtforms.validators import DataRequired app = Flask(__name__) app.config['SECRET_KEY'] = 'your_secret_key' class TaxonomyForm(FlaskForm): name = StringField('Taxonomy Name', validators=[DataRequired()]) submit = SubmitField('Add Taxonomy') @app.route('/add_taxonomy', methods=['GET', 'POST']) def add_taxonomy(): form = TaxonomyForm() if form.validate_on_submit(): taxonomy_name = form.name.data # Process the taxonomy name (e.g., save to database) return f'

Taxonomy \'{taxonomy_name}\' added!

' return render_template('add_taxonomy.html', form=form) if __name__ == '__main__': app.run(debug=True) ``` ```html Add Taxonomy

Add New Taxonomy

{{ form.csrf_token }}
{{ form.name.label }} {{ form.name() }}
{{ form.submit() }}
``` -------------------------------- ### Add Taxonomy to Repository Source: https://github.com/misp/pytaxonomies/blob/main/notebooks/create_n_edit.ipynb Shows how to add a newly created taxonomy to the project's repository. This involves updating a MANIFEST.json file and creating a new directory for the taxonomy's data. ```python from pathlib import Path import json root_json = Path('..', 'pytaxonomies', 'data', 'misp-taxonomies') with open(root_json / 'MANIFEST.json', encoding='utf8') as m: manifest = json.load(m) # Just a failsafe in case the new taxonomy needs to be modified is_update = False for t in manifest['taxonomies']: if t['name'] == new_taxonomy.name: is_update = True t['version'] = new_taxonomy.version t['description'] = new_taxonomy.description if not is_update: manifest['taxonomies'].append({'version': new_taxonomy.version, 'name': new_taxonomy.name, 'description': new_taxonomy.description}) with open(root_json / 'MANIFEST.json', 'w', encoding='utf8') as m: json.dump(manifest, m, indent=2, ensure_ascii=False) if not (root_json / new_taxonomy.name).exists(): (root_json / new_taxonomy.name).mkdir() with open(root_json / new_taxonomy.name / 'machinetag.json', 'w', encoding='utf8') as m: json.dump(new_taxonomy.to_dict(), m, indent=2, ensure_ascii=False) ``` -------------------------------- ### Initialize DataTables for PyTaxonomies Table Source: https://github.com/misp/pytaxonomies/blob/main/website/templates/main.html This snippet initializes the DataTables jQuery plugin for the HTML table with the ID 'pytaxonomies_table'. It configures the table to display 25 items per page by default and provides options for users to select the number of items displayed. ```javascript $(document).ready(function() { var table = $('#pytaxonomies_table').DataTable({ "pageLength": 25, "lengthMenu": [ [25, 25, 100, -1], [25, 50, 100, "All"] ] }); }); ``` -------------------------------- ### Taxonomy Listing and Details Source: https://github.com/misp/pytaxonomies/blob/main/website/templates/taxonomies.html This Jinja2 template iterates through a collection of taxonomies, displaying their version, name, entry count, and description. It includes conditional rendering for expanded details and links to individual taxonomy pages. ```html {% extends "main.html" %} {% block title %}Taxonomies{% endblock %} {% block content %} Taxonomies - version {{ all_taxonomies.version }} ================================================== {% for a, t in all_taxonomies.items() %} {% endfor %} Short name Long name Version Entries Description Short name Long name Version Entries Description [{{ t.name }}]({{ url_for('taxonomies', name=t.name) }}) {% if t.expanded %} {{ t.expanded }} {%endif%} {{ t.version }} {{ t.amount_entries() }} {{ t.description }} {% endblock %} ``` -------------------------------- ### Access Specific Taxonomy Details Source: https://github.com/misp/pytaxonomies/blob/main/README.md Illustrates how to retrieve detailed information about a specific taxonomy, such as 'enisa', including its description, version, name, and its own set of predicates. ```python taxonomies = Taxonomies() enisa_taxonomy = taxonomies.get('enisa') print(enisa_taxonomy.description) print(enisa_taxonomy.version) print(enisa_taxonomy.name) print(list(enisa_taxonomy.keys())) ``` -------------------------------- ### Search Form and Results Display (HTML) Source: https://github.com/misp/pytaxonomies/blob/main/website/templates/search.html This snippet renders a search form with a CSRF token, a text input for the query, and a submit button. It then displays search results, iterating through entries and showing details like name, description, and machinetag. If no entries are found for a query, it displays 'Nothing found'. ```html {% extends "main.html" %} {% block title %}Search{% endblock %} {% block content %} Search {% if query %} - {{query}} {%endif%} =========================================== {{ form.csrf_token }} {{ form.query(size=20) }} {{ form.submit }} {% if query and not entries %} Nothing found {%endif%} {% if entries %} {% for mt, val in entries.items() %} {% endfor %} Name Description Machinetag Name Description Machinetag [{{ val[0].name }}]({{ url_for('taxonomies', name=val[0].name) }}) {% if val|length == 3 %} {% if val[2].description %} {{ val[2].description }} {% elif val[2].expanded %} {{ val[2].expanded }} {%endif%} {% elif val[1].description %} {{ val[1].description }} {% else %} {{ val[1].predicate }} {%endif%} {{ mt }} {%endif%} {% endblock %} ``` -------------------------------- ### Jinja2 Template for Taxonomy Rendering Source: https://github.com/misp/pytaxonomies/blob/main/website/templates/taxonomy.html This Jinja2 template defines the structure for rendering taxonomy information. It dynamically displays taxonomy details, references, predicates, and their associated entries based on the provided data. It handles conditional rendering for descriptions, expanded values, and machine tags. ```jinja2 {% extends "main.html" %} {% block title %}Taxonomy - {{ taxonomy.name }} {% endblock %} {% block content %} {% if taxonomy.expanded %}{{ taxonomy.expanded }} {%else%} {{taxonomy.name}} {%endif%} (Version {{ taxonomy.version }}) ======================================================================================================================= ### {{ taxonomy.description }} {% if taxonomy.refs %} References: {% for r in taxonomy.refs %}* [{{r}}]({{r}}) {%endfor%} {% endif %} {% if taxonomy.has_entries() %} {% else %} {% endif %} {% if taxonomy.has_entries() %} {% else %} {% endif %} {% for p in taxonomy.predicates.values() %} {% if p.entries %} {% else %} {% endif %} {% endfor %} Description Expanded Predicate Values Colour Machinetag Description Expanded Predicate Values Colour Machinetag {% if p.description %}{{ p.description }}{% endif %} {% if p.expanded %}{{ p.expanded }}{% endif %} {{ p.predicate }} {% for e in p.entries.values() %} {% endfor %} Value Numerical Value Expanded Description Colour Machinetag {{ 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 %} {{ taxonomy.make_machinetag(p, e) }} {% if p.colour %} {% endif %} {{ taxonomy.make_machinetag(p) }} {% endblock %} ``` -------------------------------- ### Count and List Taxonomies Source: https://github.com/misp/pytaxonomies/blob/main/README.md Shows how to determine the total number of imported taxonomies and retrieve a list of their names. This helps in understanding the scope of available data. ```python taxonomies = Taxonomies() # How many taxonomies have been imported print(len(taxonomies)) # Names of the taxonomies print(list(taxonomies.keys())) ``` -------------------------------- ### Count Entries and Predicates in a Taxonomy Source: https://github.com/misp/pytaxonomies/blob/main/README.md Shows how to count the total number of entries within a specific taxonomy and the number of predicates it contains. This is useful for analyzing the complexity of a taxonomy. ```python taxonomies = Taxonomies() # Amount of entries in the 'circl' taxonomy print(taxonomies.get('circl').amount_entries()) # Amount of predicates in the 'circl' taxonomy print(len(taxonomies.get('circl'))) ``` -------------------------------- ### Save Edited Taxonomy Source: https://github.com/misp/pytaxonomies/blob/main/notebooks/create_n_edit.ipynb Details the process of saving changes made to an existing taxonomy. This involves updating the MANIFEST.json file with the new version and description, and then saving the modified taxonomy data. ```python from pathlib import Path import json root_json = Path('..', 'pytaxonomies', 'data', 'misp-taxonomies') with open(root_json / 'MANIFEST.json', encoding='utf8') as m: manifest = json.load(m) # Just a failsafe in case the new taxonomy needs to be modified is_update = False for t in manifest['taxonomies']: if t['name'] == edited_taxonomy.name: is_update = True t['version'] += 1 edited_taxonomy.version = t['version'] t['description'] = edited_taxonomy.description if not is_update: raise Exception(f'Taxonomy {edited_taxonomy.name} does not exists in the manifest.') with open(root_json / 'MANIFEST.json', 'w', encoding='utf8') as m: json.dump(manifest, m, indent=2, ensure_ascii=False) with open(root_json / edited_taxonomy.name / 'machinetag.json', 'w', encoding='utf8') as m: json.dump(edited_taxonomy.to_dict(), m, indent=2, ensure_ascii=False) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.