processors.utils   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 27
dl 0
loc 54
rs 10
c 0
b 0
f 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A is_string() 0 2 1
A full_path() 0 5 1
A post_json() 0 15 1

2 Methods

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