Exemple #1
0
def _generate_request_timestamp():
    """
    Generate a timestamp for the request. This will be embedded in the security
    context of the request to protect it against replay attacks

    :rtype: :class:`dict`
    :return: Timestamp block that can be inserted in security context
    """
    created_dt = datetime.datetime.utcnow()
    offset = datetime.timedelta(minutes=REQUEST_VALIDITY)
    created = DateTimeConverter.convert_from_datetime(created_dt)
    expires = DateTimeConverter.convert_from_datetime(created_dt + offset)
    return {EXPIRES: expires, CREATED: created}
Exemple #2
0
 def test_reorder_args_2(self):
     datetime_msg = '1978-03-04T05:06:07.100Z'
     dt = DateTimeConverter.convert_to_datetime(datetime_msg)
     actual_result = StringFormatter.format_msg(
         'long %3$d, float %2$.3f, date %1$tc', [dt, 123456.789, 123456])
     self.assertEqual(
         'long 123456, float 123456.789, date Sat 04 Mar 1978 05:06:07 AM',
         actual_result)
Exemple #3
0
 def test_multiple_args_with_grouping(self):
     datetime_msg = '1978-03-04T05:06:07.100Z'
     dt = DateTimeConverter.convert_to_datetime(datetime_msg)
     actual_result = StringFormatter.format_msg(
         'long %,d, float %,.3f, date %tc', [123456, 123456.789, dt])
     self.assertEqual(
         'long 123,456, float 123,456.789, date Sat 04 Mar 1978 05:06:07 AM',
         actual_result)
Exemple #4
0
    def visit_date_time(self, typ):
        """
        Visit a datetime value

        :type  typ: :class:`vmware.vapi.bindings.type.DateTimeType`
        :param typ: Binding type of the value
        """
        self._out_value = DateTimeConverter.convert_to_datetime(
            self._in_value.value)
Exemple #5
0
    def visit_date_time(self, typ):
        """
        Visit a datetime value

        :type  typ: :class:`vmware.vapi.bindings.type.DateTimeType`
        :param typ: Binding type of the value
        """
        dt_str = DateTimeConverter.convert_from_datetime(self._in_value)
        self._out_value = typ.definition.new_value(dt_str)
 def test_complex(self):
     datetime_msg = '1978-03-04T05:06:07.100Z'
     dt = DateTimeConverter.convert_to_datetime(datetime_msg)
     messages = {'foo.str.complex': 'string %s, long %d, float %f, time %tc'}
     rb = DictionaryResourceBundle(messages)
     lf = LocalizableMessageFactory(rb, StringFormatter)
     actual_result = lf.get_message('foo.str.complex',
                                    'hello', 123, decimal.Decimal('123.456'), dt)
     expected_result = LocalizableMessage(
         default_message='string hello, long 123, float 123.456000, time Sat 04 Mar 1978 05:06:07 AM',
         args=['hello', '123', '123.456', '1978-03-04T05:06:07.100Z'],
         id='foo.str.complex')
     self.assertEqual(expected_result, actual_result)
Exemple #7
0
    def _localize_datetime(cls, datetime_arg):
        """
        Localize a datetime object

        :type  datetime_arg: :class:`datetime.datetime`
        :param datetime_arg: Datetime object
        :rtype: :class:`str`
        :return: Localized datetime string
        """
        # Importing DateTimeConverter here to avoid circular dependency
        from vmware.vapi.bindings.datetime_helper import DateTimeConverter
        if isinstance(datetime_arg, six.string_types):
            datetime_arg = DateTimeConverter.convert_to_datetime(datetime_arg)
        if not isinstance(datetime_arg, datetime):
            raise TypeError('Argument is not a datetime')
        # XXX: strftime does not handle dates before 1900
        return datetime_arg.strftime(locale.nl_langinfo(
            locale.D_T_FMT)).strip()
Exemple #8
0
    def to_string(args):
        """
        Convert argument list to string argument list

        :type  args: :class:`list` of :class:`object`
        :param args: List of arguments
        :rtype: :class:`list` of :class:`str`
        :return: List of string arguments
        """
        result = []
        for arg in args:
            if isinstance(arg, datetime):
                result.append(
                    DateTimeConverter.convert_from_datetime(arg))
            elif (isinstance(arg, six.string_types) or
                    isinstance(arg, six.integer_types) or
                    isinstance(arg, decimal.Decimal)):
                result.append(str(arg))
            else:
                raise TypeError('Unsupported type for message argument: %s',
                                type(arg).__name__)
        return result
Exemple #9
0
 def test_datetime(self):
     datetime_msg = '1978-03-04T05:06:07.100Z'
     dt = DateTimeConverter.convert_to_datetime(datetime_msg)
     actual_result = StringFormatter.format_msg('some date %tc', [dt])
     self.assertEqual('some date Sat 04 Mar 1978 05:06:07 AM',
                      actual_result)