Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os # for deleting old files
- import time # for sleeps
- import json # for configuration
- import dbfread # for DBF files
- import datetime # for getting time
- import psycopg2 # for database
- import portalocker # for file locking
- # for watching changes in target directory
- from watchdog import observers, events
- # class for saving configuration - done
- class Configuration:
- def __init__(self):
- self.data_path = './data/'
- self.log_path = './log/'
- self.username = 'postgres'
- self.password = 'postgres'
- self.dbname = 'postgres'
- self.host = '127.0.0.1'
- self.count = 200
- self.interval = '7 days'
- def read(self):
- try:
- config = json.load(open('./settings.json'))
- if 'data_path' in config:
- self.data_path = config['data_path']
- if 'log_path' in config:
- self.log_path = config['log_path']
- if 'username' in config:
- self.username = config['username']
- if 'password' in config:
- self.password = config['password']
- if 'host' in config:
- self.host = config['host']
- if 'dbname' in config:
- self.dbname = config['dbname']
- if 'count' in config:
- self.count = config['count']
- if 'interval' in config:
- self.interval = config['interval']
- except IOError:
- print "<ERROR> Can't read configuration file!"
- # class for reading and sending records - done
- class Data:
- def __init__(self, config=Configuration()):
- self.data = []
- self.config = config
- def read_data(self, filename):
- global logger
- try:
- print "<FILE> Started a new file ..."
- for record in dbfread.DBF(self.config.data_path + filename):
- self.data.append(record)
- del_recs = dbfread.DBF(self.config.data_path + filename)
- del_recs.load()
- print "<FILE> Contains {} deleted records!".format(len(del_recs.deleted))
- logger.log("<FILE> Contains {} deleted records!".format(len(del_recs.deleted)))
- except:
- print "<ERROR> Error happened during reading file!"
- logger.log("<ERROR> Error happened during reading file!")
- def send_data(self):
- global logger
- try:
- buff = []
- for record in self.data: # surely can change it in python 3
- buff.append([record["_ID"].encode('utf-8'), record["_IN"].encode('utf-8'),
- record["_PID"].encode('utf-8'), record["_KEY"].encode('utf-8')
- , record["_VAL"].encode('utf-8')])
- connection_settings = "dbname='{}' user='{}' host='{}' password='{}'".format(self.config.dbname,
- self.config.username,
- self.config.host,
- self.config.password)
- connection = psycopg2.connect(connection_settings)
- cursor = connection.cursor()
- args_str = ','.join(cursor.mogrify("(%s,%s,%s,%s,%s)", rec) for rec in buff)
- cursor.execute("INSERT INTO store(id, in_s, pid_s, key_s, value_s) VALUES " + args_str)
- connection.commit()
- cursor.close()
- connection.close()
- print "<FILE> {} records were inserted!".format(len(buff))
- logger.log("<FILE> {} records were inserted!".format(len(buff)))
- except:
- print "<ERROR> Error happened during sending data!"
- logger.log("<ERROR> Error happened during sending data!")
- # class for logging messages - done
- class Logger:
- def __init__(self, config):
- self.log_path = config.log_path
- def log(self, message):
- try:
- log_name = datetime.datetime.now().date().__str__()
- log_file = open(self.log_path + log_name + '.txt', 'a+')
- log_file.write(message + '\n')
- log_file.close()
- except IOError:
- print "<Error> Can't open logs file!"
- return
- conf = Configuration()
- conf.read()
- logger = Logger(conf)
- print "<INFO> Settings: {}".format(conf.__dict__)
- logger.log("<INFO> Settings: {}".format(conf.__dict__))
- allowed_names = {
- "addstore.DBF",
- "addstore.DBF",
- "addstore.DBF",
- "addstore.DBF",
- "addstore.DBF",
- "addstore.DBF",
- "addstore.DBF",
- }
- # initial read (if files already exist) - done
- lock = portalocker.Lock(conf.data_path + "signal.dbf")
- while True:
- try:
- lock.acquire(fail_when_locked=False)
- break
- except IOError:
- time.sleep(1)
- data = Data(conf)
- data.read_data('addstore.dbf')
- data.send_data()
- try:
- os.remove(conf.data_path + 'addstore.dbf')
- except:
- print "<ERROR> No initial file!"
- logger.log("<ERROR> No initial file!")
- lock.release()
- # class for handling changes in directory - done
- class EventsHandler(events.FileSystemEventHandler):
- def on_created(self, event):
- global logger
- global conf
- file_name = os.path.basename(event.src_path)
- print "<FILE> Found {} in data_directory!".format(file_name)
- logger.log("<FILE> Found {} in data_directory!".format(file_name))
- if file_name in allowed_names:
- lock = portalocker.Lock(conf.data_path + "signal.dbf")
- while True:
- try:
- lock.acquire(fail_when_locked=False)
- break
- except IOError:
- time.sleep(1)
- data = Data(conf)
- data.read_data(file_name)
- data.send_data()
- try:
- os.remove(conf.data_path + file_name)
- except:
- print "<ERROR> Can't find {}!".format(file_name)
- logger.log("<ERROR> Can't find {}!".format(file_name))
- lock.release()
- eventsHandler = EventsHandler()
- observer = observers.Observer()
- observer.schedule(eventsHandler, conf.data_path, recursive=False)
- observer.start()
- try:
- while True:
- time.sleep(1)
- except KeyboardInterrupt:
- observer.stop()
- observer.join()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement