@name 取文件指定尾行数 @param path 文件路径 @param num 取尾行数 @param p 当前页 @param search 搜索关键字 @return list
(self,path, num, p=1,search = None)
| 44 | return log_list |
| 45 | |
| 46 | def GetNumLines(self,path, num, p=1,search = None): |
| 47 | """ |
| 48 | @name 取文件指定尾行数 |
| 49 | @param path 文件路径 |
| 50 | @param num 取尾行数 |
| 51 | @param p 当前页 |
| 52 | @param search 搜索关键字 |
| 53 | @return list |
| 54 | """ |
| 55 | pyVersion = sys.version_info[0] |
| 56 | max_len = 1024 * 128 * 1024 |
| 57 | try: |
| 58 | from html import escape |
| 59 | if not os.path.exists(path): return "" |
| 60 | start_line = (p - 1) * num |
| 61 | count = start_line + num |
| 62 | fp = open(path, 'rb') |
| 63 | |
| 64 | buf = "" |
| 65 | fp.seek(-1, 2) |
| 66 | if fp.read(1) == "\n": fp.seek(-1, 2) |
| 67 | data = [] |
| 68 | total_len = 0 |
| 69 | b = True |
| 70 | n = 0 |
| 71 | |
| 72 | for i in range(count): |
| 73 | while True: |
| 74 | newline_pos = str.rfind(str(buf), "\n") |
| 75 | |
| 76 | pos = fp.tell() |
| 77 | if newline_pos != -1: |
| 78 | if n >= start_line: |
| 79 | line = buf[newline_pos + 1:] |
| 80 | |
| 81 | is_res = True |
| 82 | if search: |
| 83 | is_res = False |
| 84 | if line.find(search) >= 0 or re.search(search,line): |
| 85 | is_res = True |
| 86 | |
| 87 | if is_res: |
| 88 | line_len = len(line) |
| 89 | total_len += line_len |
| 90 | sp_len = total_len - max_len |
| 91 | if sp_len > 0: |
| 92 | line = line[sp_len:] |
| 93 | try: |
| 94 | data.insert(0, escape(line)) |
| 95 | except: |
| 96 | pass |
| 97 | buf = buf[:newline_pos] |
| 98 | n += 1 |
| 99 | break |
| 100 | else: |
| 101 | if pos == 0: |
| 102 | b = False |
| 103 | break |
no test coverage detected