repository_name
stringlengths
7
55
func_path_in_repository
stringlengths
4
223
func_name
stringlengths
1
134
whole_func_string
stringlengths
75
104k
language
stringclasses
1 value
func_code_string
stringlengths
75
104k
func_code_tokens
listlengths
19
28.4k
func_documentation_string
stringlengths
1
46.9k
func_documentation_tokens
listlengths
1
1.97k
split_name
stringclasses
1 value
func_code_url
stringlengths
87
315
awesto/djangoshop-stripe
shop_stripe/payment.py
StripePayment.charge
def charge(self, cart, request): """ Use the Stripe token from the request and charge immediately. This view is invoked by the Javascript function `scope.charge()` delivered by `get_payment_request`. """ token_id = cart.extra['payment_extra_data']['token_id'] if LooseVersion(SHOP_VERSION) < LooseVersion('0.11'): charge = stripe.Charge.create( amount=cart.total.as_integer(), currency=cart.total.currency, source=token_id, description=settings.SHOP_STRIPE['PURCHASE_DESCRIPTION'] ) if charge['status'] == 'succeeded': order = OrderModel.objects.create_from_cart(cart, request) order.add_stripe_payment(charge) order.save() else: order = OrderModel.objects.create_from_cart(cart, request) charge = stripe.Charge.create( amount=cart.total.as_integer(), currency=cart.total.currency, source=token_id, transfer_group=order.get_number(), description=settings.SHOP_STRIPE['PURCHASE_DESCRIPTION'], ) if charge['status'] == 'succeeded': order.populate_from_cart(cart, request) order.add_stripe_payment(charge) order.save() if charge['status'] != 'succeeded': msg = "Stripe returned status '{status}' for id: {id}" raise stripe.error.InvalidRequestError(msg.format(**charge))
python
def charge(self, cart, request): """ Use the Stripe token from the request and charge immediately. This view is invoked by the Javascript function `scope.charge()` delivered by `get_payment_request`. """ token_id = cart.extra['payment_extra_data']['token_id'] if LooseVersion(SHOP_VERSION) < LooseVersion('0.11'): charge = stripe.Charge.create( amount=cart.total.as_integer(), currency=cart.total.currency, source=token_id, description=settings.SHOP_STRIPE['PURCHASE_DESCRIPTION'] ) if charge['status'] == 'succeeded': order = OrderModel.objects.create_from_cart(cart, request) order.add_stripe_payment(charge) order.save() else: order = OrderModel.objects.create_from_cart(cart, request) charge = stripe.Charge.create( amount=cart.total.as_integer(), currency=cart.total.currency, source=token_id, transfer_group=order.get_number(), description=settings.SHOP_STRIPE['PURCHASE_DESCRIPTION'], ) if charge['status'] == 'succeeded': order.populate_from_cart(cart, request) order.add_stripe_payment(charge) order.save() if charge['status'] != 'succeeded': msg = "Stripe returned status '{status}' for id: {id}" raise stripe.error.InvalidRequestError(msg.format(**charge))
[ "def", "charge", "(", "self", ",", "cart", ",", "request", ")", ":", "token_id", "=", "cart", ".", "extra", "[", "'payment_extra_data'", "]", "[", "'token_id'", "]", "if", "LooseVersion", "(", "SHOP_VERSION", ")", "<", "LooseVersion", "(", "'0.11'", ")", ":", "charge", "=", "stripe", ".", "Charge", ".", "create", "(", "amount", "=", "cart", ".", "total", ".", "as_integer", "(", ")", ",", "currency", "=", "cart", ".", "total", ".", "currency", ",", "source", "=", "token_id", ",", "description", "=", "settings", ".", "SHOP_STRIPE", "[", "'PURCHASE_DESCRIPTION'", "]", ")", "if", "charge", "[", "'status'", "]", "==", "'succeeded'", ":", "order", "=", "OrderModel", ".", "objects", ".", "create_from_cart", "(", "cart", ",", "request", ")", "order", ".", "add_stripe_payment", "(", "charge", ")", "order", ".", "save", "(", ")", "else", ":", "order", "=", "OrderModel", ".", "objects", ".", "create_from_cart", "(", "cart", ",", "request", ")", "charge", "=", "stripe", ".", "Charge", ".", "create", "(", "amount", "=", "cart", ".", "total", ".", "as_integer", "(", ")", ",", "currency", "=", "cart", ".", "total", ".", "currency", ",", "source", "=", "token_id", ",", "transfer_group", "=", "order", ".", "get_number", "(", ")", ",", "description", "=", "settings", ".", "SHOP_STRIPE", "[", "'PURCHASE_DESCRIPTION'", "]", ",", ")", "if", "charge", "[", "'status'", "]", "==", "'succeeded'", ":", "order", ".", "populate_from_cart", "(", "cart", ",", "request", ")", "order", ".", "add_stripe_payment", "(", "charge", ")", "order", ".", "save", "(", ")", "if", "charge", "[", "'status'", "]", "!=", "'succeeded'", ":", "msg", "=", "\"Stripe returned status '{status}' for id: {id}\"", "raise", "stripe", ".", "error", ".", "InvalidRequestError", "(", "msg", ".", "format", "(", "*", "*", "charge", ")", ")" ]
Use the Stripe token from the request and charge immediately. This view is invoked by the Javascript function `scope.charge()` delivered by `get_payment_request`.
[ "Use", "the", "Stripe", "token", "from", "the", "request", "and", "charge", "immediately", ".", "This", "view", "is", "invoked", "by", "the", "Javascript", "function", "scope", ".", "charge", "()", "delivered", "by", "get_payment_request", "." ]
train
https://github.com/awesto/djangoshop-stripe/blob/010d4642f971961cfeb415520ad819b3751281cb/shop_stripe/payment.py#L40-L74
awesto/djangoshop-stripe
shop_stripe/payment.py
OrderWorkflowMixin.refund_payment
def refund_payment(self): """ Refund the payment using Stripe's refunding API. """ Money = MoneyMaker(self.currency) filter_kwargs = { 'transaction_id__startswith': 'ch_', 'payment_method': StripePayment.namespace, } for payment in self.orderpayment_set.filter(**filter_kwargs): refund = stripe.Refund.create(charge=payment.transaction_id) if refund['status'] == 'succeeded': amount = Money(refund['amount']) / Money.subunits OrderPayment.objects.create(order=self, amount=-amount, transaction_id=refund['id'], payment_method=StripePayment.namespace) del self.amount_paid # to invalidate the cache if self.amount_paid: # proceed with other payment service providers super(OrderWorkflowMixin, self).refund_payment()
python
def refund_payment(self): """ Refund the payment using Stripe's refunding API. """ Money = MoneyMaker(self.currency) filter_kwargs = { 'transaction_id__startswith': 'ch_', 'payment_method': StripePayment.namespace, } for payment in self.orderpayment_set.filter(**filter_kwargs): refund = stripe.Refund.create(charge=payment.transaction_id) if refund['status'] == 'succeeded': amount = Money(refund['amount']) / Money.subunits OrderPayment.objects.create(order=self, amount=-amount, transaction_id=refund['id'], payment_method=StripePayment.namespace) del self.amount_paid # to invalidate the cache if self.amount_paid: # proceed with other payment service providers super(OrderWorkflowMixin, self).refund_payment()
[ "def", "refund_payment", "(", "self", ")", ":", "Money", "=", "MoneyMaker", "(", "self", ".", "currency", ")", "filter_kwargs", "=", "{", "'transaction_id__startswith'", ":", "'ch_'", ",", "'payment_method'", ":", "StripePayment", ".", "namespace", ",", "}", "for", "payment", "in", "self", ".", "orderpayment_set", ".", "filter", "(", "*", "*", "filter_kwargs", ")", ":", "refund", "=", "stripe", ".", "Refund", ".", "create", "(", "charge", "=", "payment", ".", "transaction_id", ")", "if", "refund", "[", "'status'", "]", "==", "'succeeded'", ":", "amount", "=", "Money", "(", "refund", "[", "'amount'", "]", ")", "/", "Money", ".", "subunits", "OrderPayment", ".", "objects", ".", "create", "(", "order", "=", "self", ",", "amount", "=", "-", "amount", ",", "transaction_id", "=", "refund", "[", "'id'", "]", ",", "payment_method", "=", "StripePayment", ".", "namespace", ")", "del", "self", ".", "amount_paid", "# to invalidate the cache", "if", "self", ".", "amount_paid", ":", "# proceed with other payment service providers", "super", "(", "OrderWorkflowMixin", ",", "self", ")", ".", "refund_payment", "(", ")" ]
Refund the payment using Stripe's refunding API.
[ "Refund", "the", "payment", "using", "Stripe", "s", "refunding", "API", "." ]
train
https://github.com/awesto/djangoshop-stripe/blob/010d4642f971961cfeb415520ad819b3751281cb/shop_stripe/payment.py#L104-L123
PredixDev/predixpy
predix/admin/weather.py
WeatherForecast.create
def create(self): """ Create an instance of the US Weather Forecast Service with typical starting settings. """ self.service.create() # Set env vars for immediate use zone_id = predix.config.get_env_key(self.use_class, 'zone_id') zone = self.service.settings.data['zone']['http-header-value'] os.environ[zone_id] = zone uri = predix.config.get_env_key(self.use_class, 'uri') os.environ[uri] = self.service.settings.data['uri']
python
def create(self): """ Create an instance of the US Weather Forecast Service with typical starting settings. """ self.service.create() # Set env vars for immediate use zone_id = predix.config.get_env_key(self.use_class, 'zone_id') zone = self.service.settings.data['zone']['http-header-value'] os.environ[zone_id] = zone uri = predix.config.get_env_key(self.use_class, 'uri') os.environ[uri] = self.service.settings.data['uri']
[ "def", "create", "(", "self", ")", ":", "self", ".", "service", ".", "create", "(", ")", "# Set env vars for immediate use", "zone_id", "=", "predix", ".", "config", ".", "get_env_key", "(", "self", ".", "use_class", ",", "'zone_id'", ")", "zone", "=", "self", ".", "service", ".", "settings", ".", "data", "[", "'zone'", "]", "[", "'http-header-value'", "]", "os", ".", "environ", "[", "zone_id", "]", "=", "zone", "uri", "=", "predix", ".", "config", ".", "get_env_key", "(", "self", ".", "use_class", ",", "'uri'", ")", "os", ".", "environ", "[", "uri", "]", "=", "self", ".", "service", ".", "settings", ".", "data", "[", "'uri'", "]" ]
Create an instance of the US Weather Forecast Service with typical starting settings.
[ "Create", "an", "instance", "of", "the", "US", "Weather", "Forecast", "Service", "with", "typical", "starting", "settings", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/weather.py#L33-L46
PredixDev/predixpy
predix/admin/weather.py
WeatherForecast.add_to_manifest
def add_to_manifest(self, manifest): """ Add useful details to the manifest about this service so that it can be used in an application. :param manifest: An predix.admin.app.Manifest object instance that manages reading/writing manifest config for a cloud foundry app. """ # Add this service to list of services manifest.add_service(self.service.name) # Add environment variables zone_id = predix.config.get_env_key(self.use_class, 'zone_id') manifest.add_env_var(zone_id, self.service.settings.data['zone']['http-header-value']) uri = predix.config.get_env_key(self.use_class, 'uri') manifest.add_env_var(uri, self.service.settings.data['uri']) manifest.write_manifest()
python
def add_to_manifest(self, manifest): """ Add useful details to the manifest about this service so that it can be used in an application. :param manifest: An predix.admin.app.Manifest object instance that manages reading/writing manifest config for a cloud foundry app. """ # Add this service to list of services manifest.add_service(self.service.name) # Add environment variables zone_id = predix.config.get_env_key(self.use_class, 'zone_id') manifest.add_env_var(zone_id, self.service.settings.data['zone']['http-header-value']) uri = predix.config.get_env_key(self.use_class, 'uri') manifest.add_env_var(uri, self.service.settings.data['uri']) manifest.write_manifest()
[ "def", "add_to_manifest", "(", "self", ",", "manifest", ")", ":", "# Add this service to list of services", "manifest", ".", "add_service", "(", "self", ".", "service", ".", "name", ")", "# Add environment variables", "zone_id", "=", "predix", ".", "config", ".", "get_env_key", "(", "self", ".", "use_class", ",", "'zone_id'", ")", "manifest", ".", "add_env_var", "(", "zone_id", ",", "self", ".", "service", ".", "settings", ".", "data", "[", "'zone'", "]", "[", "'http-header-value'", "]", ")", "uri", "=", "predix", ".", "config", ".", "get_env_key", "(", "self", ".", "use_class", ",", "'uri'", ")", "manifest", ".", "add_env_var", "(", "uri", ",", "self", ".", "service", ".", "settings", ".", "data", "[", "'uri'", "]", ")", "manifest", ".", "write_manifest", "(", ")" ]
Add useful details to the manifest about this service so that it can be used in an application. :param manifest: An predix.admin.app.Manifest object instance that manages reading/writing manifest config for a cloud foundry app.
[ "Add", "useful", "details", "to", "the", "manifest", "about", "this", "service", "so", "that", "it", "can", "be", "used", "in", "an", "application", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/weather.py#L48-L68
PredixDev/predixpy
predix/admin/cache.py
Cache._create_in_progress
def _create_in_progress(self): """ Creating this service is handled asynchronously so this method will simply check if the create is in progress. If it is not in progress, we could probably infer it either failed or succeeded. """ instance = self.service.service.get_instance(self.service.name) if (instance['last_operation']['state'] == 'in progress' and instance['last_operation']['type'] == 'create'): return True return False
python
def _create_in_progress(self): """ Creating this service is handled asynchronously so this method will simply check if the create is in progress. If it is not in progress, we could probably infer it either failed or succeeded. """ instance = self.service.service.get_instance(self.service.name) if (instance['last_operation']['state'] == 'in progress' and instance['last_operation']['type'] == 'create'): return True return False
[ "def", "_create_in_progress", "(", "self", ")", ":", "instance", "=", "self", ".", "service", ".", "service", ".", "get_instance", "(", "self", ".", "service", ".", "name", ")", "if", "(", "instance", "[", "'last_operation'", "]", "[", "'state'", "]", "==", "'in progress'", "and", "instance", "[", "'last_operation'", "]", "[", "'type'", "]", "==", "'create'", ")", ":", "return", "True", "return", "False" ]
Creating this service is handled asynchronously so this method will simply check if the create is in progress. If it is not in progress, we could probably infer it either failed or succeeded.
[ "Creating", "this", "service", "is", "handled", "asynchronously", "so", "this", "method", "will", "simply", "check", "if", "the", "create", "is", "in", "progress", ".", "If", "it", "is", "not", "in", "progress", "we", "could", "probably", "infer", "it", "either", "failed", "or", "succeeded", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/cache.py#L28-L39
PredixDev/predixpy
predix/admin/cache.py
Cache.create
def create(self, max_wait=180, **kwargs): """ Create an instance of the Predix Cache Service with they typical starting settings. :param max_wait: service is created asynchronously, so will only wait this number of seconds before giving up. """ # Will need to wait for the service to be provisioned before can add # service keys and get env details. self.service.create(async=True, create_keys=False) while self._create_in_progress() and max_wait > 0: time.sleep(1) max_wait -= 1 # Now get the service env (via service keys) cfg = self.service._get_service_config() self.service.settings.save(cfg) host = predix.config.get_env_key(self.use_class, 'host') os.environ[host] = self.service.settings.data['host'] password = predix.config.get_env_key(self.use_class, 'password') os.environ[password] = self.service.settings.data['password'] port = predix.config.get_env_key(self.use_class, 'port') os.environ[port] = str(self.service.settings.data['port'])
python
def create(self, max_wait=180, **kwargs): """ Create an instance of the Predix Cache Service with they typical starting settings. :param max_wait: service is created asynchronously, so will only wait this number of seconds before giving up. """ # Will need to wait for the service to be provisioned before can add # service keys and get env details. self.service.create(async=True, create_keys=False) while self._create_in_progress() and max_wait > 0: time.sleep(1) max_wait -= 1 # Now get the service env (via service keys) cfg = self.service._get_service_config() self.service.settings.save(cfg) host = predix.config.get_env_key(self.use_class, 'host') os.environ[host] = self.service.settings.data['host'] password = predix.config.get_env_key(self.use_class, 'password') os.environ[password] = self.service.settings.data['password'] port = predix.config.get_env_key(self.use_class, 'port') os.environ[port] = str(self.service.settings.data['port'])
[ "def", "create", "(", "self", ",", "max_wait", "=", "180", ",", "*", "*", "kwargs", ")", ":", "# Will need to wait for the service to be provisioned before can add", "# service keys and get env details.", "self", ".", "service", ".", "create", "(", "async", "=", "True", ",", "create_keys", "=", "False", ")", "while", "self", ".", "_create_in_progress", "(", ")", "and", "max_wait", ">", "0", ":", "time", ".", "sleep", "(", "1", ")", "max_wait", "-=", "1", "# Now get the service env (via service keys)", "cfg", "=", "self", ".", "service", ".", "_get_service_config", "(", ")", "self", ".", "service", ".", "settings", ".", "save", "(", "cfg", ")", "host", "=", "predix", ".", "config", ".", "get_env_key", "(", "self", ".", "use_class", ",", "'host'", ")", "os", ".", "environ", "[", "host", "]", "=", "self", ".", "service", ".", "settings", ".", "data", "[", "'host'", "]", "password", "=", "predix", ".", "config", ".", "get_env_key", "(", "self", ".", "use_class", ",", "'password'", ")", "os", ".", "environ", "[", "password", "]", "=", "self", ".", "service", ".", "settings", ".", "data", "[", "'password'", "]", "port", "=", "predix", ".", "config", ".", "get_env_key", "(", "self", ".", "use_class", ",", "'port'", ")", "os", ".", "environ", "[", "port", "]", "=", "str", "(", "self", ".", "service", ".", "settings", ".", "data", "[", "'port'", "]", ")" ]
Create an instance of the Predix Cache Service with they typical starting settings. :param max_wait: service is created asynchronously, so will only wait this number of seconds before giving up.
[ "Create", "an", "instance", "of", "the", "Predix", "Cache", "Service", "with", "they", "typical", "starting", "settings", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/cache.py#L41-L68
PredixDev/predixpy
predix/admin/cache.py
Cache.add_to_manifest
def add_to_manifest(self, manifest): """ Add useful details to the manifest about this service so that it can be used in an application. :param manifest: A predix.admin.app.Manifest object instance that manages reading/writing manifest config for a cloud foundry app. """ manifest.add_service(self.service.name) host = predix.config.get_env_key(self.use_class, 'host') manifest.add_env_var(host, self.service.settings.data['host']) password = predix.config.get_env_key(self.use_class, 'password') manifest.add_env_var(password, self.service.settings.data['password']) port = predix.config.get_env_key(self.use_class, 'port') manifest.add_env_var(port, self.service.settings.data['port']) manifest.write_manifest()
python
def add_to_manifest(self, manifest): """ Add useful details to the manifest about this service so that it can be used in an application. :param manifest: A predix.admin.app.Manifest object instance that manages reading/writing manifest config for a cloud foundry app. """ manifest.add_service(self.service.name) host = predix.config.get_env_key(self.use_class, 'host') manifest.add_env_var(host, self.service.settings.data['host']) password = predix.config.get_env_key(self.use_class, 'password') manifest.add_env_var(password, self.service.settings.data['password']) port = predix.config.get_env_key(self.use_class, 'port') manifest.add_env_var(port, self.service.settings.data['port']) manifest.write_manifest()
[ "def", "add_to_manifest", "(", "self", ",", "manifest", ")", ":", "manifest", ".", "add_service", "(", "self", ".", "service", ".", "name", ")", "host", "=", "predix", ".", "config", ".", "get_env_key", "(", "self", ".", "use_class", ",", "'host'", ")", "manifest", ".", "add_env_var", "(", "host", ",", "self", ".", "service", ".", "settings", ".", "data", "[", "'host'", "]", ")", "password", "=", "predix", ".", "config", ".", "get_env_key", "(", "self", ".", "use_class", ",", "'password'", ")", "manifest", ".", "add_env_var", "(", "password", ",", "self", ".", "service", ".", "settings", ".", "data", "[", "'password'", "]", ")", "port", "=", "predix", ".", "config", ".", "get_env_key", "(", "self", ".", "use_class", ",", "'port'", ")", "manifest", ".", "add_env_var", "(", "port", ",", "self", ".", "service", ".", "settings", ".", "data", "[", "'port'", "]", ")", "manifest", ".", "write_manifest", "(", ")" ]
Add useful details to the manifest about this service so that it can be used in an application. :param manifest: A predix.admin.app.Manifest object instance that manages reading/writing manifest config for a cloud foundry app.
[ "Add", "useful", "details", "to", "the", "manifest", "about", "this", "service", "so", "that", "it", "can", "be", "used", "in", "an", "application", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/cache.py#L70-L90
PredixDev/predixpy
predix/admin/acs.py
AccessControl._get_uri
def _get_uri(self): """ Will return the uri for an existing instance. """ if not self.service.exists(): logging.warning("Service does not yet exist.") return self.service.settings.data['uri']
python
def _get_uri(self): """ Will return the uri for an existing instance. """ if not self.service.exists(): logging.warning("Service does not yet exist.") return self.service.settings.data['uri']
[ "def", "_get_uri", "(", "self", ")", ":", "if", "not", "self", ".", "service", ".", "exists", "(", ")", ":", "logging", ".", "warning", "(", "\"Service does not yet exist.\"", ")", "return", "self", ".", "service", ".", "settings", ".", "data", "[", "'uri'", "]" ]
Will return the uri for an existing instance.
[ "Will", "return", "the", "uri", "for", "an", "existing", "instance", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/acs.py#L23-L30
PredixDev/predixpy
predix/admin/acs.py
AccessControl._get_zone_id
def _get_zone_id(self): """ Will return the zone id for an existing instance. """ if not self.service.exists(): logging.warning("Service does not yet exist.") return self.service.settings.data['zone']['http-header-value']
python
def _get_zone_id(self): """ Will return the zone id for an existing instance. """ if not self.service.exists(): logging.warning("Service does not yet exist.") return self.service.settings.data['zone']['http-header-value']
[ "def", "_get_zone_id", "(", "self", ")", ":", "if", "not", "self", ".", "service", ".", "exists", "(", ")", ":", "logging", ".", "warning", "(", "\"Service does not yet exist.\"", ")", "return", "self", ".", "service", ".", "settings", ".", "data", "[", "'zone'", "]", "[", "'http-header-value'", "]" ]
Will return the zone id for an existing instance.
[ "Will", "return", "the", "zone", "id", "for", "an", "existing", "instance", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/acs.py#L32-L39
PredixDev/predixpy
predix/admin/acs.py
AccessControl.create
def create(self): """ Create an instance of the Access Control Service with the typical starting settings. """ self.service.create() # Set environment variables for immediate use predix.config.set_env_value(self.use_class, 'uri', self._get_uri()) predix.config.set_env_value(self.use_class, 'zone_id', self._get_zone_id())
python
def create(self): """ Create an instance of the Access Control Service with the typical starting settings. """ self.service.create() # Set environment variables for immediate use predix.config.set_env_value(self.use_class, 'uri', self._get_uri()) predix.config.set_env_value(self.use_class, 'zone_id', self._get_zone_id())
[ "def", "create", "(", "self", ")", ":", "self", ".", "service", ".", "create", "(", ")", "# Set environment variables for immediate use", "predix", ".", "config", ".", "set_env_value", "(", "self", ".", "use_class", ",", "'uri'", ",", "self", ".", "_get_uri", "(", ")", ")", "predix", ".", "config", ".", "set_env_value", "(", "self", ".", "use_class", ",", "'zone_id'", ",", "self", ".", "_get_zone_id", "(", ")", ")" ]
Create an instance of the Access Control Service with the typical starting settings.
[ "Create", "an", "instance", "of", "the", "Access", "Control", "Service", "with", "the", "typical", "starting", "settings", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/acs.py#L47-L57
PredixDev/predixpy
predix/admin/acs.py
AccessControl.grant_client
def grant_client(self, client_id): """ Grant the given client id all the scopes and authorities needed to work with the access control service. """ zone = self.service.settings.data['zone']['oauth-scope'] scopes = ['openid', zone, 'acs.policies.read', 'acs.attributes.read', 'acs.policies.write', 'acs.attributes.write'] authorities = ['uaa.resource', zone, 'acs.policies.read', 'acs.policies.write', 'acs.attributes.read', 'acs.attributes.write'] self.service.uaa.uaac.update_client_grants(client_id, scope=scopes, authorities=authorities) return self.service.uaa.uaac.get_client(client_id)
python
def grant_client(self, client_id): """ Grant the given client id all the scopes and authorities needed to work with the access control service. """ zone = self.service.settings.data['zone']['oauth-scope'] scopes = ['openid', zone, 'acs.policies.read', 'acs.attributes.read', 'acs.policies.write', 'acs.attributes.write'] authorities = ['uaa.resource', zone, 'acs.policies.read', 'acs.policies.write', 'acs.attributes.read', 'acs.attributes.write'] self.service.uaa.uaac.update_client_grants(client_id, scope=scopes, authorities=authorities) return self.service.uaa.uaac.get_client(client_id)
[ "def", "grant_client", "(", "self", ",", "client_id", ")", ":", "zone", "=", "self", ".", "service", ".", "settings", ".", "data", "[", "'zone'", "]", "[", "'oauth-scope'", "]", "scopes", "=", "[", "'openid'", ",", "zone", ",", "'acs.policies.read'", ",", "'acs.attributes.read'", ",", "'acs.policies.write'", ",", "'acs.attributes.write'", "]", "authorities", "=", "[", "'uaa.resource'", ",", "zone", ",", "'acs.policies.read'", ",", "'acs.policies.write'", ",", "'acs.attributes.read'", ",", "'acs.attributes.write'", "]", "self", ".", "service", ".", "uaa", ".", "uaac", ".", "update_client_grants", "(", "client_id", ",", "scope", "=", "scopes", ",", "authorities", "=", "authorities", ")", "return", "self", ".", "service", ".", "uaa", ".", "uaac", ".", "get_client", "(", "client_id", ")" ]
Grant the given client id all the scopes and authorities needed to work with the access control service.
[ "Grant", "the", "given", "client", "id", "all", "the", "scopes", "and", "authorities", "needed", "to", "work", "with", "the", "access", "control", "service", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/acs.py#L59-L77
PredixDev/predixpy
predix/admin/acs.py
AccessControl.add_to_manifest
def add_to_manifest(self, manifest): """ Add useful details to the manifest about this service so that it can be used in an application. :param manifest: An predix.admin.app.Manifest object instance that manages reading/writing manifest config for a cloud foundry app. """ # Add this service to list of services manifest.add_service(self.service.name) # Add environment variables uri = predix.config.get_env_key(self.use_class, 'uri') manifest.add_env_var(uri, self._get_uri()) zone_id = predix.config.get_env_key(self.use_class, 'zone_id') manifest.add_env_var(zone_id, self._get_zone_id()) manifest.write_manifest()
python
def add_to_manifest(self, manifest): """ Add useful details to the manifest about this service so that it can be used in an application. :param manifest: An predix.admin.app.Manifest object instance that manages reading/writing manifest config for a cloud foundry app. """ # Add this service to list of services manifest.add_service(self.service.name) # Add environment variables uri = predix.config.get_env_key(self.use_class, 'uri') manifest.add_env_var(uri, self._get_uri()) zone_id = predix.config.get_env_key(self.use_class, 'zone_id') manifest.add_env_var(zone_id, self._get_zone_id()) manifest.write_manifest()
[ "def", "add_to_manifest", "(", "self", ",", "manifest", ")", ":", "# Add this service to list of services", "manifest", ".", "add_service", "(", "self", ".", "service", ".", "name", ")", "# Add environment variables", "uri", "=", "predix", ".", "config", ".", "get_env_key", "(", "self", ".", "use_class", ",", "'uri'", ")", "manifest", ".", "add_env_var", "(", "uri", ",", "self", ".", "_get_uri", "(", ")", ")", "zone_id", "=", "predix", ".", "config", ".", "get_env_key", "(", "self", ".", "use_class", ",", "'zone_id'", ")", "manifest", ".", "add_env_var", "(", "zone_id", ",", "self", ".", "_get_zone_id", "(", ")", ")", "manifest", ".", "write_manifest", "(", ")" ]
Add useful details to the manifest about this service so that it can be used in an application. :param manifest: An predix.admin.app.Manifest object instance that manages reading/writing manifest config for a cloud foundry app.
[ "Add", "useful", "details", "to", "the", "manifest", "about", "this", "service", "so", "that", "it", "can", "be", "used", "in", "an", "application", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/acs.py#L79-L98
PredixDev/predixpy
predix/admin/cf/api.py
API.get
def get(self, path): """ Generic GET with headers """ uri = self.config.get_target() + path headers = self._get_headers() logging.debug("URI=GET " + str(uri)) logging.debug("HEADERS=" + str(headers)) response = self.session.get(uri, headers=headers) if response.status_code == 200: return response.json() elif response.status_code == 401: raise predix.admin.cf.config.CloudFoundryLoginError('token invalid') else: response.raise_for_status()
python
def get(self, path): """ Generic GET with headers """ uri = self.config.get_target() + path headers = self._get_headers() logging.debug("URI=GET " + str(uri)) logging.debug("HEADERS=" + str(headers)) response = self.session.get(uri, headers=headers) if response.status_code == 200: return response.json() elif response.status_code == 401: raise predix.admin.cf.config.CloudFoundryLoginError('token invalid') else: response.raise_for_status()
[ "def", "get", "(", "self", ",", "path", ")", ":", "uri", "=", "self", ".", "config", ".", "get_target", "(", ")", "+", "path", "headers", "=", "self", ".", "_get_headers", "(", ")", "logging", ".", "debug", "(", "\"URI=GET \"", "+", "str", "(", "uri", ")", ")", "logging", ".", "debug", "(", "\"HEADERS=\"", "+", "str", "(", "headers", ")", ")", "response", "=", "self", ".", "session", ".", "get", "(", "uri", ",", "headers", "=", "headers", ")", "if", "response", ".", "status_code", "==", "200", ":", "return", "response", ".", "json", "(", ")", "elif", "response", ".", "status_code", "==", "401", ":", "raise", "predix", ".", "admin", ".", "cf", ".", "config", ".", "CloudFoundryLoginError", "(", "'token invalid'", ")", "else", ":", "response", ".", "raise_for_status", "(", ")" ]
Generic GET with headers
[ "Generic", "GET", "with", "headers" ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/cf/api.py#L50-L66
PredixDev/predixpy
predix/admin/cf/api.py
API.post
def post(self, path, data): """ Generic POST with headers """ uri = self.config.get_target() + path headers = self._post_headers() logging.debug("URI=POST " + str(uri)) logging.debug("HEADERS=" + str(headers)) logging.debug("BODY=" + str(data)) response = self.session.post(uri, headers=headers, data=json.dumps(data)) if response.status_code in (200, 201, 202): return response.json() elif response.status_code == 401: raise predix.admin.cf.config.CloudFoundryLoginError('token invalid') else: logging.debug("STATUS=" + str(response.status_code)) logging.debug("CONTENT=" + str(response.content)) response.raise_for_status()
python
def post(self, path, data): """ Generic POST with headers """ uri = self.config.get_target() + path headers = self._post_headers() logging.debug("URI=POST " + str(uri)) logging.debug("HEADERS=" + str(headers)) logging.debug("BODY=" + str(data)) response = self.session.post(uri, headers=headers, data=json.dumps(data)) if response.status_code in (200, 201, 202): return response.json() elif response.status_code == 401: raise predix.admin.cf.config.CloudFoundryLoginError('token invalid') else: logging.debug("STATUS=" + str(response.status_code)) logging.debug("CONTENT=" + str(response.content)) response.raise_for_status()
[ "def", "post", "(", "self", ",", "path", ",", "data", ")", ":", "uri", "=", "self", ".", "config", ".", "get_target", "(", ")", "+", "path", "headers", "=", "self", ".", "_post_headers", "(", ")", "logging", ".", "debug", "(", "\"URI=POST \"", "+", "str", "(", "uri", ")", ")", "logging", ".", "debug", "(", "\"HEADERS=\"", "+", "str", "(", "headers", ")", ")", "logging", ".", "debug", "(", "\"BODY=\"", "+", "str", "(", "data", ")", ")", "response", "=", "self", ".", "session", ".", "post", "(", "uri", ",", "headers", "=", "headers", ",", "data", "=", "json", ".", "dumps", "(", "data", ")", ")", "if", "response", ".", "status_code", "in", "(", "200", ",", "201", ",", "202", ")", ":", "return", "response", ".", "json", "(", ")", "elif", "response", ".", "status_code", "==", "401", ":", "raise", "predix", ".", "admin", ".", "cf", ".", "config", ".", "CloudFoundryLoginError", "(", "'token invalid'", ")", "else", ":", "logging", ".", "debug", "(", "\"STATUS=\"", "+", "str", "(", "response", ".", "status_code", ")", ")", "logging", ".", "debug", "(", "\"CONTENT=\"", "+", "str", "(", "response", ".", "content", ")", ")", "response", ".", "raise_for_status", "(", ")" ]
Generic POST with headers
[ "Generic", "POST", "with", "headers" ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/cf/api.py#L68-L88
PredixDev/predixpy
predix/admin/cf/api.py
API.delete
def delete(self, path, data=None, params=None): """ Generic DELETE with headers """ uri = self.config.get_target() + path headers = { 'Authorization': self.config.get_access_token() } logging.debug("URI=DELETE " + str(uri)) logging.debug("HEADERS=" + str(headers)) response = self.session.delete( uri, headers=headers, params=params, data=json.dumps(data)) if response.status_code == 204: return response else: logging.debug("STATUS=" + str(response.status_code)) logging.debug("CONTENT=" + str(response.content)) response.raise_for_status()
python
def delete(self, path, data=None, params=None): """ Generic DELETE with headers """ uri = self.config.get_target() + path headers = { 'Authorization': self.config.get_access_token() } logging.debug("URI=DELETE " + str(uri)) logging.debug("HEADERS=" + str(headers)) response = self.session.delete( uri, headers=headers, params=params, data=json.dumps(data)) if response.status_code == 204: return response else: logging.debug("STATUS=" + str(response.status_code)) logging.debug("CONTENT=" + str(response.content)) response.raise_for_status()
[ "def", "delete", "(", "self", ",", "path", ",", "data", "=", "None", ",", "params", "=", "None", ")", ":", "uri", "=", "self", ".", "config", ".", "get_target", "(", ")", "+", "path", "headers", "=", "{", "'Authorization'", ":", "self", ".", "config", ".", "get_access_token", "(", ")", "}", "logging", ".", "debug", "(", "\"URI=DELETE \"", "+", "str", "(", "uri", ")", ")", "logging", ".", "debug", "(", "\"HEADERS=\"", "+", "str", "(", "headers", ")", ")", "response", "=", "self", ".", "session", ".", "delete", "(", "uri", ",", "headers", "=", "headers", ",", "params", "=", "params", ",", "data", "=", "json", ".", "dumps", "(", "data", ")", ")", "if", "response", ".", "status_code", "==", "204", ":", "return", "response", "else", ":", "logging", ".", "debug", "(", "\"STATUS=\"", "+", "str", "(", "response", ".", "status_code", ")", ")", "logging", ".", "debug", "(", "\"CONTENT=\"", "+", "str", "(", "response", ".", "content", ")", ")", "response", ".", "raise_for_status", "(", ")" ]
Generic DELETE with headers
[ "Generic", "DELETE", "with", "headers" ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/cf/api.py#L113-L133
PredixDev/predixpy
predix/admin/cf/orgs.py
Org.get_orgs
def get_orgs(self): """ Returns a flat list of the names for the organizations user belongs. """ orgs = [] for resource in self._get_orgs()['resources']: orgs.append(resource['entity']['name']) return orgs
python
def get_orgs(self): """ Returns a flat list of the names for the organizations user belongs. """ orgs = [] for resource in self._get_orgs()['resources']: orgs.append(resource['entity']['name']) return orgs
[ "def", "get_orgs", "(", "self", ")", ":", "orgs", "=", "[", "]", "for", "resource", "in", "self", ".", "_get_orgs", "(", ")", "[", "'resources'", "]", ":", "orgs", ".", "append", "(", "resource", "[", "'entity'", "]", "[", "'name'", "]", ")", "return", "orgs" ]
Returns a flat list of the names for the organizations user belongs.
[ "Returns", "a", "flat", "list", "of", "the", "names", "for", "the", "organizations", "user", "belongs", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/cf/orgs.py#L30-L39
PredixDev/predixpy
predix/admin/cf/orgs.py
Org.get_apps
def get_apps(self): """ Returns a flat list of the names for the apps in the organization. """ apps = [] for resource in self._get_apps()['resources']: apps.append(resource['entity']['name']) return apps
python
def get_apps(self): """ Returns a flat list of the names for the apps in the organization. """ apps = [] for resource in self._get_apps()['resources']: apps.append(resource['entity']['name']) return apps
[ "def", "get_apps", "(", "self", ")", ":", "apps", "=", "[", "]", "for", "resource", "in", "self", ".", "_get_apps", "(", ")", "[", "'resources'", "]", ":", "apps", ".", "append", "(", "resource", "[", "'entity'", "]", "[", "'name'", "]", ")", "return", "apps" ]
Returns a flat list of the names for the apps in the organization.
[ "Returns", "a", "flat", "list", "of", "the", "names", "for", "the", "apps", "in", "the", "organization", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/cf/orgs.py#L47-L56
PredixDev/predixpy
predix/admin/cf/orgs.py
Org.add_user
def add_user(self, user_name, role='user'): """ Calls CF's associate user with org. Valid roles include `user`, `auditor`, `manager`,`billing_manager` """ role_uri = self._get_role_uri(role=role) return self.api.put(path=role_uri, data={'username': user_name})
python
def add_user(self, user_name, role='user'): """ Calls CF's associate user with org. Valid roles include `user`, `auditor`, `manager`,`billing_manager` """ role_uri = self._get_role_uri(role=role) return self.api.put(path=role_uri, data={'username': user_name})
[ "def", "add_user", "(", "self", ",", "user_name", ",", "role", "=", "'user'", ")", ":", "role_uri", "=", "self", ".", "_get_role_uri", "(", "role", "=", "role", ")", "return", "self", ".", "api", ".", "put", "(", "path", "=", "role_uri", ",", "data", "=", "{", "'username'", ":", "user_name", "}", ")" ]
Calls CF's associate user with org. Valid roles include `user`, `auditor`, `manager`,`billing_manager`
[ "Calls", "CF", "s", "associate", "user", "with", "org", ".", "Valid", "roles", "include", "user", "auditor", "manager", "billing_manager" ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/cf/orgs.py#L68-L74
PredixDev/predixpy
predix/admin/cf/orgs.py
Org.remove_user
def remove_user(self, user_name, role): """ Calls CF's remove user with org """ role_uri = self._get_role_uri(role=role) return self.api.delete(path=role_uri, data={'username': user_name})
python
def remove_user(self, user_name, role): """ Calls CF's remove user with org """ role_uri = self._get_role_uri(role=role) return self.api.delete(path=role_uri, data={'username': user_name})
[ "def", "remove_user", "(", "self", ",", "user_name", ",", "role", ")", ":", "role_uri", "=", "self", ".", "_get_role_uri", "(", "role", "=", "role", ")", "return", "self", ".", "api", ".", "delete", "(", "path", "=", "role_uri", ",", "data", "=", "{", "'username'", ":", "user_name", "}", ")" ]
Calls CF's remove user with org
[ "Calls", "CF", "s", "remove", "user", "with", "org" ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/cf/orgs.py#L84-L89
PredixDev/predixpy
predix/data/eventhub/publisher.py
Publisher.add_message
def add_message(self, id, body, tags=False): """ add messages to the rx_queue :param id: str message Id :param body: str the message body :param tags: dict[string->string] tags to be associated with the message :return: self """ if not tags: tags = {} try: self._tx_queue_lock.acquire() self._tx_queue.append( EventHub_pb2.Message(id=id, body=body, tags=tags, zone_id=self.eventhub_client.zone_id)) finally: self._tx_queue_lock.release() return self
python
def add_message(self, id, body, tags=False): """ add messages to the rx_queue :param id: str message Id :param body: str the message body :param tags: dict[string->string] tags to be associated with the message :return: self """ if not tags: tags = {} try: self._tx_queue_lock.acquire() self._tx_queue.append( EventHub_pb2.Message(id=id, body=body, tags=tags, zone_id=self.eventhub_client.zone_id)) finally: self._tx_queue_lock.release() return self
[ "def", "add_message", "(", "self", ",", "id", ",", "body", ",", "tags", "=", "False", ")", ":", "if", "not", "tags", ":", "tags", "=", "{", "}", "try", ":", "self", ".", "_tx_queue_lock", ".", "acquire", "(", ")", "self", ".", "_tx_queue", ".", "append", "(", "EventHub_pb2", ".", "Message", "(", "id", "=", "id", ",", "body", "=", "body", ",", "tags", "=", "tags", ",", "zone_id", "=", "self", ".", "eventhub_client", ".", "zone_id", ")", ")", "finally", ":", "self", ".", "_tx_queue_lock", ".", "release", "(", ")", "return", "self" ]
add messages to the rx_queue :param id: str message Id :param body: str the message body :param tags: dict[string->string] tags to be associated with the message :return: self
[ "add", "messages", "to", "the", "rx_queue", ":", "param", "id", ":", "str", "message", "Id", ":", "param", "body", ":", "str", "the", "message", "body", ":", "param", "tags", ":", "dict", "[", "string", "-", ">", "string", "]", "tags", "to", "be", "associated", "with", "the", "message", ":", "return", ":", "self" ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/data/eventhub/publisher.py#L171-L187
PredixDev/predixpy
predix/data/eventhub/publisher.py
Publisher.publish_queue
def publish_queue(self): """ Publish all messages that have been added to the queue for configured protocol :return: None """ self.last_send_time = time.time() try: self._tx_queue_lock.acquire() start_length = len(self._rx_queue) publish_amount = len(self._tx_queue) if self.config.protocol == PublisherConfig.Protocol.GRPC: self._publish_queue_grpc() else: self._publish_queue_wss() self._tx_queue = [] finally: self._tx_queue_lock.release() if self.config.publish_type == self.config.Type.SYNC: start_time = time.time() while time.time() - start_time < self.config.sync_timeout and \ len(self._rx_queue) - start_length < publish_amount: pass return self._rx_queue
python
def publish_queue(self): """ Publish all messages that have been added to the queue for configured protocol :return: None """ self.last_send_time = time.time() try: self._tx_queue_lock.acquire() start_length = len(self._rx_queue) publish_amount = len(self._tx_queue) if self.config.protocol == PublisherConfig.Protocol.GRPC: self._publish_queue_grpc() else: self._publish_queue_wss() self._tx_queue = [] finally: self._tx_queue_lock.release() if self.config.publish_type == self.config.Type.SYNC: start_time = time.time() while time.time() - start_time < self.config.sync_timeout and \ len(self._rx_queue) - start_length < publish_amount: pass return self._rx_queue
[ "def", "publish_queue", "(", "self", ")", ":", "self", ".", "last_send_time", "=", "time", ".", "time", "(", ")", "try", ":", "self", ".", "_tx_queue_lock", ".", "acquire", "(", ")", "start_length", "=", "len", "(", "self", ".", "_rx_queue", ")", "publish_amount", "=", "len", "(", "self", ".", "_tx_queue", ")", "if", "self", ".", "config", ".", "protocol", "==", "PublisherConfig", ".", "Protocol", ".", "GRPC", ":", "self", ".", "_publish_queue_grpc", "(", ")", "else", ":", "self", ".", "_publish_queue_wss", "(", ")", "self", ".", "_tx_queue", "=", "[", "]", "finally", ":", "self", ".", "_tx_queue_lock", ".", "release", "(", ")", "if", "self", ".", "config", ".", "publish_type", "==", "self", ".", "config", ".", "Type", ".", "SYNC", ":", "start_time", "=", "time", ".", "time", "(", ")", "while", "time", ".", "time", "(", ")", "-", "start_time", "<", "self", ".", "config", ".", "sync_timeout", "and", "len", "(", "self", ".", "_rx_queue", ")", "-", "start_length", "<", "publish_amount", ":", "pass", "return", "self", ".", "_rx_queue" ]
Publish all messages that have been added to the queue for configured protocol :return: None
[ "Publish", "all", "messages", "that", "have", "been", "added", "to", "the", "queue", "for", "configured", "protocol", ":", "return", ":", "None" ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/data/eventhub/publisher.py#L189-L212
PredixDev/predixpy
predix/data/eventhub/publisher.py
Publisher.ack_generator
def ack_generator(self): """ generator for acks to yield messages to the user in a async configuration :return: messages as they come in """ if self.config.is_sync(): logging.warning('cant use generator on a sync publisher') return while self._run_ack_generator: while len(self._rx_queue) != 0: logging.debug('yielding to client') yield self._rx_queue.pop(0) return
python
def ack_generator(self): """ generator for acks to yield messages to the user in a async configuration :return: messages as they come in """ if self.config.is_sync(): logging.warning('cant use generator on a sync publisher') return while self._run_ack_generator: while len(self._rx_queue) != 0: logging.debug('yielding to client') yield self._rx_queue.pop(0) return
[ "def", "ack_generator", "(", "self", ")", ":", "if", "self", ".", "config", ".", "is_sync", "(", ")", ":", "logging", ".", "warning", "(", "'cant use generator on a sync publisher'", ")", "return", "while", "self", ".", "_run_ack_generator", ":", "while", "len", "(", "self", ".", "_rx_queue", ")", "!=", "0", ":", "logging", ".", "debug", "(", "'yielding to client'", ")", "yield", "self", ".", "_rx_queue", ".", "pop", "(", "0", ")", "return" ]
generator for acks to yield messages to the user in a async configuration :return: messages as they come in
[ "generator", "for", "acks", "to", "yield", "messages", "to", "the", "user", "in", "a", "async", "configuration", ":", "return", ":", "messages", "as", "they", "come", "in" ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/data/eventhub/publisher.py#L214-L226
PredixDev/predixpy
predix/data/eventhub/publisher.py
Publisher._auto_send
def _auto_send(self): """ auto send blocking function, when the interval or the message size has been reached, publish :return: """ while True: if time.time() - self.last_send_time > self.config.async_auto_send_interval_millis or \ len(self._tx_queue) >= self.config.async_auto_send_amount: self.publish_queue()
python
def _auto_send(self): """ auto send blocking function, when the interval or the message size has been reached, publish :return: """ while True: if time.time() - self.last_send_time > self.config.async_auto_send_interval_millis or \ len(self._tx_queue) >= self.config.async_auto_send_amount: self.publish_queue()
[ "def", "_auto_send", "(", "self", ")", ":", "while", "True", ":", "if", "time", ".", "time", "(", ")", "-", "self", ".", "last_send_time", ">", "self", ".", "config", ".", "async_auto_send_interval_millis", "or", "len", "(", "self", ".", "_tx_queue", ")", ">=", "self", ".", "config", ".", "async_auto_send_amount", ":", "self", ".", "publish_queue", "(", ")" ]
auto send blocking function, when the interval or the message size has been reached, publish :return:
[ "auto", "send", "blocking", "function", "when", "the", "interval", "or", "the", "message", "size", "has", "been", "reached", "publish", ":", "return", ":" ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/data/eventhub/publisher.py#L234-L242
PredixDev/predixpy
predix/data/eventhub/publisher.py
Publisher._generate_publish_headers
def _generate_publish_headers(self): """ generate the headers for the connection to event hub service based on the provided config :return: {} headers """ headers = { 'predix-zone-id': self.eventhub_client.zone_id } token = self.eventhub_client.service._get_bearer_token() if self.config.is_grpc(): headers['authorization'] = token[(token.index(' ') + 1):] else: headers['authorization'] = token if self.config.topic == '': headers['topic'] = self.eventhub_client.zone_id + '_topic' else: headers['topic'] = self.config.topic if self.config.publish_type == self.config.Type.SYNC: headers['sync-acks'] = 'true' else: headers['sync-acks'] = 'false' headers['send-acks-interval'] = str(self.config.async_cache_ack_interval_millis) headers['acks'] = str(self.config.async_enable_acks).lower() headers['nacks'] = str(self.config.async_enable_nacks_only).lower() headers['cache-acks'] = str(self.config.async_cache_acks_and_nacks).lower() return headers
python
def _generate_publish_headers(self): """ generate the headers for the connection to event hub service based on the provided config :return: {} headers """ headers = { 'predix-zone-id': self.eventhub_client.zone_id } token = self.eventhub_client.service._get_bearer_token() if self.config.is_grpc(): headers['authorization'] = token[(token.index(' ') + 1):] else: headers['authorization'] = token if self.config.topic == '': headers['topic'] = self.eventhub_client.zone_id + '_topic' else: headers['topic'] = self.config.topic if self.config.publish_type == self.config.Type.SYNC: headers['sync-acks'] = 'true' else: headers['sync-acks'] = 'false' headers['send-acks-interval'] = str(self.config.async_cache_ack_interval_millis) headers['acks'] = str(self.config.async_enable_acks).lower() headers['nacks'] = str(self.config.async_enable_nacks_only).lower() headers['cache-acks'] = str(self.config.async_cache_acks_and_nacks).lower() return headers
[ "def", "_generate_publish_headers", "(", "self", ")", ":", "headers", "=", "{", "'predix-zone-id'", ":", "self", ".", "eventhub_client", ".", "zone_id", "}", "token", "=", "self", ".", "eventhub_client", ".", "service", ".", "_get_bearer_token", "(", ")", "if", "self", ".", "config", ".", "is_grpc", "(", ")", ":", "headers", "[", "'authorization'", "]", "=", "token", "[", "(", "token", ".", "index", "(", "' '", ")", "+", "1", ")", ":", "]", "else", ":", "headers", "[", "'authorization'", "]", "=", "token", "if", "self", ".", "config", ".", "topic", "==", "''", ":", "headers", "[", "'topic'", "]", "=", "self", ".", "eventhub_client", ".", "zone_id", "+", "'_topic'", "else", ":", "headers", "[", "'topic'", "]", "=", "self", ".", "config", ".", "topic", "if", "self", ".", "config", ".", "publish_type", "==", "self", ".", "config", ".", "Type", ".", "SYNC", ":", "headers", "[", "'sync-acks'", "]", "=", "'true'", "else", ":", "headers", "[", "'sync-acks'", "]", "=", "'false'", "headers", "[", "'send-acks-interval'", "]", "=", "str", "(", "self", ".", "config", ".", "async_cache_ack_interval_millis", ")", "headers", "[", "'acks'", "]", "=", "str", "(", "self", ".", "config", ".", "async_enable_acks", ")", ".", "lower", "(", ")", "headers", "[", "'nacks'", "]", "=", "str", "(", "self", ".", "config", ".", "async_enable_nacks_only", ")", ".", "lower", "(", ")", "headers", "[", "'cache-acks'", "]", "=", "str", "(", "self", ".", "config", ".", "async_cache_acks_and_nacks", ")", ".", "lower", "(", ")", "return", "headers" ]
generate the headers for the connection to event hub service based on the provided config :return: {} headers
[ "generate", "the", "headers", "for", "the", "connection", "to", "event", "hub", "service", "based", "on", "the", "provided", "config", ":", "return", ":", "{}", "headers" ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/data/eventhub/publisher.py#L244-L271
PredixDev/predixpy
predix/data/eventhub/publisher.py
Publisher._publisher_callback
def _publisher_callback(self, publish_ack): """ publisher callback that grpc and web socket can pass messages to address the received message onto the queue :param publish_ack: EventHub_pb2.Ack the ack received from either wss or grpc :return: None """ logging.debug("ack received: " + str(publish_ack).replace('\n', ' ')) self._rx_queue.append(publish_ack)
python
def _publisher_callback(self, publish_ack): """ publisher callback that grpc and web socket can pass messages to address the received message onto the queue :param publish_ack: EventHub_pb2.Ack the ack received from either wss or grpc :return: None """ logging.debug("ack received: " + str(publish_ack).replace('\n', ' ')) self._rx_queue.append(publish_ack)
[ "def", "_publisher_callback", "(", "self", ",", "publish_ack", ")", ":", "logging", ".", "debug", "(", "\"ack received: \"", "+", "str", "(", "publish_ack", ")", ".", "replace", "(", "'\\n'", ",", "' '", ")", ")", "self", ".", "_rx_queue", ".", "append", "(", "publish_ack", ")" ]
publisher callback that grpc and web socket can pass messages to address the received message onto the queue :param publish_ack: EventHub_pb2.Ack the ack received from either wss or grpc :return: None
[ "publisher", "callback", "that", "grpc", "and", "web", "socket", "can", "pass", "messages", "to", "address", "the", "received", "message", "onto", "the", "queue", ":", "param", "publish_ack", ":", "EventHub_pb2", ".", "Ack", "the", "ack", "received", "from", "either", "wss", "or", "grpc", ":", "return", ":", "None" ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/data/eventhub/publisher.py#L273-L281
PredixDev/predixpy
predix/data/eventhub/publisher.py
Publisher._init_grpc_publisher
def _init_grpc_publisher(self): """ initialize the grpc publisher, builds the stub and then starts the grpc manager :return: None """ self._stub = EventHub_pb2_grpc.PublisherStub(channel=self._channel) self.grpc_manager = Eventhub.GrpcManager(stub_call=self._stub.send, on_msg_callback=self._publisher_callback, metadata=self._generate_publish_headers().items())
python
def _init_grpc_publisher(self): """ initialize the grpc publisher, builds the stub and then starts the grpc manager :return: None """ self._stub = EventHub_pb2_grpc.PublisherStub(channel=self._channel) self.grpc_manager = Eventhub.GrpcManager(stub_call=self._stub.send, on_msg_callback=self._publisher_callback, metadata=self._generate_publish_headers().items())
[ "def", "_init_grpc_publisher", "(", "self", ")", ":", "self", ".", "_stub", "=", "EventHub_pb2_grpc", ".", "PublisherStub", "(", "channel", "=", "self", ".", "_channel", ")", "self", ".", "grpc_manager", "=", "Eventhub", ".", "GrpcManager", "(", "stub_call", "=", "self", ".", "_stub", ".", "send", ",", "on_msg_callback", "=", "self", ".", "_publisher_callback", ",", "metadata", "=", "self", ".", "_generate_publish_headers", "(", ")", ".", "items", "(", ")", ")" ]
initialize the grpc publisher, builds the stub and then starts the grpc manager :return: None
[ "initialize", "the", "grpc", "publisher", "builds", "the", "stub", "and", "then", "starts", "the", "grpc", "manager", ":", "return", ":", "None" ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/data/eventhub/publisher.py#L289-L297
PredixDev/predixpy
predix/data/eventhub/publisher.py
Publisher._publish_queue_grpc
def _publish_queue_grpc(self): """ send the messages in the tx queue to the GRPC manager :return: None """ messages = EventHub_pb2.Messages(msg=self._tx_queue) publish_request = EventHub_pb2.PublishRequest(messages=messages) self.grpc_manager.send_message(publish_request)
python
def _publish_queue_grpc(self): """ send the messages in the tx queue to the GRPC manager :return: None """ messages = EventHub_pb2.Messages(msg=self._tx_queue) publish_request = EventHub_pb2.PublishRequest(messages=messages) self.grpc_manager.send_message(publish_request)
[ "def", "_publish_queue_grpc", "(", "self", ")", ":", "messages", "=", "EventHub_pb2", ".", "Messages", "(", "msg", "=", "self", ".", "_tx_queue", ")", "publish_request", "=", "EventHub_pb2", ".", "PublishRequest", "(", "messages", "=", "messages", ")", "self", ".", "grpc_manager", ".", "send_message", "(", "publish_request", ")" ]
send the messages in the tx queue to the GRPC manager :return: None
[ "send", "the", "messages", "in", "the", "tx", "queue", "to", "the", "GRPC", "manager", ":", "return", ":", "None" ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/data/eventhub/publisher.py#L299-L306
PredixDev/predixpy
predix/data/eventhub/publisher.py
Publisher._publish_queue_wss
def _publish_queue_wss(self): """ send the messages down the web socket connection as a json object :return: None """ msg = [] for m in self._tx_queue: msg.append({'id': m.id, 'body': m.body, 'zone_id': m.zone_id}) self._ws.send(json.dumps(msg), opcode=websocket.ABNF.OPCODE_BINARY)
python
def _publish_queue_wss(self): """ send the messages down the web socket connection as a json object :return: None """ msg = [] for m in self._tx_queue: msg.append({'id': m.id, 'body': m.body, 'zone_id': m.zone_id}) self._ws.send(json.dumps(msg), opcode=websocket.ABNF.OPCODE_BINARY)
[ "def", "_publish_queue_wss", "(", "self", ")", ":", "msg", "=", "[", "]", "for", "m", "in", "self", ".", "_tx_queue", ":", "msg", ".", "append", "(", "{", "'id'", ":", "m", ".", "id", ",", "'body'", ":", "m", ".", "body", ",", "'zone_id'", ":", "m", ".", "zone_id", "}", ")", "self", ".", "_ws", ".", "send", "(", "json", ".", "dumps", "(", "msg", ")", ",", "opcode", "=", "websocket", ".", "ABNF", ".", "OPCODE_BINARY", ")" ]
send the messages down the web socket connection as a json object :return: None
[ "send", "the", "messages", "down", "the", "web", "socket", "connection", "as", "a", "json", "object", ":", "return", ":", "None" ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/data/eventhub/publisher.py#L314-L323
PredixDev/predixpy
predix/data/eventhub/publisher.py
Publisher._init_publisher_ws
def _init_publisher_ws(self): """ Create a new web socket connection with proper headers. """ logging.debug("Initializing new web socket connection.") url = ('wss://%s/v1/stream/messages/' % self.eventhub_client.host) headers = self._generate_publish_headers() logging.debug("URL=" + str(url)) logging.debug("HEADERS=" + str(headers)) websocket.enableTrace(False) self._ws = websocket.WebSocketApp(url, header=headers, on_message=self._on_ws_message, on_open=self._on_ws_open, on_close=self._on_ws_close) self._ws_thread = threading.Thread(target=self._ws.run_forever, kwargs={'ping_interval': 30}) self._ws_thread.daemon = True self._ws_thread.start() time.sleep(1)
python
def _init_publisher_ws(self): """ Create a new web socket connection with proper headers. """ logging.debug("Initializing new web socket connection.") url = ('wss://%s/v1/stream/messages/' % self.eventhub_client.host) headers = self._generate_publish_headers() logging.debug("URL=" + str(url)) logging.debug("HEADERS=" + str(headers)) websocket.enableTrace(False) self._ws = websocket.WebSocketApp(url, header=headers, on_message=self._on_ws_message, on_open=self._on_ws_open, on_close=self._on_ws_close) self._ws_thread = threading.Thread(target=self._ws.run_forever, kwargs={'ping_interval': 30}) self._ws_thread.daemon = True self._ws_thread.start() time.sleep(1)
[ "def", "_init_publisher_ws", "(", "self", ")", ":", "logging", ".", "debug", "(", "\"Initializing new web socket connection.\"", ")", "url", "=", "(", "'wss://%s/v1/stream/messages/'", "%", "self", ".", "eventhub_client", ".", "host", ")", "headers", "=", "self", ".", "_generate_publish_headers", "(", ")", "logging", ".", "debug", "(", "\"URL=\"", "+", "str", "(", "url", ")", ")", "logging", ".", "debug", "(", "\"HEADERS=\"", "+", "str", "(", "headers", ")", ")", "websocket", ".", "enableTrace", "(", "False", ")", "self", ".", "_ws", "=", "websocket", ".", "WebSocketApp", "(", "url", ",", "header", "=", "headers", ",", "on_message", "=", "self", ".", "_on_ws_message", ",", "on_open", "=", "self", ".", "_on_ws_open", ",", "on_close", "=", "self", ".", "_on_ws_close", ")", "self", ".", "_ws_thread", "=", "threading", ".", "Thread", "(", "target", "=", "self", ".", "_ws", ".", "run_forever", ",", "kwargs", "=", "{", "'ping_interval'", ":", "30", "}", ")", "self", ".", "_ws_thread", ".", "daemon", "=", "True", "self", ".", "_ws_thread", ".", "start", "(", ")", "time", ".", "sleep", "(", "1", ")" ]
Create a new web socket connection with proper headers.
[ "Create", "a", "new", "web", "socket", "connection", "with", "proper", "headers", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/data/eventhub/publisher.py#L325-L347
PredixDev/predixpy
predix/data/eventhub/publisher.py
Publisher._on_ws_message
def _on_ws_message(self, ws, message): """ on_message callback of websocket class, load the message into a dict and then update an Ack Object with the results :param ws: web socket connection that the message was received on :param message: web socket message in text form :return: None """ logging.debug(message) json_list = json.loads(message) for rx_ack in json_list: ack = EventHub_pb2.Ack() for key, value in rx_ack.items(): setattr(ack, key, value) self._publisher_callback(ack)
python
def _on_ws_message(self, ws, message): """ on_message callback of websocket class, load the message into a dict and then update an Ack Object with the results :param ws: web socket connection that the message was received on :param message: web socket message in text form :return: None """ logging.debug(message) json_list = json.loads(message) for rx_ack in json_list: ack = EventHub_pb2.Ack() for key, value in rx_ack.items(): setattr(ack, key, value) self._publisher_callback(ack)
[ "def", "_on_ws_message", "(", "self", ",", "ws", ",", "message", ")", ":", "logging", ".", "debug", "(", "message", ")", "json_list", "=", "json", ".", "loads", "(", "message", ")", "for", "rx_ack", "in", "json_list", ":", "ack", "=", "EventHub_pb2", ".", "Ack", "(", ")", "for", "key", ",", "value", "in", "rx_ack", ".", "items", "(", ")", ":", "setattr", "(", "ack", ",", "key", ",", "value", ")", "self", ".", "_publisher_callback", "(", "ack", ")" ]
on_message callback of websocket class, load the message into a dict and then update an Ack Object with the results :param ws: web socket connection that the message was received on :param message: web socket message in text form :return: None
[ "on_message", "callback", "of", "websocket", "class", "load", "the", "message", "into", "a", "dict", "and", "then", "update", "an", "Ack", "Object", "with", "the", "results", ":", "param", "ws", ":", "web", "socket", "connection", "that", "the", "message", "was", "received", "on", ":", "param", "message", ":", "web", "socket", "message", "in", "text", "form", ":", "return", ":", "None" ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/data/eventhub/publisher.py#L349-L363
PredixDev/predixpy
predix/admin/ie/parking.py
ParkingPlanning.create
def create(self): """ Create an instance of the Parking Planning Service with the typical starting settings. """ self.service.create() os.environ[self.__module__ + '.uri'] = self.service.settings.data['url'] os.environ[self.__module__ + '.zone_id'] = self.get_predix_zone_id()
python
def create(self): """ Create an instance of the Parking Planning Service with the typical starting settings. """ self.service.create() os.environ[self.__module__ + '.uri'] = self.service.settings.data['url'] os.environ[self.__module__ + '.zone_id'] = self.get_predix_zone_id()
[ "def", "create", "(", "self", ")", ":", "self", ".", "service", ".", "create", "(", ")", "os", ".", "environ", "[", "self", ".", "__module__", "+", "'.uri'", "]", "=", "self", ".", "service", ".", "settings", ".", "data", "[", "'url'", "]", "os", ".", "environ", "[", "self", ".", "__module__", "+", "'.zone_id'", "]", "=", "self", ".", "get_predix_zone_id", "(", ")" ]
Create an instance of the Parking Planning Service with the typical starting settings.
[ "Create", "an", "instance", "of", "the", "Parking", "Planning", "Service", "with", "the", "typical", "starting", "settings", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/ie/parking.py#L24-L31
PredixDev/predixpy
predix/admin/ie/parking.py
ParkingPlanning.add_to_manifest
def add_to_manifest(self, manifest): """ Add useful details to the manifest about this service so that it can be used in an application. :param manifest: An predix.admin.app.Manifest object instance that manages reading/writing manifest config for a cloud foundry app. """ # Add this service to list of services manifest.add_service(self.service.name) # Add environment variables manifest.add_env_var(self.__module__ + '.uri', self.service.settings.data['url']) manifest.add_env_var(self.__module__ + '.zone_id', self.get_predix_zone_id()) manifest.write_manifest()
python
def add_to_manifest(self, manifest): """ Add useful details to the manifest about this service so that it can be used in an application. :param manifest: An predix.admin.app.Manifest object instance that manages reading/writing manifest config for a cloud foundry app. """ # Add this service to list of services manifest.add_service(self.service.name) # Add environment variables manifest.add_env_var(self.__module__ + '.uri', self.service.settings.data['url']) manifest.add_env_var(self.__module__ + '.zone_id', self.get_predix_zone_id()) manifest.write_manifest()
[ "def", "add_to_manifest", "(", "self", ",", "manifest", ")", ":", "# Add this service to list of services", "manifest", ".", "add_service", "(", "self", ".", "service", ".", "name", ")", "# Add environment variables", "manifest", ".", "add_env_var", "(", "self", ".", "__module__", "+", "'.uri'", ",", "self", ".", "service", ".", "settings", ".", "data", "[", "'url'", "]", ")", "manifest", ".", "add_env_var", "(", "self", ".", "__module__", "+", "'.zone_id'", ",", "self", ".", "get_predix_zone_id", "(", ")", ")", "manifest", ".", "write_manifest", "(", ")" ]
Add useful details to the manifest about this service so that it can be used in an application. :param manifest: An predix.admin.app.Manifest object instance that manages reading/writing manifest config for a cloud foundry app.
[ "Add", "useful", "details", "to", "the", "manifest", "about", "this", "service", "so", "that", "it", "can", "be", "used", "in", "an", "application", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/ie/parking.py#L60-L78
PredixDev/predixpy
predix/app.py
Manifest.read_manifest
def read_manifest(self, encrypted=None): """ Read an existing manifest. """ with open(self.manifest_path, 'r') as input_file: self.manifest = yaml.safe_load(input_file) if 'env' not in self.manifest: self.manifest['env'] = {} if 'services' not in self.manifest: self.manifest['services'] = [] # If manifest is encrypted, use manifest key to # decrypt each value before storing in memory. if 'PREDIXPY_ENCRYPTED' in self.manifest['env']: self.encrypted = True if encrypted or self.encrypted: key = predix.config.get_crypt_key(self.manifest_key) f = Fernet(key) for var in self.manifest['env'].keys(): value = f.decrypt(bytes(self.manifest['env'][var], 'utf-8')) self.manifest['env'][var] = value.decode('utf-8') self.app_name = self.manifest['applications'][0]['name'] input_file.close()
python
def read_manifest(self, encrypted=None): """ Read an existing manifest. """ with open(self.manifest_path, 'r') as input_file: self.manifest = yaml.safe_load(input_file) if 'env' not in self.manifest: self.manifest['env'] = {} if 'services' not in self.manifest: self.manifest['services'] = [] # If manifest is encrypted, use manifest key to # decrypt each value before storing in memory. if 'PREDIXPY_ENCRYPTED' in self.manifest['env']: self.encrypted = True if encrypted or self.encrypted: key = predix.config.get_crypt_key(self.manifest_key) f = Fernet(key) for var in self.manifest['env'].keys(): value = f.decrypt(bytes(self.manifest['env'][var], 'utf-8')) self.manifest['env'][var] = value.decode('utf-8') self.app_name = self.manifest['applications'][0]['name'] input_file.close()
[ "def", "read_manifest", "(", "self", ",", "encrypted", "=", "None", ")", ":", "with", "open", "(", "self", ".", "manifest_path", ",", "'r'", ")", "as", "input_file", ":", "self", ".", "manifest", "=", "yaml", ".", "safe_load", "(", "input_file", ")", "if", "'env'", "not", "in", "self", ".", "manifest", ":", "self", ".", "manifest", "[", "'env'", "]", "=", "{", "}", "if", "'services'", "not", "in", "self", ".", "manifest", ":", "self", ".", "manifest", "[", "'services'", "]", "=", "[", "]", "# If manifest is encrypted, use manifest key to", "# decrypt each value before storing in memory.", "if", "'PREDIXPY_ENCRYPTED'", "in", "self", ".", "manifest", "[", "'env'", "]", ":", "self", ".", "encrypted", "=", "True", "if", "encrypted", "or", "self", ".", "encrypted", ":", "key", "=", "predix", ".", "config", ".", "get_crypt_key", "(", "self", ".", "manifest_key", ")", "f", "=", "Fernet", "(", "key", ")", "for", "var", "in", "self", ".", "manifest", "[", "'env'", "]", ".", "keys", "(", ")", ":", "value", "=", "f", ".", "decrypt", "(", "bytes", "(", "self", ".", "manifest", "[", "'env'", "]", "[", "var", "]", ",", "'utf-8'", ")", ")", "self", ".", "manifest", "[", "'env'", "]", "[", "var", "]", "=", "value", ".", "decode", "(", "'utf-8'", ")", "self", ".", "app_name", "=", "self", ".", "manifest", "[", "'applications'", "]", "[", "0", "]", "[", "'name'", "]", "input_file", ".", "close", "(", ")" ]
Read an existing manifest.
[ "Read", "an", "existing", "manifest", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/app.py#L69-L96
PredixDev/predixpy
predix/app.py
Manifest.create_manifest
def create_manifest(self): """ Create a new manifest and write it to disk. """ self.manifest = {} self.manifest['applications'] = [{'name': self.app_name}] self.manifest['services'] = [] self.manifest['env'] = { 'PREDIXPY_VERSION': str(predix.version), } self.write_manifest()
python
def create_manifest(self): """ Create a new manifest and write it to disk. """ self.manifest = {} self.manifest['applications'] = [{'name': self.app_name}] self.manifest['services'] = [] self.manifest['env'] = { 'PREDIXPY_VERSION': str(predix.version), } self.write_manifest()
[ "def", "create_manifest", "(", "self", ")", ":", "self", ".", "manifest", "=", "{", "}", "self", ".", "manifest", "[", "'applications'", "]", "=", "[", "{", "'name'", ":", "self", ".", "app_name", "}", "]", "self", ".", "manifest", "[", "'services'", "]", "=", "[", "]", "self", ".", "manifest", "[", "'env'", "]", "=", "{", "'PREDIXPY_VERSION'", ":", "str", "(", "predix", ".", "version", ")", ",", "}", "self", ".", "write_manifest", "(", ")" ]
Create a new manifest and write it to disk.
[ "Create", "a", "new", "manifest", "and", "write", "it", "to", "disk", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/app.py#L98-L110
PredixDev/predixpy
predix/app.py
Manifest._get_encrypted_manifest
def _get_encrypted_manifest(self): """ Returns contents of the manifest where environment variables that are secret will be encrypted without modifying the existing state in memory which will remain unencrypted. """ key = predix.config.get_crypt_key(self.manifest_key) f = Fernet(key) manifest = copy.deepcopy(self.manifest) for var in self.manifest['env'].keys(): value = str(self.manifest['env'][var]) manifest['env'][var] = f.encrypt(bytes(value, 'utf-8')).decode('utf-8') return manifest
python
def _get_encrypted_manifest(self): """ Returns contents of the manifest where environment variables that are secret will be encrypted without modifying the existing state in memory which will remain unencrypted. """ key = predix.config.get_crypt_key(self.manifest_key) f = Fernet(key) manifest = copy.deepcopy(self.manifest) for var in self.manifest['env'].keys(): value = str(self.manifest['env'][var]) manifest['env'][var] = f.encrypt(bytes(value, 'utf-8')).decode('utf-8') return manifest
[ "def", "_get_encrypted_manifest", "(", "self", ")", ":", "key", "=", "predix", ".", "config", ".", "get_crypt_key", "(", "self", ".", "manifest_key", ")", "f", "=", "Fernet", "(", "key", ")", "manifest", "=", "copy", ".", "deepcopy", "(", "self", ".", "manifest", ")", "for", "var", "in", "self", ".", "manifest", "[", "'env'", "]", ".", "keys", "(", ")", ":", "value", "=", "str", "(", "self", ".", "manifest", "[", "'env'", "]", "[", "var", "]", ")", "manifest", "[", "'env'", "]", "[", "var", "]", "=", "f", ".", "encrypt", "(", "bytes", "(", "value", ",", "'utf-8'", ")", ")", ".", "decode", "(", "'utf-8'", ")", "return", "manifest" ]
Returns contents of the manifest where environment variables that are secret will be encrypted without modifying the existing state in memory which will remain unencrypted.
[ "Returns", "contents", "of", "the", "manifest", "where", "environment", "variables", "that", "are", "secret", "will", "be", "encrypted", "without", "modifying", "the", "existing", "state", "in", "memory", "which", "will", "remain", "unencrypted", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/app.py#L112-L126
PredixDev/predixpy
predix/app.py
Manifest.write_manifest
def write_manifest(self, manifest_path=None, encrypted=None): """ Write manifest to disk. :param manifest_path: write to a different location :param encrypted: write with env data encrypted """ manifest_path = manifest_path or self.manifest_path self.manifest['env']['PREDIXPY_VERSION'] = str(predix.version) with open(manifest_path, 'w') as output_file: if encrypted or self.encrypted: self.manifest['env']['PREDIXPY_ENCRYPTED'] = self.manifest_key content = self._get_encrypted_manifest() else: content = self.manifest # shallow reference if 'PREDIXPY_ENCRYPTED' in content['env']: del(content['env']['PREDIXPY_ENCRYPTED']) yaml.safe_dump(content, output_file, default_flow_style=False, explicit_start=True) output_file.close()
python
def write_manifest(self, manifest_path=None, encrypted=None): """ Write manifest to disk. :param manifest_path: write to a different location :param encrypted: write with env data encrypted """ manifest_path = manifest_path or self.manifest_path self.manifest['env']['PREDIXPY_VERSION'] = str(predix.version) with open(manifest_path, 'w') as output_file: if encrypted or self.encrypted: self.manifest['env']['PREDIXPY_ENCRYPTED'] = self.manifest_key content = self._get_encrypted_manifest() else: content = self.manifest # shallow reference if 'PREDIXPY_ENCRYPTED' in content['env']: del(content['env']['PREDIXPY_ENCRYPTED']) yaml.safe_dump(content, output_file, default_flow_style=False, explicit_start=True) output_file.close()
[ "def", "write_manifest", "(", "self", ",", "manifest_path", "=", "None", ",", "encrypted", "=", "None", ")", ":", "manifest_path", "=", "manifest_path", "or", "self", ".", "manifest_path", "self", ".", "manifest", "[", "'env'", "]", "[", "'PREDIXPY_VERSION'", "]", "=", "str", "(", "predix", ".", "version", ")", "with", "open", "(", "manifest_path", ",", "'w'", ")", "as", "output_file", ":", "if", "encrypted", "or", "self", ".", "encrypted", ":", "self", ".", "manifest", "[", "'env'", "]", "[", "'PREDIXPY_ENCRYPTED'", "]", "=", "self", ".", "manifest_key", "content", "=", "self", ".", "_get_encrypted_manifest", "(", ")", "else", ":", "content", "=", "self", ".", "manifest", "# shallow reference", "if", "'PREDIXPY_ENCRYPTED'", "in", "content", "[", "'env'", "]", ":", "del", "(", "content", "[", "'env'", "]", "[", "'PREDIXPY_ENCRYPTED'", "]", ")", "yaml", ".", "safe_dump", "(", "content", ",", "output_file", ",", "default_flow_style", "=", "False", ",", "explicit_start", "=", "True", ")", "output_file", ".", "close", "(", ")" ]
Write manifest to disk. :param manifest_path: write to a different location :param encrypted: write with env data encrypted
[ "Write", "manifest", "to", "disk", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/app.py#L128-L150
PredixDev/predixpy
predix/app.py
Manifest.add_env_var
def add_env_var(self, key, value): """ Add the given key / value as another environment variable. """ self.manifest['env'][key] = value os.environ[key] = str(value)
python
def add_env_var(self, key, value): """ Add the given key / value as another environment variable. """ self.manifest['env'][key] = value os.environ[key] = str(value)
[ "def", "add_env_var", "(", "self", ",", "key", ",", "value", ")", ":", "self", ".", "manifest", "[", "'env'", "]", "[", "key", "]", "=", "value", "os", ".", "environ", "[", "key", "]", "=", "str", "(", "value", ")" ]
Add the given key / value as another environment variable.
[ "Add", "the", "given", "key", "/", "value", "as", "another", "environment", "variable", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/app.py#L152-L158
PredixDev/predixpy
predix/app.py
Manifest.add_service
def add_service(self, service_name): """ Add the given service to the manifest. """ if service_name not in self.manifest['services']: self.manifest['services'].append(service_name)
python
def add_service(self, service_name): """ Add the given service to the manifest. """ if service_name not in self.manifest['services']: self.manifest['services'].append(service_name)
[ "def", "add_service", "(", "self", ",", "service_name", ")", ":", "if", "service_name", "not", "in", "self", ".", "manifest", "[", "'services'", "]", ":", "self", ".", "manifest", "[", "'services'", "]", ".", "append", "(", "service_name", ")" ]
Add the given service to the manifest.
[ "Add", "the", "given", "service", "to", "the", "manifest", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/app.py#L160-L165
PredixDev/predixpy
predix/app.py
Manifest.set_os_environ
def set_os_environ(self): """ Will load any environment variables found in the manifest file into the current process for use by applications. When apps run in cloud foundry this would happen automatically. """ for key in self.manifest['env'].keys(): os.environ[key] = str(self.manifest['env'][key])
python
def set_os_environ(self): """ Will load any environment variables found in the manifest file into the current process for use by applications. When apps run in cloud foundry this would happen automatically. """ for key in self.manifest['env'].keys(): os.environ[key] = str(self.manifest['env'][key])
[ "def", "set_os_environ", "(", "self", ")", ":", "for", "key", "in", "self", ".", "manifest", "[", "'env'", "]", ".", "keys", "(", ")", ":", "os", ".", "environ", "[", "key", "]", "=", "str", "(", "self", ".", "manifest", "[", "'env'", "]", "[", "key", "]", ")" ]
Will load any environment variables found in the manifest file into the current process for use by applications. When apps run in cloud foundry this would happen automatically.
[ "Will", "load", "any", "environment", "variables", "found", "in", "the", "manifest", "file", "into", "the", "current", "process", "for", "use", "by", "applications", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/app.py#L167-L177
PredixDev/predixpy
predix/app.py
Manifest.get_client_id
def get_client_id(self): """ Return the client id that should have all the needed scopes and authorities for the services in this manifest. """ self._client_id = predix.config.get_env_value(predix.app.Manifest, 'client_id') return self._client_id
python
def get_client_id(self): """ Return the client id that should have all the needed scopes and authorities for the services in this manifest. """ self._client_id = predix.config.get_env_value(predix.app.Manifest, 'client_id') return self._client_id
[ "def", "get_client_id", "(", "self", ")", ":", "self", ".", "_client_id", "=", "predix", ".", "config", ".", "get_env_value", "(", "predix", ".", "app", ".", "Manifest", ",", "'client_id'", ")", "return", "self", ".", "_client_id" ]
Return the client id that should have all the needed scopes and authorities for the services in this manifest.
[ "Return", "the", "client", "id", "that", "should", "have", "all", "the", "needed", "scopes", "and", "authorities", "for", "the", "services", "in", "this", "manifest", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/app.py#L179-L186
PredixDev/predixpy
predix/app.py
Manifest.get_client_secret
def get_client_secret(self): """ Return the client secret that should correspond with the client id. """ self._client_secret = predix.config.get_env_value(predix.app.Manifest, 'client_secret') return self._client_secret
python
def get_client_secret(self): """ Return the client secret that should correspond with the client id. """ self._client_secret = predix.config.get_env_value(predix.app.Manifest, 'client_secret') return self._client_secret
[ "def", "get_client_secret", "(", "self", ")", ":", "self", ".", "_client_secret", "=", "predix", ".", "config", ".", "get_env_value", "(", "predix", ".", "app", ".", "Manifest", ",", "'client_secret'", ")", "return", "self", ".", "_client_secret" ]
Return the client secret that should correspond with the client id.
[ "Return", "the", "client", "secret", "that", "should", "correspond", "with", "the", "client", "id", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/app.py#L188-L194
PredixDev/predixpy
predix/app.py
Manifest.get_timeseries
def get_timeseries(self, *args, **kwargs): """ Returns an instance of the Time Series Service. """ import predix.data.timeseries ts = predix.data.timeseries.TimeSeries(*args, **kwargs) return ts
python
def get_timeseries(self, *args, **kwargs): """ Returns an instance of the Time Series Service. """ import predix.data.timeseries ts = predix.data.timeseries.TimeSeries(*args, **kwargs) return ts
[ "def", "get_timeseries", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "import", "predix", ".", "data", ".", "timeseries", "ts", "=", "predix", ".", "data", ".", "timeseries", ".", "TimeSeries", "(", "*", "args", ",", "*", "*", "kwargs", ")", "return", "ts" ]
Returns an instance of the Time Series Service.
[ "Returns", "an", "instance", "of", "the", "Time", "Series", "Service", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/app.py#L196-L202
PredixDev/predixpy
predix/app.py
Manifest.get_asset
def get_asset(self): """ Returns an instance of the Asset Service. """ import predix.data.asset asset = predix.data.asset.Asset() return asset
python
def get_asset(self): """ Returns an instance of the Asset Service. """ import predix.data.asset asset = predix.data.asset.Asset() return asset
[ "def", "get_asset", "(", "self", ")", ":", "import", "predix", ".", "data", ".", "asset", "asset", "=", "predix", ".", "data", ".", "asset", ".", "Asset", "(", ")", "return", "asset" ]
Returns an instance of the Asset Service.
[ "Returns", "an", "instance", "of", "the", "Asset", "Service", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/app.py#L204-L210
PredixDev/predixpy
predix/app.py
Manifest.get_uaa
def get_uaa(self): """ Returns an insstance of the UAA Service. """ import predix.security.uaa uaa = predix.security.uaa.UserAccountAuthentication() return uaa
python
def get_uaa(self): """ Returns an insstance of the UAA Service. """ import predix.security.uaa uaa = predix.security.uaa.UserAccountAuthentication() return uaa
[ "def", "get_uaa", "(", "self", ")", ":", "import", "predix", ".", "security", ".", "uaa", "uaa", "=", "predix", ".", "security", ".", "uaa", ".", "UserAccountAuthentication", "(", ")", "return", "uaa" ]
Returns an insstance of the UAA Service.
[ "Returns", "an", "insstance", "of", "the", "UAA", "Service", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/app.py#L212-L218
PredixDev/predixpy
predix/app.py
Manifest.get_acs
def get_acs(self): """ Returns an instance of the Asset Control Service. """ import predix.security.acs acs = predix.security.acs.AccessControl() return acs
python
def get_acs(self): """ Returns an instance of the Asset Control Service. """ import predix.security.acs acs = predix.security.acs.AccessControl() return acs
[ "def", "get_acs", "(", "self", ")", ":", "import", "predix", ".", "security", ".", "acs", "acs", "=", "predix", ".", "security", ".", "acs", ".", "AccessControl", "(", ")", "return", "acs" ]
Returns an instance of the Asset Control Service.
[ "Returns", "an", "instance", "of", "the", "Asset", "Control", "Service", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/app.py#L220-L226
PredixDev/predixpy
predix/app.py
Manifest.get_weather
def get_weather(self): """ Returns an instance of the Weather Service. """ import predix.data.weather weather = predix.data.weather.WeatherForecast() return weather
python
def get_weather(self): """ Returns an instance of the Weather Service. """ import predix.data.weather weather = predix.data.weather.WeatherForecast() return weather
[ "def", "get_weather", "(", "self", ")", ":", "import", "predix", ".", "data", ".", "weather", "weather", "=", "predix", ".", "data", ".", "weather", ".", "WeatherForecast", "(", ")", "return", "weather" ]
Returns an instance of the Weather Service.
[ "Returns", "an", "instance", "of", "the", "Weather", "Service", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/app.py#L228-L234
PredixDev/predixpy
predix/data/weather.py
WeatherForecast.get_weather_forecast_days
def get_weather_forecast_days(self, latitude, longitude, days=1, frequency=1, reading_type=None): """ Return the weather forecast for a given location. :: results = ws.get_weather_forecast_days(lat, long) for w in results['hits']: print w['start_datetime_local'] print w['reading_type'], w['reading_value'] For description of reading types: https://graphical.weather.gov/xml/docs/elementInputNames.php """ params = {} # Can get data from NWS1 or NWS3 representing 1-hr and 3-hr # intervals. if frequency not in [1, 3]: raise ValueError("Reading frequency must be 1 or 3") params['days'] = days params['source'] = 'NWS' + str(frequency) params['latitude'] = latitude params['longitude'] = longitude if reading_type: # url encoding will make spaces a + instead of %20, which service # interprets as an "and" search which is undesirable reading_type = reading_type.replace(' ', '%20') params['reading_type'] = urllib.quote_plus(reading_type) url = self.uri + '/v1/weather-forecast-days/' return self.service._get(url, params=params)
python
def get_weather_forecast_days(self, latitude, longitude, days=1, frequency=1, reading_type=None): """ Return the weather forecast for a given location. :: results = ws.get_weather_forecast_days(lat, long) for w in results['hits']: print w['start_datetime_local'] print w['reading_type'], w['reading_value'] For description of reading types: https://graphical.weather.gov/xml/docs/elementInputNames.php """ params = {} # Can get data from NWS1 or NWS3 representing 1-hr and 3-hr # intervals. if frequency not in [1, 3]: raise ValueError("Reading frequency must be 1 or 3") params['days'] = days params['source'] = 'NWS' + str(frequency) params['latitude'] = latitude params['longitude'] = longitude if reading_type: # url encoding will make spaces a + instead of %20, which service # interprets as an "and" search which is undesirable reading_type = reading_type.replace(' ', '%20') params['reading_type'] = urllib.quote_plus(reading_type) url = self.uri + '/v1/weather-forecast-days/' return self.service._get(url, params=params)
[ "def", "get_weather_forecast_days", "(", "self", ",", "latitude", ",", "longitude", ",", "days", "=", "1", ",", "frequency", "=", "1", ",", "reading_type", "=", "None", ")", ":", "params", "=", "{", "}", "# Can get data from NWS1 or NWS3 representing 1-hr and 3-hr", "# intervals.", "if", "frequency", "not", "in", "[", "1", ",", "3", "]", ":", "raise", "ValueError", "(", "\"Reading frequency must be 1 or 3\"", ")", "params", "[", "'days'", "]", "=", "days", "params", "[", "'source'", "]", "=", "'NWS'", "+", "str", "(", "frequency", ")", "params", "[", "'latitude'", "]", "=", "latitude", "params", "[", "'longitude'", "]", "=", "longitude", "if", "reading_type", ":", "# url encoding will make spaces a + instead of %20, which service", "# interprets as an \"and\" search which is undesirable", "reading_type", "=", "reading_type", ".", "replace", "(", "' '", ",", "'%20'", ")", "params", "[", "'reading_type'", "]", "=", "urllib", ".", "quote_plus", "(", "reading_type", ")", "url", "=", "self", ".", "uri", "+", "'/v1/weather-forecast-days/'", "return", "self", ".", "service", ".", "_get", "(", "url", ",", "params", "=", "params", ")" ]
Return the weather forecast for a given location. :: results = ws.get_weather_forecast_days(lat, long) for w in results['hits']: print w['start_datetime_local'] print w['reading_type'], w['reading_value'] For description of reading types: https://graphical.weather.gov/xml/docs/elementInputNames.php
[ "Return", "the", "weather", "forecast", "for", "a", "given", "location", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/data/weather.py#L36-L70
PredixDev/predixpy
predix/data/weather.py
WeatherForecast.get_weather_forecast
def get_weather_forecast(self, latitude, longitude, start, end, frequency=1, reading_type=None): """ Return the weather forecast for a given location for specific datetime specified in UTC format. :: results = ws.get_weather_forecast(lat, long, start, end) for w in results['hits']: print w['start_datetime_local'] print w['reading_type'], '=', w['reading_value'] For description of reading types: https://graphical.weather.gov/xml/docs/elementInputNames.php """ params = {} # Can get data from NWS1 or NWS3 representing 1-hr and 3-hr # intervals. if frequency not in [1, 3]: raise ValueError("Reading frequency must be 1 or 3") params['source'] = 'NWS' + str(frequency) params['latitude'] = latitude params['longitude'] = longitude params['start_datetime_utc'] = start params['end_datetime_utc'] = end if reading_type: # Not using urllib.quote_plus() because its using a + which is # being interpreted by service as an and instead of a space. reading_type = reading_type.replace(' ', '%20') params['reading_type'] = reading_type url = self.uri + '/v1/weather-forecast-datetime/' return self.service._get(url, params=params)
python
def get_weather_forecast(self, latitude, longitude, start, end, frequency=1, reading_type=None): """ Return the weather forecast for a given location for specific datetime specified in UTC format. :: results = ws.get_weather_forecast(lat, long, start, end) for w in results['hits']: print w['start_datetime_local'] print w['reading_type'], '=', w['reading_value'] For description of reading types: https://graphical.weather.gov/xml/docs/elementInputNames.php """ params = {} # Can get data from NWS1 or NWS3 representing 1-hr and 3-hr # intervals. if frequency not in [1, 3]: raise ValueError("Reading frequency must be 1 or 3") params['source'] = 'NWS' + str(frequency) params['latitude'] = latitude params['longitude'] = longitude params['start_datetime_utc'] = start params['end_datetime_utc'] = end if reading_type: # Not using urllib.quote_plus() because its using a + which is # being interpreted by service as an and instead of a space. reading_type = reading_type.replace(' ', '%20') params['reading_type'] = reading_type url = self.uri + '/v1/weather-forecast-datetime/' return self.service._get(url, params=params)
[ "def", "get_weather_forecast", "(", "self", ",", "latitude", ",", "longitude", ",", "start", ",", "end", ",", "frequency", "=", "1", ",", "reading_type", "=", "None", ")", ":", "params", "=", "{", "}", "# Can get data from NWS1 or NWS3 representing 1-hr and 3-hr", "# intervals.", "if", "frequency", "not", "in", "[", "1", ",", "3", "]", ":", "raise", "ValueError", "(", "\"Reading frequency must be 1 or 3\"", ")", "params", "[", "'source'", "]", "=", "'NWS'", "+", "str", "(", "frequency", ")", "params", "[", "'latitude'", "]", "=", "latitude", "params", "[", "'longitude'", "]", "=", "longitude", "params", "[", "'start_datetime_utc'", "]", "=", "start", "params", "[", "'end_datetime_utc'", "]", "=", "end", "if", "reading_type", ":", "# Not using urllib.quote_plus() because its using a + which is", "# being interpreted by service as an and instead of a space.", "reading_type", "=", "reading_type", ".", "replace", "(", "' '", ",", "'%20'", ")", "params", "[", "'reading_type'", "]", "=", "reading_type", "url", "=", "self", ".", "uri", "+", "'/v1/weather-forecast-datetime/'", "return", "self", ".", "service", ".", "_get", "(", "url", ",", "params", "=", "params", ")" ]
Return the weather forecast for a given location for specific datetime specified in UTC format. :: results = ws.get_weather_forecast(lat, long, start, end) for w in results['hits']: print w['start_datetime_local'] print w['reading_type'], '=', w['reading_value'] For description of reading types: https://graphical.weather.gov/xml/docs/elementInputNames.php
[ "Return", "the", "weather", "forecast", "for", "a", "given", "location", "for", "specific", "datetime", "specified", "in", "UTC", "format", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/data/weather.py#L72-L108
PredixDev/predixpy
predix/admin/service.py
CloudFoundryService._generate_name
def _generate_name(self, space, service_name, plan_name): """ Can generate a name based on the space, service name and plan. """ return str.join('-', [space, service_name, plan_name]).lower()
python
def _generate_name(self, space, service_name, plan_name): """ Can generate a name based on the space, service name and plan. """ return str.join('-', [space, service_name, plan_name]).lower()
[ "def", "_generate_name", "(", "self", ",", "space", ",", "service_name", ",", "plan_name", ")", ":", "return", "str", ".", "join", "(", "'-'", ",", "[", "space", ",", "service_name", ",", "plan_name", "]", ")", ".", "lower", "(", ")" ]
Can generate a name based on the space, service name and plan.
[ "Can", "generate", "a", "name", "based", "on", "the", "space", "service", "name", "and", "plan", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/service.py#L30-L34
PredixDev/predixpy
predix/admin/service.py
CloudFoundryService._get_config_path
def _get_config_path(self): """ Return a sensible configuration path for caching config settings. """ org = self.service.space.org.name space = self.service.space.name name = self.name return "~/.predix/%s/%s/%s.json" % (org, space, name)
python
def _get_config_path(self): """ Return a sensible configuration path for caching config settings. """ org = self.service.space.org.name space = self.service.space.name name = self.name return "~/.predix/%s/%s/%s.json" % (org, space, name)
[ "def", "_get_config_path", "(", "self", ")", ":", "org", "=", "self", ".", "service", ".", "space", ".", "org", ".", "name", "space", "=", "self", ".", "service", ".", "space", ".", "name", "name", "=", "self", ".", "name", "return", "\"~/.predix/%s/%s/%s.json\"", "%", "(", "org", ",", "space", ",", "name", ")" ]
Return a sensible configuration path for caching config settings.
[ "Return", "a", "sensible", "configuration", "path", "for", "caching", "config", "settings", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/service.py#L36-L45
PredixDev/predixpy
predix/admin/service.py
CloudFoundryService._create_service
def _create_service(self, parameters={}, **kwargs): """ Create a Cloud Foundry service that has custom parameters. """ logging.debug("_create_service()") logging.debug(str.join(',', [self.service_name, self.plan_name, self.name, str(parameters)])) return self.service.create_service(self.service_name, self.plan_name, self.name, parameters, **kwargs)
python
def _create_service(self, parameters={}, **kwargs): """ Create a Cloud Foundry service that has custom parameters. """ logging.debug("_create_service()") logging.debug(str.join(',', [self.service_name, self.plan_name, self.name, str(parameters)])) return self.service.create_service(self.service_name, self.plan_name, self.name, parameters, **kwargs)
[ "def", "_create_service", "(", "self", ",", "parameters", "=", "{", "}", ",", "*", "*", "kwargs", ")", ":", "logging", ".", "debug", "(", "\"_create_service()\"", ")", "logging", ".", "debug", "(", "str", ".", "join", "(", "','", ",", "[", "self", ".", "service_name", ",", "self", ".", "plan_name", ",", "self", ".", "name", ",", "str", "(", "parameters", ")", "]", ")", ")", "return", "self", ".", "service", ".", "create_service", "(", "self", ".", "service_name", ",", "self", ".", "plan_name", ",", "self", ".", "name", ",", "parameters", ",", "*", "*", "kwargs", ")" ]
Create a Cloud Foundry service that has custom parameters.
[ "Create", "a", "Cloud", "Foundry", "service", "that", "has", "custom", "parameters", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/service.py#L47-L56
PredixDev/predixpy
predix/admin/service.py
CloudFoundryService._delete_service
def _delete_service(self, service_only=False): """ Delete a Cloud Foundry service and any associations. """ logging.debug('_delete_service()') return self.service.delete_service(self.service_name)
python
def _delete_service(self, service_only=False): """ Delete a Cloud Foundry service and any associations. """ logging.debug('_delete_service()') return self.service.delete_service(self.service_name)
[ "def", "_delete_service", "(", "self", ",", "service_only", "=", "False", ")", ":", "logging", ".", "debug", "(", "'_delete_service()'", ")", "return", "self", ".", "service", ".", "delete_service", "(", "self", ".", "service_name", ")" ]
Delete a Cloud Foundry service and any associations.
[ "Delete", "a", "Cloud", "Foundry", "service", "and", "any", "associations", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/service.py#L58-L63
PredixDev/predixpy
predix/admin/service.py
CloudFoundryService._get_or_create_service_key
def _get_or_create_service_key(self): """ Get a service key or create one if needed. """ keys = self.service._get_service_keys(self.name) for key in keys['resources']: if key['entity']['name'] == self.service_name: return self.service.get_service_key(self.name, self.service_name) self.service.create_service_key(self.name, self.service_name) return self.service.get_service_key(self.name, self.service_name)
python
def _get_or_create_service_key(self): """ Get a service key or create one if needed. """ keys = self.service._get_service_keys(self.name) for key in keys['resources']: if key['entity']['name'] == self.service_name: return self.service.get_service_key(self.name, self.service_name) self.service.create_service_key(self.name, self.service_name) return self.service.get_service_key(self.name, self.service_name)
[ "def", "_get_or_create_service_key", "(", "self", ")", ":", "keys", "=", "self", ".", "service", ".", "_get_service_keys", "(", "self", ".", "name", ")", "for", "key", "in", "keys", "[", "'resources'", "]", ":", "if", "key", "[", "'entity'", "]", "[", "'name'", "]", "==", "self", ".", "service_name", ":", "return", "self", ".", "service", ".", "get_service_key", "(", "self", ".", "name", ",", "self", ".", "service_name", ")", "self", ".", "service", ".", "create_service_key", "(", "self", ".", "name", ",", "self", ".", "service_name", ")", "return", "self", ".", "service", ".", "get_service_key", "(", "self", ".", "name", ",", "self", ".", "service_name", ")" ]
Get a service key or create one if needed.
[ "Get", "a", "service", "key", "or", "create", "one", "if", "needed", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/service.py#L65-L76
PredixDev/predixpy
predix/admin/service.py
CloudFoundryService._get_service_config
def _get_service_config(self): """ Will get configuration for the service from a service key. """ key = self._get_or_create_service_key() config = {} config['service_key'] = [{'name': self.name}] config.update(key['entity']['credentials']) return config
python
def _get_service_config(self): """ Will get configuration for the service from a service key. """ key = self._get_or_create_service_key() config = {} config['service_key'] = [{'name': self.name}] config.update(key['entity']['credentials']) return config
[ "def", "_get_service_config", "(", "self", ")", ":", "key", "=", "self", ".", "_get_or_create_service_key", "(", ")", "config", "=", "{", "}", "config", "[", "'service_key'", "]", "=", "[", "{", "'name'", ":", "self", ".", "name", "}", "]", "config", ".", "update", "(", "key", "[", "'entity'", "]", "[", "'credentials'", "]", ")", "return", "config" ]
Will get configuration for the service from a service key.
[ "Will", "get", "configuration", "for", "the", "service", "from", "a", "service", "key", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/service.py#L78-L88
PredixDev/predixpy
predix/admin/service.py
CloudFoundryService.create
def create(self, parameters={}, create_keys=True, **kwargs): """ Create the service. """ # Create the service cs = self._create_service(parameters=parameters, **kwargs) # Create the service key to get config details and # store in local cache file. if create_keys: cfg = parameters cfg.update(self._get_service_config()) self.settings.save(cfg)
python
def create(self, parameters={}, create_keys=True, **kwargs): """ Create the service. """ # Create the service cs = self._create_service(parameters=parameters, **kwargs) # Create the service key to get config details and # store in local cache file. if create_keys: cfg = parameters cfg.update(self._get_service_config()) self.settings.save(cfg)
[ "def", "create", "(", "self", ",", "parameters", "=", "{", "}", ",", "create_keys", "=", "True", ",", "*", "*", "kwargs", ")", ":", "# Create the service", "cs", "=", "self", ".", "_create_service", "(", "parameters", "=", "parameters", ",", "*", "*", "kwargs", ")", "# Create the service key to get config details and", "# store in local cache file.", "if", "create_keys", ":", "cfg", "=", "parameters", "cfg", ".", "update", "(", "self", ".", "_get_service_config", "(", ")", ")", "self", ".", "settings", ".", "save", "(", "cfg", ")" ]
Create the service.
[ "Create", "the", "service", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/service.py#L96-L108
PredixDev/predixpy
predix/admin/service.py
PredixService._get_or_create_uaa
def _get_or_create_uaa(self, uaa): """ Returns a valid UAA instance for performing administrative functions on services. """ if isinstance(uaa, predix.admin.uaa.UserAccountAuthentication): return uaa logging.debug("Initializing a new UAA") return predix.admin.uaa.UserAccountAuthentication()
python
def _get_or_create_uaa(self, uaa): """ Returns a valid UAA instance for performing administrative functions on services. """ if isinstance(uaa, predix.admin.uaa.UserAccountAuthentication): return uaa logging.debug("Initializing a new UAA") return predix.admin.uaa.UserAccountAuthentication()
[ "def", "_get_or_create_uaa", "(", "self", ",", "uaa", ")", ":", "if", "isinstance", "(", "uaa", ",", "predix", ".", "admin", ".", "uaa", ".", "UserAccountAuthentication", ")", ":", "return", "uaa", "logging", ".", "debug", "(", "\"Initializing a new UAA\"", ")", "return", "predix", ".", "admin", ".", "uaa", ".", "UserAccountAuthentication", "(", ")" ]
Returns a valid UAA instance for performing administrative functions on services.
[ "Returns", "a", "valid", "UAA", "instance", "for", "performing", "administrative", "functions", "on", "services", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/service.py#L124-L133
PredixDev/predixpy
predix/admin/service.py
PredixService.create
def create(self, parameters={}, **kwargs): """ Create an instance of the US Weather Forecast Service with typical starting settings. """ # Add parameter during create for UAA issuer uri = self.uaa.service.settings.data['uri'] + '/oauth/token' parameters["trustedIssuerIds"] = [uri] super(PredixService, self).create(parameters=parameters, **kwargs)
python
def create(self, parameters={}, **kwargs): """ Create an instance of the US Weather Forecast Service with typical starting settings. """ # Add parameter during create for UAA issuer uri = self.uaa.service.settings.data['uri'] + '/oauth/token' parameters["trustedIssuerIds"] = [uri] super(PredixService, self).create(parameters=parameters, **kwargs)
[ "def", "create", "(", "self", ",", "parameters", "=", "{", "}", ",", "*", "*", "kwargs", ")", ":", "# Add parameter during create for UAA issuer", "uri", "=", "self", ".", "uaa", ".", "service", ".", "settings", ".", "data", "[", "'uri'", "]", "+", "'/oauth/token'", "parameters", "[", "\"trustedIssuerIds\"", "]", "=", "[", "uri", "]", "super", "(", "PredixService", ",", "self", ")", ".", "create", "(", "parameters", "=", "parameters", ",", "*", "*", "kwargs", ")" ]
Create an instance of the US Weather Forecast Service with typical starting settings.
[ "Create", "an", "instance", "of", "the", "US", "Weather", "Forecast", "Service", "with", "typical", "starting", "settings", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/service.py#L135-L143
PredixDev/predixpy
predix/admin/eventhub.py
EventHub.create
def create(self): """ Create an instance of the Time Series Service with the typical starting settings. """ self.service.create() os.environ[predix.config.get_env_key(self.use_class, 'host')] = self.get_eventhub_host() os.environ[predix.config.get_env_key(self.use_class, 'port')] = self.get_eventhub_grpc_port() os.environ[predix.config.get_env_key(self.use_class, 'wss_publish_uri')] = self.get_publish_wss_uri() os.environ[predix.config.get_env_key(self.use_class, 'zone_id')] = self.get_zone_id()
python
def create(self): """ Create an instance of the Time Series Service with the typical starting settings. """ self.service.create() os.environ[predix.config.get_env_key(self.use_class, 'host')] = self.get_eventhub_host() os.environ[predix.config.get_env_key(self.use_class, 'port')] = self.get_eventhub_grpc_port() os.environ[predix.config.get_env_key(self.use_class, 'wss_publish_uri')] = self.get_publish_wss_uri() os.environ[predix.config.get_env_key(self.use_class, 'zone_id')] = self.get_zone_id()
[ "def", "create", "(", "self", ")", ":", "self", ".", "service", ".", "create", "(", ")", "os", ".", "environ", "[", "predix", ".", "config", ".", "get_env_key", "(", "self", ".", "use_class", ",", "'host'", ")", "]", "=", "self", ".", "get_eventhub_host", "(", ")", "os", ".", "environ", "[", "predix", ".", "config", ".", "get_env_key", "(", "self", ".", "use_class", ",", "'port'", ")", "]", "=", "self", ".", "get_eventhub_grpc_port", "(", ")", "os", ".", "environ", "[", "predix", ".", "config", ".", "get_env_key", "(", "self", ".", "use_class", ",", "'wss_publish_uri'", ")", "]", "=", "self", ".", "get_publish_wss_uri", "(", ")", "os", ".", "environ", "[", "predix", ".", "config", ".", "get_env_key", "(", "self", ".", "use_class", ",", "'zone_id'", ")", "]", "=", "self", ".", "get_zone_id", "(", ")" ]
Create an instance of the Time Series Service with the typical starting settings.
[ "Create", "an", "instance", "of", "the", "Time", "Series", "Service", "with", "the", "typical", "starting", "settings", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/eventhub.py#L28-L38
PredixDev/predixpy
predix/admin/eventhub.py
EventHub.grant_client
def grant_client(self, client_id, publish=False, subscribe=False, publish_protocol=None, publish_topics=None, subscribe_topics=None, scope_prefix='predix-event-hub', **kwargs): """ Grant the given client id all the scopes and authorities needed to work with the eventhub service. """ scopes = ['openid'] authorities = ['uaa.resource'] zone_id = self.get_zone_id() # always must be part of base user scope scopes.append('%s.zones.%s.user' % (scope_prefix, zone_id)) authorities.append('%s.zones.%s.user' % (scope_prefix, zone_id)) if publish_topics is not None or subscribe_topics is not None: raise Exception("multiple topics are not currently available in preidx-py") if publish_topics is None: publish_topics = ['topic'] if subscribe_topics is None: subscribe_topics = ['topic'] if publish: # we are granting just the default topic if publish_protocol is None: scopes.append('%s.zones.%s.grpc.publish' % (scope_prefix, zone_id)) authorities.append('%s.zones.%s.grpc.publish' % (scope_prefix, zone_id)) scopes.append('%s.zones.%s.wss.publish' % (scope_prefix, zone_id)) authorities.append('%s.zones.%s.wss.publish' % (scope_prefix, zone_id)) else: scopes.append('%s.zones.%s.%s.publish' % (scope_prefix, zone_id, publish_protocol)) authorities.append('%s.zones.%s.%s.publish' % (scope_prefix, zone_id, publish_protocol)) # we are requesting multiple topics for topic in publish_topics: if publish_protocol is None: scopes.append('%s.zones.%s.%s.grpc.publish' % (scope_prefix, zone_id, topic)) scopes.append('%s.zones.%s.%s.wss.publish' % (scope_prefix, zone_id, topic)) scopes.append('%s.zones.%s.%s.user' % (scope_prefix, zone_id, topic)) authorities.append('%s.zones.%s.%s.grpc.publish' % (scope_prefix, zone_id, topic)) authorities.append('%s.zones.%s.%s.wss.publish' % (scope_prefix, zone_id, topic)) authorities.append('%s.zones.%s.%s.user' % (scope_prefix, zone_id, topic)) else: scopes.append('%s.zones.%s.%s.%s.publish' % (scope_prefix, zone_id, topic, publish_protocol)) authorities.append('%s.zones.%s.%s.%s.publish' % (scope_prefix, zone_id, topic, publish_protocol)) if subscribe: # we are granting just the default topic scopes.append('%s.zones.%s.grpc.subscribe' % (scope_prefix, zone_id)) authorities.append('%s.zones.%s.grpc.subscribe' % (scope_prefix, zone_id)) # we are requesting multiple topics for topic in subscribe_topics: scopes.append('%s.zones.%s.%s.grpc.subscribe' % (scope_prefix, zone_id, topic)) authorities.append('%s.zones.%s.%s.grpc.subscribe' % (scope_prefix, zone_id, topic)) self.service.uaa.uaac.update_client_grants(client_id, scope=scopes, authorities=authorities) return self.service.uaa.uaac.get_client(client_id)
python
def grant_client(self, client_id, publish=False, subscribe=False, publish_protocol=None, publish_topics=None, subscribe_topics=None, scope_prefix='predix-event-hub', **kwargs): """ Grant the given client id all the scopes and authorities needed to work with the eventhub service. """ scopes = ['openid'] authorities = ['uaa.resource'] zone_id = self.get_zone_id() # always must be part of base user scope scopes.append('%s.zones.%s.user' % (scope_prefix, zone_id)) authorities.append('%s.zones.%s.user' % (scope_prefix, zone_id)) if publish_topics is not None or subscribe_topics is not None: raise Exception("multiple topics are not currently available in preidx-py") if publish_topics is None: publish_topics = ['topic'] if subscribe_topics is None: subscribe_topics = ['topic'] if publish: # we are granting just the default topic if publish_protocol is None: scopes.append('%s.zones.%s.grpc.publish' % (scope_prefix, zone_id)) authorities.append('%s.zones.%s.grpc.publish' % (scope_prefix, zone_id)) scopes.append('%s.zones.%s.wss.publish' % (scope_prefix, zone_id)) authorities.append('%s.zones.%s.wss.publish' % (scope_prefix, zone_id)) else: scopes.append('%s.zones.%s.%s.publish' % (scope_prefix, zone_id, publish_protocol)) authorities.append('%s.zones.%s.%s.publish' % (scope_prefix, zone_id, publish_protocol)) # we are requesting multiple topics for topic in publish_topics: if publish_protocol is None: scopes.append('%s.zones.%s.%s.grpc.publish' % (scope_prefix, zone_id, topic)) scopes.append('%s.zones.%s.%s.wss.publish' % (scope_prefix, zone_id, topic)) scopes.append('%s.zones.%s.%s.user' % (scope_prefix, zone_id, topic)) authorities.append('%s.zones.%s.%s.grpc.publish' % (scope_prefix, zone_id, topic)) authorities.append('%s.zones.%s.%s.wss.publish' % (scope_prefix, zone_id, topic)) authorities.append('%s.zones.%s.%s.user' % (scope_prefix, zone_id, topic)) else: scopes.append('%s.zones.%s.%s.%s.publish' % (scope_prefix, zone_id, topic, publish_protocol)) authorities.append('%s.zones.%s.%s.%s.publish' % (scope_prefix, zone_id, topic, publish_protocol)) if subscribe: # we are granting just the default topic scopes.append('%s.zones.%s.grpc.subscribe' % (scope_prefix, zone_id)) authorities.append('%s.zones.%s.grpc.subscribe' % (scope_prefix, zone_id)) # we are requesting multiple topics for topic in subscribe_topics: scopes.append('%s.zones.%s.%s.grpc.subscribe' % (scope_prefix, zone_id, topic)) authorities.append('%s.zones.%s.%s.grpc.subscribe' % (scope_prefix, zone_id, topic)) self.service.uaa.uaac.update_client_grants(client_id, scope=scopes, authorities=authorities) return self.service.uaa.uaac.get_client(client_id)
[ "def", "grant_client", "(", "self", ",", "client_id", ",", "publish", "=", "False", ",", "subscribe", "=", "False", ",", "publish_protocol", "=", "None", ",", "publish_topics", "=", "None", ",", "subscribe_topics", "=", "None", ",", "scope_prefix", "=", "'predix-event-hub'", ",", "*", "*", "kwargs", ")", ":", "scopes", "=", "[", "'openid'", "]", "authorities", "=", "[", "'uaa.resource'", "]", "zone_id", "=", "self", ".", "get_zone_id", "(", ")", "# always must be part of base user scope", "scopes", ".", "append", "(", "'%s.zones.%s.user'", "%", "(", "scope_prefix", ",", "zone_id", ")", ")", "authorities", ".", "append", "(", "'%s.zones.%s.user'", "%", "(", "scope_prefix", ",", "zone_id", ")", ")", "if", "publish_topics", "is", "not", "None", "or", "subscribe_topics", "is", "not", "None", ":", "raise", "Exception", "(", "\"multiple topics are not currently available in preidx-py\"", ")", "if", "publish_topics", "is", "None", ":", "publish_topics", "=", "[", "'topic'", "]", "if", "subscribe_topics", "is", "None", ":", "subscribe_topics", "=", "[", "'topic'", "]", "if", "publish", ":", "# we are granting just the default topic", "if", "publish_protocol", "is", "None", ":", "scopes", ".", "append", "(", "'%s.zones.%s.grpc.publish'", "%", "(", "scope_prefix", ",", "zone_id", ")", ")", "authorities", ".", "append", "(", "'%s.zones.%s.grpc.publish'", "%", "(", "scope_prefix", ",", "zone_id", ")", ")", "scopes", ".", "append", "(", "'%s.zones.%s.wss.publish'", "%", "(", "scope_prefix", ",", "zone_id", ")", ")", "authorities", ".", "append", "(", "'%s.zones.%s.wss.publish'", "%", "(", "scope_prefix", ",", "zone_id", ")", ")", "else", ":", "scopes", ".", "append", "(", "'%s.zones.%s.%s.publish'", "%", "(", "scope_prefix", ",", "zone_id", ",", "publish_protocol", ")", ")", "authorities", ".", "append", "(", "'%s.zones.%s.%s.publish'", "%", "(", "scope_prefix", ",", "zone_id", ",", "publish_protocol", ")", ")", "# we are requesting multiple topics", "for", "topic", "in", "publish_topics", ":", "if", "publish_protocol", "is", "None", ":", "scopes", ".", "append", "(", "'%s.zones.%s.%s.grpc.publish'", "%", "(", "scope_prefix", ",", "zone_id", ",", "topic", ")", ")", "scopes", ".", "append", "(", "'%s.zones.%s.%s.wss.publish'", "%", "(", "scope_prefix", ",", "zone_id", ",", "topic", ")", ")", "scopes", ".", "append", "(", "'%s.zones.%s.%s.user'", "%", "(", "scope_prefix", ",", "zone_id", ",", "topic", ")", ")", "authorities", ".", "append", "(", "'%s.zones.%s.%s.grpc.publish'", "%", "(", "scope_prefix", ",", "zone_id", ",", "topic", ")", ")", "authorities", ".", "append", "(", "'%s.zones.%s.%s.wss.publish'", "%", "(", "scope_prefix", ",", "zone_id", ",", "topic", ")", ")", "authorities", ".", "append", "(", "'%s.zones.%s.%s.user'", "%", "(", "scope_prefix", ",", "zone_id", ",", "topic", ")", ")", "else", ":", "scopes", ".", "append", "(", "'%s.zones.%s.%s.%s.publish'", "%", "(", "scope_prefix", ",", "zone_id", ",", "topic", ",", "publish_protocol", ")", ")", "authorities", ".", "append", "(", "'%s.zones.%s.%s.%s.publish'", "%", "(", "scope_prefix", ",", "zone_id", ",", "topic", ",", "publish_protocol", ")", ")", "if", "subscribe", ":", "# we are granting just the default topic", "scopes", ".", "append", "(", "'%s.zones.%s.grpc.subscribe'", "%", "(", "scope_prefix", ",", "zone_id", ")", ")", "authorities", ".", "append", "(", "'%s.zones.%s.grpc.subscribe'", "%", "(", "scope_prefix", ",", "zone_id", ")", ")", "# we are requesting multiple topics", "for", "topic", "in", "subscribe_topics", ":", "scopes", ".", "append", "(", "'%s.zones.%s.%s.grpc.subscribe'", "%", "(", "scope_prefix", ",", "zone_id", ",", "topic", ")", ")", "authorities", ".", "append", "(", "'%s.zones.%s.%s.grpc.subscribe'", "%", "(", "scope_prefix", ",", "zone_id", ",", "topic", ")", ")", "self", ".", "service", ".", "uaa", ".", "uaac", ".", "update_client_grants", "(", "client_id", ",", "scope", "=", "scopes", ",", "authorities", "=", "authorities", ")", "return", "self", ".", "service", ".", "uaa", ".", "uaac", ".", "get_client", "(", "client_id", ")" ]
Grant the given client id all the scopes and authorities needed to work with the eventhub service.
[ "Grant", "the", "given", "client", "id", "all", "the", "scopes", "and", "authorities", "needed", "to", "work", "with", "the", "eventhub", "service", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/eventhub.py#L40-L100
PredixDev/predixpy
predix/admin/eventhub.py
EventHub.get_eventhub_host
def get_eventhub_host(self): """ returns the publish grpc endpoint for ingestion. """ for protocol in self.service.settings.data['publish']['protocol_details']: if protocol['protocol'] == 'grpc': return protocol['uri'][0:protocol['uri'].index(':')]
python
def get_eventhub_host(self): """ returns the publish grpc endpoint for ingestion. """ for protocol in self.service.settings.data['publish']['protocol_details']: if protocol['protocol'] == 'grpc': return protocol['uri'][0:protocol['uri'].index(':')]
[ "def", "get_eventhub_host", "(", "self", ")", ":", "for", "protocol", "in", "self", ".", "service", ".", "settings", ".", "data", "[", "'publish'", "]", "[", "'protocol_details'", "]", ":", "if", "protocol", "[", "'protocol'", "]", "==", "'grpc'", ":", "return", "protocol", "[", "'uri'", "]", "[", "0", ":", "protocol", "[", "'uri'", "]", ".", "index", "(", "':'", ")", "]" ]
returns the publish grpc endpoint for ingestion.
[ "returns", "the", "publish", "grpc", "endpoint", "for", "ingestion", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/eventhub.py#L102-L108
PredixDev/predixpy
predix/admin/eventhub.py
EventHub.add_to_manifest
def add_to_manifest(self, manifest): """ Add useful details to the manifest about this service so that it can be used in an application. :param manifest: An predix.admin.app.Manifest object instance that manages reading/writing manifest config for a cloud foundry app. """ # Add this service to list of services manifest.add_service(self.service.name) # Add environment variables manifest.add_env_var(predix.config.get_env_key(self.use_class, 'host'), self.get_eventhub_host()) manifest.add_env_var(predix.config.get_env_key(self.use_class, 'port'), self.get_eventhub_grpc_port()) manifest.add_env_var(predix.config.get_env_key(self.use_class, 'wss_publish_uri'), self.get_publish_wss_uri()) manifest.add_env_var(predix.config.get_env_key(self.use_class, 'zone_id'), self.get_zone_id()) manifest.write_manifest()
python
def add_to_manifest(self, manifest): """ Add useful details to the manifest about this service so that it can be used in an application. :param manifest: An predix.admin.app.Manifest object instance that manages reading/writing manifest config for a cloud foundry app. """ # Add this service to list of services manifest.add_service(self.service.name) # Add environment variables manifest.add_env_var(predix.config.get_env_key(self.use_class, 'host'), self.get_eventhub_host()) manifest.add_env_var(predix.config.get_env_key(self.use_class, 'port'), self.get_eventhub_grpc_port()) manifest.add_env_var(predix.config.get_env_key(self.use_class, 'wss_publish_uri'), self.get_publish_wss_uri()) manifest.add_env_var(predix.config.get_env_key(self.use_class, 'zone_id'), self.get_zone_id()) manifest.write_manifest()
[ "def", "add_to_manifest", "(", "self", ",", "manifest", ")", ":", "# Add this service to list of services", "manifest", ".", "add_service", "(", "self", ".", "service", ".", "name", ")", "# Add environment variables", "manifest", ".", "add_env_var", "(", "predix", ".", "config", ".", "get_env_key", "(", "self", ".", "use_class", ",", "'host'", ")", ",", "self", ".", "get_eventhub_host", "(", ")", ")", "manifest", ".", "add_env_var", "(", "predix", ".", "config", ".", "get_env_key", "(", "self", ".", "use_class", ",", "'port'", ")", ",", "self", ".", "get_eventhub_grpc_port", "(", ")", ")", "manifest", ".", "add_env_var", "(", "predix", ".", "config", ".", "get_env_key", "(", "self", ".", "use_class", ",", "'wss_publish_uri'", ")", ",", "self", ".", "get_publish_wss_uri", "(", ")", ")", "manifest", ".", "add_env_var", "(", "predix", ".", "config", ".", "get_env_key", "(", "self", ".", "use_class", ",", "'zone_id'", ")", ",", "self", ".", "get_zone_id", "(", ")", ")", "manifest", ".", "write_manifest", "(", ")" ]
Add useful details to the manifest about this service so that it can be used in an application. :param manifest: An predix.admin.app.Manifest object instance that manages reading/writing manifest config for a cloud foundry app.
[ "Add", "useful", "details", "to", "the", "manifest", "about", "this", "service", "so", "that", "it", "can", "be", "used", "in", "an", "application", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/eventhub.py#L133-L151
PredixDev/predixpy
predix/data/blobstore.py
BlobStore._get_host
def _get_host(self): """ Returns the host address for an instance of Blob Store service from environment inspection. """ if 'VCAP_SERVICES' in os.environ: services = json.loads(os.getenv('VCAP_SERVICES')) host = services['predix-blobstore'][0]['credentials']['host'] else: host = predix.config.get_env_value(self, 'host') # Protocol may not always be included in host setting if 'https://' not in host: host = 'https://' + host return host
python
def _get_host(self): """ Returns the host address for an instance of Blob Store service from environment inspection. """ if 'VCAP_SERVICES' in os.environ: services = json.loads(os.getenv('VCAP_SERVICES')) host = services['predix-blobstore'][0]['credentials']['host'] else: host = predix.config.get_env_value(self, 'host') # Protocol may not always be included in host setting if 'https://' not in host: host = 'https://' + host return host
[ "def", "_get_host", "(", "self", ")", ":", "if", "'VCAP_SERVICES'", "in", "os", ".", "environ", ":", "services", "=", "json", ".", "loads", "(", "os", ".", "getenv", "(", "'VCAP_SERVICES'", ")", ")", "host", "=", "services", "[", "'predix-blobstore'", "]", "[", "0", "]", "[", "'credentials'", "]", "[", "'host'", "]", "else", ":", "host", "=", "predix", ".", "config", ".", "get_env_value", "(", "self", ",", "'host'", ")", "# Protocol may not always be included in host setting", "if", "'https://'", "not", "in", "host", ":", "host", "=", "'https://'", "+", "host", "return", "host" ]
Returns the host address for an instance of Blob Store service from environment inspection.
[ "Returns", "the", "host", "address", "for", "an", "instance", "of", "Blob", "Store", "service", "from", "environment", "inspection", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/data/blobstore.py#L50-L65
PredixDev/predixpy
predix/data/blobstore.py
BlobStore._get_access_key_id
def _get_access_key_id(self): """ Returns the access key for an instance of Blob Store service from environment inspection. """ if 'VCAP_SERVICES' in os.environ: services = json.loads(os.getenv('VCAP_SERVICES')) return services['predix-blobstore'][0]['credentials']['access_key_id'] else: return predix.config.get_env_value(self, 'access_key_id')
python
def _get_access_key_id(self): """ Returns the access key for an instance of Blob Store service from environment inspection. """ if 'VCAP_SERVICES' in os.environ: services = json.loads(os.getenv('VCAP_SERVICES')) return services['predix-blobstore'][0]['credentials']['access_key_id'] else: return predix.config.get_env_value(self, 'access_key_id')
[ "def", "_get_access_key_id", "(", "self", ")", ":", "if", "'VCAP_SERVICES'", "in", "os", ".", "environ", ":", "services", "=", "json", ".", "loads", "(", "os", ".", "getenv", "(", "'VCAP_SERVICES'", ")", ")", "return", "services", "[", "'predix-blobstore'", "]", "[", "0", "]", "[", "'credentials'", "]", "[", "'access_key_id'", "]", "else", ":", "return", "predix", ".", "config", ".", "get_env_value", "(", "self", ",", "'access_key_id'", ")" ]
Returns the access key for an instance of Blob Store service from environment inspection.
[ "Returns", "the", "access", "key", "for", "an", "instance", "of", "Blob", "Store", "service", "from", "environment", "inspection", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/data/blobstore.py#L67-L76
PredixDev/predixpy
predix/data/blobstore.py
BlobStore.list_objects
def list_objects(self, bucket_name=None, **kwargs): """ This method is primarily for illustration and just calls the boto3 client implementation of list_objects but is a common task for first time Predix BlobStore users. """ if not bucket_name: bucket_name = self.bucket_name return self.client.list_objects(Bucket=bucket_name, **kwargs)
python
def list_objects(self, bucket_name=None, **kwargs): """ This method is primarily for illustration and just calls the boto3 client implementation of list_objects but is a common task for first time Predix BlobStore users. """ if not bucket_name: bucket_name = self.bucket_name return self.client.list_objects(Bucket=bucket_name, **kwargs)
[ "def", "list_objects", "(", "self", ",", "bucket_name", "=", "None", ",", "*", "*", "kwargs", ")", ":", "if", "not", "bucket_name", ":", "bucket_name", "=", "self", ".", "bucket_name", "return", "self", ".", "client", ".", "list_objects", "(", "Bucket", "=", "bucket_name", ",", "*", "*", "kwargs", ")" ]
This method is primarily for illustration and just calls the boto3 client implementation of list_objects but is a common task for first time Predix BlobStore users.
[ "This", "method", "is", "primarily", "for", "illustration", "and", "just", "calls", "the", "boto3", "client", "implementation", "of", "list_objects", "but", "is", "a", "common", "task", "for", "first", "time", "Predix", "BlobStore", "users", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/data/blobstore.py#L108-L115
PredixDev/predixpy
predix/data/blobstore.py
BlobStore.upload_file
def upload_file(self, src_filepath, dest_filename=None, bucket_name=None, **kwargs): """ This method is primarily for illustration and just calls the boto3 client implementation of upload_file but is a common task for first time Predix BlobStore users. """ if not bucket_name: bucket_name = self.bucket_name if not dest_filename: dest_filename = src_filepath return self.client.upload_file(src_filepath, bucket_name, dest_filename, **kwargs)
python
def upload_file(self, src_filepath, dest_filename=None, bucket_name=None, **kwargs): """ This method is primarily for illustration and just calls the boto3 client implementation of upload_file but is a common task for first time Predix BlobStore users. """ if not bucket_name: bucket_name = self.bucket_name if not dest_filename: dest_filename = src_filepath return self.client.upload_file(src_filepath, bucket_name, dest_filename, **kwargs)
[ "def", "upload_file", "(", "self", ",", "src_filepath", ",", "dest_filename", "=", "None", ",", "bucket_name", "=", "None", ",", "*", "*", "kwargs", ")", ":", "if", "not", "bucket_name", ":", "bucket_name", "=", "self", ".", "bucket_name", "if", "not", "dest_filename", ":", "dest_filename", "=", "src_filepath", "return", "self", ".", "client", ".", "upload_file", "(", "src_filepath", ",", "bucket_name", ",", "dest_filename", ",", "*", "*", "kwargs", ")" ]
This method is primarily for illustration and just calls the boto3 client implementation of upload_file but is a common task for first time Predix BlobStore users.
[ "This", "method", "is", "primarily", "for", "illustration", "and", "just", "calls", "the", "boto3", "client", "implementation", "of", "upload_file", "but", "is", "a", "common", "task", "for", "first", "time", "Predix", "BlobStore", "users", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/data/blobstore.py#L117-L127
PredixDev/predixpy
predix/admin/cf/config.py
Config._get_cloud_foundry_config
def _get_cloud_foundry_config(self): """ Reads the local cf CLI cache stored in the users home directory. """ config = os.path.expanduser(self.config_file) if not os.path.exists(config): raise CloudFoundryLoginError('You must run `cf login` to authenticate') with open(config, "r") as data: return json.load(data)
python
def _get_cloud_foundry_config(self): """ Reads the local cf CLI cache stored in the users home directory. """ config = os.path.expanduser(self.config_file) if not os.path.exists(config): raise CloudFoundryLoginError('You must run `cf login` to authenticate') with open(config, "r") as data: return json.load(data)
[ "def", "_get_cloud_foundry_config", "(", "self", ")", ":", "config", "=", "os", ".", "path", ".", "expanduser", "(", "self", ".", "config_file", ")", "if", "not", "os", ".", "path", ".", "exists", "(", "config", ")", ":", "raise", "CloudFoundryLoginError", "(", "'You must run `cf login` to authenticate'", ")", "with", "open", "(", "config", ",", "\"r\"", ")", "as", "data", ":", "return", "json", ".", "load", "(", "data", ")" ]
Reads the local cf CLI cache stored in the users home directory.
[ "Reads", "the", "local", "cf", "CLI", "cache", "stored", "in", "the", "users", "home", "directory", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/cf/config.py#L28-L38
PredixDev/predixpy
predix/admin/cf/config.py
Config.get_organization_guid
def get_organization_guid(self): """ Returns the GUID for the organization currently targeted. """ if 'PREDIX_ORGANIZATION_GUID' in os.environ: return os.environ['PREDIX_ORGANIZATION_GUID'] else: info = self._get_organization_info() for key in ('Guid', 'GUID'): if key in info.keys(): return info[key] raise ValueError('Unable to determine cf organization guid')
python
def get_organization_guid(self): """ Returns the GUID for the organization currently targeted. """ if 'PREDIX_ORGANIZATION_GUID' in os.environ: return os.environ['PREDIX_ORGANIZATION_GUID'] else: info = self._get_organization_info() for key in ('Guid', 'GUID'): if key in info.keys(): return info[key] raise ValueError('Unable to determine cf organization guid')
[ "def", "get_organization_guid", "(", "self", ")", ":", "if", "'PREDIX_ORGANIZATION_GUID'", "in", "os", ".", "environ", ":", "return", "os", ".", "environ", "[", "'PREDIX_ORGANIZATION_GUID'", "]", "else", ":", "info", "=", "self", ".", "_get_organization_info", "(", ")", "for", "key", "in", "(", "'Guid'", ",", "'GUID'", ")", ":", "if", "key", "in", "info", ".", "keys", "(", ")", ":", "return", "info", "[", "key", "]", "raise", "ValueError", "(", "'Unable to determine cf organization guid'", ")" ]
Returns the GUID for the organization currently targeted.
[ "Returns", "the", "GUID", "for", "the", "organization", "currently", "targeted", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/cf/config.py#L75-L86
PredixDev/predixpy
predix/admin/cf/config.py
Config.get_space_guid
def get_space_guid(self): """ Returns the GUID for the space currently targeted. Can be set by environment variable with PREDIX_SPACE_GUID. Can be determined by ~/.cf/config.json. """ if 'PREDIX_SPACE_GUID' in os.environ: return os.environ['PREDIX_SPACE_GUID'] else: info = self._get_space_info() for key in ('Guid', 'GUID'): if key in info.keys(): return info[key] raise ValueError('Unable to determine cf space guid')
python
def get_space_guid(self): """ Returns the GUID for the space currently targeted. Can be set by environment variable with PREDIX_SPACE_GUID. Can be determined by ~/.cf/config.json. """ if 'PREDIX_SPACE_GUID' in os.environ: return os.environ['PREDIX_SPACE_GUID'] else: info = self._get_space_info() for key in ('Guid', 'GUID'): if key in info.keys(): return info[key] raise ValueError('Unable to determine cf space guid')
[ "def", "get_space_guid", "(", "self", ")", ":", "if", "'PREDIX_SPACE_GUID'", "in", "os", ".", "environ", ":", "return", "os", ".", "environ", "[", "'PREDIX_SPACE_GUID'", "]", "else", ":", "info", "=", "self", ".", "_get_space_info", "(", ")", "for", "key", "in", "(", "'Guid'", ",", "'GUID'", ")", ":", "if", "key", "in", "info", ".", "keys", "(", ")", ":", "return", "info", "[", "key", "]", "raise", "ValueError", "(", "'Unable to determine cf space guid'", ")" ]
Returns the GUID for the space currently targeted. Can be set by environment variable with PREDIX_SPACE_GUID. Can be determined by ~/.cf/config.json.
[ "Returns", "the", "GUID", "for", "the", "space", "currently", "targeted", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/cf/config.py#L103-L117
PredixDev/predixpy
predix/config.py
get_crypt_key
def get_crypt_key(key_path): """ Get the user's PredixPy manifest key. Generate and store one if not yet generated. """ key_path = os.path.expanduser(key_path) if os.path.exists(key_path): with open(key_path, 'r') as data: key = data.read() else: key = Fernet.generate_key() with open(key_path, 'w') as output: output.write(key) return key
python
def get_crypt_key(key_path): """ Get the user's PredixPy manifest key. Generate and store one if not yet generated. """ key_path = os.path.expanduser(key_path) if os.path.exists(key_path): with open(key_path, 'r') as data: key = data.read() else: key = Fernet.generate_key() with open(key_path, 'w') as output: output.write(key) return key
[ "def", "get_crypt_key", "(", "key_path", ")", ":", "key_path", "=", "os", ".", "path", ".", "expanduser", "(", "key_path", ")", "if", "os", ".", "path", ".", "exists", "(", "key_path", ")", ":", "with", "open", "(", "key_path", ",", "'r'", ")", "as", "data", ":", "key", "=", "data", ".", "read", "(", ")", "else", ":", "key", "=", "Fernet", ".", "generate_key", "(", ")", "with", "open", "(", "key_path", ",", "'w'", ")", "as", "output", ":", "output", ".", "write", "(", "key", ")", "return", "key" ]
Get the user's PredixPy manifest key. Generate and store one if not yet generated.
[ "Get", "the", "user", "s", "PredixPy", "manifest", "key", ".", "Generate", "and", "store", "one", "if", "not", "yet", "generated", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/config.py#L12-L26
PredixDev/predixpy
predix/config.py
get_env_key
def get_env_key(obj, key=None): """ Return environment variable key to use for lookups within a namespace represented by the package name. For example, any varialbes for predix.security.uaa are stored as PREDIX_SECURITY_UAA_KEY """ return str.join('_', [obj.__module__.replace('.','_').upper(), key.upper()])
python
def get_env_key(obj, key=None): """ Return environment variable key to use for lookups within a namespace represented by the package name. For example, any varialbes for predix.security.uaa are stored as PREDIX_SECURITY_UAA_KEY """ return str.join('_', [obj.__module__.replace('.','_').upper(), key.upper()])
[ "def", "get_env_key", "(", "obj", ",", "key", "=", "None", ")", ":", "return", "str", ".", "join", "(", "'_'", ",", "[", "obj", ".", "__module__", ".", "replace", "(", "'.'", ",", "'_'", ")", ".", "upper", "(", ")", ",", "key", ".", "upper", "(", ")", "]", ")" ]
Return environment variable key to use for lookups within a namespace represented by the package name. For example, any varialbes for predix.security.uaa are stored as PREDIX_SECURITY_UAA_KEY
[ "Return", "environment", "variable", "key", "to", "use", "for", "lookups", "within", "a", "namespace", "represented", "by", "the", "package", "name", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/config.py#L28-L37
PredixDev/predixpy
predix/config.py
get_env_value
def get_env_value(obj, attribute): """ Returns the environment variable value for the attribute of the given object. For example `get_env_value(predix.security.uaa, 'uri')` will return value of environment variable PREDIX_SECURITY_UAA_URI. """ varname = get_env_key(obj, attribute) var = os.environ.get(varname) if not var: raise ValueError("%s must be set in your environment." % varname) return var
python
def get_env_value(obj, attribute): """ Returns the environment variable value for the attribute of the given object. For example `get_env_value(predix.security.uaa, 'uri')` will return value of environment variable PREDIX_SECURITY_UAA_URI. """ varname = get_env_key(obj, attribute) var = os.environ.get(varname) if not var: raise ValueError("%s must be set in your environment." % varname) return var
[ "def", "get_env_value", "(", "obj", ",", "attribute", ")", ":", "varname", "=", "get_env_key", "(", "obj", ",", "attribute", ")", "var", "=", "os", ".", "environ", ".", "get", "(", "varname", ")", "if", "not", "var", ":", "raise", "ValueError", "(", "\"%s must be set in your environment.\"", "%", "varname", ")", "return", "var" ]
Returns the environment variable value for the attribute of the given object. For example `get_env_value(predix.security.uaa, 'uri')` will return value of environment variable PREDIX_SECURITY_UAA_URI.
[ "Returns", "the", "environment", "variable", "value", "for", "the", "attribute", "of", "the", "given", "object", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/config.py#L39-L53
PredixDev/predixpy
predix/config.py
set_env_value
def set_env_value(obj, attribute, value): """ Set the environment variable value for the attribute of the given object. For example, `set_env_value(predix.security.uaa, 'uri', 'http://...')` will set the environment variable PREDIX_SECURITY_UAA_URI to the given uri. """ varname = get_env_key(obj, attribute) os.environ[varname] = value return varname
python
def set_env_value(obj, attribute, value): """ Set the environment variable value for the attribute of the given object. For example, `set_env_value(predix.security.uaa, 'uri', 'http://...')` will set the environment variable PREDIX_SECURITY_UAA_URI to the given uri. """ varname = get_env_key(obj, attribute) os.environ[varname] = value return varname
[ "def", "set_env_value", "(", "obj", ",", "attribute", ",", "value", ")", ":", "varname", "=", "get_env_key", "(", "obj", ",", "attribute", ")", "os", ".", "environ", "[", "varname", "]", "=", "value", "return", "varname" ]
Set the environment variable value for the attribute of the given object. For example, `set_env_value(predix.security.uaa, 'uri', 'http://...')` will set the environment variable PREDIX_SECURITY_UAA_URI to the given uri.
[ "Set", "the", "environment", "variable", "value", "for", "the", "attribute", "of", "the", "given", "object", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/config.py#L55-L66
PredixDev/predixpy
predix/admin/cf/services.py
Service.get_instance_guid
def get_instance_guid(self, service_name): """ Returns the GUID for the service instance with the given name. """ summary = self.space.get_space_summary() for service in summary['services']: if service['name'] == service_name: return service['guid'] raise ValueError("No service with name '%s' found." % (service_name))
python
def get_instance_guid(self, service_name): """ Returns the GUID for the service instance with the given name. """ summary = self.space.get_space_summary() for service in summary['services']: if service['name'] == service_name: return service['guid'] raise ValueError("No service with name '%s' found." % (service_name))
[ "def", "get_instance_guid", "(", "self", ",", "service_name", ")", ":", "summary", "=", "self", ".", "space", ".", "get_space_summary", "(", ")", "for", "service", "in", "summary", "[", "'services'", "]", ":", "if", "service", "[", "'name'", "]", "==", "service_name", ":", "return", "service", "[", "'guid'", "]", "raise", "ValueError", "(", "\"No service with name '%s' found.\"", "%", "(", "service_name", ")", ")" ]
Returns the GUID for the service instance with the given name.
[ "Returns", "the", "GUID", "for", "the", "service", "instance", "with", "the", "given", "name", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/cf/services.py#L31-L41
PredixDev/predixpy
predix/admin/cf/services.py
Service._get_service_bindings
def _get_service_bindings(self, service_name): """ Return the service bindings for the service instance. """ instance = self.get_instance(service_name) return self.api.get(instance['service_bindings_url'])
python
def _get_service_bindings(self, service_name): """ Return the service bindings for the service instance. """ instance = self.get_instance(service_name) return self.api.get(instance['service_bindings_url'])
[ "def", "_get_service_bindings", "(", "self", ",", "service_name", ")", ":", "instance", "=", "self", ".", "get_instance", "(", "service_name", ")", "return", "self", ".", "api", ".", "get", "(", "instance", "[", "'service_bindings_url'", "]", ")" ]
Return the service bindings for the service instance.
[ "Return", "the", "service", "bindings", "for", "the", "service", "instance", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/cf/services.py#L43-L48
PredixDev/predixpy
predix/admin/cf/services.py
Service.delete_service_bindings
def delete_service_bindings(self, service_name): """ Remove service bindings to applications. """ instance = self.get_instance(service_name) return self.api.delete(instance['service_bindings_url'])
python
def delete_service_bindings(self, service_name): """ Remove service bindings to applications. """ instance = self.get_instance(service_name) return self.api.delete(instance['service_bindings_url'])
[ "def", "delete_service_bindings", "(", "self", ",", "service_name", ")", ":", "instance", "=", "self", ".", "get_instance", "(", "service_name", ")", "return", "self", ".", "api", ".", "delete", "(", "instance", "[", "'service_bindings_url'", "]", ")" ]
Remove service bindings to applications.
[ "Remove", "service", "bindings", "to", "applications", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/cf/services.py#L50-L55
PredixDev/predixpy
predix/admin/cf/services.py
Service._get_service_keys
def _get_service_keys(self, service_name): """ Return the service keys for the given service. """ guid = self.get_instance_guid(service_name) uri = "/v2/service_instances/%s/service_keys" % (guid) return self.api.get(uri)
python
def _get_service_keys(self, service_name): """ Return the service keys for the given service. """ guid = self.get_instance_guid(service_name) uri = "/v2/service_instances/%s/service_keys" % (guid) return self.api.get(uri)
[ "def", "_get_service_keys", "(", "self", ",", "service_name", ")", ":", "guid", "=", "self", ".", "get_instance_guid", "(", "service_name", ")", "uri", "=", "\"/v2/service_instances/%s/service_keys\"", "%", "(", "guid", ")", "return", "self", ".", "api", ".", "get", "(", "uri", ")" ]
Return the service keys for the given service.
[ "Return", "the", "service", "keys", "for", "the", "given", "service", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/cf/services.py#L57-L63
PredixDev/predixpy
predix/admin/cf/services.py
Service.get_service_keys
def get_service_keys(self, service_name): """ Returns a flat list of the names of the service keys for the given service. """ keys = [] for key in self._get_service_keys(service_name)['resources']: keys.append(key['entity']['name']) return keys
python
def get_service_keys(self, service_name): """ Returns a flat list of the names of the service keys for the given service. """ keys = [] for key in self._get_service_keys(service_name)['resources']: keys.append(key['entity']['name']) return keys
[ "def", "get_service_keys", "(", "self", ",", "service_name", ")", ":", "keys", "=", "[", "]", "for", "key", "in", "self", ".", "_get_service_keys", "(", "service_name", ")", "[", "'resources'", "]", ":", "keys", ".", "append", "(", "key", "[", "'entity'", "]", "[", "'name'", "]", ")", "return", "keys" ]
Returns a flat list of the names of the service keys for the given service.
[ "Returns", "a", "flat", "list", "of", "the", "names", "of", "the", "service", "keys", "for", "the", "given", "service", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/cf/services.py#L65-L74
PredixDev/predixpy
predix/admin/cf/services.py
Service.get_service_key
def get_service_key(self, service_name, key_name): """ Returns the service key details. Similar to `cf service-key`. """ for key in self._get_service_keys(service_name)['resources']: if key_name == key['entity']['name']: guid = key['metadata']['guid'] uri = "/v2/service_keys/%s" % (guid) return self.api.get(uri) return None
python
def get_service_key(self, service_name, key_name): """ Returns the service key details. Similar to `cf service-key`. """ for key in self._get_service_keys(service_name)['resources']: if key_name == key['entity']['name']: guid = key['metadata']['guid'] uri = "/v2/service_keys/%s" % (guid) return self.api.get(uri) return None
[ "def", "get_service_key", "(", "self", ",", "service_name", ",", "key_name", ")", ":", "for", "key", "in", "self", ".", "_get_service_keys", "(", "service_name", ")", "[", "'resources'", "]", ":", "if", "key_name", "==", "key", "[", "'entity'", "]", "[", "'name'", "]", ":", "guid", "=", "key", "[", "'metadata'", "]", "[", "'guid'", "]", "uri", "=", "\"/v2/service_keys/%s\"", "%", "(", "guid", ")", "return", "self", ".", "api", ".", "get", "(", "uri", ")", "return", "None" ]
Returns the service key details. Similar to `cf service-key`.
[ "Returns", "the", "service", "key", "details", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/cf/services.py#L82-L95
PredixDev/predixpy
predix/admin/cf/services.py
Service.create_service_key
def create_service_key(self, service_name, key_name): """ Create a service key for the given service. """ if self.has_key(service_name, key_name): logging.warning("Reusing existing service key %s" % (key_name)) return self.get_service_key(service_name, key_name) body = { 'service_instance_guid': self.get_instance_guid(service_name), 'name': key_name } return self.api.post('/v2/service_keys', body)
python
def create_service_key(self, service_name, key_name): """ Create a service key for the given service. """ if self.has_key(service_name, key_name): logging.warning("Reusing existing service key %s" % (key_name)) return self.get_service_key(service_name, key_name) body = { 'service_instance_guid': self.get_instance_guid(service_name), 'name': key_name } return self.api.post('/v2/service_keys', body)
[ "def", "create_service_key", "(", "self", ",", "service_name", ",", "key_name", ")", ":", "if", "self", ".", "has_key", "(", "service_name", ",", "key_name", ")", ":", "logging", ".", "warning", "(", "\"Reusing existing service key %s\"", "%", "(", "key_name", ")", ")", "return", "self", ".", "get_service_key", "(", "service_name", ",", "key_name", ")", "body", "=", "{", "'service_instance_guid'", ":", "self", ".", "get_instance_guid", "(", "service_name", ")", ",", "'name'", ":", "key_name", "}", "return", "self", ".", "api", ".", "post", "(", "'/v2/service_keys'", ",", "body", ")" ]
Create a service key for the given service.
[ "Create", "a", "service", "key", "for", "the", "given", "service", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/cf/services.py#L97-L110
PredixDev/predixpy
predix/admin/cf/services.py
Service.delete_service_key
def delete_service_key(self, service_name, key_name): """ Delete a service key for the given service. """ key = self.get_service_key(service_name, key_name) logging.info("Deleting service key %s for service %s" % (key, service_name)) return self.api.delete(key['metadata']['url'])
python
def delete_service_key(self, service_name, key_name): """ Delete a service key for the given service. """ key = self.get_service_key(service_name, key_name) logging.info("Deleting service key %s for service %s" % (key, service_name)) return self.api.delete(key['metadata']['url'])
[ "def", "delete_service_key", "(", "self", ",", "service_name", ",", "key_name", ")", ":", "key", "=", "self", ".", "get_service_key", "(", "service_name", ",", "key_name", ")", "logging", ".", "info", "(", "\"Deleting service key %s for service %s\"", "%", "(", "key", ",", "service_name", ")", ")", "return", "self", ".", "api", ".", "delete", "(", "key", "[", "'metadata'", "]", "[", "'url'", "]", ")" ]
Delete a service key for the given service.
[ "Delete", "a", "service", "key", "for", "the", "given", "service", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/cf/services.py#L112-L118
PredixDev/predixpy
predix/admin/cf/services.py
Service.get_instance
def get_instance(self, service_name): """ Retrieves a service instance with the given name. """ for resource in self.space._get_instances(): if resource['entity']['name'] == service_name: return resource['entity']
python
def get_instance(self, service_name): """ Retrieves a service instance with the given name. """ for resource in self.space._get_instances(): if resource['entity']['name'] == service_name: return resource['entity']
[ "def", "get_instance", "(", "self", ",", "service_name", ")", ":", "for", "resource", "in", "self", ".", "space", ".", "_get_instances", "(", ")", ":", "if", "resource", "[", "'entity'", "]", "[", "'name'", "]", "==", "service_name", ":", "return", "resource", "[", "'entity'", "]" ]
Retrieves a service instance with the given name.
[ "Retrieves", "a", "service", "instance", "with", "the", "given", "name", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/cf/services.py#L120-L126
PredixDev/predixpy
predix/admin/cf/services.py
Service.get_service_plan_for_service
def get_service_plan_for_service(self, service_name): """ Return the service plans available for a given service. """ services = self.get_services() for service in services['resources']: if service['entity']['label'] == service_name: response = self.api.get(service['entity']['service_plans_url']) return response['resources']
python
def get_service_plan_for_service(self, service_name): """ Return the service plans available for a given service. """ services = self.get_services() for service in services['resources']: if service['entity']['label'] == service_name: response = self.api.get(service['entity']['service_plans_url']) return response['resources']
[ "def", "get_service_plan_for_service", "(", "self", ",", "service_name", ")", ":", "services", "=", "self", ".", "get_services", "(", ")", "for", "service", "in", "services", "[", "'resources'", "]", ":", "if", "service", "[", "'entity'", "]", "[", "'label'", "]", "==", "service_name", ":", "response", "=", "self", ".", "api", ".", "get", "(", "service", "[", "'entity'", "]", "[", "'service_plans_url'", "]", ")", "return", "response", "[", "'resources'", "]" ]
Return the service plans available for a given service.
[ "Return", "the", "service", "plans", "available", "for", "a", "given", "service", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/cf/services.py#L134-L142
PredixDev/predixpy
predix/admin/cf/services.py
Service.get_service_plan_guid
def get_service_plan_guid(self, service_name, plan_name): """ Return the service plan GUID for the given service / plan. """ for plan in self.get_service_plan_for_service(service_name): if plan['entity']['name'] == plan_name: return plan['metadata']['guid'] return None
python
def get_service_plan_guid(self, service_name, plan_name): """ Return the service plan GUID for the given service / plan. """ for plan in self.get_service_plan_for_service(service_name): if plan['entity']['name'] == plan_name: return plan['metadata']['guid'] return None
[ "def", "get_service_plan_guid", "(", "self", ",", "service_name", ",", "plan_name", ")", ":", "for", "plan", "in", "self", ".", "get_service_plan_for_service", "(", "service_name", ")", ":", "if", "plan", "[", "'entity'", "]", "[", "'name'", "]", "==", "plan_name", ":", "return", "plan", "[", "'metadata'", "]", "[", "'guid'", "]", "return", "None" ]
Return the service plan GUID for the given service / plan.
[ "Return", "the", "service", "plan", "GUID", "for", "the", "given", "service", "/", "plan", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/cf/services.py#L144-L152
PredixDev/predixpy
predix/admin/cf/services.py
Service.create_service
def create_service(self, service_type, plan_name, service_name, params, async=False, **kwargs): """ Create a service instance. """ if self.space.has_service_with_name(service_name): logging.warning("Service already exists with that name.") return self.get_instance(service_name) if self.space.has_service_of_type(service_type): logging.warning("Service type already exists.") guid = self.get_service_plan_guid(service_type, plan_name) if not guid: raise ValueError("No service plan named: %s" % (plan_name)) body = { 'name': service_name, 'space_guid': self.space.guid, 'service_plan_guid': guid, 'parameters': params } uri = '/v2/service_instances?accepts_incomplete=true' if async: uri += '&async=true' return self.api.post(uri, body)
python
def create_service(self, service_type, plan_name, service_name, params, async=False, **kwargs): """ Create a service instance. """ if self.space.has_service_with_name(service_name): logging.warning("Service already exists with that name.") return self.get_instance(service_name) if self.space.has_service_of_type(service_type): logging.warning("Service type already exists.") guid = self.get_service_plan_guid(service_type, plan_name) if not guid: raise ValueError("No service plan named: %s" % (plan_name)) body = { 'name': service_name, 'space_guid': self.space.guid, 'service_plan_guid': guid, 'parameters': params } uri = '/v2/service_instances?accepts_incomplete=true' if async: uri += '&async=true' return self.api.post(uri, body)
[ "def", "create_service", "(", "self", ",", "service_type", ",", "plan_name", ",", "service_name", ",", "params", ",", "async", "=", "False", ",", "*", "*", "kwargs", ")", ":", "if", "self", ".", "space", ".", "has_service_with_name", "(", "service_name", ")", ":", "logging", ".", "warning", "(", "\"Service already exists with that name.\"", ")", "return", "self", ".", "get_instance", "(", "service_name", ")", "if", "self", ".", "space", ".", "has_service_of_type", "(", "service_type", ")", ":", "logging", ".", "warning", "(", "\"Service type already exists.\"", ")", "guid", "=", "self", ".", "get_service_plan_guid", "(", "service_type", ",", "plan_name", ")", "if", "not", "guid", ":", "raise", "ValueError", "(", "\"No service plan named: %s\"", "%", "(", "plan_name", ")", ")", "body", "=", "{", "'name'", ":", "service_name", ",", "'space_guid'", ":", "self", ".", "space", ".", "guid", ",", "'service_plan_guid'", ":", "guid", ",", "'parameters'", ":", "params", "}", "uri", "=", "'/v2/service_instances?accepts_incomplete=true'", "if", "async", ":", "uri", "+=", "'&async=true'", "return", "self", ".", "api", ".", "post", "(", "uri", ",", "body", ")" ]
Create a service instance.
[ "Create", "a", "service", "instance", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/cf/services.py#L154-L182
PredixDev/predixpy
predix/admin/cf/services.py
Service.delete_service
def delete_service(self, service_name, params=None): """ Delete the service of the given name. It may fail if there are any service keys or app bindings. Use purge() if you want to delete it all. """ if not self.space.has_service_with_name(service_name): logging.warning("Service not found so... succeeded?") return True guid = self.get_instance_guid(service_name) logging.info("Deleting service %s with guid %s" % (service_name, guid)) # MAINT: this endpoint changes in newer version of api return self.api.delete("/v2/service_instances/%s?accepts_incomplete=true" % (guid), params=params)
python
def delete_service(self, service_name, params=None): """ Delete the service of the given name. It may fail if there are any service keys or app bindings. Use purge() if you want to delete it all. """ if not self.space.has_service_with_name(service_name): logging.warning("Service not found so... succeeded?") return True guid = self.get_instance_guid(service_name) logging.info("Deleting service %s with guid %s" % (service_name, guid)) # MAINT: this endpoint changes in newer version of api return self.api.delete("/v2/service_instances/%s?accepts_incomplete=true" % (guid), params=params)
[ "def", "delete_service", "(", "self", ",", "service_name", ",", "params", "=", "None", ")", ":", "if", "not", "self", ".", "space", ".", "has_service_with_name", "(", "service_name", ")", ":", "logging", ".", "warning", "(", "\"Service not found so... succeeded?\"", ")", "return", "True", "guid", "=", "self", ".", "get_instance_guid", "(", "service_name", ")", "logging", ".", "info", "(", "\"Deleting service %s with guid %s\"", "%", "(", "service_name", ",", "guid", ")", ")", "# MAINT: this endpoint changes in newer version of api", "return", "self", ".", "api", ".", "delete", "(", "\"/v2/service_instances/%s?accepts_incomplete=true\"", "%", "(", "guid", ")", ",", "params", "=", "params", ")" ]
Delete the service of the given name. It may fail if there are any service keys or app bindings. Use purge() if you want to delete it all.
[ "Delete", "the", "service", "of", "the", "given", "name", ".", "It", "may", "fail", "if", "there", "are", "any", "service", "keys", "or", "app", "bindings", ".", "Use", "purge", "()", "if", "you", "want", "to", "delete", "it", "all", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/admin/cf/services.py#L184-L199
PredixDev/predixpy
predix/data/timeseries.py
TimeSeries._get_query_uri
def _get_query_uri(self): """ Returns the URI endpoint for performing queries of a Predix Time Series instance from environment inspection. """ if 'VCAP_SERVICES' in os.environ: services = json.loads(os.getenv('VCAP_SERVICES')) predix_timeseries = services['predix-timeseries'][0]['credentials'] return predix_timeseries['query']['uri'].partition('/v1')[0] else: return predix.config.get_env_value(self, 'query_uri')
python
def _get_query_uri(self): """ Returns the URI endpoint for performing queries of a Predix Time Series instance from environment inspection. """ if 'VCAP_SERVICES' in os.environ: services = json.loads(os.getenv('VCAP_SERVICES')) predix_timeseries = services['predix-timeseries'][0]['credentials'] return predix_timeseries['query']['uri'].partition('/v1')[0] else: return predix.config.get_env_value(self, 'query_uri')
[ "def", "_get_query_uri", "(", "self", ")", ":", "if", "'VCAP_SERVICES'", "in", "os", ".", "environ", ":", "services", "=", "json", ".", "loads", "(", "os", ".", "getenv", "(", "'VCAP_SERVICES'", ")", ")", "predix_timeseries", "=", "services", "[", "'predix-timeseries'", "]", "[", "0", "]", "[", "'credentials'", "]", "return", "predix_timeseries", "[", "'query'", "]", "[", "'uri'", "]", ".", "partition", "(", "'/v1'", ")", "[", "0", "]", "else", ":", "return", "predix", ".", "config", ".", "get_env_value", "(", "self", ",", "'query_uri'", ")" ]
Returns the URI endpoint for performing queries of a Predix Time Series instance from environment inspection.
[ "Returns", "the", "URI", "endpoint", "for", "performing", "queries", "of", "a", "Predix", "Time", "Series", "instance", "from", "environment", "inspection", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/data/timeseries.py#L73-L83
PredixDev/predixpy
predix/data/timeseries.py
TimeSeries._get_query_zone_id
def _get_query_zone_id(self): """ Returns the ZoneId for performing queries of a Predix Time Series instance from environment inspection. """ if 'VCAP_SERVICES' in os.environ: services = json.loads(os.getenv('VCAP_SERVICES')) predix_timeseries = services['predix-timeseries'][0]['credentials'] return predix_timeseries['query']['zone-http-header-value'] else: return predix.config.get_env_value(self, 'query_zone_id')
python
def _get_query_zone_id(self): """ Returns the ZoneId for performing queries of a Predix Time Series instance from environment inspection. """ if 'VCAP_SERVICES' in os.environ: services = json.loads(os.getenv('VCAP_SERVICES')) predix_timeseries = services['predix-timeseries'][0]['credentials'] return predix_timeseries['query']['zone-http-header-value'] else: return predix.config.get_env_value(self, 'query_zone_id')
[ "def", "_get_query_zone_id", "(", "self", ")", ":", "if", "'VCAP_SERVICES'", "in", "os", ".", "environ", ":", "services", "=", "json", ".", "loads", "(", "os", ".", "getenv", "(", "'VCAP_SERVICES'", ")", ")", "predix_timeseries", "=", "services", "[", "'predix-timeseries'", "]", "[", "0", "]", "[", "'credentials'", "]", "return", "predix_timeseries", "[", "'query'", "]", "[", "'zone-http-header-value'", "]", "else", ":", "return", "predix", ".", "config", ".", "get_env_value", "(", "self", ",", "'query_zone_id'", ")" ]
Returns the ZoneId for performing queries of a Predix Time Series instance from environment inspection.
[ "Returns", "the", "ZoneId", "for", "performing", "queries", "of", "a", "Predix", "Time", "Series", "instance", "from", "environment", "inspection", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/data/timeseries.py#L85-L95
PredixDev/predixpy
predix/data/timeseries.py
TimeSeries._get_datapoints
def _get_datapoints(self, params): """ Will make a direct REST call with the given json body payload to get datapoints. """ url = self.query_uri + '/v1/datapoints' return self.service._get(url, params=params)
python
def _get_datapoints(self, params): """ Will make a direct REST call with the given json body payload to get datapoints. """ url = self.query_uri + '/v1/datapoints' return self.service._get(url, params=params)
[ "def", "_get_datapoints", "(", "self", ",", "params", ")", ":", "url", "=", "self", ".", "query_uri", "+", "'/v1/datapoints'", "return", "self", ".", "service", ".", "_get", "(", "url", ",", "params", "=", "params", ")" ]
Will make a direct REST call with the given json body payload to get datapoints.
[ "Will", "make", "a", "direct", "REST", "call", "with", "the", "given", "json", "body", "payload", "to", "get", "datapoints", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/data/timeseries.py#L171-L177
PredixDev/predixpy
predix/data/timeseries.py
TimeSeries.get_values
def get_values(self, *args, **kwargs): """ Convenience method that for simple single tag queries will return just the values to be iterated on. """ if isinstance(args[0], list): raise ValueError("Can only get_values() for a single tag.") response = self.get_datapoints(*args, **kwargs) for value in response['tags'][0]['results'][0]['values']: yield [datetime.datetime.utcfromtimestamp(value[0]/1000), value[1], value[2]]
python
def get_values(self, *args, **kwargs): """ Convenience method that for simple single tag queries will return just the values to be iterated on. """ if isinstance(args[0], list): raise ValueError("Can only get_values() for a single tag.") response = self.get_datapoints(*args, **kwargs) for value in response['tags'][0]['results'][0]['values']: yield [datetime.datetime.utcfromtimestamp(value[0]/1000), value[1], value[2]]
[ "def", "get_values", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "if", "isinstance", "(", "args", "[", "0", "]", ",", "list", ")", ":", "raise", "ValueError", "(", "\"Can only get_values() for a single tag.\"", ")", "response", "=", "self", ".", "get_datapoints", "(", "*", "args", ",", "*", "*", "kwargs", ")", "for", "value", "in", "response", "[", "'tags'", "]", "[", "0", "]", "[", "'results'", "]", "[", "0", "]", "[", "'values'", "]", ":", "yield", "[", "datetime", ".", "datetime", ".", "utcfromtimestamp", "(", "value", "[", "0", "]", "/", "1000", ")", ",", "value", "[", "1", "]", ",", "value", "[", "2", "]", "]" ]
Convenience method that for simple single tag queries will return just the values to be iterated on.
[ "Convenience", "method", "that", "for", "simple", "single", "tag", "queries", "will", "return", "just", "the", "values", "to", "be", "iterated", "on", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/data/timeseries.py#L183-L195
PredixDev/predixpy
predix/data/timeseries.py
TimeSeries.get_datapoints
def get_datapoints(self, tags, start=None, end=None, order=None, limit=None, qualities=None, attributes=None, measurement=None, aggregations=None, post=False): """ Returns all of the datapoints that match the given query. - tags: list or string identifying the name/tag (ie. "temp") - start: data after this, absolute or relative (ie. '1w-ago' or 1494015972386) - end: data before this value - order: ascending (asc) or descending (desc) - limit: only return a few values (ie. 25) - qualities: data quality value (ie. [ts.GOOD, ts.UNCERTAIN]) - attributes: dictionary of key-values (ie. {'unit': 'mph'}) - measurement: tuple of operation and value (ie. ('gt', 30)) - aggregations: summary statistics on data results (ie. 'avg') - post: POST query instead of GET (caching implication) A few additional observations: - allow service to do most data validation - order is applied before limit so resultset will differ The returned results match what the service response is so you'll need to unpack it as appropriate. Oftentimes what you want for a simple single tag query will be: response['tags'][0]['results'][0]['values'] """ params = {} # Documentation says start is required for GET but not POST, but # seems to be required all the time, so using sensible default. if not start: start = '1w-ago' logging.warning("Defaulting query for data with start date %s" % (start)) # Start date can be absolute or relative, only certain legal values # but service will throw error if used improperly. (ms, s, mi, h, d, # w, mm, y). Relative dates must end in -ago. params['start'] = start # Docs say when making POST with a start that end must also be # specified, but this does not seem to be the case. if end: # MAINT: error when end < start which is handled by service params['end'] = end params['tags'] = [] if not isinstance(tags, list): tags = [tags] for tag in tags: query = {} query['name'] = tag # Limit resultset with an integer value if limit: query['limit'] = int(limit) # Order must be 'asc' or 'desc' but will get sensible error # from service. if order: query['order'] = order # Filters are complex and support filtering by # quality, measurement, and attributes. filters = {} # Check for the quality of the datapoints if qualities is not None: if isinstance(qualities, int) or isinstance(qualities, str): qualities = [qualities] # Timeseries expects quality to be a string, not integer, # so coerce each into a string for i, quality in enumerate(qualities): qualities[i] = str(quality) filters['qualities'] = {"values": qualities} # Check for attributes on the datapoints, expected to be # a dictionary of key / value pairs that datapoints must match. if attributes is not None: if not isinstance(attributes, dict): raise ValueError("Attribute filters must be dictionary.") filters['attributes'] = attributes # Check for measurements that meets a given comparison operation # such as ge, gt, eq, ne, le, lt if measurement is not None: filters['measurements'] = { 'condition': measurement[0], 'values': measurement[1] } # If we found any filters add them to the query if filters: query['filters'] = filters # Handle any additional aggregations of dataset if aggregations is not None: if not isinstance(aggregations, list): aggregations = [aggregations] query['aggregations'] = [] for aggregation in aggregations: query['aggregations'].append({ 'sampling': {'datapoints': 1}, 'type': aggregation }) params['tags'].append(query) if post: return self._post_datapoints(params) else: return self._get_datapoints({"query": json.dumps(params)})
python
def get_datapoints(self, tags, start=None, end=None, order=None, limit=None, qualities=None, attributes=None, measurement=None, aggregations=None, post=False): """ Returns all of the datapoints that match the given query. - tags: list or string identifying the name/tag (ie. "temp") - start: data after this, absolute or relative (ie. '1w-ago' or 1494015972386) - end: data before this value - order: ascending (asc) or descending (desc) - limit: only return a few values (ie. 25) - qualities: data quality value (ie. [ts.GOOD, ts.UNCERTAIN]) - attributes: dictionary of key-values (ie. {'unit': 'mph'}) - measurement: tuple of operation and value (ie. ('gt', 30)) - aggregations: summary statistics on data results (ie. 'avg') - post: POST query instead of GET (caching implication) A few additional observations: - allow service to do most data validation - order is applied before limit so resultset will differ The returned results match what the service response is so you'll need to unpack it as appropriate. Oftentimes what you want for a simple single tag query will be: response['tags'][0]['results'][0]['values'] """ params = {} # Documentation says start is required for GET but not POST, but # seems to be required all the time, so using sensible default. if not start: start = '1w-ago' logging.warning("Defaulting query for data with start date %s" % (start)) # Start date can be absolute or relative, only certain legal values # but service will throw error if used improperly. (ms, s, mi, h, d, # w, mm, y). Relative dates must end in -ago. params['start'] = start # Docs say when making POST with a start that end must also be # specified, but this does not seem to be the case. if end: # MAINT: error when end < start which is handled by service params['end'] = end params['tags'] = [] if not isinstance(tags, list): tags = [tags] for tag in tags: query = {} query['name'] = tag # Limit resultset with an integer value if limit: query['limit'] = int(limit) # Order must be 'asc' or 'desc' but will get sensible error # from service. if order: query['order'] = order # Filters are complex and support filtering by # quality, measurement, and attributes. filters = {} # Check for the quality of the datapoints if qualities is not None: if isinstance(qualities, int) or isinstance(qualities, str): qualities = [qualities] # Timeseries expects quality to be a string, not integer, # so coerce each into a string for i, quality in enumerate(qualities): qualities[i] = str(quality) filters['qualities'] = {"values": qualities} # Check for attributes on the datapoints, expected to be # a dictionary of key / value pairs that datapoints must match. if attributes is not None: if not isinstance(attributes, dict): raise ValueError("Attribute filters must be dictionary.") filters['attributes'] = attributes # Check for measurements that meets a given comparison operation # such as ge, gt, eq, ne, le, lt if measurement is not None: filters['measurements'] = { 'condition': measurement[0], 'values': measurement[1] } # If we found any filters add them to the query if filters: query['filters'] = filters # Handle any additional aggregations of dataset if aggregations is not None: if not isinstance(aggregations, list): aggregations = [aggregations] query['aggregations'] = [] for aggregation in aggregations: query['aggregations'].append({ 'sampling': {'datapoints': 1}, 'type': aggregation }) params['tags'].append(query) if post: return self._post_datapoints(params) else: return self._get_datapoints({"query": json.dumps(params)})
[ "def", "get_datapoints", "(", "self", ",", "tags", ",", "start", "=", "None", ",", "end", "=", "None", ",", "order", "=", "None", ",", "limit", "=", "None", ",", "qualities", "=", "None", ",", "attributes", "=", "None", ",", "measurement", "=", "None", ",", "aggregations", "=", "None", ",", "post", "=", "False", ")", ":", "params", "=", "{", "}", "# Documentation says start is required for GET but not POST, but", "# seems to be required all the time, so using sensible default.", "if", "not", "start", ":", "start", "=", "'1w-ago'", "logging", ".", "warning", "(", "\"Defaulting query for data with start date %s\"", "%", "(", "start", ")", ")", "# Start date can be absolute or relative, only certain legal values", "# but service will throw error if used improperly. (ms, s, mi, h, d,", "# w, mm, y). Relative dates must end in -ago.", "params", "[", "'start'", "]", "=", "start", "# Docs say when making POST with a start that end must also be", "# specified, but this does not seem to be the case.", "if", "end", ":", "# MAINT: error when end < start which is handled by service", "params", "[", "'end'", "]", "=", "end", "params", "[", "'tags'", "]", "=", "[", "]", "if", "not", "isinstance", "(", "tags", ",", "list", ")", ":", "tags", "=", "[", "tags", "]", "for", "tag", "in", "tags", ":", "query", "=", "{", "}", "query", "[", "'name'", "]", "=", "tag", "# Limit resultset with an integer value", "if", "limit", ":", "query", "[", "'limit'", "]", "=", "int", "(", "limit", ")", "# Order must be 'asc' or 'desc' but will get sensible error", "# from service.", "if", "order", ":", "query", "[", "'order'", "]", "=", "order", "# Filters are complex and support filtering by", "# quality, measurement, and attributes.", "filters", "=", "{", "}", "# Check for the quality of the datapoints", "if", "qualities", "is", "not", "None", ":", "if", "isinstance", "(", "qualities", ",", "int", ")", "or", "isinstance", "(", "qualities", ",", "str", ")", ":", "qualities", "=", "[", "qualities", "]", "# Timeseries expects quality to be a string, not integer,", "# so coerce each into a string", "for", "i", ",", "quality", "in", "enumerate", "(", "qualities", ")", ":", "qualities", "[", "i", "]", "=", "str", "(", "quality", ")", "filters", "[", "'qualities'", "]", "=", "{", "\"values\"", ":", "qualities", "}", "# Check for attributes on the datapoints, expected to be", "# a dictionary of key / value pairs that datapoints must match.", "if", "attributes", "is", "not", "None", ":", "if", "not", "isinstance", "(", "attributes", ",", "dict", ")", ":", "raise", "ValueError", "(", "\"Attribute filters must be dictionary.\"", ")", "filters", "[", "'attributes'", "]", "=", "attributes", "# Check for measurements that meets a given comparison operation", "# such as ge, gt, eq, ne, le, lt", "if", "measurement", "is", "not", "None", ":", "filters", "[", "'measurements'", "]", "=", "{", "'condition'", ":", "measurement", "[", "0", "]", ",", "'values'", ":", "measurement", "[", "1", "]", "}", "# If we found any filters add them to the query", "if", "filters", ":", "query", "[", "'filters'", "]", "=", "filters", "# Handle any additional aggregations of dataset", "if", "aggregations", "is", "not", "None", ":", "if", "not", "isinstance", "(", "aggregations", ",", "list", ")", ":", "aggregations", "=", "[", "aggregations", "]", "query", "[", "'aggregations'", "]", "=", "[", "]", "for", "aggregation", "in", "aggregations", ":", "query", "[", "'aggregations'", "]", ".", "append", "(", "{", "'sampling'", ":", "{", "'datapoints'", ":", "1", "}", ",", "'type'", ":", "aggregation", "}", ")", "params", "[", "'tags'", "]", ".", "append", "(", "query", ")", "if", "post", ":", "return", "self", ".", "_post_datapoints", "(", "params", ")", "else", ":", "return", "self", ".", "_get_datapoints", "(", "{", "\"query\"", ":", "json", ".", "dumps", "(", "params", ")", "}", ")" ]
Returns all of the datapoints that match the given query. - tags: list or string identifying the name/tag (ie. "temp") - start: data after this, absolute or relative (ie. '1w-ago' or 1494015972386) - end: data before this value - order: ascending (asc) or descending (desc) - limit: only return a few values (ie. 25) - qualities: data quality value (ie. [ts.GOOD, ts.UNCERTAIN]) - attributes: dictionary of key-values (ie. {'unit': 'mph'}) - measurement: tuple of operation and value (ie. ('gt', 30)) - aggregations: summary statistics on data results (ie. 'avg') - post: POST query instead of GET (caching implication) A few additional observations: - allow service to do most data validation - order is applied before limit so resultset will differ The returned results match what the service response is so you'll need to unpack it as appropriate. Oftentimes what you want for a simple single tag query will be: response['tags'][0]['results'][0]['values']
[ "Returns", "all", "of", "the", "datapoints", "that", "match", "the", "given", "query", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/data/timeseries.py#L209-L326
PredixDev/predixpy
predix/data/timeseries.py
TimeSeries._create_connection
def _create_connection(self): """ Create a new websocket connection with proper headers. """ logging.debug("Initializing new websocket connection.") headers = { 'Authorization': self.service._get_bearer_token(), 'Predix-Zone-Id': self.ingest_zone_id, 'Content-Type': 'application/json', } url = self.ingest_uri logging.debug("URL=" + str(url)) logging.debug("HEADERS=" + str(headers)) # Should consider connection pooling and longer timeouts return websocket.create_connection(url, header=headers)
python
def _create_connection(self): """ Create a new websocket connection with proper headers. """ logging.debug("Initializing new websocket connection.") headers = { 'Authorization': self.service._get_bearer_token(), 'Predix-Zone-Id': self.ingest_zone_id, 'Content-Type': 'application/json', } url = self.ingest_uri logging.debug("URL=" + str(url)) logging.debug("HEADERS=" + str(headers)) # Should consider connection pooling and longer timeouts return websocket.create_connection(url, header=headers)
[ "def", "_create_connection", "(", "self", ")", ":", "logging", ".", "debug", "(", "\"Initializing new websocket connection.\"", ")", "headers", "=", "{", "'Authorization'", ":", "self", ".", "service", ".", "_get_bearer_token", "(", ")", ",", "'Predix-Zone-Id'", ":", "self", ".", "ingest_zone_id", ",", "'Content-Type'", ":", "'application/json'", ",", "}", "url", "=", "self", ".", "ingest_uri", "logging", ".", "debug", "(", "\"URL=\"", "+", "str", "(", "url", ")", ")", "logging", ".", "debug", "(", "\"HEADERS=\"", "+", "str", "(", "headers", ")", ")", "# Should consider connection pooling and longer timeouts", "return", "websocket", ".", "create_connection", "(", "url", ",", "header", "=", "headers", ")" ]
Create a new websocket connection with proper headers.
[ "Create", "a", "new", "websocket", "connection", "with", "proper", "headers", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/data/timeseries.py#L367-L383
PredixDev/predixpy
predix/data/timeseries.py
TimeSeries._get_websocket
def _get_websocket(self, reuse=True): """ Reuse existing connection or create a new connection. """ # Check if still connected if self.ws and reuse: if self.ws.connected: return self.ws logging.debug("Stale connection, reconnecting.") self.ws = self._create_connection() return self.ws
python
def _get_websocket(self, reuse=True): """ Reuse existing connection or create a new connection. """ # Check if still connected if self.ws and reuse: if self.ws.connected: return self.ws logging.debug("Stale connection, reconnecting.") self.ws = self._create_connection() return self.ws
[ "def", "_get_websocket", "(", "self", ",", "reuse", "=", "True", ")", ":", "# Check if still connected", "if", "self", ".", "ws", "and", "reuse", ":", "if", "self", ".", "ws", ".", "connected", ":", "return", "self", ".", "ws", "logging", ".", "debug", "(", "\"Stale connection, reconnecting.\"", ")", "self", ".", "ws", "=", "self", ".", "_create_connection", "(", ")", "return", "self", ".", "ws" ]
Reuse existing connection or create a new connection.
[ "Reuse", "existing", "connection", "or", "create", "a", "new", "connection", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/data/timeseries.py#L385-L397
PredixDev/predixpy
predix/data/timeseries.py
TimeSeries._send_to_timeseries
def _send_to_timeseries(self, message): """ Establish or reuse socket connection and send the given message to the timeseries service. """ logging.debug("MESSAGE=" + str(message)) result = None try: ws = self._get_websocket() ws.send(json.dumps(message)) result = ws.recv() except (websocket.WebSocketConnectionClosedException, Exception) as e: logging.debug("Connection failed, will try again.") logging.debug(e) ws = self._get_websocket(reuse=False) ws.send(json.dumps(message)) result = ws.recv() logging.debug("RESULT=" + str(result)) return result
python
def _send_to_timeseries(self, message): """ Establish or reuse socket connection and send the given message to the timeseries service. """ logging.debug("MESSAGE=" + str(message)) result = None try: ws = self._get_websocket() ws.send(json.dumps(message)) result = ws.recv() except (websocket.WebSocketConnectionClosedException, Exception) as e: logging.debug("Connection failed, will try again.") logging.debug(e) ws = self._get_websocket(reuse=False) ws.send(json.dumps(message)) result = ws.recv() logging.debug("RESULT=" + str(result)) return result
[ "def", "_send_to_timeseries", "(", "self", ",", "message", ")", ":", "logging", ".", "debug", "(", "\"MESSAGE=\"", "+", "str", "(", "message", ")", ")", "result", "=", "None", "try", ":", "ws", "=", "self", ".", "_get_websocket", "(", ")", "ws", ".", "send", "(", "json", ".", "dumps", "(", "message", ")", ")", "result", "=", "ws", ".", "recv", "(", ")", "except", "(", "websocket", ".", "WebSocketConnectionClosedException", ",", "Exception", ")", "as", "e", ":", "logging", ".", "debug", "(", "\"Connection failed, will try again.\"", ")", "logging", ".", "debug", "(", "e", ")", "ws", "=", "self", ".", "_get_websocket", "(", "reuse", "=", "False", ")", "ws", ".", "send", "(", "json", ".", "dumps", "(", "message", ")", ")", "result", "=", "ws", ".", "recv", "(", ")", "logging", ".", "debug", "(", "\"RESULT=\"", "+", "str", "(", "result", ")", ")", "return", "result" ]
Establish or reuse socket connection and send the given message to the timeseries service.
[ "Establish", "or", "reuse", "socket", "connection", "and", "send", "the", "given", "message", "to", "the", "timeseries", "service", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/data/timeseries.py#L399-L420
PredixDev/predixpy
predix/data/timeseries.py
TimeSeries.queue
def queue(self, name, value, quality=None, timestamp=None, attributes=None): """ To reduce network traffic, you can buffer datapoints and then flush() anything in the queue. :param name: the name / label / tag for sensor data :param value: the sensor reading or value to record :param quality: the quality value, use the constants BAD, GOOD, etc. (optional and defaults to UNCERTAIN) :param timestamp: the time the reading was recorded in epoch milliseconds (optional and defaults to now) :param attributes: dictionary for any key-value pairs to store with the reading (optional) """ # Get timestamp first in case delay opening websocket connection # and it must have millisecond accuracy if not timestamp: timestamp = int(round(time.time() * 1000)) else: # Coerce datetime objects to epoch if isinstance(timestamp, datetime.datetime): timestamp = int(round(int(timestamp.strftime('%s')) * 1000)) # Only specific quality values supported if quality not in [self.BAD, self.GOOD, self.NA, self.UNCERTAIN]: quality = self.UNCERTAIN # Check if adding to queue of an existing tag and add second datapoint for point in self._queue: if point['name'] == name: point['datapoints'].append([timestamp, value, quality]) return # If adding new tag, initialize and set any attributes datapoint = { "name": name, "datapoints": [[timestamp, value, quality]] } # Attributes are extra details for a datapoint if attributes is not None: if not isinstance(attributes, dict): raise ValueError("Attributes are expected to be a dictionary.") # Validate rules for attribute keys to provide guidance. invalid_value = ':;= ' has_invalid_value = re.compile(r'[%s]' % (invalid_value)).search has_valid_key = re.compile(r'^[\w\.\/\-]+$').search for (key, val) in list(attributes.items()): # Values cannot be empty if (val == '') or (val is None): raise ValueError("Attribute (%s) must have a non-empty value." % (key)) # Values should be treated as a string for regex validation val = str(val) # Values cannot contain certain arbitrary characters if bool(has_invalid_value(val)): raise ValueError("Attribute (%s) cannot contain (%s)." % (key, invalid_value)) # Attributes have to be alphanumeric-ish if not bool(has_valid_key): raise ValueError("Key (%s) not alphanumeric-ish." % (key)) datapoint['attributes'] = attributes self._queue.append(datapoint) logging.debug("QUEUE: " + str(len(self._queue)))
python
def queue(self, name, value, quality=None, timestamp=None, attributes=None): """ To reduce network traffic, you can buffer datapoints and then flush() anything in the queue. :param name: the name / label / tag for sensor data :param value: the sensor reading or value to record :param quality: the quality value, use the constants BAD, GOOD, etc. (optional and defaults to UNCERTAIN) :param timestamp: the time the reading was recorded in epoch milliseconds (optional and defaults to now) :param attributes: dictionary for any key-value pairs to store with the reading (optional) """ # Get timestamp first in case delay opening websocket connection # and it must have millisecond accuracy if not timestamp: timestamp = int(round(time.time() * 1000)) else: # Coerce datetime objects to epoch if isinstance(timestamp, datetime.datetime): timestamp = int(round(int(timestamp.strftime('%s')) * 1000)) # Only specific quality values supported if quality not in [self.BAD, self.GOOD, self.NA, self.UNCERTAIN]: quality = self.UNCERTAIN # Check if adding to queue of an existing tag and add second datapoint for point in self._queue: if point['name'] == name: point['datapoints'].append([timestamp, value, quality]) return # If adding new tag, initialize and set any attributes datapoint = { "name": name, "datapoints": [[timestamp, value, quality]] } # Attributes are extra details for a datapoint if attributes is not None: if not isinstance(attributes, dict): raise ValueError("Attributes are expected to be a dictionary.") # Validate rules for attribute keys to provide guidance. invalid_value = ':;= ' has_invalid_value = re.compile(r'[%s]' % (invalid_value)).search has_valid_key = re.compile(r'^[\w\.\/\-]+$').search for (key, val) in list(attributes.items()): # Values cannot be empty if (val == '') or (val is None): raise ValueError("Attribute (%s) must have a non-empty value." % (key)) # Values should be treated as a string for regex validation val = str(val) # Values cannot contain certain arbitrary characters if bool(has_invalid_value(val)): raise ValueError("Attribute (%s) cannot contain (%s)." % (key, invalid_value)) # Attributes have to be alphanumeric-ish if not bool(has_valid_key): raise ValueError("Key (%s) not alphanumeric-ish." % (key)) datapoint['attributes'] = attributes self._queue.append(datapoint) logging.debug("QUEUE: " + str(len(self._queue)))
[ "def", "queue", "(", "self", ",", "name", ",", "value", ",", "quality", "=", "None", ",", "timestamp", "=", "None", ",", "attributes", "=", "None", ")", ":", "# Get timestamp first in case delay opening websocket connection", "# and it must have millisecond accuracy", "if", "not", "timestamp", ":", "timestamp", "=", "int", "(", "round", "(", "time", ".", "time", "(", ")", "*", "1000", ")", ")", "else", ":", "# Coerce datetime objects to epoch", "if", "isinstance", "(", "timestamp", ",", "datetime", ".", "datetime", ")", ":", "timestamp", "=", "int", "(", "round", "(", "int", "(", "timestamp", ".", "strftime", "(", "'%s'", ")", ")", "*", "1000", ")", ")", "# Only specific quality values supported", "if", "quality", "not", "in", "[", "self", ".", "BAD", ",", "self", ".", "GOOD", ",", "self", ".", "NA", ",", "self", ".", "UNCERTAIN", "]", ":", "quality", "=", "self", ".", "UNCERTAIN", "# Check if adding to queue of an existing tag and add second datapoint", "for", "point", "in", "self", ".", "_queue", ":", "if", "point", "[", "'name'", "]", "==", "name", ":", "point", "[", "'datapoints'", "]", ".", "append", "(", "[", "timestamp", ",", "value", ",", "quality", "]", ")", "return", "# If adding new tag, initialize and set any attributes", "datapoint", "=", "{", "\"name\"", ":", "name", ",", "\"datapoints\"", ":", "[", "[", "timestamp", ",", "value", ",", "quality", "]", "]", "}", "# Attributes are extra details for a datapoint", "if", "attributes", "is", "not", "None", ":", "if", "not", "isinstance", "(", "attributes", ",", "dict", ")", ":", "raise", "ValueError", "(", "\"Attributes are expected to be a dictionary.\"", ")", "# Validate rules for attribute keys to provide guidance.", "invalid_value", "=", "':;= '", "has_invalid_value", "=", "re", ".", "compile", "(", "r'[%s]'", "%", "(", "invalid_value", ")", ")", ".", "search", "has_valid_key", "=", "re", ".", "compile", "(", "r'^[\\w\\.\\/\\-]+$'", ")", ".", "search", "for", "(", "key", ",", "val", ")", "in", "list", "(", "attributes", ".", "items", "(", ")", ")", ":", "# Values cannot be empty", "if", "(", "val", "==", "''", ")", "or", "(", "val", "is", "None", ")", ":", "raise", "ValueError", "(", "\"Attribute (%s) must have a non-empty value.\"", "%", "(", "key", ")", ")", "# Values should be treated as a string for regex validation", "val", "=", "str", "(", "val", ")", "# Values cannot contain certain arbitrary characters", "if", "bool", "(", "has_invalid_value", "(", "val", ")", ")", ":", "raise", "ValueError", "(", "\"Attribute (%s) cannot contain (%s).\"", "%", "(", "key", ",", "invalid_value", ")", ")", "# Attributes have to be alphanumeric-ish", "if", "not", "bool", "(", "has_valid_key", ")", ":", "raise", "ValueError", "(", "\"Key (%s) not alphanumeric-ish.\"", "%", "(", "key", ")", ")", "datapoint", "[", "'attributes'", "]", "=", "attributes", "self", ".", "_queue", ".", "append", "(", "datapoint", ")", "logging", ".", "debug", "(", "\"QUEUE: \"", "+", "str", "(", "len", "(", "self", ".", "_queue", ")", ")", ")" ]
To reduce network traffic, you can buffer datapoints and then flush() anything in the queue. :param name: the name / label / tag for sensor data :param value: the sensor reading or value to record :param quality: the quality value, use the constants BAD, GOOD, etc. (optional and defaults to UNCERTAIN) :param timestamp: the time the reading was recorded in epoch milliseconds (optional and defaults to now) :param attributes: dictionary for any key-value pairs to store with the reading (optional)
[ "To", "reduce", "network", "traffic", "you", "can", "buffer", "datapoints", "and", "then", "flush", "()", "anything", "in", "the", "queue", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/data/timeseries.py#L422-L498
PredixDev/predixpy
predix/data/timeseries.py
TimeSeries.send
def send(self, name=None, value=None, **kwargs): """ Can accept a name/tag and value to be queued and then send anything in the queue to the time series service. Optional parameters include setting quality, timestamp, or attributes. See spec for queue() for complete list of options. Example of sending a batch of values: queue('temp', 70.1) queue('humidity', 20.4) send() Example of sending one and flushing queue immediately send('temp', 70.3) send('temp', 70.4, quality=ts.GOOD, attributes={'unit': 'F'}) """ if name and value: self.queue(name, value, **kwargs) timestamp = int(round(time.time() * 1000)) # The label "name" or "tag" is sometimes used ambiguously msg = { "messageId": timestamp, "body": self._queue } self._queue = [] return self._send_to_timeseries(msg)
python
def send(self, name=None, value=None, **kwargs): """ Can accept a name/tag and value to be queued and then send anything in the queue to the time series service. Optional parameters include setting quality, timestamp, or attributes. See spec for queue() for complete list of options. Example of sending a batch of values: queue('temp', 70.1) queue('humidity', 20.4) send() Example of sending one and flushing queue immediately send('temp', 70.3) send('temp', 70.4, quality=ts.GOOD, attributes={'unit': 'F'}) """ if name and value: self.queue(name, value, **kwargs) timestamp = int(round(time.time() * 1000)) # The label "name" or "tag" is sometimes used ambiguously msg = { "messageId": timestamp, "body": self._queue } self._queue = [] return self._send_to_timeseries(msg)
[ "def", "send", "(", "self", ",", "name", "=", "None", ",", "value", "=", "None", ",", "*", "*", "kwargs", ")", ":", "if", "name", "and", "value", ":", "self", ".", "queue", "(", "name", ",", "value", ",", "*", "*", "kwargs", ")", "timestamp", "=", "int", "(", "round", "(", "time", ".", "time", "(", ")", "*", "1000", ")", ")", "# The label \"name\" or \"tag\" is sometimes used ambiguously", "msg", "=", "{", "\"messageId\"", ":", "timestamp", ",", "\"body\"", ":", "self", ".", "_queue", "}", "self", ".", "_queue", "=", "[", "]", "return", "self", ".", "_send_to_timeseries", "(", "msg", ")" ]
Can accept a name/tag and value to be queued and then send anything in the queue to the time series service. Optional parameters include setting quality, timestamp, or attributes. See spec for queue() for complete list of options. Example of sending a batch of values: queue('temp', 70.1) queue('humidity', 20.4) send() Example of sending one and flushing queue immediately send('temp', 70.3) send('temp', 70.4, quality=ts.GOOD, attributes={'unit': 'F'})
[ "Can", "accept", "a", "name", "/", "tag", "and", "value", "to", "be", "queued", "and", "then", "send", "anything", "in", "the", "queue", "to", "the", "time", "series", "service", ".", "Optional", "parameters", "include", "setting", "quality", "timestamp", "or", "attributes", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/data/timeseries.py#L500-L534
PredixDev/predixpy
predix/data/dbaas.py
PostgreSQL.execute
def execute(self, statement, *args, **kwargs): """ This convenience method will execute the query passed in as is. For more complex functionality you may want to use the sqlalchemy engine directly, but this serves as an example implementation. :param select_query: SQL statement to execute that will identify the resultset of interest. """ with self.engine.connect() as conn: s = sqlalchemy.sql.text(statement) return conn.execute(s, **kwargs)
python
def execute(self, statement, *args, **kwargs): """ This convenience method will execute the query passed in as is. For more complex functionality you may want to use the sqlalchemy engine directly, but this serves as an example implementation. :param select_query: SQL statement to execute that will identify the resultset of interest. """ with self.engine.connect() as conn: s = sqlalchemy.sql.text(statement) return conn.execute(s, **kwargs)
[ "def", "execute", "(", "self", ",", "statement", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "with", "self", ".", "engine", ".", "connect", "(", ")", "as", "conn", ":", "s", "=", "sqlalchemy", ".", "sql", ".", "text", "(", "statement", ")", "return", "conn", ".", "execute", "(", "s", ",", "*", "*", "kwargs", ")" ]
This convenience method will execute the query passed in as is. For more complex functionality you may want to use the sqlalchemy engine directly, but this serves as an example implementation. :param select_query: SQL statement to execute that will identify the resultset of interest.
[ "This", "convenience", "method", "will", "execute", "the", "query", "passed", "in", "as", "is", ".", "For", "more", "complex", "functionality", "you", "may", "want", "to", "use", "the", "sqlalchemy", "engine", "directly", "but", "this", "serves", "as", "an", "example", "implementation", "." ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/data/dbaas.py#L84-L96
PredixDev/predixpy
predix/data/eventhub/client.py
Eventhub.shutdown
def shutdown(self): """ Shutdown the client, shutdown the sub clients and stop the health checker :return: None """ self._run_health_checker = False if self.publisher is not None: self.publisher.shutdown() if self.subscriber is not None: self.subscriber.shutdown()
python
def shutdown(self): """ Shutdown the client, shutdown the sub clients and stop the health checker :return: None """ self._run_health_checker = False if self.publisher is not None: self.publisher.shutdown() if self.subscriber is not None: self.subscriber.shutdown()
[ "def", "shutdown", "(", "self", ")", ":", "self", ".", "_run_health_checker", "=", "False", "if", "self", ".", "publisher", "is", "not", "None", ":", "self", ".", "publisher", ".", "shutdown", "(", ")", "if", "self", ".", "subscriber", "is", "not", "None", ":", "self", ".", "subscriber", ".", "shutdown", "(", ")" ]
Shutdown the client, shutdown the sub clients and stop the health checker :return: None
[ "Shutdown", "the", "client", "shutdown", "the", "sub", "clients", "and", "stop", "the", "health", "checker", ":", "return", ":", "None" ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/data/eventhub/client.py#L49-L59
PredixDev/predixpy
predix/data/eventhub/client.py
Eventhub.get_service_env_value
def get_service_env_value(self, key): """ Get a env variable as defined by the service admin :param key: the base of the key to use :return: the env if it exists """ service_key = predix.config.get_env_key(self, key) value = os.environ[service_key] if not value: raise ValueError("%s env unset" % key) return value
python
def get_service_env_value(self, key): """ Get a env variable as defined by the service admin :param key: the base of the key to use :return: the env if it exists """ service_key = predix.config.get_env_key(self, key) value = os.environ[service_key] if not value: raise ValueError("%s env unset" % key) return value
[ "def", "get_service_env_value", "(", "self", ",", "key", ")", ":", "service_key", "=", "predix", ".", "config", ".", "get_env_key", "(", "self", ",", "key", ")", "value", "=", "os", ".", "environ", "[", "service_key", "]", "if", "not", "value", ":", "raise", "ValueError", "(", "\"%s env unset\"", "%", "key", ")", "return", "value" ]
Get a env variable as defined by the service admin :param key: the base of the key to use :return: the env if it exists
[ "Get", "a", "env", "variable", "as", "defined", "by", "the", "service", "admin", ":", "param", "key", ":", "the", "base", "of", "the", "key", "to", "use", ":", "return", ":", "the", "env", "if", "it", "exists" ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/data/eventhub/client.py#L75-L85
PredixDev/predixpy
predix/data/eventhub/client.py
Eventhub._init_channel
def _init_channel(self): """ build the grpc channel used for both publisher and subscriber :return: None """ host = self._get_host() port = self._get_grpc_port() if 'TLS_PEM_FILE' in os.environ: with open(os.environ['TLS_PEM_FILE'], mode='rb') as f: # b is important -> binary file_content = f.read() credentials = grpc.ssl_channel_credentials(root_certificates=file_content) else: credentials = grpc.ssl_channel_credentials() self._channel = grpc.secure_channel(host + ":" + port, credentials=credentials) self._init_health_checker()
python
def _init_channel(self): """ build the grpc channel used for both publisher and subscriber :return: None """ host = self._get_host() port = self._get_grpc_port() if 'TLS_PEM_FILE' in os.environ: with open(os.environ['TLS_PEM_FILE'], mode='rb') as f: # b is important -> binary file_content = f.read() credentials = grpc.ssl_channel_credentials(root_certificates=file_content) else: credentials = grpc.ssl_channel_credentials() self._channel = grpc.secure_channel(host + ":" + port, credentials=credentials) self._init_health_checker()
[ "def", "_init_channel", "(", "self", ")", ":", "host", "=", "self", ".", "_get_host", "(", ")", "port", "=", "self", ".", "_get_grpc_port", "(", ")", "if", "'TLS_PEM_FILE'", "in", "os", ".", "environ", ":", "with", "open", "(", "os", ".", "environ", "[", "'TLS_PEM_FILE'", "]", ",", "mode", "=", "'rb'", ")", "as", "f", ":", "# b is important -> binary", "file_content", "=", "f", ".", "read", "(", ")", "credentials", "=", "grpc", ".", "ssl_channel_credentials", "(", "root_certificates", "=", "file_content", ")", "else", ":", "credentials", "=", "grpc", ".", "ssl_channel_credentials", "(", ")", "self", ".", "_channel", "=", "grpc", ".", "secure_channel", "(", "host", "+", "\":\"", "+", "port", ",", "credentials", "=", "credentials", ")", "self", ".", "_init_health_checker", "(", ")" ]
build the grpc channel used for both publisher and subscriber :return: None
[ "build", "the", "grpc", "channel", "used", "for", "both", "publisher", "and", "subscriber", ":", "return", ":", "None" ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/data/eventhub/client.py#L87-L103
PredixDev/predixpy
predix/data/eventhub/client.py
Eventhub._init_health_checker
def _init_health_checker(self): """ start the health checker stub and start a thread to ping it every 30 seconds :return: None """ stub = Health_pb2_grpc.HealthStub(channel=self._channel) self._health_check = stub.Check health_check_thread = threading.Thread(target=self._health_check_thread) health_check_thread.daemon = True health_check_thread.start()
python
def _init_health_checker(self): """ start the health checker stub and start a thread to ping it every 30 seconds :return: None """ stub = Health_pb2_grpc.HealthStub(channel=self._channel) self._health_check = stub.Check health_check_thread = threading.Thread(target=self._health_check_thread) health_check_thread.daemon = True health_check_thread.start()
[ "def", "_init_health_checker", "(", "self", ")", ":", "stub", "=", "Health_pb2_grpc", ".", "HealthStub", "(", "channel", "=", "self", ".", "_channel", ")", "self", ".", "_health_check", "=", "stub", ".", "Check", "health_check_thread", "=", "threading", ".", "Thread", "(", "target", "=", "self", ".", "_health_check_thread", ")", "health_check_thread", ".", "daemon", "=", "True", "health_check_thread", ".", "start", "(", ")" ]
start the health checker stub and start a thread to ping it every 30 seconds :return: None
[ "start", "the", "health", "checker", "stub", "and", "start", "a", "thread", "to", "ping", "it", "every", "30", "seconds", ":", "return", ":", "None" ]
train
https://github.com/PredixDev/predixpy/blob/a0cb34cf40f716229351bb6d90d6ecace958c81f/predix/data/eventhub/client.py#L105-L114