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
def execute(self): linux_info = linux.LinuxInfo() ps_info = linux_info.ps(False) if ps_info is not None: for ps_item in ps_info: p_id = ps_item.get('pid') p_cmd = ps_item.get('cmd') if string_utils.is_numeric(self.pid): if str(self.pid) == p_id: return else: if str(self.pid) == p_cmd: return execute.run(self.action)
def read_file(filename, tail= 'all', encoding = 'utf-8', strip = False): if string_utils.is_blank(filename): raise IOError('filename is None') text = [] try: text_file = codecs.open(filename, 'r', encoding) text = [line.strip() if strip else line for line in text_file.readlines()] if tail and tail != 'all': if string_utils.is_numeric(tail): _tail = int(tail) _size = len(text) if _tail < _size: text = text[_size - _tail : _size] text_file.close() except IOError: text = None return text
def awk(target, pattern, separator, action): ''' awk : pattern scanning and text processing language @param target: string list or text file name @param pattern: regex pattern @param separator: line separator @param action: column index list or function: str[]=action(str[]) @summary: list= ['1:huiyugeng:male', '2:zhuzhu:male'] print awk.awk(list, '', ':', [1]) output: ['huiyugeng', 'zhuzhu'] ''' text = grep.grep(target, pattern) if not text: return None if str_utils.is_blank(separator): separator = ' ' result = [] for line in text: if line != None and separator in line: split_text = str_utils.split(line, separator) if not action: result.append(split_text) elif type(action) == types.ListType: temp_line = [] for column in action: if str_utils.is_numeric(column): temp_line.append(split_text[column]) result.append(temp_line) elif type(action) == types.FunctionType: temp_line = action(split_text) if temp_line != None and type(temp_line) == types.ListType: result.append(temp_line) return result
def read_file(filename, tail='all', encoding='utf-8', strip=False): if string_utils.is_blank(filename): raise IOError('filename is None') text = [] try: text_file = codecs.open(filename, 'r', encoding) text = [ line.strip() if strip else line for line in text_file.readlines() ] if tail and tail != 'all': if string_utils.is_numeric(tail): _tail = int(tail) _size = len(text) if _tail < _size: text = text[_size - _tail:_size] text_file.close() except IOError: text = None return 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
def test_is_numeric(self): self.assertEqual(string_utils.is_numeric(None), False) self.assertEqual(string_utils.is_numeric(21), True) self.assertEqual(string_utils.is_numeric('21'), True) self.assertEqual(string_utils.is_numeric('true'), False) self.assertEqual(string_utils.is_numeric(True), False)