def validate(self, metadata_specs):
     '''
     Check that this metadata has the correct metadata keys and that it has
     metadata values of the correct types.
     '''
     expected_keys = set(spec.key for spec in metadata_specs)
     for key in self._metadata_keys:
         if key not in expected_keys:
             raise UsageError('Unexpected metadata key: %s' % (key, ))
     for spec in metadata_specs:
         if spec.key in self._metadata_keys:
             value = getattr(self, spec.key)
             if spec.type is float and isinstance(value, int):
                 # cast int to float
                 value = float(value)
             # Validate formatted string fields
             if issubclass(spec.type,
                           str) and spec.formatting is not None and value:
                 try:
                     if spec.formatting == 'duration':
                         formatting.parse_duration(value)
                     elif spec.formatting == 'size':
                         formatting.parse_size(value)
                     elif spec.formatting == 'date':
                         formatting.parse_datetime(value)
                 except ValueError as e:
                     raise UsageError(str(e))
             if value is not None and not isinstance(value, spec.type):
                 raise UsageError(
                     'Metadata value for %s should be of type %s, was %s (type %s)'
                     % (spec.key, spec.type.__name__, value,
                        type(value).__name__))
         elif not spec.generated and not spec.optional:
             raise UsageError('Missing metadata key: %s' % (spec.key, ))
Esempio n. 2
0
 def validate(self, metadata_specs):
     '''
     Check that this metadata has the correct metadata keys and that it has
     metadata values of the correct types.
     '''
     expected_keys = set(spec.key for spec in metadata_specs)
     for key in self._metadata_keys:
         if key not in expected_keys:
             raise UsageError('Unexpected metadata key: %s' % (key,))
     for spec in metadata_specs:
         if spec.key in self._metadata_keys:
             value = getattr(self, spec.key)
             if spec.type is float and isinstance(value, int):
                 # cast int to float
                 value = float(value)
             # Validate formatted string fields
             if issubclass(spec.type, basestring) and spec.formatting is not None and value:
                 try:
                     if spec.formatting == 'duration':
                         formatting.parse_duration(value)
                     elif spec.formatting == 'size':
                         formatting.parse_size(value)
                     elif spec.formatting == 'date':
                         formatting.parse_datetime(value)
                 except ValueError as e:
                     raise UsageError(e.message)
             if value is not None and not isinstance(value, spec.type):
                 raise UsageError(
                     'Metadata value for %s should be of type %s, was %s (type %s)'
                     % (spec.key, spec.type.__name__, value, type(value).__name__)
                 )
         elif not spec.generated:
             raise UsageError('Missing metadata key: %s' % (spec.key,))
Esempio n. 3
0
    def check_version(self, server_version):
        # Enforce checking version at most once every 24 hours
        epoch_str = formatting.datetime_str(
            datetime.datetime.utcfromtimestamp(0))
        last_check_str = self.state.get('last_check_version_datetime',
                                        epoch_str)
        last_check_dt = formatting.parse_datetime(last_check_str)
        now = datetime.datetime.utcnow()
        if (now - last_check_dt) < datetime.timedelta(days=1):
            return
        self.state['last_check_version_datetime'] = formatting.datetime_str(
            now)
        self.save_state()

        # Print notice if server version is newer
        if list(map(int, server_version.split('.'))) > list(
                map(int, CODALAB_VERSION.split('.'))):
            message = (
                "NOTICE: "
                "The instance you are connected to is running CodaLab v{}. "
                "You are currently using an older v{} of the CLI. "
                "Please update codalab using\n"
                "   pip install -U codalab\n").format(server_version,
                                                      CODALAB_VERSION)
            sys.stderr.write(message)
Esempio n. 4
0
    def check_version(self, server_version):
        # Enforce checking version at most once every 24 hours
        epoch_str = formatting.datetime_str(datetime.datetime.utcfromtimestamp(0))
        last_check_str = self.state.get('last_check_version_datetime', epoch_str)
        last_check_dt = formatting.parse_datetime(last_check_str)
        now = datetime.datetime.now()
        if (now - last_check_dt) < datetime.timedelta(days=1):
            return
        self.state['last_check_version_datetime'] = formatting.datetime_str(now)
        self.save_state()

        # Print notice if server version is newer
        if map(int, server_version.split('.')) > map(int, CODALAB_VERSION.split('.')):
            message = (
                "NOTICE: "
                "The instance you are connected to is running CodaLab v{}. "
                "You are currently using an older v{} of the CLI. "
                "Please update codalab using\n"
                "   pip install -U codalab\n"
            ).format(server_version, CODALAB_VERSION)
            sys.stderr.write(message)