Completed
Push — master ( b98938...d37950 )
by Gus
01:14
created

OdinError   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 12
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 12
rs 10
c 1
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 3 1
A __str__() 0 3 1
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
from __future__ import unicode_literals
4
from termcolor import colored
5
import requests
6
import json
7
import os
8
9
10
def post_json(service, json_data):
11
    # POST json to the server API
12
    #response = requests.post(service, json={"text":"{}".format(text)})
13
    # for older versions of requests, use the call below
14
    #print("SERVICE: {}".format(service))
15
    response = requests.post(service,
16
                             data=json_data,
17
                             headers={'content-type': 'application/json; charset=utf-8'},
18
                             timeout=None
19
                             )
20
    # response content should be utf-8
21
    #response.encoding = "utf-8"
22
    content = response.content.decode("utf-8")
23
    #print("CONTENT: {}".format(content))
24
    return json.loads(content)
25
26
def full_path(p):
27
    """
28
    Expand a path.  Supports "~" shortcut.
29
    """
30
    return os.path.abspath(os.path.normpath(os.path.expanduser(p)))
31
32
class LabelManager(object):
33
    """
34
    Keep track of common labels
35
    """
36
    UNKNOWN = "UNKNOWN"
37
    # the O in IOB notation
38
    O = "O"
39
40
41
class OdinError(Exception):
42
    """
43
    An error encountered while parsing an Odin rule.
44
    """
45
46
    def __init__(self, rules, message):
47
        self.rules = rules
48
        self.message = message
49
50
    def __str__(self):
51
        cn = colored(self.__class__.__name__, color="red", attrs=["bold"])
52
        return "{}: {}\n{}".format(cn, self.message, self.rules)
53