Beispiel #1
0
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))
Beispiel #2
0
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.')
Beispiel #3
0
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))
Beispiel #4
0
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))
Beispiel #5
0
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)
Beispiel #6
0
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)
Beispiel #7
0
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.'
Beispiel #8
0
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.'
Beispiel #9
0
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,
Beispiel #10
0
def insert(filename): 
    """ Adding one file to the db """
    logger.info('Adding %s.' % filename)
    umm = xml2dict(filename)
    insert_one(umm)