Skip to content

Instantly share code, notes, and snippets.

@aficionado
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aficionado/df6cbd3db5d4dd53a378 to your computer and use it in GitHub Desktop.
Save aficionado/df6cbd3db5d4dd53a378 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Moves a resource to a project
"""
##############################################################################
# Copyright (c) 2015 BigML, Inc
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
##############################################################################
import sys
import os
import argparse
from bigml.api import BigML
from bigml.resourcehandler import RESOURCE_RE
def get_resource_type(resource):
"""Returns the associated resource type for a resource
"""
if not isinstance(resource, basestring):
raise ValueError("Failed to parse a resource string or structure.")
for resource_type, resource_re in RESOURCE_RE.items():
if resource_re.match(resource):
return resource_type
return None
def main(args=sys.argv[1:]):
"""Parses command-line parameters and calls the actual main function.
"""
parser = argparse.ArgumentParser(
description="Moves a resource to a project",
epilog="BigML, Inc")
# resource
parser.add_argument('--resource',
required=True,
action='store',
dest='resource',
help="Resource")
# project
parser.add_argument('--project',
required=True,
action='store',
dest='project',
help="Target project")
# BigML username
parser.add_argument('--username',
required=False,
action='store',
dest='username',
help="BigML username")
# BigML API key
parser.add_argument('--api_key',
required=False,
action='store',
dest='api_key',
help="BigML API key")
# BigML domain
parser.add_argument('--domain',
required=False,
action='store',
dest='domain',
help="BigML domain")
# BigML development model
parser.add_argument('--dev',
required=False,
action='store_true',
dest='dev_mode',
default=False,
help="BigML development mode")
args = parser.parse_args(args)
resource = args.resource
project = args.project
username = args.username or os.environ.get('BIGML_USERNAME')
api_key = args.api_key or os.environ.get('BIGML_API_KEY')
domain = args.domain or os.environ.get('BIGML_DOMAIN') or 'bigml.io'
dev_mode = args.dev_mode
update = {'project': project}
if not (username and api_key and domain):
sys.exit("Please check BigML authentication parameters")
api = BigML(username, api_key, dev_mode=dev_mode, domain=domain)
resource_type = get_resource_type(args.resource)
if resource_type is None:
sys.exit("Wrong resource identifier")
if api.get_project(project) is None:
sys.exit("Wrong project identifier")
resource = getattr(api, 'update_%s' % resource_type)(resource, update)
api.ok(resource)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment