当前位置: 首页>>代码示例>>Python>>正文


Python HydroShare.addResourceFile方法代码示例

本文整理汇总了Python中hs_restclient.HydroShare.addResourceFile方法的典型用法代码示例。如果您正苦于以下问题:Python HydroShare.addResourceFile方法的具体用法?Python HydroShare.addResourceFile怎么用?Python HydroShare.addResourceFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在hs_restclient.HydroShare的用法示例。


在下文中一共展示了HydroShare.addResourceFile方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_create_get_delete_resource_file

# 需要导入模块: from hs_restclient import HydroShare [as 别名]
# 或者: from hs_restclient.HydroShare import addResourceFile [as 别名]
    def test_create_get_delete_resource_file(self):
        hs = HydroShare()
        # Add
        res_id = '511debf8858a4ea081f78d66870da76c'
        fpath = 'mocks/data/another_resource_file.txt'
        fname = os.path.basename(fpath)
        resp = hs.addResourceFile(res_id, fpath)
        self.assertEqual(resp, res_id)

        # Get
        tmpdir = tempfile.mkdtemp()
        res_file = hs.getResourceFile(res_id, fname, destination=tmpdir)
        self.assertTrue(filecmp.cmp(res_file, fpath, shallow=False))
        shutil.rmtree(tmpdir)

        # Delete
        delres = hs.deleteResourceFile(res_id, fname)
        self.assertEqual(delres, res_id)
开发者ID:mtpain,项目名称:hs_restclient,代码行数:20,代码来源:test_hs_restclient.py

示例2: hydroshare

# 需要导入模块: from hs_restclient import HydroShare [as 别名]
# 或者: from hs_restclient.HydroShare import addResourceFile [as 别名]
class hydroshare():
    def __init__(self, username=None, password=None, cache=True):
        self.hs = None
        self.content = {}

        # connect to hydroshare using OAUTH2
        authfile = os.path.expanduser("~/.hs_auth")
        if os.path.exists(authfile):
            with open(authfile, 'rb') as f:
                token, cid = pickle.load(f)
            auth = HydroShareAuthOAuth2(cid, '', token=token)
        else:
            # connect to hydroshare using Basic Authentication
            self.cache = cache
            if cache:
                utilities.load_environment(os.path.join(
                                           os.environ['NOTEBOOK_HOME'],
                                           '.env'))

            self.auth_path = os.environ.get('NOTEBOOK_HOME',
                                            '/home/jovyan/.auth')

            uname = username
            if uname is None:
                if 'HS_USR_NAME' in os.environ.keys():
                    uname = os.environ['HS_USR_NAME']

            if password is None:
                # get a secure connection to hydroshare
                auth = self.getSecureConnection(uname)
            else:
                print('WARNING: THIS IS NOT A SECURE METHOD OF CONNECTING TO '
                      'HYDROSHARE...AVOID TYPING CREDENTIALS AS PLAIN TEXT')
                auth = HydroShareAuthBasic(username=uname, password=password)

        try:
            self.hs = HydroShare(auth=auth)
            self.hs.getUserInfo()
            print('Successfully established a connection with HydroShare')

        except HydroShareHTTPException as e:
            print('Failed to establish a connection with HydroShare.\n  '
                  'Please check that you provided the correct credentials.\n'
                  '%s' % e)
            # remove the cached authentication
            if os.path.exists(self.auth_path):
                os.remove(self.auth_path)
            return None

        # set the HS resource download directory
        download_dir = os.environ.get('JUPYTER_DOWNLOADS', 'Downloads')
        if not os.path.isdir(download_dir):
            os.makedirs(download_dir)
        self.download_dir = download_dir

    def _addContentToExistingResource(self, resid, content_files):

        for f in content_files:
            self.hs.addResourceFile(resid, f)

    def getSecureConnection(self, username=None):
        """Establishes a secure connection with hydroshare.

        args:
        -- email: email address associated with hydroshare

        returns:
        -- hydroshare api connection
        """

        if not os.path.exists(self.auth_path):
            print('\nThe hs_utils library requires a secure connection to '
                  'your HydroShare account.')
            if username is None:
                username = input('Please enter your HydroShare username: ') \
                        .strip()
            p = getpass.getpass('Enter the HydroShare password for user '
                                '\'%s\': ' % username)
            auth = HydroShareAuthBasic(username=username, password=p)

            if self.cache:
                with open(self.auth_path, 'wb') as f:
                    pickle.dump(auth, f, protocol=2)

        else:

            with open(self.auth_path, 'rb') as f:
                auth = pickle.load(f)

        return auth

    def getResourceMetadata(self, resid):
        """Gets metadata for a specified resource.

        args:
        -- resid: hydroshare resource id

        returns:
        -- resource metadata object
        """
#.........这里部分代码省略.........
开发者ID:hydroshare,项目名称:hydroshare-jupyterhub,代码行数:103,代码来源:hydroshare.py

示例3: upload_to_hs

# 需要导入模块: from hs_restclient import HydroShare [as 别名]
# 或者: from hs_restclient.HydroShare import addResourceFile [as 别名]
def upload_to_hs(id,file):
    auth = HydroShareAuthBasic(username='mbayles2', password='lego2695')
    hs = HydroShare(auth=auth)
    fpath = '/path/to/somefile.txt'
    resource_id = hs.addResourceFile('id', file)
开发者ID:mmbayles,项目名称:TimeSeries-Converter,代码行数:7,代码来源:controllers.py

示例4: post_hs

# 需要导入模块: from hs_restclient import HydroShare [as 别名]
# 或者: from hs_restclient.HydroShare import addResourceFile [as 别名]
def post_hs(ctx, username, password, modelrun_dir, include_shear_nc,
            resource_title, keyword):
    """Post the model run data to HydroShare"""
    # iterate over files and folders of interest, adding files to resource
    # RipCAS files are the vegetation .asc; TODO XXX include base RipCAS XXX TODO

    export_dir = os.path.join(modelrun_dir, 'export')
    veg_export_dir = os.path.join(export_dir, 'vegetation')

    if os.path.isdir(export_dir):
        shutil.rmtree(export_dir)

    os.mkdir(export_dir)
    os.mkdir(veg_export_dir)

    veg_pattern = os.path.join(modelrun_dir, 'ripcas-*', 'vegetation.asc')
    for tstep, veg_map in enumerate(glob.glob(veg_pattern)):

        veg_map_path = os.path.join(
            export_dir, 'vegetation', 'vegetation-%s.asc' % tstep
        )
        shutil.copy(veg_map, veg_map_path)

    shutil.make_archive(veg_export_dir, 'zip', veg_export_dir)

    # connect
    hs = HydroShare(
        auth=HydroShareAuthBasic(username=username, password=password)
    )

    # create new resource
    rtype = 'GenericResource'

    r_id = hs.createResource(
       rtype, resource_title, keywords=keyword  # , abstract=abstract
    )

    print "adding vegmap archive file %s to resource %s" % (veg_map_path, r_id)
    hs.addResourceFile(r_id, os.path.join(export_dir, 'vegetation.zip'))

    inputs_dir = os.path.join(modelrun_dir, 'inputs')
    inputs_export_basename = os.path.join(export_dir, 'inputs')
    shutil.make_archive(inputs_export_basename, 'zip', inputs_dir)


    print "adding inputs archive file %s to resource %s" % (veg_map_path, r_id)
    hs.addResourceFile(
        r_id, os.path.join(export_dir, 'inputs.zip')
    )

    shear_export_dir = os.path.join(export_dir, 'shear')
    os.mkdir(shear_export_dir)

    shear_pattern = os.path.join(modelrun_dir, 'dflow-*', 'shear_out.asc')
    for tstep, shear_map in enumerate(glob.glob(shear_pattern)):

        shear_map_path = os.path.join(
            export_dir, 'shear', 'shear-%s.asc' % tstep
        )
        shutil.copy(shear_map, shear_map_path)

    shutil.make_archive(shear_export_dir, 'zip', shear_export_dir)

    print "adding shear archive file %s to resource %s" % (veg_map_path, r_id)
    hs.addResourceFile(
        r_id, os.path.join(export_dir, 'shear.zip')
    )
开发者ID:VirtualWatershed,项目名称:CoRD,代码行数:69,代码来源:cord.py


注:本文中的hs_restclient.HydroShare.addResourceFile方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。