当前位置: 首页>>代码示例>>Python>>正文


Python reflection.AttributeInfo类代码示例

本文整理汇总了Python中ggrc.models.reflection.AttributeInfo的典型用法代码示例。如果您正苦于以下问题:Python AttributeInfo类的具体用法?Python AttributeInfo怎么用?Python AttributeInfo使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了AttributeInfo类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: do_update_attr

  def do_update_attr(cls, obj, json_obj, attr):
    """Perform the update to ``obj`` required to make the attribute attr
    equivalent in ``obj`` and ``json_obj``.
    """
    class_attr = getattr(obj.__class__, attr)
    attr_reflection = AttributeInfo.get_attr(obj.__class__, "_api_attrs", attr)
    update_raw = attr in AttributeInfo.gather_update_raw(obj.__class__)
    if update_raw:
      # The attribute has a special setter that can handle raw json fields
      # properly. This is used for special mappings such as custom attribute
      # values
      attr_name = attr
      value = json_obj.get(attr_name)
    elif isinstance(attr_reflection, SerializableAttribute):
      attr_name = attr
      value = json_obj.get(attr)

      if value:
        value = attr_reflection.deserialize(value)
    elif hasattr(attr, '__call__'):
      # The attribute has been decorated with a callable, grab the name and
      # invoke the callable to get the value
      attr_name = attr.attr_name
      value = attr(cls, obj, json_obj)
    elif not hasattr(cls, class_attr.__class__.__name__):
      # The attribute is a function on the obj like custom_attributes in
      # CustomAttributable mixin
      attr_name = attr
      value = class_attr(obj, json_obj)
    else:
      # Lookup the method to use to perform the update. Use reflection to
      # key off of the type of the attribute and invoke the method of the
      # same name.
      attr_name = attr
      method = getattr(cls, class_attr.__class__.__name__)
      value = method(obj, json_obj, attr_name, class_attr)
    if (isinstance(value, (set, list)) and
        not update_raw and (
        not hasattr(class_attr, 'property') or not
        hasattr(class_attr.property, 'columns') or not isinstance(
            class_attr.property.columns[0].type,
            JsonType)
    )):
      cls._do_update_collection(obj, value, attr_name)
    else:
      try:
        setattr(obj, attr_name, value)
      except AttributeError as error:
        logger.error('Unable to set attribute %s: %s', attr_name, error)
        raise
开发者ID:google,项目名称:ggrc-core,代码行数:50,代码来源:json.py

示例2: log_json

  def log_json(self):
    """Get a dict with attributes and related objects of self.

    This method converts additionally person-mapping attributes and owners
    to person stubs.
    """
    from ggrc import models

    res = self.log_json_base()

    for attr in self._people_log_mappings:
      if hasattr(self, attr):
        value = getattr(self, attr)
        # hardcoded [:-3] is used to strip "_id" suffix
        res[attr[:-3]] = self._person_stub(value) if value else None

    for attr_name in AttributeInfo.gather_publish_attrs(self.__class__):
      if is_attr_of_type(self, attr_name, models.Option):
        attr = getattr(self, attr_name)
        if attr:
          stub = create_stub(attr)
          stub["title"] = attr.title
        else:
          stub = None
        res[attr_name] = stub
    return res
开发者ID:Smotko,项目名称:ggrc-core,代码行数:26,代码来源:__init__.py

示例3: _get_model_properties

def _get_model_properties():
  """Get indexable properties for all snapshottable objects

  Args:
    None
  Returns:
    tuple(class_properties dict, custom_attribute_definitions dict) - Tuple of
        dictionaries, first one representing a list of searchable attributes
        for every model and second one representing dictionary of custom
        attribute definition attributes.
  """
  # pylint: disable=protected-access
  from ggrc.models import all_models
  class_properties = dict()
  klass_names = Types.all

  cadef_klass_names = {
      getattr(all_models, klass)._inflector.table_singular
      for klass in klass_names
  }

  cad_query = db.session.query(
      models.CustomAttributeDefinition.id,
      models.CustomAttributeDefinition.title,
  ).filter(
      models.CustomAttributeDefinition.definition_type.in_(cadef_klass_names)
  )

  for klass_name in klass_names:
    model_attributes = AttributeInfo.gather_attrs(
        getattr(all_models, klass_name), '_fulltext_attrs')
    class_properties[klass_name] = model_attributes

  return class_properties, cad_query.all()
开发者ID:VinnieJohns,项目名称:ggrc-core,代码行数:34,代码来源:indexer.py

示例4: _set_attr_name_map

  def _set_attr_name_map(self):
    """ build a map for attributes names and display names

    Dict containing all display_name to attr_name mappings
    for all objects used in the current query
    Example:
        { Program: {"Program URL": "url", "Code": "slug", ...} ...}
    """
    self.attr_name_map = {}
    for object_query in self.query:
      object_name = object_query["object_name"]
      object_class = self.object_map[object_name]
      tgt_class = object_class
      if object_name == "Snapshot":
        child_type = self._get_snapshot_child_type(object_query)
        tgt_class = getattr(models.all_models, child_type, object_class)
      aliases = AttributeInfo.gather_aliases(tgt_class)
      self.attr_name_map[tgt_class] = {}
      for key, value in aliases.items():
        filter_by = None
        if isinstance(value, dict):
          filter_name = value.get("filter_by", None)
          if filter_name is not None:
            filter_by = getattr(tgt_class, filter_name, None)
          name = value["display_name"]
        else:
          name = value
        if name:
          self.attr_name_map[tgt_class][name.lower()] = (key.lower(),
                                                         filter_by)
开发者ID:VinnieJohns,项目名称:ggrc-core,代码行数:30,代码来源:query_helper.py

示例5: _get_reserved_names

  def _get_reserved_names(cls, definition_type):
    """Get a list of all attribute names in all objects.

    On first call this function computes all possible names that can be used by
    any model and stores them in a static frozen set. All later calls just get
    this set.

    Returns:
      frozen set containing all reserved attribute names for the current
      object.
    """
    # pylint: disable=protected-access
    # The _inflector is a false positive in our app.
    with benchmark("Generate a list of all reserved attribute names"):
      if not cls._reserved_names.get(definition_type):
        definition_map = {model._inflector.table_singular: model
                          for model in ggrc.models.all_models.all_models}
        definition_map.update({model._inflector.model_singular: model
                              for model in ggrc.models.all_models.all_models})

        definition_model = definition_map.get(definition_type)
        if not definition_model:
          raise ValueError("Invalid definition type")

        aliases = AttributeInfo.gather_aliases(definition_model)
        cls._reserved_names[definition_type] = frozenset(
            (value["display_name"] if isinstance(
                value, dict) else value).lower()
            for value in aliases.values() if value
        )
      return cls._reserved_names[definition_type]
开发者ID:Smotko,项目名称:ggrc-core,代码行数:31,代码来源:attributevalidator.py

示例6: _get_properties

  def _get_properties(self, obj):
    """Get indexable properties and values.

    Properties should be returned in the following format:
    {
      property1: {
        subproperty1: value1,
        subproperty2: value2,
        ...
      },
      ...
    }
    If there is no subproperty - empty string is used as a key
    """
    if obj.type == "Snapshot":
      # Snapshots do not have any indexable content. The object content for
      # snapshots is stored in the revision. Snapshots can also be made for
      # different models so we have to get fulltext attrs for the actual child
      # that was snapshotted and get data for those from the revision content.
      tgt_class = getattr(ggrc.models.all_models, obj.child_type, None)
      if not tgt_class:
        return {}
      attrs = AttributeInfo.gather_attrs(tgt_class, '_fulltext_attrs')
      return {attr: {"": obj.revision.content.get(attr)} for attr in attrs}

    return {attr: {"": getattr(obj, attr)} for attr in self._fulltext_attrs}
开发者ID:VinnieJohns,项目名称:ggrc-core,代码行数:26,代码来源:recordbuilder.py

示例7: _attribute_name_map

 def _attribute_name_map(self):
   """Get property to name mapping for object attributes."""
   model = getattr(models.all_models, self.child_type, None)
   if not model:
     logger.warning("Exporting invalid snapshot model: %s", self.child_type)
     return {}
   aliases = AttributeInfo.gather_visible_aliases(model)
   aliases.update(AttributeInfo.get_acl_definitions(model))
   aliases.update(self.CUSTOM_SNAPSHOT_ALIASES)
   if self.MAPPINGS_KEY in self.fields:
     aliases.update(self.SNAPSHOT_MAPPING_ALIASES)
   name_map = {
       key: value["display_name"] if isinstance(value, dict) else value
       for key, value in aliases.iteritems()
   }
   orderd_keys = AttributeInfo.get_column_order(name_map.keys())
   return OrderedDict((key, name_map[key]) for key in orderd_keys)
开发者ID:google,项目名称:ggrc-core,代码行数:17,代码来源:snapshot_block.py

示例8: get_fulltext_attrs

  def get_fulltext_attrs(cls):
    # type: () -> Dict[unicode, fulltext.attributes.FullTextAttr]
    """Get all fulltext attributes represented as FullTextAttribute objects """

    raw_attrs = AttributeInfo.gather_attrs(cls, '_fulltext_attrs')
    # Convert attrs represented as string into FullTextAttr objects
    attrs = [attr if isinstance(attr, fulltext.attributes.FullTextAttr) else
             fulltext.attributes.FullTextAttr(attr, attr)
             for attr in raw_attrs]

    return attrs
开发者ID:google,项目名称:ggrc-core,代码行数:11,代码来源:mixin.py

示例9: test_gather_aliases

  def test_gather_aliases(self):
    """Test gather all aliases."""
    class Child(object):
      # pylint: disable=too-few-public-methods
      _aliases = {
          "child_normal": "normal",
          "child_extended": {
              "display_name": "Extended",
          },
          "child_filter_only": {
              "display_name": "Extended",
              "filter_only": True,
          },
      }

    class Parent(Child):
      # pylint: disable=too-few-public-methods
      _aliases = {
          "parent_normal": "normal",
          "parent_extended": {
              "display_name": "Extended",
          },
          "parent_filter_only": {
              "display_name": "Extended",
              "filter_only": True,
          },
      }

    self.assertEqual(
        AttributeInfo.gather_aliases(Parent),
        {
            "parent_normal": "normal",
            "parent_extended": {
                "display_name": "Extended",
            },
            "parent_filter_only": {
                "display_name": "Extended",
                "filter_only": True,
            },
            "child_normal": "normal",
            "child_extended": {
                "display_name": "Extended",
            },
            "child_filter_only": {
                "display_name": "Extended",
                "filter_only": True,
            },
        }
    )
开发者ID:egorhm,项目名称:ggrc-core,代码行数:49,代码来源:test_reflection.py

示例10: get_fulltext_parsed_value

def get_fulltext_parsed_value(klass, key):
  """Get fulltext parser if it's exists """
  attrs = AttributeInfo.gather_attrs(klass, '_fulltext_attrs')
  if not issubclass(klass, Indexed):
    return
  for attr in attrs:
    if isinstance(attr, FullTextAttr) and attr.with_template:
      attr_key = klass.PROPERTY_TEMPLATE.format(attr.alias)
    elif isinstance(attr, FullTextAttr):
      attr_key = attr.alias
    else:
      attr_key = klass.PROPERTY_TEMPLATE.format(attr)
      attr = FullTextAttr(key, key)
    if attr_key == key:
      return attr
开发者ID:zidarsk8,项目名称:ggrc-core,代码行数:15,代码来源:autocast.py

示例11: insert_definition

  def insert_definition(self, definition):
    """Insert a new custom attribute definition into database

    Args:
      definition: dictionary with field_name: value
    """
    from ggrc.models.custom_attribute_definition \
        import CustomAttributeDefinition
    field_names = AttributeInfo.gather_create_attrs(
        CustomAttributeDefinition)

    data = {fname: definition.get(fname) for fname in field_names}
    data["definition_type"] = self._inflector.table_singular
    cad = CustomAttributeDefinition(**data)
    db.session.add(cad)
开发者ID:VinnieJohns,项目名称:ggrc-core,代码行数:15,代码来源:customattributable.py

示例12: _get_model_names

  def _get_model_names(cls, model):
    """Get tuple of all attribute names for model.

    Args:
        model: Model class.

    Returns:
        Tuple of all attributes for provided model.
    """
    if not model:
      raise ValueError("Invalid definition type")
    aliases = AttributeInfo.gather_aliases(model)
    return (
        (value["display_name"] if isinstance(value, dict) else value).lower()
        for value in aliases.values() if value
    )
开发者ID:egorhm,项目名称:ggrc-core,代码行数:16,代码来源:attributevalidator.py

示例13: get_object_column_definitions

def get_object_column_definitions(object_class, fields=None,
                                  include_hidden=False):
  """Attach additional info to attribute definitions.

  Fetches the attribute info (_aliases) for the given object class and adds
  additional data (handler class, validator function, default value) )needed
  for imports.

  Args:
    object_class (db.Model): Model for which we want to get column definitions
      for imports.
    include_hidden (bool): Flag which specifies if we should include column
      handlers for hidden attributes (they marked as 'hidden'
      in _aliases dict).

  Returns:
    dict: Updated attribute definitions dict with additional data.
  """
  attributes = AttributeInfo.get_object_attr_definitions(
      object_class,
      fields=fields,
      include_hidden=include_hidden
  )
  column_handlers = model_column_handlers(object_class)
  for key, attr in attributes.iteritems():
    handler_key = attr.get("handler_key", key)
    # check full handler keys
    handler = column_handlers.get(handler_key)
    if not handler:
      # check handler key prefixes
      handler = column_handlers.get(handler_key.split(":")[0])
    if not handler:
      # use default handler
      handler = handlers.ColumnHandler
    validator = None
    default = None
    if attr["type"] == AttributeInfo.Type.PROPERTY:
      validator = getattr(object_class, "validate_{}".format(key), None)
      default = getattr(object_class, "default_{}".format(key), None)

    attr["handler"] = attr.get("handler", handler)
    attr["validator"] = attr.get("validator", validator)
    attr["default"] = attr.get("default", default)
  return attributes
开发者ID:google,项目名称:ggrc-core,代码行数:44,代码来源:import_helper.py

示例14: get_all_attributes_json

def get_all_attributes_json(load_custom_attributes=False):
  """Get a list of all attribute definitions

  This exports all attributes related to a given model, including custom
  attributes and mapping attributes, that are used in csv import and export.
  """
  with benchmark('Loading all attributes JSON'):
    published = {}
    ca_cache = collections.defaultdict(list)
    if load_custom_attributes:
      definitions = models.CustomAttributeDefinition.eager_query().group_by(
          models.CustomAttributeDefinition.title,
          models.CustomAttributeDefinition.definition_type)
      for attr in definitions:
        ca_cache[attr.definition_type].append(attr)
    for model in all_models.all_models:
      published[model.__name__] = \
          AttributeInfo.get_attr_definitions_array(model, ca_cache=ca_cache)
    return as_json(published)
开发者ID:zidarsk8,项目名称:ggrc-core,代码行数:19,代码来源:__init__.py

示例15: _get_class_properties

def _get_class_properties():
  """Get indexable properties for all models

  Args:
    None
  Returns:
    class_properties dict - representing a list of searchable attributes
                            for every model
  """
  class_properties = defaultdict(list)
  for klass_name in Types.all:
    full_text_attrs = AttributeInfo.gather_attrs(
        getattr(all_models, klass_name), '_fulltext_attrs'
    )
    for attr in full_text_attrs:
      if not isinstance(attr, FullTextAttr):
        attr = FullTextAttr(attr, attr)
      class_properties[klass_name].append(attr)
  return class_properties
开发者ID:google,项目名称:ggrc-core,代码行数:19,代码来源:indexer.py


注:本文中的ggrc.models.reflection.AttributeInfo类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。