summaryrefslogtreecommitdiff
path: root/cesar/maximus/python/lib/cesar/defineparser.py
blob: e5f7c4518c40adc700f03af0c1e14ce15fc40d60 (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
#!/usr/bin/python

#############################################################################
#  Copyright (C) 2011 Spidcom
#############################################################################

def define_parser (filename):
    """
    Get constant defines into a python dict.
    param filename  :  the string to parse the file.
    return a dict containing the defines.

    Supported defines:
      #define FOO 2
      #define BAR FOO

    Not supported defines: All Macros set to check values, computes values,
    etc...
    """
    import os
    import re
    # Read the file.
    fd = open (filename, 'r')
    f = fd.read ()
    fd.close ()
    defines = \
        re.findall (r'(?m)^#define\s+([A-Z_0-9]+)\s+((?:0x)?[0-9A-Z_]+)\s*$',
                f)
    defs = {}
    defines_dict = dict (defines)
    for i in defines:
        a = i[0]
        if re.match (r'^[A-Z][A-Z_0-9]*$', i[1]):
            b = defines_dict[i[1]]
        else:
            b = i[1]
        defs[a] = int (b, 0)
    return defs