summaryrefslogtreecommitdiffstats
path: root/themis/config/utils.py
blob: 77aa3a34082a54e4d16ec270e5afcf8881200199 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from zope import component
from plone.registry.interfaces import IRegistry
from interfaces import IThemisSettings

def get_document_type_for_mail(mail):
    settings = component.getUtility(IRegistry).forInterface(IThemisSettings, False)
    mapping = settings.mail_category_mapping
    values = []
    if mail.categorie_de_courrier:
        values.extend(mail.categorie_de_courrier)
    if mail.sous_categorie_de_courrier:
        values.extend(mail.sous_categorie_de_courrier)
    for line in mapping.splitlines():
        if not line:
            continue
        if line.startswith('#'):
            continue
        category, object_type = line.strip().split('|')
        if category in values:
            return object_type
    return None

def get_document_location_for_mail(mail):
    settings = component.getUtility(IRegistry).forInterface(IThemisSettings, False)
    return settings.documents_path

def get_ocr_location(doctype):
    settings = component.getUtility(IRegistry).forInterface(IThemisSettings, False)
    return getattr(settings, doctype+'_path')

def get_categories_from_ocr_code(ocr_code, doctype):
    mapping_name = 'ocr_code_%s_mapping' % doctype
    if mapping_name == 'ocr_code_incoming_mails_mapping':
        mapping_name = 'ocr_code_mapping'
    settings = component.getUtility(IRegistry).forInterface(IThemisSettings, False)
    mapping = getattr(settings, mapping_name)
    if mapping is None:
        mapping = ''
    for line in mapping.splitlines():
        if not line:
            continue
        if line.startswith('#'):
            continue
        if line.count('|') == 2:
            code, category, subcategory = line.strip().split('|')
            folder = None
        if line.count('|') == 3:
            code, category, subcategory, folder = line.strip().split('|')
            subcategory = subcategory or None
        elif line.count('|') == 1:
            code, category = line.strip().split('|')
            subcategory, folder = None, None
        if code == ocr_code:
            return category, subcategory, folder
    return None