def SetUp(self):
     symbols = {'len': len, 'test_transform': lambda x: 'test'}
     aliases = {'y': resource_lex.Lexer('a.b.c').KeyWithAttribute()}
     self.defaults = resource_projection_parser.Parse(
         '(compound.string:alias=s, floating:alias=z)',
         aliases=aliases,
         symbols=symbols)
Exemple #2
0
    def __init__(self, command, args, resources):
        """Constructor.

    Args:
      command: The Command object.
      args: The argparse.Namespace given to the command.Run().
      resources: The resources to display, returned by command.Run().
    """
        self._args = args
        self._command = command
        self._default_format_used = False
        self._defaults = None
        self._format = None
        self._info = command.ResourceInfo(args)
        self._printer = None
        self._printer_is_initialized = False
        self._resources = resources
        symbols = {}
        if self._info:
            symbols['collection'] = (
                lambda r, undefined='': self._info.collection or undefined)
        geturi = command.GetUriFunc()
        if geturi:
            self._transform_uri = lambda r, undefined='': geturi(r
                                                                 ) or undefined
            symbols['uri'] = self._transform_uri
        else:
            self._transform_uri = resource_transform.TransformUri
        self._defaults = resource_projection_parser.Parse(None,
                                                          symbols=symbols)
    def __init__(self, command, args, resources=None):
        """Constructor.

    Args:
      command: The Command object.
      args: The argparse.Namespace given to the command.Run().
      resources: The resources to display, returned by command.Run(). May be
        omitted if only GetFormat() will be called.
    """
        self._args = args
        self._command = command
        self._default_format_used = False
        self._defaults = None
        self._format = None
        self._info = command.ResourceInfo(args)
        self._printer = None
        self._printer_is_initialized = False
        self._resources = resources
        self._defaults = resource_projection_parser.Parse(
            None, defaults=command.Defaults())
        self._defaults.symbols[resource_transform.GetTypeDataName(
            'conditionals')] = args
        if self._info:
            self._defaults.symbols['collection'] = (
                lambda r, undefined='': self._info.collection or undefined)
        geturi = command.GetUriFunc()
        if geturi:
            self._transform_uri = lambda r, undefined='': geturi(r
                                                                 ) or undefined
            self._defaults.symbols['uri'] = self._transform_uri
        else:
            self._transform_uri = resource_transform.TransformUri
Exemple #4
0
    def _GetResourceInfoFormat(self):
        """Determines the format from the resource registry if any.

    Returns:
      format: The format string, None if there is no resource registry info
          for the command.
    """
        info = self._command.ResourceInfo(self._args)
        if not info:
            return None
        styles = ['list']
        if _GetFlag(self._args, 'simple_list'):
            styles.insert(0, 'simple')
        for style in styles:
            attr = '{0}_format'.format(style)
            fmt = getattr(info, attr, None)
            if fmt:
                break
        else:
            return None
        symbols = info.GetTransforms()
        if symbols or info.defaults:
            self._defaults = resource_projection_parser.Parse(
                info.defaults, defaults=self._defaults, symbols=symbols)
        return fmt
Exemple #5
0
 def SetUp(self):
     aliases = {
         'i': resource_lex.Lexer('integer').Key(),
         'v': resource_lex.Lexer('compound.string.value').Key(),
     }
     self.defaults = resource_projection_parser.Parse(
         '(compound.string:alias=s, floating:alias=f)', aliases=aliases)
     self.rewrite = resource_filter_scrub.Backend().Rewrite
Exemple #6
0
 def _GetResourceInfoDefaults(self):
     """Returns the default symbols for --filter and --format."""
     if not self._info:
         return None
     symbols = self._info.GetTransforms()
     if not symbols and not self._info.defaults:
         return None
     return resource_projection_parser.Parse(self._info.defaults,
                                             symbols=symbols)
def Compile(expression='', defaults=None, symbols=None, by_columns=False):
  """Compiles a resource projection expression.

  Args:
    expression: The resource projection string.
    defaults: resource_projection_spec.ProjectionSpec defaults.
    symbols: Transform function symbol table dict indexed by function name.
    by_columns: Project to a list of columns if True.

  Returns:
    A Projector containing the compiled expression ready for Evaluate().
  """
  projection = resource_projection_parser.Parse(
      expression, defaults=defaults, symbols=symbols, compiler=Compile)
  return Projector(projection, by_columns=by_columns)
  def RunSubTest(self, expression, deprecated=False):

    def _Error(resource=None):
      """Always raises ValueError for testing.

      Args:
        resource: The resource object.

      Raises:
        ValueError: Always for testing.
      """
      _ = resource
      raise ValueError('Transform function value error.')

    default_symbols = {
        'date': resource_transform.TransformDate,
        'len': lambda r, x=None: resource_transform.TransformLen(x or r),
    }
    defaults = resource_projection_parser.Parse(
        '(compound.string:alias=s, floating:alias=f)',
        symbols=default_symbols,
        aliases=self._ALIASES)
    symbols = {
        'error': _Error,  # 'error' not a magic name.
    }
    defaults = resource_projection_spec.ProjectionSpec(
        defaults=defaults, symbols=symbols)
    evaluate = resource_filter.Compile(expression, defaults=defaults).Evaluate
    if isinstance(self.resource, list):
      results = []
      for r in self.resource:
        results.append(evaluate(r))
      return results
    actual = evaluate(self.resource)
    err = self.GetErr()
    self.ClearErr()
    warning = ('WARNING: --filter : operator evaluation is changing for '
               'consistency across Google APIs.')
    if err and not deprecated:
      self.fail('Error [%s] not expected.' % err)
    elif not err and deprecated:
      self.fail('Warning [%s] expected.' % warning)
    elif err and deprecated and warning not in err:
      self.fail('Warning [%s] expected but got [%s].' % (warning, err))
    return actual
Exemple #9
0
    def __init__(self, command, args, resources):
        """Constructor.

    Args:
      command: The Command object.
      args: The argparse.Namespace given to the command.Run().
      resources: The resources to display, returned by command.Run().
    """
        self._args = args
        self._command = command
        self._default_format_used = False
        self._defaults = None
        self._info = command.ResourceInfo(args)
        self._resources = resources
        symbols = {
            'collection': lambda x: self._info.collection
            if self._info else None
        }
        self._defaults = resource_projection_parser.Parse(None,
                                                          symbols=symbols)
Exemple #10
0
 def __init__(self):
   super(IdentityProjector, self).__init__(resource_projection_parser.Parse())