Ejemplo n.º 1
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
Ejemplo n.º 2
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)
Ejemplo n.º 3
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)
Ejemplo n.º 4
0
def grep(target, pattern, number=False, model="e"):
    """
    grep: print lines matching a pattern
    
    @param target:string list or text file name
    @param pattern: regex pattern or line number pattern or reduce function: bool=action(str)
    @param number: with line number
    @param model: s: substring model, e: regex model, n: line number model, a: action model
    
    @summary: list= ['1:huiyugeng:male', '2:zhuzhu:male', '3:maomao:female']
              print grep.grep(list, '^(?!.*female).*$', ':', [1])
              output: ['huiyugeng', 'zhuzhu']
    """

    if isinstance(target, basestring):
        text = text_file.read_file(target)
    elif isinstance(target, list):
        text = target
    else:
        text = None

    if not text:
        return None

    line_num = 1
    result = []

    for line_text in text:
        line_text = str(line_text)
        if __match(line_num, line_text, model, pattern):
            line_text = __print(line_num, line_text, number)
            if line_text != None:
                result.append(line_text)

        line_num = line_num + 1

    return result
Ejemplo n.º 5
0
def grep(target, pattern, number=False, model='e'):
    '''
    grep: print lines matching a pattern
    
    @param target:string list or text file name
    @param pattern: regex pattern or line number pattern or reduce function: bool=action(str)
    @param number: with line number
    @param model: s: substring model, e: regex model, n: line number model, a: action model
    
    @summary: list= ['1:huiyugeng:male', '2:zhuzhu:male', '3:maomao:female']
              print grep.grep(list, '^(?!.*female).*$', ':', [1])
              output: ['huiyugeng', 'zhuzhu']
    '''

    if isinstance(target, basestring):
        text = text_file.read_file(target)
    elif isinstance(target, list):
        text = target
    else:
        text = None

    if not text:
        return None

    line_num = 1
    result = []

    for line_text in text:
        line_text = str(line_text)
        if __match(line_num, line_text, model, pattern):
            line_text = __print(line_num, line_text, number)
            if line_text != None:
                result.append(line_text)

        line_num = line_num + 1

    return result
Ejemplo n.º 6
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