Python six.text_type() Examples
The following are 30
code examples of six.text_type().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
six
, or try the search function
.
Example #1
Source File: models.py From figures with MIT License | 7 votes |
def get_prep_value(self, value): if value is self.Empty or value is None: return '' # CharFields should use '' as their empty value, rather than None if isinstance(value, six.string_types): value = self.KEY_CLASS.from_string(value) assert isinstance(value, self.KEY_CLASS), "%s is not an instance of %s" % (value, self.KEY_CLASS) serialized_key = six.text_type(_strip_value(value)) if serialized_key.endswith('\n'): # An opaque key object serialized to a string with a trailing newline. # Log the value - but do not modify it. log.warning(u'{}:{}:{}:get_prep_value: Invalid key: {}.'.format( self.model._meta.db_table, # pylint: disable=protected-access self.name, self.KEY_CLASS.__name__, repr(serialized_key) )) return serialized_key
Example #2
Source File: api.py From gnocchi with Apache License 2.0 | 6 votes |
def patch(self): ap = pecan.request.indexer.get_archive_policy(self.archive_policy) if not ap: abort(404, six.text_type( indexer.NoSuchArchivePolicy(self.archive_policy))) enforce("update archive policy", ap) body = deserialize_and_validate(voluptuous.Schema({ voluptuous.Required("definition"): ArchivePolicyDefinitionSchema, })) # Validate the data try: ap_items = [archive_policy.ArchivePolicyItem(**item) for item in body['definition']] except ValueError as e: abort(400, six.text_type(e)) try: return pecan.request.indexer.update_archive_policy( self.archive_policy, ap_items) except indexer.UnsupportedArchivePolicyChange as e: abort(400, six.text_type(e))
Example #3
Source File: api.py From gnocchi with Apache License 2.0 | 6 votes |
def post(self): schema = pecan.request.indexer.get_resource_type_schema() body = deserialize_and_validate(schema) body["state"] = "creating" try: rt = schema.resource_type_from_dict(**body) except resource_type.InvalidResourceAttribute as e: abort(400, "Invalid input: %s" % e) enforce("create resource type", body) try: rt = pecan.request.indexer.create_resource_type(rt) except indexer.ResourceTypeAlreadyExists as e: abort(409, six.text_type(e)) set_resp_location_hdr("/resource_type/" + rt.name) pecan.response.status = 201 return rt
Example #4
Source File: forms.py From django-payfast with MIT License | 6 votes |
def is_payfast_ip_address(ip_address_str): """ Return True if ip_address_str matches one of PayFast's server IP addresses. Setting: `PAYFAST_IP_ADDRESSES` :type ip_address_str: str :rtype: bool """ # TODO: Django system check for validity? payfast_ip_addresses = getattr(settings, 'PAYFAST_IP_ADDRESSES', conf.DEFAULT_PAYFAST_IP_ADDRESSES) if sys.version_info < (3,): # Python 2 usability: Coerce str to unicode, to avoid very common TypeErrors. # (On Python 3, this should generally not happen: # let unexpected bytes values fail as expected.) ip_address_str = unicode(ip_address_str) # noqa: F821 payfast_ip_addresses = [unicode(address) for address in payfast_ip_addresses] # noqa: F821 return any(ip_address(ip_address_str) in ip_network(payfast_address) for payfast_address in payfast_ip_addresses)
Example #5
Source File: api.py From django-payfast with MIT License | 6 votes |
def _prepare_signable_fields( valid_field_order, # type: Sequence[str] data_fields, # type: Mapping[str, str] ): # type: (...) -> SignableFields """ Prepare PayFast submission variables for signing, using the given field order. :raise ValueError: If `data_fields` contains any unexpected field names not in `valid_field_order`. """ present_fields = (set(data_fields.keys()) if sys.version_info < (3,) else data_fields.keys()) extra_fields = present_fields - set(valid_field_order) if extra_fields: raise ValueError('Data contains unexpected fields: {!r}'.format(extra_fields)) return [ (name, data_fields[name]) for name in valid_field_order if name in data_fields ]
Example #6
Source File: text_encoder.py From fine-lm with MIT License | 6 votes |
def _escape_token(token, alphabet): """Escape away underscores and OOV characters and append '_'. This allows the token to be expressed as the concatenation of a list of subtokens from the vocabulary. The underscore acts as a sentinel which allows us to invertibly concatenate multiple such lists. Args: token: A unicode string to be escaped. alphabet: A set of all characters in the vocabulary's alphabet. Returns: escaped_token: An escaped unicode string. Raises: ValueError: If the provided token is not unicode. """ if not isinstance(token, six.text_type): raise ValueError("Expected string type for token, got %s" % type(token)) token = token.replace(u"\\", u"\\\\").replace(u"_", u"\\u") ret = [c if c in alphabet and c != u"\n" else r"\%d;" % ord(c) for c in token] return u"".join(ret) + "_"
Example #7
Source File: api.py From gnocchi with Apache License 2.0 | 6 votes |
def post(self): resource = pecan.request.indexer.get_resource( self.resource_type, self.resource_id) if not resource: abort(404, six.text_type(indexer.NoSuchResource(self.resource_id))) enforce("update resource", resource) metrics = deserialize_and_validate(MetricsSchema) try: r = pecan.request.indexer.update_resource( self.resource_type, self.resource_id, metrics=metrics, append_metrics=True, create_revision=False) except (indexer.NoSuchMetric, indexer.NoSuchArchivePolicy, ValueError) as e: abort(400, six.text_type(e)) except indexer.NamedMetricAlreadyExists as e: abort(409, six.text_type(e)) except indexer.NoSuchResource as e: abort(404, six.text_type(e)) return r.metrics
Example #8
Source File: api.py From gnocchi with Apache License 2.0 | 6 votes |
def _lookup(self, name, *remainder): m = pecan.request.indexer.list_metrics( details=True, attribute_filter={"and": [ {"=": {"name": name}}, {"=": {"resource_id": self.resource_id}}, ]}) if m: return MetricController(m[0]), remainder resource = pecan.request.indexer.get_resource(self.resource_type, self.resource_id) if resource: abort(404, six.text_type(indexer.NoSuchMetric(name))) else: abort(404, six.text_type(indexer.NoSuchResource(self.resource_id)))
Example #9
Source File: _util.py From multibootusb with GNU General Public License v2.0 | 6 votes |
def property_value_to_bytes(value): """ Return a byte string, which represents the given ``value`` in a way suitable as raw value of an udev property. If ``value`` is a boolean object, it is converted to ``'1'`` or ``'0'``, depending on whether ``value`` is ``True`` or ``False``. If ``value`` is a byte string already, it is returned unchanged. Anything else is simply converted to a unicode string, and then passed to :func:`ensure_byte_string`. """ # udev represents boolean values as 1 or 0, therefore an explicit # conversion to int is required for boolean values if isinstance(value, bool): value = int(value) if isinstance(value, bytes): return value else: return ensure_byte_string(six.text_type(value))
Example #10
Source File: api.py From gnocchi with Apache License 2.0 | 6 votes |
def post(self): creator = pecan.request.auth_helper.get_current_user( pecan.request) body = deserialize_and_validate(self.MetricSchema) resource_id = body.get('resource_id') if resource_id is not None: resource_id = resource_id[1] try: m = pecan.request.indexer.create_metric( uuid.uuid4(), creator, resource_id=resource_id, name=body.get('name'), unit=body.get('unit'), archive_policy_name=body['archive_policy_name']) except indexer.NoSuchArchivePolicy as e: abort(400, six.text_type(e)) except indexer.NamedMetricAlreadyExists as e: abort(400, e) set_resp_location_hdr("/metric/" + str(m.id)) pecan.response.status = 201 return m
Example #11
Source File: resource_type.py From gnocchi with Apache License 2.0 | 6 votes |
def __init__(self, *args, **kwargs): super(ResourceTypeSchemaManager, self).__init__(*args, **kwargs) type_schemas = tuple([ext.plugin.meta_schema() for ext in self.extensions]) self._schema = voluptuous.Schema({ "name": six.text_type, voluptuous.Required("attributes", default={}): { six.text_type: voluptuous.Any(*tuple(type_schemas)) } }) type_schemas = tuple([ext.plugin.meta_schema(for_update=True) for ext in self.extensions]) self._schema_for_update = voluptuous.Schema({ "name": six.text_type, voluptuous.Required("attributes", default={}): { six.text_type: voluptuous.Any(*tuple(type_schemas)) } })
Example #12
Source File: api.py From gnocchi with Apache License 2.0 | 6 votes |
def post(self): enforce("create archive policy rule", {}) ArchivePolicyRuleSchema = voluptuous.Schema({ voluptuous.Required("name"): six.text_type, voluptuous.Required("metric_pattern"): six.text_type, voluptuous.Required("archive_policy_name"): six.text_type, }) body = deserialize_and_validate(ArchivePolicyRuleSchema) enforce("create archive policy rule", body) try: ap = pecan.request.indexer.create_archive_policy_rule( body['name'], body['metric_pattern'], body['archive_policy_name'] ) except indexer.ArchivePolicyRuleAlreadyExists as e: abort(409, six.text_type(e)) except indexer.NoSuchArchivePolicy as e: abort(400, e) location = "/archive_policy_rule/" + ap.name set_resp_location_hdr(location) pecan.response.status = 201 return ap
Example #13
Source File: test_rest.py From gnocchi with Apache License 2.0 | 6 votes |
def test_delete_resource_with_metrics(self): metric = self.app.post_json( "/v1/metric", params={'archive_policy_name': "high"}) metric_id = json.loads(metric.text)['id'] metric_name = six.text_type(uuid.uuid4()) self.attributes['metrics'] = {metric_name: metric_id} self.app.get("/v1/metric/" + metric_id, status=200) self.app.post_json("/v1/resource/" + self.resource_type, params=self.attributes) self.app.get("/v1/resource/" + self.resource_type + "/" + self.attributes['id'], status=200) self.app.delete("/v1/resource/" + self.resource_type + "/" + self.attributes['id'], status=204) self.app.get("/v1/resource/" + self.resource_type + "/" + self.attributes['id'], status=404) self.app.get("/v1/metric/" + metric_id, status=404)
Example #14
Source File: test_message.py From oslo.i18n with Apache License 2.0 | 6 votes |
def test_translate_message_with_param_from_unicoded_obj(self, mock_translation): message_with_params = 'A message: %s' es_translation = 'A message in Spanish: %s' param = 'A Message param' translations = {message_with_params: es_translation} translator = fakes.FakeTranslations.translator({'es': translations}) mock_translation.side_effect = translator msg = _message.Message(message_with_params) msg = msg % param default_translation = message_with_params % param expected_translation = es_translation % param obj = utils.SomeObject(msg) unicoded_obj = six.text_type(obj) self.assertEqual(expected_translation, unicoded_obj.translation('es')) self.assertEqual(default_translation, unicoded_obj.translation('XX'))
Example #15
Source File: api.py From gnocchi with Apache License 2.0 | 5 votes |
def _lookup(self, archive_policy_rule, *remainder): apr = pecan.request.indexer.get_archive_policy_rule( archive_policy_rule ) if apr: return ArchivePolicyRuleController(apr), remainder abort(404, six.text_type( indexer.NoSuchArchivePolicyRule(archive_policy_rule)))
Example #16
Source File: api.py From gnocchi with Apache License 2.0 | 5 votes |
def _lookup(self, resource_type, *remainder): try: pecan.request.indexer.get_resource_type(resource_type) except indexer.NoSuchResourceType as e: abort(404, six.text_type(e)) return SearchResourceTypeController(resource_type), remainder
Example #17
Source File: base.py From gnocchi with Apache License 2.0 | 5 votes |
def _skip_decorator(func): @functools.wraps(func) def skip_if_not_implemented(*args, **kwargs): try: return func(*args, **kwargs) except exceptions.NotImplementedError as e: raise testcase.TestSkipped(six.text_type(e)) return skip_if_not_implemented
Example #18
Source File: api.py From gnocchi with Apache License 2.0 | 5 votes |
def _lookup(self, object_type, resource_type, key, metric_name, *remainder): if object_type != "resource" or key != "metric": # NOTE(sileht): we want the raw 404 message here # so use directly pecan pecan.abort(404) try: pecan.request.indexer.get_resource_type(resource_type) except indexer.NoSuchResourceType as e: abort(404, six.text_type(e)) return AggregationResourceController(resource_type, metric_name), remainder
Example #19
Source File: api.py From gnocchi with Apache License 2.0 | 5 votes |
def get_metric(self, metric=None, start=None, stop=None, aggregation='mean', reaggregation=None, granularity=None, needed_overlap=100.0, fill=None, refresh=False, resample=None): if pecan.request.method == 'GET': try: metric_ids = voluptuous.Schema( self.MetricIDsSchema, required=True)(arg_to_list(metric)) except voluptuous.Error as e: abort(400, "Invalid input: %s" % e) else: self._workaround_pecan_issue_88() metric_ids = deserialize_and_validate(self.MetricIDsSchema) metric_ids = [six.text_type(m) for m in metric_ids] # Check RBAC policy metrics = pecan.request.indexer.list_metrics( attribute_filter={"in": {"id": metric_ids}}) missing_metric_ids = (set(metric_ids) - set(six.text_type(m.id) for m in metrics)) if missing_metric_ids: # Return one of the missing one in the error abort(404, six.text_type(storage.MetricDoesNotExist( missing_metric_ids.pop()))) return self.get_cross_metric_measures_from_objs( metrics, start, stop, aggregation, reaggregation, granularity, needed_overlap, fill, refresh, resample)
Example #20
Source File: api.py From gnocchi with Apache License 2.0 | 5 votes |
def BackwardCompatibleMeasuresList(v): v = voluptuous.Schema( voluptuous.Any(MeasuresListSchema, {voluptuous.Optional("archive_policy_name"): six.text_type, voluptuous.Optional("unit"): six.text_type, "measures": MeasuresListSchema}), required=True)(v) if isinstance(v, dict): return v else: # Old format return {"measures": v}
Example #21
Source File: api.py From gnocchi with Apache License 2.0 | 5 votes |
def ResourceSchema(schema): base_schema = { voluptuous.Optional('started_at'): utils.to_datetime, voluptuous.Optional('ended_at'): utils.to_datetime, voluptuous.Optional('user_id'): voluptuous.Any(None, six.text_type), voluptuous.Optional('project_id'): voluptuous.Any(None, six.text_type), voluptuous.Optional('metrics'): MetricsSchema, } base_schema.update(schema) return base_schema
Example #22
Source File: api.py From gnocchi with Apache License 2.0 | 5 votes |
def delete(self): # NOTE(jd) I don't think there's any point in fetching and passing the # archive policy here, as the rule is probably checking the actual role # of the user, not the content of the AP. enforce("delete archive policy", {}) try: pecan.request.indexer.delete_archive_policy(self.archive_policy) except indexer.NoSuchArchivePolicy as e: abort(404, six.text_type(e)) except indexer.ArchivePolicyInUse as e: abort(400, six.text_type(e))
Example #23
Source File: api.py From gnocchi with Apache License 2.0 | 5 votes |
def post(self): enforce("create archive policy", {}) # NOTE(jd): Initialize this one at run-time because we rely on conf conf = pecan.request.conf valid_agg_methods = list( archive_policy.ArchivePolicy.VALID_AGGREGATION_METHODS_VALUES ) ArchivePolicySchema = voluptuous.Schema({ voluptuous.Required("name"): six.text_type, voluptuous.Required("back_window", default=0): voluptuous.All( voluptuous.Coerce(int), voluptuous.Range(min=0), ), voluptuous.Required( "aggregation_methods", default=list(conf.archive_policy.default_aggregation_methods)): valid_agg_methods, voluptuous.Required("definition"): ArchivePolicyDefinitionSchema, }) body = deserialize_and_validate(ArchivePolicySchema) # Validate the data try: ap = archive_policy.ArchivePolicy.from_dict(body) except ValueError as e: abort(400, six.text_type(e)) enforce("create archive policy", ap) try: ap = pecan.request.indexer.create_archive_policy(ap) except indexer.ArchivePolicyAlreadyExists as e: abort(409, six.text_type(e)) location = "/archive_policy/" + ap.name set_resp_location_hdr(location) pecan.response.status = 201 return ap
Example #24
Source File: api.py From gnocchi with Apache License 2.0 | 5 votes |
def delete(self): try: pecan.request.indexer.get_resource_type(self._name) except indexer.NoSuchResourceType as e: abort(404, six.text_type(e)) enforce("delete resource type", resource_type) try: pecan.request.indexer.delete_resource_type(self._name) except (indexer.NoSuchResourceType, indexer.ResourceTypeInUse) as e: abort(400, six.text_type(e))
Example #25
Source File: api.py From gnocchi with Apache License 2.0 | 5 votes |
def get(self): try: rt = pecan.request.indexer.get_resource_type(self._name) except indexer.NoSuchResourceType as e: abort(404, six.text_type(e)) enforce("get resource type", rt) return rt
Example #26
Source File: api.py From gnocchi with Apache License 2.0 | 5 votes |
def get(self, **kwargs): details = get_bool_param('details', kwargs) pagination_opts = get_pagination_options( kwargs, RESOURCE_DEFAULT_PAGINATION) resource = pecan.request.indexer.get_resource( self.resource_type, self.resource_id) if not resource: abort(404, six.text_type(indexer.NoSuchResource(self.resource_id))) enforce("get resource", resource) try: resources = pecan.request.indexer.list_resources( self.resource_type, attribute_filter={"=": {"id": self.resource_id}}, details=details, history=True, **pagination_opts ) if resources and len(resources) >= pagination_opts['limit']: marker = "%s@%s" % (resources[-1].id, resources[-1].revision) set_resp_link_hdr(marker, kwargs, pagination_opts) return resources except indexer.IndexerException as e: abort(400, six.text_type(e))
Example #27
Source File: api.py From gnocchi with Apache License 2.0 | 5 votes |
def get_all(self): resource = pecan.request.indexer.get_resource( self.resource_type, self.resource_id) if not resource: abort(404, six.text_type(indexer.NoSuchResource(self.resource_id))) enforce("get resource", resource) return pecan.request.indexer.list_metrics( attribute_filter={"=": {"resource_id": self.resource_id}})
Example #28
Source File: api.py From gnocchi with Apache License 2.0 | 5 votes |
def delete(self): # NOTE(jd) I don't think there's any point in fetching and passing the # archive policy rule here, as the rule is probably checking the actual # role of the user, not the content of the AP rule. enforce("delete archive policy rule", {}) try: pecan.request.indexer.delete_archive_policy_rule( self.archive_policy_rule.name ) except indexer.NoSuchArchivePolicyRule as e: abort(404, six.text_type(e))
Example #29
Source File: api.py From gnocchi with Apache License 2.0 | 5 votes |
def delete(self): self.enforce_metric("delete metric") try: pecan.request.indexer.delete_metric(self.metric.id) except indexer.NoSuchMetric as e: abort(404, six.text_type(e))
Example #30
Source File: api.py From gnocchi with Apache License 2.0 | 5 votes |
def _lookup(self, id, *remainder): try: metric_id = uuid.UUID(id) except ValueError: abort(404, six.text_type(indexer.NoSuchMetric(id))) # Load details for ACL metrics = pecan.request.indexer.list_metrics( attribute_filter={"=": {"id": metric_id}}, details=True) if not metrics: abort(404, six.text_type(indexer.NoSuchMetric(id))) return MetricController(metrics[0]), remainder # NOTE(jd) Define this method as it was a voluptuous schema – it's just a # smarter version of a voluptuous schema, no?