Пример #1
0
def __map_with_keyword(string, keywords):
    '''
    Parse string with keywords
    
    @param string: String for parse
    @param keywords: keyword list
    
    @summary: str = 'rule service 0 protocol tcp dst-port 80 to 81'
              keywords = ['service', 'protocol', 'dst-port']
              print parse_string(str, ' ', keywords)
    
              Output: {'service':'0', 'protocol':'tcp', 'dst-port', '80 to 81'}
    '''
    value_map = {}
    
    separator = ' '
        
    key, value = None, None

    items = string.split(separator)
    for item in items:
        if string_utils.is_not_empty(item) and item.strip() in keywords:
            if value != None and key != None:
                value_map[key] = value.strip()
            key, value = item, ''
        else:
            if string_utils.is_not_blank(value) and string_utils.is_not_blank(item):
                value = value + separator + item
            
    # Add last item to resultset   
    if key != None and value != None:
        value_map[key] = value.strip()
        
    return value_map
Пример #2
0
def __map_with_keyword(string, keywords):
    '''
    Parse string with keywords
    
    @param string: String for parse
    @param keywords: keyword list
    
    @summary: str = 'rule service 0 protocol tcp dst-port 80 to 81'
              keywords = ['service', 'protocol', 'dst-port']
              print parse_string(str, ' ', keywords)
    
              Output: {'service':'0', 'protocol':'tcp', 'dst-port', '80 to 81'}
    '''
    value_map = {}

    separator = ' '

    key, value = None, None

    items = string.split(separator)
    for item in items:
        if string_utils.is_not_empty(item) and item.strip() in keywords:
            if value != None and key != None:
                value_map[key] = value.strip()
            key, value = item, ''
        else:
            if string_utils.is_not_blank(value) and string_utils.is_not_blank(
                    item):
                value = value + separator + item

    # Add last item to resultset
    if key != None and value != None:
        value_map[key] = value.strip()

    return value_map
Пример #3
0
def __read_script(script, encoding = 'utf8'):
    file_reg = '\[(.*)\]'
    cmd_reg = '(\w+)\s*:\s*(.*)'
    
    tag_file = None
    cmd_set = {}
    
    if isinstance(script, str):
        content = text_file.read_file(script, 'all', encoding, True)
    elif isinstance(script, list):
        content = script
    elif isinstance(script, dict):
        return script
    else:
        raise IOError('script must be a filename or script command list')
    
    for line in content:
        if str_utils.is_empty(line):
            continue
        line = line.strip()
        if str_utils.startswith(line, '#'):
            continue
        if regex_utils.check_line(file_reg, line):
            tag_file = regex_utils.parse_line(file_reg, line)[0]
            if str_utils.is_not_empty(tag_file):
                tag_file = tag_file.strip()
        elif regex_utils.check_line(cmd_reg, line):
            cmd_item = regex_utils.parse_line(cmd_reg, line)
            if tag_file is not None and cmd_item is not None and len(cmd_item) == 2:
                cmd_list = cmd_set.get(tag_file) if cmd_set.has_key(tag_file) else []
                cmd_list.append(cmd_item)
                cmd_set[tag_file] = cmd_list
                
    return cmd_set
Пример #4
0
def edit(script, base_path=None, encoding='utf8', output='w'):
    cmd_set = __read_script(script)
    for filename in cmd_set:
        if str_utils.is_empty(filename):
            continue
        cmd_list = cmd_set.get(filename)
        if str_utils.is_not_empty(base_path):
            if filename.startswith('/'):
                filename = base_path + filename
            else:
                filename = os.path.join(base_path, filename)
        if os.path.exists(filename):
            if os.path.isdir(filename):
                continue
        else:
            __create(filename)
        content = text_file.read_file(filename, 'all', encoding, False)
        content = content if content else []
        for cmd_item in cmd_list:
            cmd = cmd_item[0].strip()
            cmd_arg = cmd_item[1].strip()
            if str_utils.is_not_empty(cmd):
                cmd = cmd.lower()
                if cmd == 'd':
                    content = __delete(cmd_arg, content)
                elif cmd == 'u':
                    content = __update(cmd_arg, content)
                elif cmd == 'a':
                    content = __add(cmd_arg, content)
                elif cmd == 'n':
                    value = __reduce_value(cmd_arg)
                    content.append(value)
                elif cmd == 'c':
                    content = []

        content = [line for line in content if line != None]
        if output == 'w':
            text_file.write_file(filename, content, encoding, '')
        else:
            print ''.join(content)
Пример #5
0
def edit(script, base_path = None, encoding = 'utf8', output = 'w'):
    cmd_set = __read_script(script)
    for filename in cmd_set:
        if str_utils.is_empty(filename):
            continue
        cmd_list = cmd_set.get(filename)
        if str_utils.is_not_empty(base_path):
            if filename.startswith('/'):
                filename = base_path + filename
            else:
                filename = os.path.join(base_path, filename)
        if os.path.exists(filename):
            if os.path.isdir(filename):
                continue
        else:
            __create(filename)
        content = text_file.read_file(filename, 'all', encoding, False)
        content = content if content else []
        for cmd_item in cmd_list:
            cmd = cmd_item[0].strip()
            cmd_arg = cmd_item[1].strip()
            if str_utils.is_not_empty(cmd):
                cmd = cmd.lower()
                if cmd == 'd':
                    content = __delete(cmd_arg, content)
                elif cmd == 'u':
                    content = __update(cmd_arg, content)
                elif cmd == 'a':
                    content = __add(cmd_arg, content)
                elif cmd == 'n':
                    value = __reduce_value(cmd_arg)
                    content.append(value)
                elif cmd == 'c':
                    content = []
                        
        content = [line for line in content if line != None]
        if output == 'w':
            text_file.write_file(filename, content, encoding, '')
        else:
            print ''.join(content)
Пример #6
0
def tar(filelist, tar_path = None, exclude = None):
        
    if string_utils.is_not_empty(tar_path):
        t = tarfile.open(name = tar_path, mode = __get_tar_mode(tar_path))
    else: 
        fileobj = tempfile.NamedTemporaryFile()
        t = tarfile.open(mode = 'w', fileobj=fileobj)
    
    exclude = exclude or []

    if isinstance(filelist, str):
        filepath = os.path.abspath(filelist)
        filelist = []
        if os.path.isdir(filepath):
            for root, dirs, files in os.walk(filepath):
                for _file in files:
                    match = True
                    for pattern in exclude:
                        if regex_utils.check_line(pattern, _file):
                            match = False
                            break
                    if match:
                        filelist.append(os.path.join(root, _file))
        elif os.path.isfile(filepath):
            filelist = [filelist]
            
    if isinstance(filelist, list):     
        for _file in filelist:
            t.add(_file)

    t.close()
    
    if string_utils.is_not_empty(tar_path):
        return tar_path
    else:
        fileobj.seek(0)
        return fileobj
Пример #7
0
def list_dir(dir_path, file_filter = None):
    file_list = []
    if string_utils.is_not_empty(dir_path):
        if os.path.isdir(dir_path):
            filename_list = os.listdir(dir_path)
            for filename in filename_list:
                if file_filter and isinstance(file_filter, FileFilter):
                    if file_filter.filter(filename):
                        file_list.append(os.path.join(dir_path, filename))
                else:
                    file_list.append(os.path.join(dir_path, filename))
    else:
        file_list = None
        
    return file_list
Пример #8
0
def __read_script(script, encoding='utf8'):
    file_reg = '\[(.*)\]'
    cmd_reg = '(\w+)\s*:\s*(.*)'

    tag_file = None
    cmd_set = {}

    if isinstance(script, str):
        content = text_file.read_file(script, 'all', encoding, True)
    elif isinstance(script, list):
        content = script
    elif isinstance(script, dict):
        return script
    else:
        raise IOError('script must be a filename or script command list')

    for line in content:
        if str_utils.is_empty(line):
            continue
        line = line.strip()
        if str_utils.startswith(line, '#'):
            continue
        if regex_utils.check_line(file_reg, line):
            tag_file = regex_utils.parse_line(file_reg, line)[0]
            if str_utils.is_not_empty(tag_file):
                tag_file = tag_file.strip()
        elif regex_utils.check_line(cmd_reg, line):
            cmd_item = regex_utils.parse_line(cmd_reg, line)
            if tag_file is not None and cmd_item is not None and len(
                    cmd_item) == 2:
                cmd_list = cmd_set.get(tag_file) if cmd_set.has_key(
                    tag_file) else []
                cmd_list.append(cmd_item)
                cmd_set[tag_file] = cmd_list

    return cmd_set