示例#1
0
文件: sed.py 项目: PinaeOS/py-text
def exec_cmd(argv):
    try:
        filename = None
        pattern = None
        model = 'e'
        replace = None
        action = None
        output = 'rt'
        
        if len(argv) > 2:
            opts, _ = getopt.getopt(argv[2:],'hf:p:m:r:a:o:', ['help', '--file', '--pattern', '--model', '--replace', '--action', '--output'])
            for name, value in opts:
                if name in ('-h', '--help'):
                    show_help()
                if name in ('-f', '--file'):
                    filename = value
                if name in ('-p', '--pattern'):
                    pattern = value
                if name in ('-m', '--model'):
                    model = value
                if name in ('-r', '--replace'):
                    replace = value
                if name in ('-a', '--action'):
                    action = value
                if name in ('-o', '--output'):
                    output = value
                    
            if str_utils.is_empty(filename) or not os.path.exists(filename):
                print 'error : could not find file : ' + filename
                sys.exit()
            if str_utils.is_empty(pattern):
                print 'error : pattern is empty'
                sys.exit()
            if str_utils.is_empty(action):
                print 'error : action is Empty'
                sys.exit()
            if action in ['s', 'i', 'a'] and str_utils.is_empty(replace):
                print 'error : replace content is Empty'
                sys.exit()
                
            result = sed(filename, pattern, model, replace, action, output)
            if output == 'rt':
                print result
            elif output == 'rl':
                if result and isinstance(result, list):
                    print ''.join(result)
                
        else:
            show_help()
    except GetoptError, e:
        print 'error : ' + e.msg
示例#2
0
文件: awk.py 项目: PinaeOS/py-text
def exec_cmd(argv):
    try:
        filename = None
        pattern = None
        separator = ' '
        columns = None
        
        if len(argv) > 2:
            opts, _ = getopt.getopt(argv[2:],'hf:p:s:c:', ['help', '--file', '--pattern', '--separator', '--column'])
            for name, value in opts:
                if name in ('-h', '--help'):
                    show_help()
                if name in ('-f', '--file'):
                    filename = value
                if name in ('-p', '--pattern'):
                    pattern = value
                if name in ('-s', '--separator'):
                    separator = value
                if name in ('-c', '--column'):
                    if value != None:
                        columns = [int(c) for c in value.split(',') if str_utils.is_numeric(c.strip())]
                    
            if str_utils.is_empty(filename) or not os.path.exists(filename):
                print 'error : could not find file : ' + filename
                sys.exit()
                
            print awk(filename, pattern, separator, columns)
        else:
            show_help()
    except GetoptError, e:
        print 'error : ' + e.msg
示例#3
0
def size(filename):
    if string_utils.is_empty(filename):
        return 0
    
    size = 0
    with codecs.open(filename, 'r') as f:
        for _ in f:
            size = size + 1
    return size
示例#4
0
 def filter(self, filename):
     if string_utils.is_empty(filename):
         return False
     
     if hasattr(self, "file_filter"):
         flag = False
         for filter_item in self.file_filter:
             if regex_utils.check_line(str(filter_item), filename):
                 flag = True
                 break
         return flag
     else:
         return True
示例#5
0
def split(filename, size, dir_path, ext_fn, encoding = 'utf-8'):
    
    name = os.path.basename(filename)
    if string_utils.is_empty(name):
        name = 'split'
    
    with codecs.open(filename, 'r', encoding) as f:
        file_counter = 1
        text = []
        for line in f:
            text.append(line)
            if len(text) >= size:
                write_file(dir_path + os.sep + name + '_' + str(file_counter) + '.' + ext_fn, text, encoding)
                text = []
                file_counter = file_counter + 1
        if len(text) > 0:
            write_file(dir_path + os.sep + name + '_' + str(file_counter) + '.' + ext_fn, text, encoding)
    pass
示例#6
0
 def test_is_empty(self):
     self.assertEqual(string_utils.is_empty(None), True)
     self.assertEqual(string_utils.is_empty(' '), True)
     self.assertEqual(string_utils.is_empty('word'), False)
     self.assertEqual(string_utils.is_empty(True), False)
     self.assertEqual(string_utils.is_empty(21), False)