simple utility script to take a newline-separated list of facts and insert to database
for Facts.py, supporting the format (if you want to call it that) supported by FactFile.py
This commit is contained in:
parent
2b06c79d67
commit
6ebca3ff59
61
scripts/factfile-to-facts.py
Normal file
61
scripts/factfile-to-facts.py
Normal file
@ -0,0 +1,61 @@
|
||||
"""
|
||||
factfiles-to-facts - insert facts from flat file to database
|
||||
Copyright (C) 2010 Brian S. Stephan
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import getpass
|
||||
import os
|
||||
import sqlite3
|
||||
import socket
|
||||
import sys
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('-f', '--factfile')
|
||||
parser.add_argument('-c', '--category')
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.factfile == None or args.category == None:
|
||||
print('script needs both -c (category) and -f (filename)')
|
||||
sys.exit(2)
|
||||
|
||||
# database
|
||||
dbfile = 'dr.botzo.data'
|
||||
db = sqlite3.connect(dbfile)
|
||||
|
||||
# open file
|
||||
if os.path.isfile(args.factfile):
|
||||
with open(args.factfile, 'r') as file:
|
||||
lines = file.readlines()
|
||||
for line in lines:
|
||||
try:
|
||||
line = line.rstrip()
|
||||
print('inserting \'' + line + '\'')
|
||||
db.execute('''INSERT INTO facts_facts (category, fact, who, userhost)
|
||||
VALUES (?,?,?,?)''', (args.category, line.decode('utf-8', 'ignore'), getpass.getuser(), getpass.getuser() + '@' + socket.getfqdn()))
|
||||
except sqlite3.Error as e:
|
||||
db.rollback()
|
||||
print("sqlite error: " + str(e))
|
||||
sys.exit(2)
|
||||
|
||||
db.commit()
|
||||
else:
|
||||
print('filename \'' + whats[0] + '\' doesn\'t exist')
|
||||
|
||||
|
||||
# vi:tabstop=4:expandtab:autoindent
|
||||
# kate: indent-mode python;indent-width 4;replace-tabs on;
|
Loading…
Reference in New Issue
Block a user