def show(self, not_less, noncolor, c): """ Shows (print/save to html) one hit. * input: - show_hash **is not used** - less: True/False show only first line of a hit, dont show path file:/// - noncolor: True/False use color to show a path and a filename - www_browser: save the result as a html file and open it in www_browser * output: - '\tfile:///home/magnus/Dropbox/pdf\n\tfile:///home/magnus/Dropbox/pdf/XXXXXXX.pdf' or html code for opening in www_browser """ #if not self.is_empty: # out = '\t [' + self.filetype + '] ' + self.id + ") \ # file://"+self.path.replace(' ','\ ')+"" #out = '\t [' + self.filetype + '] ' + self.id + ") \ # file://"+self.path.replace(' ','%20')+"" if noncolor: print '\t\'' + path.dirname(self.path) + \ '/' + path.basename(self.path) + "'" else: print_red_and_blue( str(c) + '\t\'' + path.dirname(self.path) + '/', '' + path.basename(self.path) + "'") # hack #1 out = '' dir_file = True if dir_file: out = "\tfile://" + \ path.dirname(self.path).strip().replace(' ', '%20') + "\n" #out = '\t [' + self.filetype + '] ' + \ #self.id + ") " + '' + " \t\tfile://"+ #self.path.replace(' ','%20')+"" out += "\tfile://" + self.path.replace(' ', '%20') + "" # @todo #if show_hash: # out += '\n\t' + hash_file(self.path)[0] #out = '\t [' + self.filetype + '] ' + self.id + ") \ # file:'//"+self.path+"'" # NO #out = '\t [' + self.filetype + '] ' + self.id + ") \ # 'file://"+self.path+"'" # NO if not_less: print out return out.replace('file://', '').replace('%20', ' ')
def show(self, not_less, noncolor, c): """ Shows (print/save to html) one hit. * input: - show_hash **is not used** - less: True/False show only first line of a hit, dont show path file:/// - noncolor: True/False use color to show a path and a filename - www_browser: save the result as a html file and open it in www_browser * output: - '\tfile:///home/magnus/Dropbox/pdf\n\tfile:///home/magnus/Dropbox/pdf/XXXXXXX.pdf' or html code for opening in www_browser """ #if not self.is_empty: # out = '\t [' + self.filetype + '] ' + self.id + ") \ # file://"+self.path.replace(' ','\ ')+"" #out = '\t [' + self.filetype + '] ' + self.id + ") \ # file://"+self.path.replace(' ','%20')+"" if noncolor: print '\t\'' + path.dirname(self.path) + \ '/' + path.basename(self.path) + "'" else: print_red_and_blue(str(c) + '\t\'' + path.dirname(self.path) + '/', '' + path.basename(self.path) + "'") # hack #1 out = '' dir_file = True if dir_file: out = "\tfile://" + \ path.dirname(self.path).strip().replace(' ', '%20') + "\n" #out = '\t [' + self.filetype + '] ' + \ #self.id + ") " + '' + " \t\tfile://"+ #self.path.replace(' ','%20')+"" out += "\tfile://" + self.path.replace(' ', '%20') + "" # @todo #if show_hash: # out += '\n\t' + hash_file(self.path)[0] #out = '\t [' + self.filetype + '] ' + self.id + ") \ # file:'//"+self.path+"'" # NO #out = '\t [' + self.filetype + '] ' + self.id + ") \ # 'file://"+self.path+"'" # NO if not_less: print out return out.replace('file://', '').replace('%20', ' ')
def search(self, arguments, opt): """ This searches :-) This function is way too big and should be split up (it has 267 lines and up to 45 local variables!) * input: - arguments & opt based on cmd input by user """ list_with_action = True # if x 2 related to firefox's bookmarks if opt.bookmarks: # sqlite3 is required! import sqlite3 conn = sqlite3.connect(FF_SQLITE_DATABASE) c = conn.cursor() c.execute("select title, url from moz_places;") conn.commit() results = c.fetchall() if 1: for r in results: title = r[0] if title: pass else: title = '' line = title + r[1] # a stupid way word = arguments[0] try: word2 = arguments[1] except IndexError: word2 = '' if compile(word, I).search(line) \ and compile(word2, I).search(line): print_red_and_blue(title, ' ' + r[1]) exit(1) if opt.bookmarks_folder: # dirty but works # django required! try: from django.conf import settings except ImportError: print 'ImportError: Install python-django to run this feature: sudo apt-get install python-django or sudo pip install django' exit(1) DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': FF_SQLITE_DATABASE, 'HOST': '', 'PORT': '', } } settings.configure(DATABASES=DATABASES) from orm.models import MozBookmarks show_path = False phrase = argv[2].strip() print 'phrase:', phrase, '\n' bookmarks = MozBookmarks.objects.all() c = 0 for b in bookmarks: if b.fk is None and b.title != '': # b.fk = has bookmarks try: path = b.title.strip() except AttributeError: print '-- b.title:', b.title while b.parent != 1: parent = MozBookmarks.objects.get(id=b.parent) path = parent.title.strip() + '/' + path b = parent if show_path: print path if path.lower().find(phrase.lower()) > -1: print path c += 1 exit(1) if opt.invert: arguments.reverse() # main search body words = '*' + '*'.join(arguments) + '*' # '*a*b*c*' words_rex = '.*' + '.*'.join(arguments) + '.*' # '.*a.*b*.c*' if opt.global_search: places = PLACES_GLOBAL else: places = PLACES_LOCAL places.append(get_hostname()) if opt.grep_here: places = ['current dir'] status = 'grepping the current directory' cmd = GREP_CMD + " '" + words.replace('*', '') + "' *" if opt.verbose: print 'cmd: ', cmd out = getoutput(cmd).strip() for l in out.split('\n'): if l.find(':') > -1: try: filename, text = l.split(':') filename += ':' except: items = l.split(':') filename = items[0] text = ':'.join(items[1:]) print_red_and_blue(filename, text) elif l.find('-') > -1: try: filename, text = l.split('-') filename += '-' except: items = l.split('-') filename = items[0] text = '-'.join(items[1:]) print_red_and_blue(filename, text) else: print l exit(1) if opt.wholename: # or basename wholename_or_basename = ' -w ' else: wholename_or_basename = ' -b ' if opt.find_dir: places = ['find directories@' + get_hostname()] if opt.find_find: places = ['find@' + get_hostname()] if opt.find_tu: places = ['find here -t tu@' + get_hostname()] # reset file paths_text_file = open('/home/' + getuser() + '/.mmfinder-paths', 'w') paths_text_file_to_open = open( '/home/' + getuser() + '/.mmfinder-paths-to-open', 'w') mmterminalpathtext = '' mmterminalpathtext_to_open = '' html_hits = '' c = 1 for p in places: hr_text(p + '...') html_hits += '#' + p + '\n' status = '' if opt.pdf_find: status = 'pdf searching...' cmd = "locate -d " + PATH_DB + p + '.db' + " " + \ wholename_or_basename + " -i -r '" + words_rex + "pdf" elif opt.document_find: status = 'document searching...' extensions = '$|'.join(EXTENSIONS_OF_DOCUMENTS) cmd = "locate -d " + PATH_DB + p + '.db' + " " + \ wholename_or_basename + " -i --regex '" + \ words_rex + ".*(" + extensions + ")'" elif opt.find_media: status = 'document searching...' extensions = '$|'.join(EXTENSIONS_OF_MEDIA) cmd = "locate -d " + PATH_DB + p + '.db' + " " + \ wholename_or_basename + " -i --regex '" + \ words_rex + ".*(" + extensions + ")'" elif opt.rex: status = 'rex searching...' word = arguments[0] # <--- !!! cmd = "locate -d " + PATH_DB + p + '.db' + " " + \ wholename_or_basename + " -i --regex '" + word + "'" elif opt.find_tu: status = 'finding here (tutaj)...' cmd = "find '" + getcwd() + "' -iname '" + words + "'" elif opt.find_find: status = 'finding /home/...' cmd = "find ~ -iname '" + words + "'" elif opt.find_dir: status = 'finding a dir /...' cmd = "find ~ -iname '" + words + "' -type d" # very slow :-( if False: if word.startswith('^'): word_without = word.replace('^', '') cmd = "locate -d " + PATH_DB + p + '.db' + \ " -e -i -r '/" + word_without + \ "*' | xarguments file" #locate -r '/*python2.7*/' | less else: cmd = "locate -d " + PATH_DB + p + '.db' + \ " -e -i -r '/*" + word + "*/'" \ #locate -r '/*python2.7*/' | less else: status = 'basic search...' cmd = "locate -d " + PATH_DB + p + '.db' + \ " " + wholename_or_basename + " -i '" + words + "'" if opt.un_grep: cmd = cmd + " | grep -v '" + opt.un_grep + "'" if opt.verbose: print '# status:', status print '# cmd', cmd # execute! out = getoutput(cmd).strip() if opt.dev: hr_text('dev::out') print out hr() if out and list_with_action: for item in out.strip().split('\n'): # @@ BUG @@ id = IDS[c] id = 'a' # TO FIX h = Hit(id, item) self.items.append(h) h.check_filetype() #print i.is_dir #if i.is_dir and find_dir: # i.show() #print 'x' #if not find_dir: hit = h.show(opt.not_less, opt.noncolor, c) if opt.www_browser: html_hits += hit + '\n' else: mmterminalpathtext += hit.split('\n')[0].strip() \ + ' #' + str(c) + '\n' mmterminalpathtext_to_open += hit.split('\n')[1].strip() \ + ' #' + str(c) + '\n' c += 1 print # and out becuase don't 'press key' for empty outputs if opt.key and out: raw_input('[press key]') if opt.www_browser: html_text = '<pre>' html_text += html_hits.replace('\n', '</br>') html_text += '<pre>' fn = HTML_FN f = open(fn, 'w') f.write(html_text) f.close() cmd = HTML_CMD + ' ' + fn print cmd system(cmd) paths_text_file.write(mmterminalpathtext) paths_text_file_to_open.write(mmterminalpathtext_to_open) paths_text_file.close()
def search(self, arguments, opt): """ This searches :-) This function is way too big and should be split up (it has 267 lines and up to 45 local variables!) * input: - arguments & opt based on cmd input by user """ list_with_action = True # if x 2 related to firefox's bookmarks if opt.bookmarks: # sqlite3 is required! import sqlite3 conn = sqlite3.connect(FF_SQLITE_DATABASE) c = conn.cursor() c.execute("select title, url from moz_places;") conn.commit() results = c.fetchall() if 1: for r in results: title = r[0] if title: pass else: title = '' line = title + r[1] # a stupid way word = arguments[0] try: word2 = arguments[1] except IndexError: word2 = '' if compile(word, I).search(line) \ and compile(word2, I).search(line): print_red_and_blue(title, ' ' + r[1]) exit(1) if opt.bookmarks_folder: # dirty but works # django required! try: from django.conf import settings except ImportError: print 'ImportError: Install python-django to run this feature: sudo apt-get install python-django or sudo pip install django' exit(1) DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': FF_SQLITE_DATABASE, 'HOST': '', 'PORT': '', } } settings.configure(DATABASES=DATABASES) from orm.models import MozBookmarks show_path = False phrase = argv[2].strip() print 'phrase:', phrase, '\n' bookmarks = MozBookmarks.objects.all() c = 0 for b in bookmarks: if b.fk is None and b.title != '': # b.fk = has bookmarks try: path = b.title.strip() except AttributeError: print '-- b.title:', b.title while b.parent != 1: parent = MozBookmarks.objects.get(id=b.parent) path = parent.title.strip() + '/' + path b = parent if show_path: print path if path.lower().find(phrase.lower()) > -1: print path c += 1 exit(1) if opt.invert: arguments.reverse() # main search body words = '*' + '*'.join(arguments) + '*' # '*a*b*c*' words_rex = '.*' + '.*'.join(arguments) + '.*' # '.*a.*b*.c*' if opt.global_search: places = PLACES_GLOBAL else: places = PLACES_LOCAL places.append(get_hostname()) if opt.grep_here: places = ['current dir'] status = 'grepping the current directory' cmd = GREP_CMD + " '" + words.replace('*', '') + "' *" if opt.verbose: print 'cmd: ', cmd out = getoutput(cmd).strip() for l in out.split('\n'): if l.find(':') > -1: try: filename, text = l.split(':') filename += ':' except: items = l.split(':') filename = items[0] text = ':'.join(items[1:]) print_red_and_blue(filename, text) elif l.find('-') > -1: try: filename, text = l.split('-') filename += '-' except: items = l.split('-') filename = items[0] text = '-'.join(items[1:]) print_red_and_blue(filename, text) else: print l exit(1) if opt.wholename: # or basename wholename_or_basename = ' -w ' else: wholename_or_basename = ' -b ' if opt.find_dir: places = ['find directories@' + get_hostname()] if opt.find_find: places = ['find@' + get_hostname()] if opt.find_tu: places = ['find here -t tu@' + get_hostname()] # reset file paths_text_file = open('/home/' + getuser() + '/.mmfinder-paths', 'w') paths_text_file_to_open = open('/home/' + getuser() + '/.mmfinder-paths-to-open', 'w') mmterminalpathtext = '' mmterminalpathtext_to_open = '' html_hits = '' c = 1 for p in places: hr_text(p + '...') html_hits += '#' + p + '\n' status = '' if opt.pdf_find: status = 'pdf searching...' cmd = "locate -d " + PATH_DB + p + '.db' + " " + \ wholename_or_basename + " -i -r '" + words_rex + "pdf" elif opt.document_find: status = 'document searching...' extensions = '$|'.join(EXTENSIONS_OF_DOCUMENTS) cmd = "locate -d " + PATH_DB + p + '.db' + " " + \ wholename_or_basename + " -i --regex '" + \ words_rex + ".*(" + extensions + ")'" elif opt.find_media: status = 'document searching...' extensions = '$|'.join(EXTENSIONS_OF_MEDIA) cmd = "locate -d " + PATH_DB + p + '.db' + " " + \ wholename_or_basename + " -i --regex '" + \ words_rex + ".*(" + extensions + ")'" elif opt.rex: status = 'rex searching...' word = arguments[0] # <--- !!! cmd = "locate -d " + PATH_DB + p + '.db' + " " + \ wholename_or_basename + " -i --regex '" + word + "'" elif opt.find_tu: status = 'finding here (tutaj)...' cmd = "find '" + getcwd() + "' -iname '" + words + "'" elif opt.find_find: status = 'finding /home/...' cmd = "find ~ -iname '" + words + "'" elif opt.find_dir: status = 'finding a dir /...' cmd = "find ~ -iname '" + words + "' -type d" # very slow :-( if False: if word.startswith('^'): word_without = word.replace('^', '') cmd = "locate -d " + PATH_DB + p + '.db' + \ " -e -i -r '/" + word_without + \ "*' | xarguments file" #locate -r '/*python2.7*/' | less else: cmd = "locate -d " + PATH_DB + p + '.db' + \ " -e -i -r '/*" + word + "*/'" \ #locate -r '/*python2.7*/' | less else: status = 'basic search...' cmd = "locate -d " + PATH_DB + p + '.db' + \ " " + wholename_or_basename + " -i '" + words + "'" if opt.un_grep: cmd = cmd + " | grep -v '" + opt.un_grep + "'" if opt.verbose: print '# status:', status print '# cmd', cmd # execute! out = getoutput(cmd).strip() if opt.dev: hr_text('dev::out') print out hr() if out and list_with_action: for item in out.strip().split('\n'): # @@ BUG @@ id = IDS[c] id = 'a' # TO FIX h = Hit(id, item) self.items.append(h) h.check_filetype() #print i.is_dir #if i.is_dir and find_dir: # i.show() #print 'x' #if not find_dir: hit = h.show(opt.not_less, opt.noncolor, c) if opt.www_browser: html_hits += hit + '\n' else: mmterminalpathtext += hit.split('\n')[0].strip() \ + ' #' + str(c) + '\n' mmterminalpathtext_to_open += hit.split('\n')[1].strip() \ + ' #' + str(c) + '\n' c += 1 print # and out becuase don't 'press key' for empty outputs if opt.key and out: raw_input('[press key]') if opt.www_browser: html_text = '<pre>' html_text += html_hits.replace('\n', '</br>') html_text += '<pre>' fn = HTML_FN f = open(fn, 'w') f.write(html_text) f.close() cmd = HTML_CMD + ' ' + fn print cmd system(cmd) paths_text_file.write(mmterminalpathtext) paths_text_file_to_open.write(mmterminalpathtext_to_open) paths_text_file.close()