def upload_asset(auth, config):
    """
    Checks if a local asset has been uploaded before and if not,
    performs an upload operation on the single asset.
    
    :Args:
        - auth (:class:`.Credentials`): instance of the Credentials class
          as returned by authentication()
        - config (:class:`.Configuration`): instance of the Configuration
          class as returned by logging_mode()
    """
    
    asset_mgr = FileManager(auth, cfg=config)
    asset_to_upload = asset_mgr.file_from_path(ASSET)

    if asset_to_upload.is_uploaded():
        print("{0} already uploaded.".format(asset_to_upload))
        return

    else:
        conf = "{0} MBs to be uploaded. Continue? (yes/no) ".format(
            len(asset_to_upload)/1024/1024)

        if input(conf)[0].lower() == 'y':
            try:
                asset_to_upload.upload()
                print("{0} uploaded successfully.".format(asset_to_upload))

            except Exception as e:
                print("Upload failed: {0}".format(e))

        else:
            print("Upload aborted.")
            return
def upload_assets(auth, config):
    """
    Uploads a specified collection of assets either from a set created
    from scratch, or from the contents of existing directory.
    
    :Args:
        - auth (:class:`.Credentials`): instance of the Credentials class
          as returned by authentication()
        - config (:class:`.Configuration`): instance of the Configuration
          class as returned by logging_mode()
    """
    asset_mgr = FileManager(auth, cfg=config)
    asset_to_add = asset_mgr.file_from_path(ASSET)

    # Creates FileCollection object
    asset_set = asset_mgr.create_file_set(asset_to_add)

    # Extends a FileCollection object with another FileCollection
    asset_set.extend(asset_mgr.files_from_dir(ASSET_DIR))

    try:
        # force=true uploads the assets in the asset collection regardless of
        # whether they have been uploaded before.
        asset_set.upload(force=True)
        print("Assets uploaded successfully.")

    except RestCallException as e:
        print("failed: {0}".format(e))
    def test_filemgr_file_from_path(self, mock_file, mock_api, mock_creds,
                                    mock_cfg):
        """Test file_from_path"""

        mgr = FileManager(mock_creds, cfg=mock_cfg)
        ufile = mgr.file_from_path("c:\\test.txt")
        mock_file.assert_called_with(mock.ANY, "c:\\test.txt")
        self.assertIsNotNone(ufile)

        ufile = mgr.file_from_path(None)
        mock_file.assert_called_with(mock.ANY, 'None')
        self.assertIsNotNone(ufile)

        ufile = mgr.file_from_path(42)
        mock_file.assert_called_with(mock.ANY, "42")
        self.assertIsNotNone(ufile)
    def test_filemgr_file_from_path(self,
                                    mock_file,
                                    mock_api,
                                    mock_creds,
                                    mock_cfg):
        """Test file_from_path"""

        mgr = FileManager(mock_creds, cfg=mock_cfg)
        ufile = mgr.file_from_path("c:\\test.txt")
        mock_file.assert_called_with(mock.ANY, "c:\\test.txt")
        self.assertIsNotNone(ufile)

        ufile = mgr.file_from_path(None)
        mock_file.assert_called_with(mock.ANY, 'None')
        self.assertIsNotNone(ufile)

        ufile = mgr.file_from_path(42)
        mock_file.assert_called_with(mock.ANY, "42")
        self.assertIsNotNone(ufile)