Welcome, guest | Sign In | My Account | Store | Cart

Save your snippets/codes to sqlite3 database, search, edit, and delete.

Python, 172 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# Name: pySnipnix.py
# Author: pantuts
# Description: Saving your snippets to sqlite3 database.
# Agreement: You can use, modify, or redistribute this tool under
# the terms of GNU General Public License (GPLv3).
# This tool is for educational purposes only. Any damage you make will not affect the author.
# first run: python pySnipnix.py

#!/usr/bin/python

import argparse
import sqlite3
import re
import sys

# important, create the file
fileN = open('database.db', 'a+')

def main():
	
	# add all arguments needed
	# for argument that need FILE use [ type=argparse.FileType('r') ]
	parser = argparse.ArgumentParser(description=None, usage='python %(prog)s -h --help -f file -s search -a \'title\' \'code here\' -e id -d id -v --version')
	parser.add_argument('-f', metavar='filename', type=argparse.FileType('r'), dest='filename', help='File for database')
	parser.add_argument('-s', metavar='string', dest='search', help='Search for string in database')
	parser.add_argument('-a', metavar='string', dest='add', nargs=2, help='Add snippet. You should use \'\' for long string')
	parser.add_argument('-e', metavar='id', type=int, dest='edit', help='Edit snippet')
	parser.add_argument('-d', metavar='id', type=int, dest='delete', help='Delete from database')
	parser.add_argument('-S', dest='show', action='store_true', help='Show all records')
	parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0', help='Print version')
	
	# parse all arguments to 'args'
	args = parser.parse_args()
	
	# database connection
	conn = sqlite3.connect('database.db')
	cur = conn.cursor()
	
	def createTable():
		cmd = 'CREATE TABLE IF NOT EXISTS snippets (id INTEGER PRIMARY KEY AUTOINCREMENT, title VARCHAR(50), code VARCHAR NOT NULL)'
		cur.execute(cmd)
		conn.commit()
		
	def insertSnippets():
		# convert the string to lowercase and then execute
		lst = [args.add[0].lower() , args.add[1].lower()]
		cmd = 'INSERT INTO snippets VALUES (NULL, ?, ?)'
		cur.execute(cmd, lst)
		conn.commit()
		
		print('\nNew Snippet...')
		print('Snippet Title: \t%s' % args.add[0])
		print('Code: \t\t%s' % args.add[1])
		
	def editSnippets():
		cur.execute('SELECT * FROM snippets where id=%s' % str(args.edit))
		res = cur.fetchone()
		if res is None:
			print('No record to edit!')
		else:
			resl = [result for result in res]
			print('Current title >> ' + resl[1])
			ed1 = input('Title (Leave black, same title): ')
			print('Current snippet >> ' + resl[2])
			ed2 = input('Code: ')
			if ed1 is '':
				ed1 = resl[1]
			cur.executemany('UPDATE snippets SET title=\'%s\', code=\'%s\' WHERE id=?' % (ed1.lower(), ed2.lower()), str(resl[0]))
			conn.commit()
			print('Done!\n')
	
	def deleteSnippets():
		print('\nDeleting record with ID %s ...' % str(args.delete))
		
		# first find if record exists and return false if not found
		cur.execute('SELECT * FROM snippets where id=%s' % str(args.delete))
		res = cur.fetchone()
		if res is None:
			print('No record to delete!')
		else:
			cmd = 'DELETE FROM snippets where id=%s' % str(args.delete)
			cur.execute(cmd)
			conn.commit()
			print('Deleted!\n')
	
	def showOrSearch(cmd):
		# creating conn.create_function explanation: 1st(string to be used inside SQL), 2nd(count of arguments), 3rd(the function created)
		def matchPattern(pattern, columnName):
			pat = re.compile(pattern)
			return pat.search(columnName) is not None
		conn.create_function('matchPattern', 2, matchPattern)
		
		if cmd == 2:
			cur.execute('SELECT * FROM snippets WHERE matchPattern(\'%s\', title)' % str(args.search.lower()))
		else:
			cmd = 1
			cur.execute('SELECT * FROM snippets')
		res = cur.fetchall()
		# create empty dict, process filter keys and values
		s = {'id':{}, 'title':{}, 'code':{}}
		print('\nRecords result...')
		for result in res:
			s['id'] = result[0]
			s['title'] = result[1]
			s['code'] = result[2]
			print('[%s]\t[ %s ]---------->[ %s ]\n' % (s['id'], s['title'], s['code']))
		
	# invoke creation of table
	createTable()
	
	if len(sys.argv) < 1:
	    parser.print_help()
	
	# do filtering when -f 'filename' is correct
	if args.filename is not None:
		
		if args.show:
			if args.add or args.delete or args.search or args.edit:
				print('\nYou can\'t use other options with -S option')
				exit()
			else:
				args.add = None
				args.delete = None
				args.search = None
				args.edit = None
				showOrSearch(1)
		
		elif args.edit:
			if args.add or args.delete or args.search:
				print('\nYou can\'t use other options with -e option')
				exit()
			else:
				args.search = None
				args.add = None
				args.delete = None
				editSnippets()
		
		else:
			if args.add is None and args.delete is None and args.search is None:
				print('\n!!!!!You need to specify addional arguments to process the database!!!!!\n')
				parser.print_help()
			
			if args.search:
				args.edit = None			
				showOrSearch(2)
			
			if args.add:
				args.edit = None
				args.search = None				
				insertSnippets()
				
			if args.delete:
				args.search = None
				args.edit = None
				deleteSnippets()
				
	
	else:
		parser.print_help()
	
	# close our connection to the database
	conn.commit()
	conn.close()	
    
def by():
	print('\n[Script by: pantuts]')
	print('[email: pantuts@gmail.com]')

if __name__=="__main__":
	main()
	# close our file
	fileN.close()

	
Created by p@ntut$ on Fri, 12 Oct 2012 (GPL3)
Python recipes (4591)
p@ntut$'s recipes (7)

Required Modules

Other Information and Tasks

  • Licensed under the GPL 3
  • Viewed 6830 times
  • Revision 5 (updated 9 years ago)