def ExtractFilename(self, flagfile_str): """Returns filename from a flagfile_str of form -[-]flagfile=filename. The cases of --flagfile foo and -flagfile foo shouldn't be hitting this function, as they are dealt with in the level above this function. Args: flagfile_str: flagfile string. Returns: str filename from a flagfile_str of form -[-]flagfile=filename. Raises: Error: when illegal --flagfile provided. """ if flagfile_str.startswith('--flagfile='): return os.path.expanduser( (flagfile_str[(len('--flagfile=')):]).strip()) elif flagfile_str.startswith('-flagfile='): return os.path.expanduser( (flagfile_str[(len('-flagfile=')):]).strip()) else: raise exceptions.Error('Hit illegal --flagfile type: %s' % flagfile_str)
def __setitem__(self, name, flag): """Registers a new flag variable.""" fl = self.FlagDict() if not isinstance(flag, _flag.Flag): raise exceptions.IllegalFlagValueError(flag) if str is bytes and isinstance(name, unicode): # When using Python 2 with unicode_literals, allow it but encode it # into the bytes type we require. name = name.encode('utf-8') if not isinstance(name, type('')): raise exceptions.Error('Flag name must be a string') if not name: raise exceptions.Error('Flag name cannot be empty') if name in fl and not flag.allow_override and not fl[ name].allow_override: module, module_name = _helpers.GetCallingModuleObjectAndName() if (self.FindModuleDefiningFlag(name) == module_name and id(module) != self.FindModuleIdDefiningFlag(name)): # If the flag has already been defined by a module with the same name, # but a different ID, we can stop here because it indicates that the # module is simply being imported a subsequent time. return raise exceptions.DuplicateFlagError.from_flag(name, self) short_name = flag.short_name # If a new flag overrides an old one, we need to cleanup the old flag's # modules if it's not registered. flags_to_cleanup = set() if short_name is not None: if (short_name in fl and not flag.allow_override and not fl[short_name].allow_override): raise exceptions.DuplicateFlagError.from_flag(short_name, self) if short_name in fl and fl[short_name] != flag: flags_to_cleanup.add(fl[short_name]) fl[short_name] = flag if (name not in fl # new flag or fl[name].using_default_value or not flag.using_default_value): if name in fl and fl[name] != flag: flags_to_cleanup.add(fl[name]) fl[name] = flag for f in flags_to_cleanup: self._CleanupUnregisteredFlagFromModuleDicts(f)
def set_up(self, region, org_name, org_guid, space_name, space_guid, dashdb_guid=None, dashdb_name=None): try: #try to create app and services and bind them if dashdb_name: #if 'dashdb_name' is specified by user, #then we do not have to create a new dashdb instance. #we only need to create an app with name 'ML*SERVICE' #and a new predictive analytics service instance and bind them. spss_credentials = cf.create_app_and_bind_services( region, 'ML*SERVICE', space_guid, self.token, dashdb=False)['Predictive Analytics'] #However, we still have to retrieve Dashdb instance credentials dashdb_credentials = cf.get_dashdb_credentials( region, dashdb_guid, self.token) #store the name of bound services self.dashDB_instance = dashdb_name self.predictive_analytics_instance = 'ML*SERVICE-PA-DONT-DELETE' else: #if 'dashdb_name' is not provided, #we create a new dashdb instance and bind it with 'ML*SERVICE' app credentials = cf.create_app_and_bind_services(region, 'ML*SERVICE', space_guid, self.token, dashdb=True) spss_credentials = credentials['Predictive Analytics'] dashdb_credentials = credentials['dashDB'] #store the name of bound services self.dashDB_instance = 'ML*SERVICE-DASHDB-DONT-DELETE' self.predictive_analytics_instance = 'ML*SERVICE-PA-DONT-DELETE' #if setup app and services succesfully, store the relevant infomation print spss_credentials print dashdb_credentials self.region = region self.organization_name = org_name self.organization_guid = org_guid self.space_name = space_name self.space_guid = space_guid self.dashDB_service_address = dashdb_credentials[0] self.dashDB_service_user = dashdb_credentials[1] self.dashDB_service_pwd = dashdb_credentials[2] self.predictive_analytics_url = spss_credentials[0] self.predictive_analytics_access_key = spss_credentials[1] except e.UnknownCloudFoundryError as error: raise e.Error(msg='Create app/services failed: ' + error.msg)
def serialize(self): if self.value is None: return '' if self.boolean: if self.value: return '--%s' % self.name else: return '--no%s' % self.name else: if not self.serializer: raise exceptions.Error('Serializer not present for flag %s' % self.name) return '--%s=%s' % (self.name, self.serializer.serialize( self.value))
def __get_backend(self): if not self.__backend: backend_name = self.__pivot.value if backend_name not in self.__backends: raise exception.Error('Invalid backend: %s' % backend_name) backend = self.__backends[backend_name] if isinstance(backend, tuple): name = backend[0] fromlist = backend[1] else: name = backend fromlist = backend self.__backend = __import__(name, None, None, fromlist) logging.info('backend %s', self.__backend) return self.__backend
def serialize(self): if not self.serializer: raise exceptions.Error('Serializer not present for flag %s' % self.name) if self.value is None: return '' s = '' multi_value = self.value for self.value in multi_value: if s: s += ' ' s += Flag.serialize(self) self.value = multi_value return s
def __init__(self, parser, serializer, name, default, help_string, short_name=None, boolean=False, allow_override=False, allow_cpp_override=False, allow_hide_cpp=False, allow_overwrite=True, parse_default=True): self.name = name if not help_string: help_string = '(no help available)' self.help = help_string self.short_name = short_name self.boolean = boolean self.present = 0 self.parser = parser self.serializer = serializer self.allow_override = allow_override self.allow_cpp_override = allow_cpp_override self.allow_hide_cpp = allow_hide_cpp self.allow_overwrite = allow_overwrite self.using_default_value = True self._value = None self.validators = [] if allow_hide_cpp and allow_cpp_override: raise exceptions.Error( "Can't have both allow_hide_cpp (means use Python flag) and " 'allow_cpp_override (means use C++ flag after InitGoogle)') if parse_default: self._set_default(default) else: self.default = default
def GetValue(): # pylint: disable=cell-var-from-loop try: return next(args) if value is None else value except StopIteration: raise exceptions.Error('Missing value for flag ' + arg)
def deploy_sample_and_get_all_models(self, sample_model_names): """ Deploy sample models to service instances and retrieve models from them. This method is used when user sets up their catalog. Params: ----- sample_model_names: list This list contains zero or any number of the following sample modes: 'SAMPLE_CUSTOMER', 'SAMPLE_DRUG', 'SAMPLE_CHURN', 'SAMPLE_ACQUISITION' """ models = [] try: #first try to deploy all the sample models the user specified if 'churn' in sample_model_names: churn = model.Model.create_dashdb_model( 'SAMPLE_CHURN', self, 'CUSTOMER_CHURN', 'CENSOR', 'CUST_ID', True) models.append(churn) if 'acquisition' in sample_model_names: acqui = model.Model.create_dashdb_model( 'SAMPLE_ACQUISITION', self, 'CUSTOMER_ACQUISITION', 'ACQUIRED', 'CUST_ID', True) models.append(acqui) if 'drug' in sample_model_names: path = './app/sample_models/Drug.str' with open(path, "rb") as str_file: drug = model.Model.create_spss_model( 'SAMPLE_DRUG', self, 'Drug.str', str_file) models.append(drug) if 'customer' in sample_model_names: path = './app/sample_models/catalog_timeseries.str' with open(path, "rb") as str_file: customer = model.Model.create_spss_model( 'SAMPLE_CUSTOMER', self, 'catalog_timeseries.str', str_file) models.append(customer) #then check the existing models in bound services deployed_spss_models = model.SPSSModel.get_deployed_models(self) print deployed_spss_models deployed_dashdb_models = model.DashdbModel.get_deployed_models( self) print deployed_dashdb_models deployed_models = deployed_spss_models + deployed_dashdb_models print "deployed: " print deployed_models #the existing models include the sample models we just deployed, #so we have to exclude these sample models left_models = [] for each_model in deployed_models: if not each_model.model_name in [ 'SAMPLE_CUSTOMER', 'SAMPLE_DRUG', 'SAMPLE_CHURN', 'SAMPLE_ACQUISITION' ]: left_models.append(each_model) models += left_models print models #then for each model, retrieve its metadata and store them to database for each_model in models: each_model.retrieve_metadata() each_model.commit() except Exception as error: if 'msg' in error.__dict__: #if 'msg' is one of the attributes of the error, #it means this is an error defined this application raise e.Error( msg= 'Deploying sample models or retrieving existing models failed: ' + error.msg) else: raise error