helpers.py 9.0 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
'''
L
LiFeng 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#
# libocispec - a C library for parsing OCI spec files.
#
# Copyright (C) 2017, 2019 Giuseppe Scrivano <giuseppe@scrivano.org>
# Copyright (C) Huawei Technologies., Ltd. 2018-2019. All rights reserved.
#
# libocispec is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# libocispec is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with libocispec.  If not, see <http://www.gnu.org/licenses/>.
#
O
overweight 已提交
26 27 28 29
#!/usr/bin/python -Es
import os
import sys

O
openeuler-iSula 已提交
30
def append_separator(substr):
O
overweight 已提交
31 32 33 34 35
    '''
    Description: append only '_' at last position of subStr
    Interface: None
    History: 2019-09-20
    '''
O
openeuler-iSula 已提交
36 37
    if substr and substr[-1] != '_':
        substr.append('_')
O
overweight 已提交
38

O
openeuler-iSula 已提交
39
def conv_to_c_style(name):
O
overweight 已提交
40 41 42 43 44 45 46 47
    '''
    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 已提交
48
    substr = []
O
overweight 已提交
49 50 51 52
    preindex = 0
    index = 0
    for index, char in enumerate(name):
        if char == '_':
O
openeuler-iSula 已提交
53 54
            append_separator(substr)
            substr.append(name[preindex:index].lower())
O
overweight 已提交
55 56 57
            preindex = index + 1
        if not char.isupper() and name[preindex].isupper() and \
                name[preindex + 1].isupper():
O
openeuler-iSula 已提交
58 59
            append_separator(substr)
            substr.append(name[preindex:index - 1].lower())
O
overweight 已提交
60 61 62
            preindex = index - 1
            continue
        if char.isupper() and index > 0 and name[index - 1].islower():
O
openeuler-iSula 已提交
63 64
            append_separator(substr)
            substr.append(name[preindex:index].lower())
O
overweight 已提交
65 66 67 68
            preindex = index

    if preindex <= index and index >= 0 and name[index] != '_' and \
            preindex != 0:
O
openeuler-iSula 已提交
69 70 71
        append_separator(substr)
    substr.append(name[preindex:index + 1].lower())
    result = ''.join(substr)
O
overweight 已提交
72 73
    return result

O
openeuler-iSula 已提交
74
def get_map_c_types(typ):
O
overweight 已提交
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
    '''
    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 已提交
113
def valid_basic_map_name(typ):
O
overweight 已提交
114 115 116 117 118 119 120 121
    '''
    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 已提交
122
def make_basic_map_name(mapname):
O
overweight 已提交
123 124 125 126 127 128
    '''
    Description: Make basic map name
    Interface: None
    History: 2019-06-17
    '''
    basic_map_types = ('string', 'int', 'bool')
O
openeuler-iSula 已提交
129
    parts = conv_to_c_style(mapname).split('_')
O
overweight 已提交
130 131 132 133 134 135 136 137
    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 已提交
138
def get_name_substr(name, prefix):
O
overweight 已提交
139 140 141 142 143 144 145 146
    '''
    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 已提交
147
def get_prefixe_name(name, prefix):
O
overweight 已提交
148 149 150 151 152 153 154 155 156 157 158
    '''
    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 已提交
159
def get_prefixe_pointer(name, typ, prefix):
O
overweight 已提交
160 161 162 163 164 165
    '''
    Description: Make pointer name
    Interface: None
    History: 2019-06-17
    '''
    if typ != 'object' and typ != 'mapStringObject' and \
O
openeuler-iSula 已提交
166
            not valid_basic_map_name(typ):
O
overweight 已提交
167
        return ""
O
openeuler-iSula 已提交
168 169
    return '%s *' % make_basic_map_name(typ) if valid_basic_map_name(typ) \
        else "%s *" % get_prefixe_name(name, prefix)
O
overweight 已提交
170

O
openeuler-iSula 已提交
171
def judge_complex(typ):
O
overweight 已提交
172 173 174 175 176 177 178
    '''
    Description: Check compound object
    Interface: None
    History: 2019-06-17
    '''
    return typ in ('object', 'array', 'mapStringObject')

O
openeuler-iSula 已提交
179
def judge_data_type(typ):
O
overweight 已提交
180 181 182 183 184 185 186 187 188 189
    '''
    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 已提交
190
def judge_data_pointer_type(typ):
O
overweight 已提交
191 192 193 194 195 196 197 198 199
    '''
    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 已提交
200
def obtain_data_pointer_type(typ):
O
overweight 已提交
201 202 203 204 205 206 207 208
    '''
    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 已提交
209
def obtain_pointer(name, typ, prefix):
O
overweight 已提交
210 211 212 213 214
    '''
    Description: Obtain pointer string
    Interface: None
    History: 2019-06-17
    '''
O
openeuler-iSula 已提交
215
    ptr = get_prefixe_pointer(name, typ, prefix)
O
overweight 已提交
216 217 218 219 220 221
    if ptr != "":
        return ptr

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

O
openeuler-iSula 已提交
222
class CombinateName(object):
O
overweight 已提交
223
    '''
O
openeuler-iSula 已提交
224
    Description: Store CombinateName information
O
overweight 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    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 已提交
246
        return CombinateName(prefix_name + leaf, leaf)
O
overweight 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262


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 已提交
263
        self.name = conv_to_c_style(name.name.replace('.', '_'))
O
overweight 已提交
264
        self.origname = name.leaf or name.name
O
openeuler-iSula 已提交
265
        self.fixname = conv_to_c_style(self.origname.replace('.', '_'))
O
overweight 已提交
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 300 301 302 303 304 305 306



    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 已提交
307
        self.fileprefix = conv_to_c_style( \
O
overweight 已提交
308 309 310 311 312 313 314 315 316 317 318 319 320
            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 已提交
321 322 323 324