コード例 #1
0
 def fromString(self, content):
     """FromString digests the string input and returns a Digest.
     TODO not sure what this is intended for.
     """
     if not isinstance(content, str):
         bot.exit("input must be string")
     content = bytes(content, "utf-8")
     return self.fromBytes(content)
コード例 #2
0
 def fromReader(self, ioReader):
     """FromReader returns the digest of the reader using the algorithm.
     the input must be type bytes, usually from io.BytesIO.read().
     This function likely isn't needed, but is provided to mirror
     the GoLang implementation.
     """
     if not isinstance(ioReader, io.BytesIO):
         bot.exit("input must be io.BytesIO")
     return self.fromBytes(ioReader.read())
コード例 #3
0
ファイル: digest.py プロジェクト: pombredanne/oci-python
    def verifier(self):
        """Verifier returns a writer object that can be used to verify a stream of
           content against the digest. If the digest is invalid, the method will panic.
        """
        from .verifiers import hashVerifier

        hashObj = self.algorithm.hash()
        if not hashObj:
            bot.exit("Algorithm is not available")
        return hashVerifier(hashObj, digest=self)
コード例 #4
0
    def add(self, name, value):
        """add a value to an existing attribute, normally when used by a client"""
        if name not in self.attrs:
            bot.exit("%s is not a valid attribute." % name)

        attr = self.attrs[name]

        # Don't validate the type if provided is empty
        if value:
            if not attr.set(value):
                bot.exit("%s must be type %s." % (name, attr.attType))
コード例 #5
0
    def load(self, content, validate=True):
        """given a dictionary load into its respective object
        if validate is True, we require it to be completely valid.
        """
        # import code
        # code.interact(local=locals())

        if not isinstance(content, dict):
            bot.exit("Please provide a dictionary or list to load.")

        # Look up attributes based on jsonKey
        lookup = self.generate_json_lookup()

        for key, value in content.items():
            att = lookup.get(key)
            if not att:
                bot.exit("%s is not a valid json attribute." % key)

        # If we get here, all parameters are valid, replace
        self._clear_values()

        for key, value in content.items():
            att = lookup.get(key)
            valid = att.set(value)
            if not valid and validate:
                bot.exit("%s (%s) is not valid." % (att.name, att.jsonName))

        # Validate the entire structure
        if validate:
            if not self.validate():
                bot.exit("%s is invalid" % self)
        return self
コード例 #6
0
ファイル: digest.py プロジェクト: pombredanne/oci-python
    def sepIndex(self):
        """return the index of the : separator or the index
           that separtes the extra content provided in the algorithm name.
        """
        try:
            algorithm, encoded = (self).split(":")
        except:
            bot.exit("empty digest or algorithm")

        # Empty algorithm or encoded portion
        if not algorithm or not encoded:
            bot.exit("empty digest or algorithm")

        match = re.search("[+._-]", algorithm)
        if match:
            return match.start()
        return self.index(":", 1)
コード例 #7
0
    def validate(self):
        """Validate checks that the contents of self (the digest) is valid"""
        if not self:
            bot.exit("Empty digest")

        regexp = "^[a-z0-9]+(?:[+._-][a-z0-9]+)*:[a-zA-Z0-9=_-]+$"

        # Must match for a digest
        if not re.search(regexp, self):
            raise ErrDigestInvalidFormat()

        algorithm, encoded = (self).split(":")

        # Remove the extra component, if there
        match = re.search("[+._-]", algorithm)
        if match:
            algorithm = algorithm[:match.start()]
        algorithm = Algorithm(algorithm)

        # Also checks if algorithm.available()
        return algorithm.validate(encoded)
コード例 #8
0
ファイル: struct.py プロジェクト: semoac/oci-python
    def load(self, content, validate=True):
        '''given a dictionary load into its respective object
           if validate is True, we require it to be completely valid.
        '''
        if not isinstance(content, dict):
            bot.exit("Please provide a dictionary to load.")

        # Look up attributes based on jsonKey
        lookup = self.generate_json_lookup()

        # Go through each value for the content, validate
        for key, value in content.items():
            att = lookup.get(key)
            if not att:
                bot.exit("%s is not a valid json attribute." % key)

        # If we get here, all parameters are valid, replace
        self._clear_values()
        for key, value in content.items():
            att = lookup.get(key)
            valid = att.set(value)
            if not valid and validate:
                bot.exit("%s (%s) is not valid." % (att.name, att.jsonName))
            self.attrs[att.name] = att

        # Return the updated instance
        return self