def get_contents(path, host, username, password): """ Will return a list of the contents in a given path on a FTP-server. """ try: logger.info('Connecting to ftp://%s:%s@%s' % (username, password, host)) ftp = FTP(host, username, password) except Exception, exception: logger.error('Error connecting to %s: %s' % (host, exception))
def update(): """ Doing the actual updating """ logger.info('Updating the db...') last = get_last() logger.info('Starting at %s' % str(last)) filelist = get_contents(config.path, config.ftphost, config.username, config.password) filelist.sort() def func(item): itemnumber = int(item.split('.')[0]) if itemnumber <= last: return False else: return True filelist = filter(func, filelist) if len(filelist) > 0: logger.info('Will update %i items.' % len(filelist)) get_filelist(filelist, config.path, config.ftphost, config.username, config.password) logger.info('Done retrieving files. Now inserting to db.') chdir(config.tempdir) for item in listdir(config.tempdir): insert(item) else: logger.info('Nothing to update. Done.')
def get_filelist(files, path, host, username, password, localpath=''): """ Will retrieve a list of files from a specific path on a given host, and store it locally at some (optional) path """ try: logger.info('Connecting to ftp://%s:%s@%s' % (username, password, host)) ftp = FTP(host, username, password) except Exception, exception: logger.error('Error connecting to %s: %s' % (host, exception))
def get_file(filename, path, host, username, password, localpath=''): """ Will retrieve a specified file from a specific path on a given host, and store it a the local path. """ try: logger.info('Connecting to ftp://%s:%s@%s' % (username, password, host)) ftp = FTP(host, username, password) except Exception, exception: logger.error('Error connecting to %s: %s' % (host, exception))
def insert_path(path): """ Adding all files in the folder """ logger.info('Adding everything in %s.' % path) chdir(path) try: for item in listdir(path): insert(item) except OSError, exception: logger.error('Could not read files in path: %s' % exception)
def clean(): """ Erasing all temporary files """ logger.info('Cleaning the temporary directory.') chdir(config.tempdir) try: for item in listdir(config.tempdir): remove(item) except OSError, exception: logger.error('Could not delete files: %s' % exception)
def last(): """ Displays the last UMM that exists in the database. """ logger.info('Presenting information on last update.') umm = get_last(True) if umm is not None: messageid = umm['umm_number'] registered = umm['registered'] print 'Last UMM with id %i was registered %s.' % \ (messageid, str(registered)) else: print 'Empty database - no UMMs stored.'
def first(): """ Displays the first UMM that exists in the database. """ logger.info('Presenting the first UMM in the database.') umm = get_first() if umm is not None: messageid = umm['umm_number'] registered = umm['registered'] print 'First UMM with id %i was registered %s.' % \ (messageid, registered) else: print 'Empty database - no UMM stored.'
from ummreader.config import LOGGER as logger from ummreader.config import UmmConfig config = UmmConfig() __all__ = ['get_file', 'get_filelist', 'get_contents'] def get_file(filename, path, host, username, password, localpath=''): """ Will retrieve a specified file from a specific path on a given host, and store it a the local path. """ try: logger.info('Connecting to ftp://%s:%s@%s' % (username, password, host)) ftp = FTP(host, username, password) except Exception, exception: logger.error('Error connecting to %s: %s' % (host, exception)) logger.info('Changing path to %s on FTP server' % path) ftp.cwd(path) if exists(localpath): chdir(localpath) else: if not exists(config.tempdir): mkdir(config.tempdir) chdir(config.tempdir) logger.info('Retriving and storing %s.' % filename) with open(filename, 'w') as outfile: ftp.retrbinary('RETR %s' % filename, outfile.write) ftp.quit() outfile.close() def get_filelist(files, path, host, username, password, localpath=''): """ Will retrieve a list of files from a specific path on a given host,
def insert(filename): """ Adding one file to the db """ logger.info('Adding %s.' % filename) umm = xml2dict(filename) insert_one(umm)