def test_upload_delete(self):
     cloud_store = DropboxCloudstore(
         config=self.configuration.get_cloudstores()[0])
     # Upload file
     logger.info('Uploading file..')
     file_to_upload = open(
         os.path.join(self.test_data_path, self.file_name), 'rb').read()
     cloud_store.upload_file(file_to_upload, '/tests/' + self.file_name)
     # Check if it was uploaded
     self.assertIn(self.file_name, cloud_store.ls('/tests/').keys())
     # Delete it
     cloud_store.delete_file('/tests/' + self.file_name)
     # Check if it was deleted
     self.assertNotIn(self.file_name, cloud_store.ls('/tests/').keys())
Example #2
0
def main():
    """
    :Example:
    python main.py -m run_mode_1
                   -c confs/template_conf.yml
                   -l logs/output.log
    """

    # Initializing
    args = _argparser()
    _setup_log(args.log, args.debug)
    logger.info("Starting in run mode: {0}".format(args.run_mode))
    # Load the configuration
    configuration = Configuration(config_src=args.config_file)
    # Init the Cloudstore
    cloud_store = DropboxCloudstore(config=configuration.get_cloudstores()[0])
    # Init the Datastore
    data_store = MySqlDatastore(**configuration.get_datastores()[0])
    # Init the Email App
    gmail_configuration = configuration.get_email_apps()[0]
    gmail_app = GmailEmailApp(config=configuration.get_email_apps()[0])

    # Mysql examples
    logger.info("\n\nMYSQL EXAMPLE\n-------------------------")
    logger.info("\n\nTables in current DB: {0}".format(
        list(data_store.show_tables())))
    logger.info("Creating Table: orders")
    table_schema = """ order_id INT(6) PRIMARY KEY,
                       order_type VARCHAR(30) NOT NULL,
                       location VARCHAR(30) NOT NULL """
    data_store.create_table(table='orders', schema=table_schema)
    logger.info("Tables in current DB:\n{0}".format(
        list(data_store.show_tables())))
    logger.info("Inserting into orders the values:\n(1 simple newyork)..")
    insert_data = {
        "order_id": 1,
        "order_type": "plain",
        "location": "new_york"
    }
    data_store.insert_into_table(table='orders', data=insert_data)
    logger.info("SELECT * FROM orders;\n{0}".format(
        data_store.select_from_table(table='orders')))
    logger.info("Deleting the inserted row from table orders..")
    data_store.delete_from_table(table='orders', where='order_id=1')
    logger.info("SELECT * FROM orders;\n{0}".format(
        data_store.select_from_table(table='orders')))
    logger.info("Dropping Table: orders")
    data_store.drop_table(table='orders')
    logger.info("Tables in current DB:\n{0}".format(
        list(data_store.show_tables())))

    # Dropbox examples
    logger.info("\n\nDROPBOX EXAMPLE\n-------------------------")
    logger.info("List of files in Dropbox /python_template:\n{0}".format(
        list(cloud_store.ls(path='/python_template').keys())))
    upload_path = "/python_template/file1.txt"
    file_content = "test file content"
    logger.info("Uploading file {file} with content:\n{content}".format(
        file=upload_path, content=file_content))
    cloud_store.upload_file(file_bytes=file_content.encode(),
                            upload_path=upload_path)
    logger.info("List of files in Dropbox /python_template:\n{0}".format(
        list(cloud_store.ls(path='/python_template').keys())))
    downloaded_file = cloud_store.download_file(frompath=upload_path)
    logger.info(
        "Downloaded file and its content is:\n{0}".format(downloaded_file))
    cloud_store.delete_file(file_path=upload_path)
    logger.info("Deleting file {file}..".format(file=upload_path))
    logger.info("List of files in Dropbox /python_template:\n{0}".format(
        list(cloud_store.ls(path='/python_template').keys())))

    # Gmail examples
    logger.info("\n\nGMAIL EXAMPLE\n-------------------------")
    subject = "Email example"
    body = "<h1>This is an html body example</h1><br><b>This goes to the html argument. " \
           "You can use the text argument for plain text.</b>"
    emails_list = [gmail_configuration['email_address']]
    attachments_paths = [os.path.join('data', 'sample_data.txt')]
    logger.info(
        "Sending email with `subject` = `{subject}`, `from,to,cc,bcc,reply_to` = `{email_addr}`, "
        "`html` = `{body}` and `attachments` = `{attachments}`".format(
            subject=subject,
            email_addr=emails_list[0],
            body=body,
            attachments=attachments_paths))
    gmail_app.send_email(subject=subject,
                         to=emails_list,
                         cc=emails_list,
                         bcc=emails_list,
                         html=body,
                         attachments=attachments_paths,
                         sender=emails_list[0],
                         reply_to=emails_list[0])
 def tearDownClass(cls):
     cloud_store = DropboxCloudstore(
         config=cls.configuration.get_cloudstores()[0])
     cloud_store.delete_file('/tests')