aboutsummaryrefslogtreecommitdiffstats
path: root/eo_parser/helper.py
blob: 5e0acdc4a9e947492cbe56f0746607bddfe5f4fe (plain) (blame)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import os
from string import capwords

class _const:
  class ConstError(TypeError): pass
  def __setattr__(self,name,value):
    if self.__dict__.has_key(name):
#       raise self.ConstError("Can't rebind const(%s)"%name)
      print "Can't rebind const: \"%s\""%name
      return
    self.__dict__[name]=value
  def __init__(self):
    # most of constants are used as internal dict keys,
    # but some of them are also used as tags in XML
    # Changing this constans will only spoil the view of XML

    self.NAME = "name" #used as xml-tag
    self.C_NAME = "c_name" #used as xml-tag
    self.MODULE = "module" #used as xml-tag
    self.TYPE = "type" #used as xml-tag
    self.CLASS_CONSTRUCTOR = "class_constructor"
    self.BASE_ID = "base_id"
    self.FUNCS = "funcs"
    self.PARAMETERS = "parameters"
    self.OP_ID = "op_id"

    self.OP_DESC = "op_desccc"
    self.EV_DESC = "ev_desccc"

    self.GET_FUNCTION = "get_function"#used as xml-tag
    self.DEFINES = "defines"
    self.PARENT = "parent"#used as xml-tag
    self.PARENTS = "parents"
    self.H_FILE = "h_file"
    self.C_FILE = "c_file"
    self.XML_FILE = "xml_file"
    self.MACRO = "macro"  #EO_CLASS  #used as xml-tag
    self.C_MACRO = "c_macro"#used as xml-tag
    self.OP_MACROS = "op_macros" #dict of macros from @def with parameters

    self.CLASS_TYPE_MIXIN =  "EO_CLASS_TYPE_MIXIN"
    self.CLASS_TYPE_REGULAR = "EO_CLASS_TYPE_REGULAR"
    self.CLASS_DESC_OPS = "EO_CLASS_DESCRIPTION_OPS"
    self.EO_DEFINE_CLASS = "EO_DEFINE_CLASS"
    self.EO_DEFINE_CLASS_STATIC =  "EO_DEFINE_CLASS_STATIC"
    self.EO_OP_DESCRIPTION =  "EO_OP_DESCRIPTION"
    self.EO_OP_DESCRIPTION_SENTINEL = "EO_OP_DESCRIPTION_SENTINEL"
    self.SUB_ID = "SUB_ID_"
    self.EO_TYPECHECK = "EO_TYPECHECK"

    self.SET_ONLY = 0
    self.GET_ONLY = 1
    self.SET_GET = 2
    self.METHOD = "method" #used as type id for property; and as xml tag

    self.EXTENSIONS = "extensions"  #used as xml-tag
    self.INSTANTIATEABLE = "instantiateable" #used as xml-tag
    self.CLASS = "class" #used as xml-tag
    self.INCLUDE = "include" #used as xml-tag
    self.TYPENAME = "typename" #used as xml-tag
    self.EXTERN_FUNCTION = "extern_function" #used as xml-tag
    self.EVENT = "event" #used as xml-tag
    self.EVENTS = "events" #used as xml-tag
    self.METHODS = "methods" #used as xml-tag
    self.XML_SUB_ID = "sub_id" #used as xml-tag
    self.PARAMETER = "parameter" #used as xml-tag
    self.C_TYPENAME = "c_typename" #used as xml-tag
    self.PRIMARY_TYPE = "primary_type" #used as xml-tag
    self.DIRECTION = "direction" #used as xml-tag


def isC(s):
  #FIXME when parsing eobase we can catch "eo.c" with EO_DEFINE_CLASS
  path_lst = s.split('.')
  if len(path_lst) == 2:
    if path_lst[1] in ["c", "cc", "cpp"]:
      return True
    else:
      return False


def isH(s):
  path_lst = s.split('.')
  if len(path_lst) == 2:
    if path_lst[1] in ["h"]:
      return True
    else:
      return False

def isXML(s):
  path_lst = s.split('.')
  if len(path_lst) == 2:
    if path_lst[1] in ["xml"]:
      return True
    else:
      return False

#  dir_files_get(_directories, func, recursive)
#
#  _directories - input list of dirs,
#  recursive - recursive lookup or not
#
#  Recursively(if True) walks through directories.
#  Builds abs path for files.
#
#  Returns list of absolute paths

def dir_files_get(_directories, recursive = True):
  res = []
  if recursive:
    for d in _directories:
      for root, dirs, files in os.walk(d):
        tmp = []
        for f in files:
          tmp.append(os.path.join(root,f))
        res += tmp
  else:
    for d in _directories:
      tmp = []
      for f in os.listdir(d):
        tmp.append(os.path.join(d, f))
      res += tmp

  res = list(set(res))
  return res


#  abs_path_get(_paths)
#
#  _paths - list of dirs or files
#  _warning - boolean; exit if path doesn't exist
#
#  Builds abs path and checks if it exists for each path in list.
#
#  Returns list of abs paths
#
def abs_path_get(_paths, _warning = True):
   if type(_paths) == list:
     res = []
     lst = list(set(_paths)) #remove multiple items from list
     for d in lst:
       path_tmp = d
       path_tmp = os.path.expanduser(path_tmp)
       path_tmp = os.path.abspath(path_tmp)
       if os.path.exists(path_tmp):
         res.append(path_tmp)
       else:
         if _warning:
           print "ERROR: path %s doesn't exist... Aborting..."%path_tmp
           exit(1)
     return res


#  normalize_names(_lst)
#
#  _lst - list of class names
#
#  Normalizes class names like following:
#  Some_class name --> SomeClassName
#
#  Returns list of normalized names
#
def normalize_names(_lst):
   res = []
   for l in _lst:
     l = l.replace("-"," ")
     l = l.replace("_"," ")
     l = capwords(l)
     l = "".join(l.split())
     res.append(l)
   return res