示例#1
0
文件: task.py 项目: CrankOne/lamia
 def add_parameters(self, ps):
     L = logging.getLogger()
     if self._userDefaults is None:
         self._userDefaults = {}
     usedUserDfts = set(self._userDefaults.keys())
     self._argNames = set()
     for pName, pDescr in ps.items():
         if '@' != pName[0]:
             shortcut, name = _argparse_par(
                 pName)  #list(filter(lambda x: x, _argparse_par(pName)))
             self._argNames.add(name if name else shortcut)
             assert (name or shortcut)
             if name and name in self._userDefaults:
                 pDescr['default'] = self._userDefaults[name]
                 L.debug('{name} default is set/overriden by users {value}'.
                         format(name=name if name else shortcut,
                                value=self._userDefaults[name]))
                 usedUserDfts.add(name)
             if shortcut: shortcut = '-' + inflection.dasherize(shortcut)
             if name: name = '--' + inflection.dasherize(name)
             nms = list(filter(lambda x: x, [shortcut, name]))
             self.argParser.add_argument(*nms, **pDescr)
             L.debug('"%s" named command-line arg added' % (', '.join(nms)))
         else:
             self.argParser.add_argument(pName[1:], **pDescr)
             L.debug('Added a positional argument.')
     return usedUserDfts
    def parse(self, stream, media_type=None, parser_context=None):
        """Convert JSONAPI data to JSON data"""
        content = super(JsonApiParser, self).parse(stream, media_type=media_type, parser_context=parser_context)

        self.view = parser_context.get("view", None)

        if "data" in content:
            serializer = self.view.get_serializer(instance=None)
            fields = serializer.get_fields()

            resource_data = {}
            for attr_name, val in six.iteritems(content["data"].pop("attributes", {})):
                resource_data[underscore(attr_name)] = val
            relationships = content["data"].pop("relationships", {})
            if content["data"].get("id"):
                resource_data.update(id=content["data"].pop("id"))

            for field_name, field in six.iteritems(fields):
                if dasherize(field_name) not in relationships:
                    continue

                related_field = get_serializer(field)

                if isinstance(related_field, (RelatedField, BaseSerializer)):
                    rel_data = relationships[dasherize(field_name)].get("data")
                    if rel_data:
                        if isinstance(rel_data, list):
                            rel_data = [data.get("id") for data in rel_data]
                        else:
                            rel_data = rel_data.get("id")
                    resource_data[underscore(field_name)] = rel_data
            return resource_data
        return {}
def model_to_command_args(Model):
    '''Take a model class and return a list of args that will create an
    equivalent model in Ember.'''

    fields_to_generate = {
    }

    # iterate over all the fields in this model and create args to build
    # equivalent fields in the Ember model
    for field in Model._meta.get_fields():

        if field.name == 'id':
            # Ember automatically generates the id field, we shouldn't
            # include it
            continue

        was_assigned = False

        for ember_type, django_field_classes in FIELD_TYPE_MAP.items():
            if field.__class__ in django_field_classes:
                if ember_type not in fields_to_generate:
                    fields_to_generate[ember_type] = []

                fields_to_generate[ember_type].append(field)
                was_assigned = True

        if not was_assigned:
            # 'string' is the default tpye -- we didn't match anything else, so
            # we'll assign to that
            if 'string' not in fields_to_generate:
                fields_to_generate['string'] = []

            fields_to_generate['string'].append(field)

    field_args = []
    for ember_type, model_fields in fields_to_generate.items():
        for field in model_fields:
            emberized_field_name = camelize(
                underscore(field.name), uppercase_first_letter=False)

            field_arg = emberized_field_name + ':' + ember_type

            if ember_type in ('belongs-to', 'has-many'):
                # relation fields should also specify what they're related to
                relation_emberized = dasherize(underscore(
                    field.rel.to.__name__))
                field_arg += ':' + relation_emberized

            field_args.append(field_arg)

    full_args = ['generate', 'model', dasherize(underscore(Model.__name__))]
    full_args += field_args

    return full_args
def model_to_command_args(Model):
    '''Take a model class and return a list of args that will create an
    equivalent model in Ember.'''

    fields_to_generate = {}

    # iterate over all the fields in this model and create args to build
    # equivalent fields in the Ember model
    for field in Model._meta.get_fields():

        if field.name == 'id':
            # Ember automatically generates the id field, we shouldn't
            # include it
            continue

        was_assigned = False

        for ember_type, django_field_classes in FIELD_TYPE_MAP.items():
            if field.__class__ in django_field_classes:
                if ember_type not in fields_to_generate:
                    fields_to_generate[ember_type] = []

                fields_to_generate[ember_type].append(field)
                was_assigned = True

        if not was_assigned:
            # 'string' is the default tpye -- we didn't match anything else, so
            # we'll assign to that
            if 'string' not in fields_to_generate:
                fields_to_generate['string'] = []

            fields_to_generate['string'].append(field)

    field_args = []
    for ember_type, model_fields in fields_to_generate.items():
        for field in model_fields:
            emberized_field_name = camelize(underscore(field.name),
                                            uppercase_first_letter=False)

            field_arg = emberized_field_name + ':' + ember_type

            if ember_type in ('belongs-to', 'has-many'):
                # relation fields should also specify what they're related to
                relation_emberized = dasherize(
                    underscore(field.rel.to.__name__))
                field_arg += ':' + relation_emberized

            field_args.append(field_arg)

    full_args = ['generate', 'model', dasherize(underscore(Model.__name__))]
    full_args += field_args

    return full_args
示例#5
0
def format_keys(obj, format_type=None):
    """
    Overwrites rest_framework_json_api/utils.py

    Does not recursively format dictionary keys. Only top level keys are formatted.

    :format_type: Either 'dasherize', 'camelize' or 'underscore'
    """
    if format_type is None:
        format_type = getattr(api_settings, "JSON_API_FORMAT_KEYS", False)

    if format_type in ("dasherize", "camelize", "underscore", "capitalize"):

        if isinstance(obj, dict):
            formatted = OrderedDict()
            for key, value in obj.items():
                if format_type == "dasherize":
                    # inflection can't dasherize camelCase
                    key = inflection.underscore(key)
                    formatted[inflection.dasherize(key)] = value
                elif format_type == "camelize":
                    formatted[inflection.camelize(key, False)] = value
                elif format_type == "capitalize":
                    formatted[inflection.camelize(key)] = value
                elif format_type == "underscore":
                    formatted[inflection.underscore(key)] = value
            return formatted
        if isinstance(obj, list):
            return [format_keys(item, format_type) for item in obj]
        else:
            return obj
    else:
        return obj
示例#6
0
def format_keys(obj, format_type=None):
    if format_type is None:
        format_type = getattr(settings, 'JSON_API_FORMAT_FIELD_NAMES', False)

    if format_type in ('dasherize', 'camelize', 'underscore', 'capitalize'):
        if isinstance(obj, dict):
            formatted = OrderedDict()
            for key, value in obj.items():
                if format_type == 'dasherize':
                    # inflection can't dasherize camelCase
                    key = inflection.underscore(key)
                    formatted[inflection.dasherize(key)] \
                        = format_keys(value, format_type)
                elif format_type == 'camelize':
                    formatted[inflection.camelize(key, False)] \
                        = format_keys(value, format_type)
                elif format_type == 'capitalize':
                    formatted[inflection.camelize(key)] \
                        = format_keys(value, format_type)
                elif format_type == 'underscore':
                    formatted[inflection.underscore(key)] \
                        = format_keys(value, format_type)
            return formatted
        if isinstance(obj, list):
            return [format_keys(item, format_type) for item in obj]
        else:
            return obj
    else:
        return obj
示例#7
0
 def get_type(cls):
     if cls.type_ is None:
         type_ = dasherize(underscore(cls.__name__))
         return type_.replace('model', '').strip('-')
     if not isinstance(cls.type_, str):
         raise Error('"type_" must be a string')
     return cls.type_
示例#8
0
 def list_commands(self, ctx):
     rv = []
     for filename in os.listdir(self.cmd_folder):
         if filename.endswith(".py") and filename.startswith("cmd_"):
             rv.append(inflection.dasherize(filename[4:-3]))
     rv.sort()
     return rv
def format_keys(obj, format_type=None):
    """
    Takes either a dict or list and returns it with camelized keys only if
    JSON_API_FORMAT_KEYS is set.

    :format_type: Either 'dasherize', 'camelize' or 'underscore'
    """
    if format_type is None:
        format_type = getattr(settings, "JSON_API_FORMAT_KEYS", False)

    if format_type in ("dasherize", "camelize", "underscore", "capitalize"):

        if isinstance(obj, dict):
            formatted = OrderedDict()
            for key, value in obj.items():
                if format_type == "dasherize":
                    # inflection can't dasherize camelCase
                    key = inflection.underscore(key)
                    formatted[inflection.dasherize(key)] = format_keys(value, format_type)
                elif format_type == "camelize":
                    formatted[inflection.camelize(key, False)] = format_keys(value, format_type)
                elif format_type == "capitalize":
                    formatted[inflection.camelize(key)] = format_keys(value, format_type)
                elif format_type == "underscore":
                    formatted[inflection.underscore(key)] = format_keys(value, format_type)
            return formatted
        if isinstance(obj, list):
            return [format_keys(item, format_type) for item in obj]
        else:
            return obj
    else:
        return obj
示例#10
0
    def reserve(self,
                answers=None,
                fields=None,
                discount_amount=0,
                discount_percentage=0):
        body = {
            'discountAmount': discount_amount,
            'discountPercentage': discount_percentage,
        }

        self._merge_booking_fields(body, fields)

        if answers is not None:
            body.update({
                'answers': {
                    'answers': [{
                        'type': dasherize(key),
                        'answer': value,
                    } for key, value in answers.items()]
                }
            })

        res = self.api.post('/booking.json/guest/%s/reserve' % self.session_id,
                            body)

        self._reservation_state = res.json()

        return self
示例#11
0
 def base_name(self):  # pragma: no cover
     if not self.model:
         raise ValueError(f'expected {self} ".model" or ".base_name" to be defined')
     if self.router:
         return self.router.get_default_basename(self)
     default = inflection.dasherize(self.model._meta.verbose_name.lower().replace(' ', '_'))
     return default
    def _render_relationships(self, resource):
        """Render the resource's relationships."""
        relationships = {}
        related_models = resource.__mapper__.relationships.keys()
        primary_key_val = getattr(resource, self.primary_key)
        if self.dasherize:
            mapped_relationships = {
                x: dasherize(underscore(x)) for x in related_models}
        else:
            mapped_relationships = {x: x for x in related_models}

        for model in related_models:
            relationships[mapped_relationships[model]] = {
                'links': {
                    'self': '/{}/{}/relationships/{}'.format(
                        resource.__tablename__,
                        primary_key_val,
                        mapped_relationships[model]),
                    'related': '/{}/{}/{}'.format(
                        resource.__tablename__,
                        primary_key_val,
                        mapped_relationships[model])
                }
            }

        return relationships
示例#13
0
        def on_wrapper(cls):
            context = cls.get_class_context()
            base = inflection.pluralize(inflection.dasherize(inflection.underscore(cls.__name__)))
            if prefix is not None:
                base = f'{prefix.strip("/")}/{base}'
            if 'create' not in exclude:
                cls.add_endpoint(HttpEndpoint(
                    route=f'/{base}',
                    method='post',
                    message=f'{context}.Create{cls.__name__}',
                    gateway=gateway,
                    service=cls,
                    secured=config.get('create', {}).get('secured', True),
                    scopes=config.get('create', {}).get('scopes', [f'{context}.{cls.__name__}.write'])
                ))

            if 'update' not in exclude:
                cls.add_endpoint(HttpEndpoint(
                    route=f'/{base}/{{{cls.id_name()}}}',
                    method='put',
                    message=f'{context}.Update{cls.__name__}',
                    gateway=gateway,
                    service=cls,
                    secured=config.get('update', {}).get('secured', True),
                    scopes=config.get('update', {}).get('scopes', [f'{context}.{cls.__name__}.write'])
                ))

            if 'delete' not in exclude:
                cls.add_endpoint(HttpEndpoint(
                    route=f'/{base}/{{{cls.id_name()}}}',
                    method='delete',
                    message=f'{context}.Delete{cls.__name__}',
                    gateway=gateway,
                    service=cls,
                    secured=config.get('delete', {}).get('secured', True),
                    scopes=config.get('delete', {}).get('scopes', [f'{context}.{cls.__name__}.write'])
                ))

            if 'read' not in exclude:
                cls.add_endpoint(HttpEndpoint(
                    route=f'/{base}',
                    method='get',
                    message=f'{context}.{inflection.pluralize(cls.__name__)}',
                    gateway=gateway,
                    service=cls,
                    secured=config.get('read', {}).get('secured', True),
                    scopes=config.get('read', {}).get('scopes', [f'{context}.{cls.__name__}.read'])
                ))

                cls.add_endpoint(HttpEndpoint(
                    route=f'/{base}/{{{cls.id_name()}}}',
                    method='get',
                    message=f'{context}.{inflection.pluralize(cls.__name__)}',
                    gateway=gateway,
                    service=cls,
                    secured=config.get('read', {}).get('secured', True),
                    scopes=config.get('read', {}).get('scopes', [f'{context}.{cls.__name__}.read'])
                ))

            return cls
示例#14
0
def _perform_backup_restore(connection_info: ConnectionInfo,
                            process_args: List[str], options: Dict[str, Any],
                            task: Task):
    """Call out to pg_dump or pg_restore using the arguments given and additional arguments built from the given options dict"""
    for option, value in options.items():
        # Don't add the option to the arguments if it is not set
        if value is None or value is False:
            continue
        # Replace underscores with dashes in the option name
        key_name = inflection.dasherize(option)
        if value is True:
            # The option is a boolean flag, so just add the option
            process_args.insert(-1, f'--{key_name}')
        else:
            # The option has a value, so add the flag with its value
            process_args.insert(-1, f'--{key_name}={value}')
    with task.cancellation_lock:
        if task.canceled:
            return TaskResult(TaskStatus.CANCELED)
        try:
            os.putenv('PGPASSWORD',
                      connection_info.details.options.get('password') or '')
            dump_restore_process = subprocess.Popen(process_args,
                                                    stderr=subprocess.PIPE)
            task.on_cancel = dump_restore_process.terminate
            _, stderr = dump_restore_process.communicate()
        except subprocess.SubprocessError as err:
            dump_restore_process.kill()
            return TaskResult(TaskStatus.FAILED, str(err))
    if dump_restore_process.returncode != 0:
        return TaskResult(TaskStatus.FAILED, str(stderr, 'utf-8'))
    return TaskResult(TaskStatus.SUCCEEDED)
示例#15
0
    def __new__(
        cls,
        name: Union[str, Type["Workflow"]],
        bases: Tuple[Type["Workflow"], ...],
        props: Dict[str, Any],
        **kwargs,
    ):
        workflow_name = dasherize(underscore(name))

        props["kind"] = "Workflow"
        props["api_version"] = "argoproj.io/v1alpha1"

        metadata_dict = dict(name='', generate_name=f"{workflow_name}-")
        metadata_dict.update(props.get("__metadata__", {}))

        # Required fields
        props["metadata"]: V1ObjectMeta = V1ObjectMeta(**metadata_dict)
        props["spec"] = {
            k: props.pop(k)
            for k in V1alpha1WorkflowSpec.attribute_map if props.get(k)
        }
        props["status"] = {}

        bases = (*bases, cls.__model__)
        klass = super().__new__(cls, name, bases, props)

        if name == "Workflow":
            # No need to initialize any further
            return klass

        cls.__compile(klass, name, bases, props)

        return klass
示例#16
0
    def parse(self):
        """ Parse the configuration file

        Parsing is a three step process:

        1) Read the cloud specific configurations
        2) Read the topology specification
        3) Add the cloud specific configuration to the topology
        configuration.

        """
        self._tree = ET.parse(self._file)

        # 1) Read the cloud specific configurations
        self.read_cloud_configuration()

        for entity in Entity.entities():
            classify = inflection.camelize(inflection.singularize(entity))
            dasherize = inflection.dasherize(inflection.underscore(classify))
            pluralize = inflection.pluralize(dasherize)
            add_name = 'add_' + inflection.underscore(dasherize)
            ext_name = 'extend_' + inflection.underscore(dasherize)
            path = './' + pluralize + '/' + dasherize

            # 2) Parse topology specific configuration
            self._parse_group('./' + pluralize + '/' + dasherize,
                              getattr(self, add_name))

            # 3) Parse cloud specific configuration for topology
            self._parse_group(
                './' + self.cloud + '/' + pluralize + '/add-values',
                getattr(self, ext_name))
 def add_relationship(self, resource, rel_name, relationship):
     dash_name = dasherize(rel_name)
     if dash_name not in resource["relationships"]:
         resource["relationships"][dash_name] = OrderedDict({
             "data": None
         })
     if relationship.get("data"):
         try:
             rel_id = relationship.get("data").get("id")  # Serialized data
         except AttributeError:
             rel_id = relationship.get("data")  # Only ID
         resource["relationships"][dasherize(rel_name)]["data"] = \
             OrderedDict([
                 ("id", force_text(rel_id)),
                 ("type", relationship.get("type")),
             ])
    def __init__(self, field):
        self.field = field

        # Try to name this XSD type

        # Note: should we use field.context.key ?
        # The problem is that when dealing with List/Dict fields, the value_type
        # field also inherits the parent's context

        name = field.getName() or str(inflection.parameterize(field.title))
        if not name:
            try:
                name = field.getTaggedValue("name")
            except KeyError:
                pass

        assert name, "The field should be named"

        name = inflection.underscore(name)
        self.typename = inflection.camelize(name)
        self.name = inflection.dasherize(name)

        # Compute occurence indicators

        self.min_occurs = 1 if field.required else 0
        self.max_occurs = 1
示例#19
0
    def __init__(self, field):
        self.field = field

        # Try to name this XSD type

        # Note: should we use field.context.key ?
        # The problem is that when dealing with List/Dict fields, the value_type
        # field also inherits the parent's context

        name = field.getName() or str(inflection.parameterize(field.title))
        if not name:
            try:
                name = field.getTaggedValue('name')
            except KeyError:
                pass

        assert name, 'The field should be named'

        name = inflection.underscore(name)
        self.typename = inflection.camelize(name)
        self.name = inflection.dasherize(name)

        # Compute occurence indicators

        self.min_occurs = 1 if field.required else 0
        self.max_occurs = 1
    def _render_attributes(self, resource):
        """Render the resources's attributes."""
        attributes = {}
        attrs_to_ignore = set()

        for key, relationship in resource.__mapper__.relationships.items():
            attrs_to_ignore.update(set(
                [column.name for column in relationship.local_columns]).union(
                    {key}))

        if self.dasherize:
            mapped_fields = {x: dasherize(underscore(x)) for x in self.fields}
        else:
            mapped_fields = {x: x for x in self.fields}

        for attribute in self.fields:
            if attribute == self.primary_key:
                continue
            # Per json-api spec, we cannot render foreign keys
            # or relationsips in attributes.
            if attribute in attrs_to_ignore:
                raise AttributeError
            try:
                value = getattr(resource, attribute)
                if isinstance(value, datetime.datetime):
                    attributes[mapped_fields[attribute]] = value.isoformat()
                else:
                    attributes[mapped_fields[attribute]] = value
            except AttributeError:
                raise

        return attributes
示例#21
0
def convert_schema(value: str,
                   usage: str,
                   question_name: Optional[str] = None) -> str:
    """
    Converts the return values from question class into the appropriate type
    (as a link to the pybatfish datamodel or specifier description, if applicable)
    """
    allowed_usages = ["input", "output"]
    if usage not in allowed_usages:
        raise ValueError(
            f"Invalid conversion type: {usage}, expected one of: {allowed_usages}"
        )

    if value.startswith("Set<"):
        inner = value[4:-1]  # strip prefix and suffix ">"
        return "Set of {}".format(convert_schema(inner, usage, question_name))
    elif value.startswith("List<"):
        inner = value[5:-1]  # strip prefix and suffix ">"
        return "List of {}".format(convert_schema(inner, usage, question_name))
    elif value.lower() in _BASE_TYPES:
        return _BASE_TYPES[value.lower()]
    elif value.endswith("Spec"):
        # specifiers follow a nice pattern. Almost.
        # 1. Handle special cases
        # 2. convert camel case to a dashed version
        # 3. turn "spec" into "specifier"
        value = _SPECIFIER_CONVERSIONS.get(value.lower(), value)
        slug = re.sub(r"spec$", "specifier", dasherize(underscore(value)))
        text = value[:1].capitalize() + value[1:]
        return f"[{text}](../specifiers.md#{slug})"
    elif usage == "input":
        slug = _INPUT_TYPES[value.lower()]
        if slug.startswith("pybatfish.datamodel"):
            text = slug.split(".")[-1]  # The class name
            return f"[{text}](../datamodel.rst#{slug})"
        else:
            return slug
    elif usage == "output":
        slug = _OUTPUT_TYPES[value.lower()]
        if slug.startswith("pybatfish.datamodel"):
            text = slug.split(".")[-1]  # The class name
            return f"[{text}](../datamodel.rst#{slug})"
        elif slug.lower() == "selfdescribing":
            if question_name is None:
                raise ValueError(
                    "Converting selfdescribing schema requires a question name"
                )
            try:
                return _SELF_DESCRIBING_CONVERSIONS[question_name]
            except KeyError:
                raise KeyError(
                    f"Error: unknown selfdescribing schema usage in question {question_name}"
                )
        else:
            return slug
    else:
        raise ValueError(
            f"Error: Unable to convert based on parameters - value: {value}, type {usage}, question {question_name}"
        )
示例#22
0
        def get(this):
            name = inflection.dasherize(inflection.underscore(this.__class__.__name__))
            if _key not in this.config:
                return {}

            return [val for key, val in
                    this.env.config.get(_get_config_method).iteritems() if
                    val.name in this.config[_key].split(",")]
    def __init__(self, obj):
        self.obj = obj

        # Try to name this XSD type

        name = str(inflection.underscore(type(obj).__name__))
        self.typename = inflection.camelize(name)
        self.name = name = inflection.dasherize(name)
示例#24
0
 def add_parameters(self, ps):
     L = logging.getLogger()
     self._argNames = set()
     for pName, pDescr in ps.items():
         if '@' != pName[0]:
             shortcut, name = _argparse_par(
                 pName)  #list(filter(lambda x: x, _argparse_par(pName)))
             self._argNames.add(name if name else shortcut)
             assert (name or shortcut)
             if shortcut: shortcut = '-' + inflection.dasherize(shortcut)
             if name: name = '--' + inflection.dasherize(name)
             nms = list(filter(lambda x: x, [shortcut, name]))
             self.argParser.add_argument(*nms, **pDescr)
             L.debug('"%s" named command-line arg added' % (', '.join(nms)))
         else:
             self.argParser.add_argument(pName[1:], **pDescr)
             L.debug('Added a positional argument.')
示例#25
0
文件: view.py 项目: virtusize/nap
 def __init__(self):
     cls = self.__class__
     self.controller = ensure_instance(cls.controller)
     self.serializer = ensure_instance(cls.serializer)
     self.filter_chain = [ensure_instance(f) for f in cls.filter_chain]
     self.endpoint_prefix = cls.endpoint_prefix if hasattr(cls, 'endpoint_prefix') else underscore(pluralize(self.controller.model.__name__))
     self.dashed_endpoint = dasherize(self.endpoint_prefix)
     self.query_filter = UnderscoreFilter()
示例#26
0
        def get(this):
            name = inflection.dasherize(inflection.underscore(this.__class__.__name__))
            if _key not in this.config:
                return None

            return next(val for key, val in
                    this.env.config.get(_get_config_method).iteritems() if
                    (val.name == this.config[_key]))
示例#27
0
    def _tableize(cls):
        """Tableize the model name.

        :return:
        """
        name = inflection.tableize(cls.__name__)
        name = inflection.dasherize(name)
        return name
示例#28
0
    def __init__(self, obj):
        self.obj = obj

        # Try to name this XSD type

        name = str(inflection.underscore(type(obj).__name__))
        self.typename = inflection.camelize(name)
        self.name = name = inflection.dasherize(name)
示例#29
0
    def get_headers(self, key_format=None):
        '''Gets all the headers, optionally formatted.'''
        if not key_format:
            return self._headers

        if key_format.lower() == 'html':
            dasherize_header = lambda x: dasherize('x_' + str(x).lower()) if x not in self._http_headers else dasherize(str(x).lower())
            return {dasherize_header(key): value for key, value in self._headers.items()}
示例#30
0
    def handle(self, handler_input):
        # type: (HandlerInput) -> Response
        country_value = ask_utils.get_slot_value(handler_input=handler_input,
                                                 slot_name="country")
        case_value = ask_utils.get_slot_value(handler_input=handler_input,
                                              slot_name="cases")
        speak_output = "Sorry, I had trouble doing what you asked. Please try again."

        if (country_value and case_value):
            country_value = {
                'usa': 'us',
                'united states': 'us',
                'united states of america': 'us',
                'united kingdom': 'uk',
                'uae': 'united-arab-emirates',
                'ivory coast': 'cote-d-ivoire',
                'palestine': 'state-of-palestine',
                'congo': 'democratic-republic-of-the-congo',
                'vietnam': 'viet-nam',
            }.get(country_value.lower(), country_value)

            if case_value in [
                    'deaths', 'death', 'recovered', 'recovering', 'recover',
                    'confirmed', 'confirming', 'confirm'
            ]:
                try:
                    if country_value == 'worldwide':
                        response = requests.get(
                            "https://www.worldometers.info/coronavirus")
                    else:
                        response = requests.get(
                            "https://www.worldometers.info/coronavirus/country/{}"
                            .format(inflection.dasherize(
                                country_value.lower())))

                    soup = BeautifulSoup(response.text, 'html.parser')
                    figures = soup.find_all('div', id='maincounter-wrap')

                    if case_value in ['confirmed', 'confirming', 'confirm']:
                        confirmed = figures[0].find('span').text
                        speak_output = "{} confirmed cases".format(confirmed)

                    if case_value in ['deaths', 'death']:
                        deaths = figures[1].find('span').text
                        speak_output = "{} deaths".format(deaths)

                    if case_value in ['recovered', 'recovering', 'recover']:
                        recovered = figures[2].find('span').text
                        speak_output = "{} recovered cases".format(recovered)

                except:
                    speak_output = "Sorry, I had trouble doing what you asked. Please try again."

        return (
            handler_input.response_builder.speak(speak_output).ask(
                speak_output)
            # .ask("add a reprompt if you want to keep the session open for the user to respond")
            .response)
示例#31
0
    def __new__(cls, f: Callable[..., T]):
        """Workflow spec for V1alpha1Template."""
        self = super().__new__(cls, f)
        self.name = dasherize(f.__code__.co_name)

        self.template: str = None
        self.template_ref: V1alpha1TemplateRef = None

        return self
示例#32
0
def to_dasherized_plural(name):
    """
    Pluralizes a name, as well as transforming camelCase
    name into dasherized formatting
    """
    plural = inflection.pluralize(name)
    underscore = inflection.underscore(plural)
    dasherized = inflection.dasherize(underscore)
    return dasherized
示例#33
0
    def model_directories(cls):
      """
      Return the directories in which to look for models.

      Defaults to pluralized module name in the modules directory.
      """
      plural = inflection.pluralize(
        inflection.dasherize(inflection.underscore(cls.__name__))).lower()
      return [plural]
    def get_attributes_data(self, obj, serializer):
        model = serializer.Meta.model
        resource_type = get_resource_type(model)
        attributes = OrderedDict([("id", None), ("type", resource_type)])

        for field_name, field in six.iteritems(serializer.get_fields()):
            if isinstance(field, (RelatedField, ManyRelatedField)):
                continue
            attributes[dasherize(field_name)] = obj.get(field_name)
        return attributes
示例#35
0
        def model_directories(cls):
            """
      Return the directories in which to look for models.

      Defaults to pluralized module name in the modules directory.
      """
            plural = inflection.pluralize(
                inflection.dasherize(inflection.underscore(
                    cls.__name__))).lower()
            return [plural]
示例#36
0
 def to_representation(self, value):
     return [
         {
             'title': _('This field is required'),
             'code': 'required',
             'source': {
                 'pointer': '/data/attributes/{}'.format(inflection.dasherize(field).replace('.', '/'))
             }
         } for field in value
     ]
示例#37
0
def component(name, path, classname, selector):
    """To Create Component."""
    click.echo("Creating component %s ..." % name)
    comp_create(inflection.underscore(name), path,
                (lambda c: c if c else inflection.camelize(name))(classname),
                (lambda s: s
                 if s else inflection.dasherize(inflection.underscore(name))
                 )(selector))

    click.echo("Component %s Created" % name)
示例#38
0
 def to_representation(self, value):
     return [
         {
             'title': str(error),
             'code': error.code,
             'source': {
                 'pointer': '/data/attributes/{}'.format(inflection.dasherize(error.field).replace('.', '/'))
             }
         } for error in value
     ]
 def check_assoc(self, assoc):
     include_opt = self.renderer.request.query_params.get("include")
     if not include_opt:
         return False
     include_opt = include_opt.split(",")
     for opt in include_opt:
         if re.match(r"^{}".format(
                 dasherize(assoc.replace(".", "\."))), opt):
             return True
     return False
示例#40
0
 def process(s: str, name: str) -> None:
   if assets: 
     print(registry.assets)
     return 
   if language == 'html':
     print(BeautifulSoup(codegen.tohtml(registry, s), 'html.parser').prettify())
   else:
     print(codegen.__getattribute__(f'render_{language}_component')(
       inflection.dasherize(inflection.underscore(name)) if language == 'imba' or language == "mithril" else name,
       codegen.__getattribute__(f'to{language}')(registry, s)))
示例#41
0
        def get(this):
            name  = inflection.dasherize(inflection.underscore(this.__class__.__name__))
            names = inflection.pluralize(name)
            entities = this.env.config.get(_get_method).iteritems()

            for key, entity in entities:
                if (name in entity.config) and (entity.config[name] == this.name):
                    return entity
                elif (names in entity.config) and (entity.config[names] == this.name):
                    return entity
            return None
示例#42
0
文件: base.py 项目: Code4SA/pmg-cms-2
    def register(cls, target):
        name = target.__name__

        if not hasattr(target, 'resource_content_type'):
            # The underscored content_type for this resource. Used primarily by ElasticSearch.
            target.resource_content_type = underscore(name)

        if not hasattr(target, 'slug_prefix'):
            # The prefix for url slugs
            target.slug_prefix = dasherize(target.resource_content_type)

        resource_slugs[target.slug_prefix] = target
示例#43
0
文件: utils.py 项目: eReuse/DeviceHub
 def resource(string: str):
     """
         :param string: String can be type, resource or python case
     """
     try:
         prefix, resulting_type = Naming.pop_prefix(string)
         prefix += Naming.RESOURCE_PREFIX
     except IndexError:
         prefix = ""
         resulting_type = string
     resulting_type = inflection.dasherize(inflection.underscore(resulting_type))
     return prefix + (inflection.pluralize(resulting_type) if Naming._pluralize(resulting_type) else resulting_type)
def exception_handler(exc, context):
    response = drf_exception_handler(exc, context)
    if getattr(context.get('view'), 'bypass_jsonapi_exception_handler', False):
        context['view'].is_errored = True
        return response
    if response is None:
        # Unhandled exceptions will raise a 500 error.
        return

    errors = []
    # Handle views errors
    if isinstance(response.data, list):
        for message in response.data:
            errors.append({
                "detail": message,
                "source": {
                    "pointer": "/data",
                },
                "status": six.text_type(response.status_code),
            })
    # Handle all other errors
    elif isinstance(response.data, dict):
        for field, error in response.data.items():
            # Field errors
            if isinstance(error, list):
                field = dasherize(field)
                pointer = "/data/attributes/{}".format(field)
                for message in error:
                    errors.append({
                        "detail": message,
                        "source": {
                            "pointer": pointer,
                        },
                        "status": six.text_type(response.status_code),
                    })
            # Other errors (e.g. Http404, PermissionDenied, throttling errors)
            elif field == "detail" and isinstance(error, six.string_types):
                errors.append({
                    "detail": error,
                    "source": {
                        "pointer": "/data",
                    },
                    "status": six.text_type(response.status_code),
                })

    response.data = OrderedDict([
        ("jsonapi", OrderedDict([("version", "1.0")])),
        ("errors", errors)
    ])
    context['view'].is_errored = True
    return response
def format_keys(obj, format_type=None):
    """
    .. warning::

        `format_keys` function and `JSON_API_FORMAT_KEYS` setting are deprecated and will be
        removed in the future.
        Use `format_field_names` and `JSON_API_FORMAT_FIELD_NAMES` instead. Be aware that
        `format_field_names` only formats keys and preserves value.

    Takes either a dict or list and returns it with camelized keys only if
    JSON_API_FORMAT_KEYS is set.

    :format_type: Either 'dasherize', 'camelize', 'capitalize' or 'underscore'
    """
    warnings.warn(
        "`format_keys` function and `JSON_API_FORMAT_KEYS` setting are deprecated and will be "
        "removed in the future. "
        "Use `format_field_names` and `JSON_API_FORMAT_FIELD_NAMES` instead. Be aware that "
        "`format_field_names` only formats keys and preserves value.",
        DeprecationWarning
    )

    if format_type is None:
        format_type = json_api_settings.FORMAT_KEYS

    if format_type in ('dasherize', 'camelize', 'underscore', 'capitalize'):

        if isinstance(obj, dict):
            formatted = OrderedDict()
            for key, value in obj.items():
                if format_type == 'dasherize':
                    # inflection can't dasherize camelCase
                    key = inflection.underscore(key)
                    formatted[inflection.dasherize(key)] \
                        = format_keys(value, format_type)
                elif format_type == 'camelize':
                    formatted[inflection.camelize(key, False)] \
                        = format_keys(value, format_type)
                elif format_type == 'capitalize':
                    formatted[inflection.camelize(key)] \
                        = format_keys(value, format_type)
                elif format_type == 'underscore':
                    formatted[inflection.underscore(key)] \
                        = format_keys(value, format_type)
            return formatted
        if isinstance(obj, list):
            return [format_keys(item, format_type) for item in obj]
        else:
            return obj
    else:
        return obj
def format_value(value, format_type=None):
    if format_type is None:
        format_type = json_api_settings.format_type
    if format_type == 'dasherize':
        # inflection can't dasherize camelCase
        value = inflection.underscore(value)
        value = inflection.dasherize(value)
    elif format_type == 'camelize':
        value = inflection.camelize(value, False)
    elif format_type == 'capitalize':
        value = inflection.camelize(value)
    elif format_type == 'underscore':
        value = inflection.underscore(value)
    return value
示例#47
0
def format_value(value, format_type=None):
    if format_type is None:
        format_type = getattr(settings, 'JSON_API_FORMAT_KEYS', False)
    if format_type == 'dasherize':
        # inflection can't dasherize camelCase
        value = inflection.underscore(value)
        value = inflection.dasherize(value)
    elif format_type == 'camelize':
        value = inflection.camelize(value, False)
    elif format_type == 'capitalize':
        value = inflection.camelize(value)
    elif format_type == 'underscore':
        value = inflection.underscore(value)
    return value
def prepare_json_requests(topic_names, identifiers, datasets_folder):
    res = []
    
    output_dir = os.path.join(OUTPUT_BASE_DIR, 'json', 'dataset-topics')
    try:
       os.makedirs(output_dir)
    except OSError as ex:
        if not ex.errno == os.errno.EEXIST:
            raise ex
    
    logging.info('Using output directory at %s' %(output_dir))
    
    munge = lambda name: dasherize(underscore(name))
    
    for identifier in identifiers:
        md_file = os.path.join(datasets_folder, '%s.xml' % (identifier))
        md = etree.parse(md_file)
        q_identifier = md.xpath(
            '/gmd:MD_Metadata' +
                '/gmd:identificationInfo/gmd:MD_DataIdentification' + 
                    '/gmd:citation/gmd:CI_Citation' +
                        '/gmd:identifier/gmd:RS_Identifier/gmd:code/gco:CharacterString',
            namespaces=namespaces)
        assert len(q_identifier) == 1
        assert q_identifier[0].text == str(identifier), 'Found an unexpected identifier!' 
        q_topic = md.xpath(
            '/gmd:MD_Metadata' + 
                '/gmd:identificationInfo/gmd:MD_DataIdentification' + 
                    '/gmd:topicCategory/gmd:MD_TopicCategoryCode',
            namespaces=namespaces)
        for i, r in enumerate(q_topic):
            code = r.text
            name = munge(code)
            if not name in topic_names:
                logging.warn('Cannot map topic code %r to a topic group' % (code))
                continue
            # Prepare a member_create api request
            req = {
                'id': name, # group's name-or-id
                'object': str(identifier),
                'object_type': 'package',
                'capacity': 'public',
            }
            outfile = os.path.join(output_dir, '%s-topic-%d.json' %(identifier, i))
            with open(outfile, 'w') as ofp:
                ofp.write(json.dumps(req))
            res.append(dict(path=os.path.realpath(outfile)))
            
    print json.dumps(res)
def get_resource_type(model):
    RESOURCE_TYPE_EXTRACTOR = getattr(
        settings, "REST_FRAMEWORK", None).get("RESOURCE_TYPE_EXTRACTOR", None)
    if RESOURCE_TYPE_EXTRACTOR:
        try:
            parts = RESOURCE_TYPE_EXTRACTOR.split(".")
            module_path, class_name = ".".join(parts[:-1]), parts[-1]
            module = importlib.import_module(module_path)
            return getattr(module, class_name)(model)
        except (ImportError, AttributeError) as e:
            msg = ("Could not import '{}' for API setting "
                   "'RESOURCE_TYPE_EXTRACTOR'. {}: {}.".format(
                       RESOURCE_TYPE_EXTRACTOR, e.__class__.__name__, e))
            raise ImportError(msg)
    return force_text(dasherize(underscore(model._meta.object_name)).strip())
示例#50
0
def build_style_string(style, separator=' '):
    """
        Creates key value pairs as a string for the given Style with the given separator
        between the paris
    :param style:
    :param separator: Default ' '
    :return:
    """

    style_string = separator.join(
        map_dict(
            lambda key, value: '{key}: {value};'.format(key=dasherize(key), value=map_value(value)),
            compact_dict(style)))

    return style_string
示例#51
0
    def _translate_name(name):
        """Translate the class name to the API endpoint.

        For example, Car would become cars, FastCar would become fast-cars.

        Args:
            name (string): Camel case name (singular)

        Returns:
            string: A pluraised, dasherized string.
        """
        underscored = inflection.underscore(name)
        dasherized = inflection.dasherize(underscored)
        words = dasherized.split('-')
        last_word = words.pop()
        words.append(inflection.pluralize(last_word))
        return '-'.join(words)
 def add_relationships(self, resource, rel_name, relationship):
     dash_name = dasherize(rel_name)
     if dash_name not in resource["relationships"]:
         resource["relationships"][dash_name] = OrderedDict({
             "data": []
         })
     if relationship.get("data"):
         for data in relationship.get("data"):
             try:
                 rel_id = data.get("id")  # Serialized data
             except AttributeError:
                 rel_id = data  # Only IDs
             resource["relationships"][dash_name]["data"].append(
                 OrderedDict([
                     ("id", force_text(rel_id)),
                     ("type", relationship.get("type")),
                 ])
             )
示例#53
0
def format_value(value, format_type='dasherize'):
    """
    Alter the format of a string value.  This is useful in
    ensuring that the keys representing serialized model field names
    are in the proper format.
    """
    if format_type == 'dasherize':
        # 2 steps because inflection library does not
        # directly dasherize camelCase
        value = inflection.underscore(value)
        value = inflection.dasherize(value)
    elif format_type == 'camelize':
        value = inflection.camelize(value, False)
    elif format_type == 'capitalize':
        value = inflection.camelize(value)
    elif format_type == 'underscore':
        value = inflection.underscore(value)
    return value
示例#54
0
    def is_included(self, resource):
        """
        Checks if a resource is already in the helper's ``included``.

        Arguments:
            resource (dict): Typically, a dict representation of a persisted Django model.

        Returns:
            True if the primary key for the resource ``type`` is already a part
            of the helper's included resources. False otherwise.
        """
        resource_type = resource['type']
        resource_type = inflection.dasherize(resource_type)
        if resource_type in self._key_inventory:
            current_primary_keys = self._key_inventory[resource_type]
            return True if resource['id'] in current_primary_keys else False
        else:
            return False
示例#55
0
def format_keys(obj, format_type='dasherize'):
    """
    Passed dict/list gets its keys transformed. The default target format is
    dasherize, as this matches the Ember framework's JSON API conventions.
    """
    if isinstance(obj, dict):
        formatted = OrderedDict()
        for key, value in obj.items():
            if format_type == 'dasherize':
                formatted[inflection.dasherize(key)] = value
            elif format_type == 'camelize':
                formatted[inflection.camelize(key, False)] = value
            elif format_type == 'underscore':
                formatted[inflection.underscore(key)] = value
        return formatted
    if isinstance(obj, list):
        return [format_keys(item, format_type) for item in obj]
    else:
        return obj
示例#56
0
    def add_resource(self, resource):
        """
        Adds a resource not previously added into the helper.

        Arguments:
            resource (dict): Typically, a dict representation of a persisted Django model.

        Returns:
            True if the resource was appended to the helper's included resources.
            False otherwise.
        """
        if not self.is_included(resource):
            self._resources.append(resource)
            resource_type = resource['type']
            resource_type = inflection.dasherize(resource_type)
            if resource_type in self._key_inventory:
                self._key_inventory[resource_type].append(resource['id'])
            else:
                self._key_inventory[resource_type] = [resource['id']]
            return True
        else:
            # Not added because the 'type' and 'id' combo already exists
            return False
示例#57
0
def test_dasherize(input, expected):
    assert inflection.dasherize(input) == expected