helpers.py 8.8 KB
Newer Older
O
overweight 已提交
1 2 3 4 5 6
# -*- coding: utf-8 -*-
'''
Description: helper class and functions
Interface: None
History: 2019-06-17
'''
O
openeuler-iSula 已提交
7
# - Copyright (C) Huawei Technologies., Ltd. 2018-2019. All rights reserved.
O
overweight 已提交
8 9 10 11 12 13 14 15 16 17
# - clibcni licensed under the Mulan PSL v1.
# - You can use this software according to the terms and conditions of the Mulan PSL v1.
# - You may obtain a copy of Mulan PSL v1 at:
# -     http://license.coscl.org.cn/MulanPSL
# - THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# - IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# - PURPOSE.
# - See the Mulan PSL v1 for more details.
# - Description: generate json
# - Author: tanyifeng
O
openeuler-iSula 已提交
18
# - Create: 2018-04-25
O
overweight 已提交
19 20 21 22
#!/usr/bin/python -Es
import os
import sys

O
openeuler-iSula 已提交
23
def append_separator(substr):
O
overweight 已提交
24 25 26 27 28
    '''
    Description: append only '_' at last position of subStr
    Interface: None
    History: 2019-09-20
    '''
O
openeuler-iSula 已提交
29 30
    if substr and substr[-1] != '_':
        substr.append('_')
O
overweight 已提交
31

O
openeuler-iSula 已提交
32
def conv_to_c_style(name):
O
overweight 已提交
33 34 35 36 37 38 39 40
    '''
    Description: convert name to linux c format
    Interface: None
    History: 2019-06-17
    '''
    if name is None or name == "":
        return ""
    name = name.replace('.', '_').replace('-', '_').replace('/', '_')
O
openeuler-iSula 已提交
41
    substr = []
O
overweight 已提交
42 43 44 45
    preindex = 0
    index = 0
    for index, char in enumerate(name):
        if char == '_':
O
openeuler-iSula 已提交
46 47
            append_separator(substr)
            substr.append(name[preindex:index].lower())
O
overweight 已提交
48 49 50
            preindex = index + 1
        if not char.isupper() and name[preindex].isupper() and \
                name[preindex + 1].isupper():
O
openeuler-iSula 已提交
51 52
            append_separator(substr)
            substr.append(name[preindex:index - 1].lower())
O
overweight 已提交
53 54 55
            preindex = index - 1
            continue
        if char.isupper() and index > 0 and name[index - 1].islower():
O
openeuler-iSula 已提交
56 57
            append_separator(substr)
            substr.append(name[preindex:index].lower())
O
overweight 已提交
58 59 60 61
            preindex = index

    if preindex <= index and index >= 0 and name[index] != '_' and \
            preindex != 0:
O
openeuler-iSula 已提交
62 63 64
        append_separator(substr)
    substr.append(name[preindex:index + 1].lower())
    result = ''.join(substr)
O
overweight 已提交
65 66
    return result

O
openeuler-iSula 已提交
67
def get_map_c_types(typ):
O
overweight 已提交
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
    '''
    Description: Get map c types
    Interface: None
    History: 2019-06-17
    '''
    map_c_types = {
        'byte': 'uint8_t',
        'string': 'char *',
        'integer': 'int',
        'boolean': 'bool',
        'double': 'double',
        'int8': 'int8_t',
        "int16": 'int16_t',
        "int32": "int32_t",
        "int64": "int64_t",
        'uint8': 'uint8_t',
        "uint16": 'uint16_t',
        "uint32": "uint32_t",
        "uint64": "uint64_t",
        "UID": "uid_t",
        "GID": "gid_t",
        "booleanPointer": "bool *",
        'bytePointer': 'uint8_t *',
        'integerPointer': 'int *',
        'doublePointer': 'double *',
        'int8Pointer': 'int8_t *',
        "int16Pointer": 'int16_t *',
        "int32Pointer": "int32_t *",
        "int64Pointer": "int64_t *",
        'uint8Pointer': 'uint8_t *',
        "uint16Pointer": 'uint16_t *',
        "uint32Pointer": "uint32_t *",
        "uint64Pointer": "uint64_t *",
    }
    if typ in map_c_types:
        return map_c_types[typ]
    return ""

O
openeuler-iSula 已提交
106
def valid_basic_map_name(typ):
O
overweight 已提交
107 108 109 110 111 112 113 114
    '''
    Description: Valid basic map name
    Interface: None
    History: 2019-06-17
    '''
    return typ != 'mapStringObject' and hasattr(typ, 'startswith') and \
        typ.startswith('map')

O
openeuler-iSula 已提交
115
def make_basic_map_name(mapname):
O
overweight 已提交
116 117 118 119 120 121
    '''
    Description: Make basic map name
    Interface: None
    History: 2019-06-17
    '''
    basic_map_types = ('string', 'int', 'bool')
O
openeuler-iSula 已提交
122
    parts = conv_to_c_style(mapname).split('_')
O
overweight 已提交
123 124 125 126 127 128 129 130
    if len(parts) != 3 or parts[0] != 'map' or \
            (parts[1] not in basic_map_types) or \
            (parts[2] not in basic_map_types):
        print('Invalid map name: %s') % mapname
        sys.exit(1)
    return "json_map_%s_%s" % (parts[1], parts[2])


O
openeuler-iSula 已提交
131
def get_name_substr(name, prefix):
O
overweight 已提交
132 133 134 135 136 137 138 139
    '''
    Description: Make array name
    Interface: None
    History: 2019-06-17
    '''
    return "%s_element" % prefix if name is None or name == "" or prefix == name \
        else "%s_%s_element" % (prefix, name)

O
openeuler-iSula 已提交
140
def get_prefixe_name(name, prefix):
O
overweight 已提交
141 142 143 144 145 146 147 148 149 150 151
    '''
    Description: Make name
    Interface: None
    History: 2019-06-17
    '''
    if name is None or name == "" or prefix.endswith(name):
        return "%s" % prefix
    if prefix is None or prefix == "" or prefix == name or name.endswith(prefix):
        return "%s" % name
    return "%s_%s" % (prefix, name)

O
openeuler-iSula 已提交
152
def get_prefixe_pointer(name, typ, prefix):
O
overweight 已提交
153 154 155 156 157 158
    '''
    Description: Make pointer name
    Interface: None
    History: 2019-06-17
    '''
    if typ != 'object' and typ != 'mapStringObject' and \
O
openeuler-iSula 已提交
159
            not valid_basic_map_name(typ):
O
overweight 已提交
160
        return ""
O
openeuler-iSula 已提交
161 162
    return '%s *' % make_basic_map_name(typ) if valid_basic_map_name(typ) \
        else "%s *" % get_prefixe_name(name, prefix)
O
overweight 已提交
163

O
openeuler-iSula 已提交
164
def judge_complex(typ):
O
overweight 已提交
165 166 167 168 169 170 171
    '''
    Description: Check compound object
    Interface: None
    History: 2019-06-17
    '''
    return typ in ('object', 'array', 'mapStringObject')

O
openeuler-iSula 已提交
172
def judge_data_type(typ):
O
overweight 已提交
173 174 175 176 177 178 179 180 181 182
    '''
    Description: Check numeric type
    Interface: None
    History: 2019-06-17
    '''
    if (typ.startswith("int") or typ.startswith("uint")) and \
            "Pointer" not in typ:
        return True
    return typ in ("integer", "UID", "GID", "double")

O
openeuler-iSula 已提交
183
def judge_data_pointer_type(typ):
O
overweight 已提交
184 185 186 187 188 189 190 191 192
    '''
    Description: Check numeric pointer type
    Interface: None
    History: 2019-06-17
    '''
    if (typ.startswith("int") or typ.startswith("uint")) and "Pointer" in typ:
        return True
    return False

O
openeuler-iSula 已提交
193
def obtain_data_pointer_type(typ):
O
overweight 已提交
194 195 196 197 198 199 200 201
    '''
    Description: Get numeric pointer type
    Interface: None
    History: 2019-06-17
    '''
    index = typ.find("Pointer")
    return typ[0:index] if index != -1 else ""

O
openeuler-iSula 已提交
202
def obtain_pointer(name, typ, prefix):
O
overweight 已提交
203 204 205 206 207
    '''
    Description: Obtain pointer string
    Interface: None
    History: 2019-06-17
    '''
O
openeuler-iSula 已提交
208
    ptr = get_prefixe_pointer(name, typ, prefix)
O
overweight 已提交
209 210 211 212 213 214
    if ptr != "":
        return ptr

    return "char *" if typ == "string" else \
        ("%s *" % typ if typ == "ArrayOfStrings" else "")

O
openeuler-iSula 已提交
215
class CombinateName(object):
O
overweight 已提交
216
    '''
O
openeuler-iSula 已提交
217
    Description: Store CombinateName information
O
overweight 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
    Interface: None
    History: 2019-06-17
    '''

    def __init__(self, name, leaf=None):
        self.name = name
        self.leaf = leaf

    def __repr__(self):
        return self.name

    def __str__(self):
        return self.name

    def append(self, leaf):
        '''
        Description: append name
        Interface: None
        History: 2019-06-17
        '''
        prefix_name = self.name + '_' if self.name != "" else ""
O
openeuler-iSula 已提交
239
        return CombinateName(prefix_name + leaf, leaf)
O
overweight 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255


class Unite(object):
    '''
    Description: Store Unite information
    Interface: None
    History: 2019-06-17
    '''
    def __init__(self, name, typ, children, subtyp=None, subtypobj=None, subtypname=None, \
        required=None):
        self.typ = typ
        self.children = children
        self.subtyp = subtyp
        self.subtypobj = subtypobj
        self.subtypname = subtypname
        self.required = required
O
openeuler-iSula 已提交
256
        self.name = conv_to_c_style(name.name.replace('.', '_'))
O
overweight 已提交
257
        self.origname = name.leaf or name.name
O
openeuler-iSula 已提交
258
        self.fixname = conv_to_c_style(self.origname.replace('.', '_'))
O
overweight 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299



    def __repr__(self):
        if self.subtyp is not None:
            return "name:(%s) type:(%s -> %s)" \
                % (self.name, self.typ, self.subtyp)
        return "name:(%s) type:(%s)" % (self.name, self.typ)

    def __str__(self):
        return self.__repr__(self)


class FilePath(object):
    '''
    Description: Store filepath information
    Interface: None
    History: 2019-06-17
    '''
    def __init__(self, name):
        self.name = os.path.realpath(name)
        self.dirname = os.path.dirname(self.name)
        self.basename = os.path.basename(self.name)

    def __repr__(self):
        return "{name:(%s) dirname:(%s) basename:(%s)}" \
            % (self.name, self.dirname, self.basename)

    def __str__(self):
        return self.__repr__(self)


class SchemaInfo(object):
    '''
    Description: Store schema information
    Interface: None
    History: 2019-06-17
    '''

    def __init__(self, name, header, source, prefix, filesdir, refs=None):
        self.name = name
O
openeuler-iSula 已提交
300
        self.fileprefix = conv_to_c_style( \
O
overweight 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313
            name.basename.replace('.', '_').replace('-', '_'))
        self.header = header
        self.source = source
        self.prefix = prefix
        self.refs = refs
        self.filesdir = os.path.realpath(filesdir)

    def __repr__(self):
        return "{name:(%s) header:(%s) source:(%s) prefix:(%s)}" \
            % (self.name, self.header, self.source, self.prefix)

    def __str__(self):
        return self.__repr__(self)
O
openeuler-iSula 已提交
314 315 316 317