Giter Club home page Giter Club logo

capparselib's Introduction

capparselib

Coverage Status Python package

This module parses and 'normalizes' alerts sent using the Common Alerting Protocol. It currently can handle XML in CAP1.1 and CAP1.2 format, and supports ATOM feeds.

This module originally returned an 'Event' object, but now simply returns a dictionary. It currently does not lookup geocode items or any other items, but please make a request if you want sample code to do this ([email protected]). Alert validation via signatures is also not supported, yet.

Installation

You can install it from source with:

$ python setup.py install

You can also install it directly with pip:

$ pip install capparselib

Usage

Basic usage includes (from the source directory):

>>> from capparselib.parsers import CAPParser
>>> f = r'test/data/weather.cap'
>>> src = open(f, 'r').read()
>>> alert_list = CAPParser(src).as_dict()

The CAPParser class returns a list of alerts, which are each a dictionary of items. The library provides its own default mapping of the alert fields. For instance, fields with names 'headline' (CAP1.2) and 'title' (CAP1.1) are both renamed to 'cap_headline'. Using the above basic usage example, you can then access fields as needed:

>>> alert = alert_list[0]
>>> alert['cap_sender']
    '[email protected]'
>>> alert.keys()
    ['cap_scope', 'cap_sender', 'cap_note', 'cap_status',
    'cap_id', 'cap_message_type', 'cap_sent', 'cap_info']
>>> alert['cap_info'].keys()
    ['cap_area', 'cap_sender', 'cap_expires', 'cap_severity',
    'cap_event', 'cap_certainty', 'cap_urgency', 'cap_event_code',
    'cap_effective', 'cap_description', 'cap_parameter', 'cap_headline',
    'cap_instruction', 'cap_category']
>>> alert['cap_info']['cap_severity']
    'Severe'

Instead of using the default mapping, you can use your own or those provided extra in the file cap_mappings.py (currently just one extra). Here is how:

>>> from capparselib.parsers import CAPParser
>>> import capparselib.cap_mappings 
>>> f = r'test/data/weather.cap'
>>> src = open(f, 'r').read()
>>> alert_list = CAPParser(src, mappings=cap_mappings.CAP_1_2_MAPPINGS).as_dict()

Testing

There are quite a number of tests included with various payloads.

You can run tests quite easily with:

$ pip install -r src/requirements-test.txt 
$ python setup.py test

This will install necessary dependencies, and run the included tests.

Publishing

Deployment to PyPi should happen automatically when the package version numbers gets incremented and a new tag is added to a commit.

To test a deploy, you can do the following locally (notes for myself):

$ pip install twine
$ inv build package upload-test

Then can test installation with:

$ pip install -i https://test.pypi.org/simple/ capparselib

capparselib's People

Contributors

kelvinn avatar dependabot-preview[bot] avatar leorocher avatar helgiborg avatar dependabot[bot] avatar pyup-bot avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.