I've been away from programming for a long time, but my desire to make something has reached its limit It doesn't make much sense to make it yourself It was going to be a good rehabilitation practice, so I started making it.
The things (planned) that we are planning to realize are as follows. -A list of files under a certain directory can be displayed (the contents can also be displayed). -Keyword (regular expression) search is possible -Display the result on GUI (Can I display it on a WEB browser?)
I haven't done the GUI part at all yet, but I was able to search. The composition is like this so far.
source file | function |
---|---|
AxMemoBrowser.py | Body. Call when exe is made |
PyCMemo.py | Class to implement the command equivalent around CUI |
PyCBrowser.py | Class that implements around GUI |
PyCSearchResult.py | Class for saving search results |
Implemented as follows. I'm new to it, so there are likely to be many better implementations.
AxMemoBrowser.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2014/11/26
@author: kuromame
'''
from PyCMemo import PyCMemo
#cmemo = PyCMemo("/Users/kuromame/Desktop");
cmemo = PyCMemo();
#cmemo.ReadDir();
cmemo.SearchString("variable");
PyCMemo.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2014/11/26
@author: kuromame
'''
#Import required modules
import os;
import sys;
import re;
import codecs;
class PyCMemo:
def __init__(self,root_dir=''):
#Initialization of class members
print('[PyCMemo.__init__]');
self.pwd = ''; #Root directory when searching
self.file_list = []; #List of searched files
self.pattern = []; #Search word(Regular expression possible).リスト形式で最後尾が最新のSearch word.
self.isRead = 0; #Flag for confirming whether you have searched even once
#Specify the current directory or the specified directory
if root_dir != '':
self.pwd = root_dir;
else:
self.pwd = '';
def ReadDir(self):
#Variable initialization
print('[PyCMemo.ReadDir]');
if self.pwd == '':
self.pwd = os.path.abspath(os.path.dirname(__file__));
print(self.pwd);
#Search under the current directory.
#Search from the top down.
for root, dirs, files in os.walk(self.pwd):
for file_ in files:
full_path = os.path.join(root, file_);
self.file_list.append(full_path);
print(full_path);
#Sort search results
self.file_list.sort();
#print(self.file_list);
def SearchString(self,pattern):
#Variable initialization
print('[PyCMemo.SearchString]');
self.pattern.append(pattern);
#Regular expression search engine initialization
ptn = re.compile(self.pattern[-1]);
#Call ReadDir if the file has never been read
if self.isRead == 0: self.ReadDir(),
self.isRead = 1;
#Recursively search for files
for fname in self.file_list:
print('=Start search=');
print(fname);
try:
fp = codecs.open(fname,'r','utf-8');
line_num = 1;
try:
for line in fp.readlines():
if ptn.search(line): print(fname,line_num,line),
line_num += 1;
#print(fname,line_num);
except UnicodeDecodeError:
print("Could not search due to an encoding error.")
finally:
#Do nothing unexpectedly.(Originally, it is necessary to devise such as throwing an exception)
pass
finally:
fp.close();
print('=Search end=')
By the way, from this time on, I started to put the code on GitHub in practice. https://github.com/ambitious-kuromame/AxMemoBrowser
Recommended Posts