コード例 #1
0
    def test_flatmap(self):
        good: Optional[int] = 3
        bad: Optional[int] = None

        self.assertEqual(5,
                         get_optional(flatmap_optional(good, lambda x: x + 2)))
        self.assertIsNone(flatmap_optional(bad, lambda x: x + 2))
コード例 #2
0
def load_json_helper(
    root_dir: str,
    file_name: Union[str, PurePath],
    class_handle: Type[Serializable[T]],
    subdirectories: List[str] = None,
) -> Optional[T]:
    """
    Wrapper around JSON deserialization that, given a directory name and file name (including
    the ".json" suffix) as well as an optional handle to the class type, will load the contents
    of the file and return an instance of the desired class, if it fits. If anything fails, the
    result should be `None`. No exceptions will be raised outside of this method, but all such
    errors will be logged as part of the ElectionGuard log.

    Note: if the file_name is actually a path-like object, the root_dir and subdirectories are ignored,
    and the path is directly loaded.

    :param root_dir: top-level directory where we'll be reading files
    :param file_name: name of the file, including the suffix, excluding any directories leading up to the file
    :param class_handle: the class, itself, that we're trying to deserialize to
    :param subdirectories: path elements to be introduced between `root_dir` and the file; empty-list means no subdirectory
    :returns: the contents of the file, or `None` if there was an error
    """
    file_contents = load_file_helper(root_dir, file_name, subdirectories)
    return flatmap_optional(
        file_contents, lambda f: decode_json_file_contents(f, class_handle))
コード例 #3
0
    def read_json_file(
        self,
        file_name: Union[PurePath, str],
        class_handle: Type[Serializable[T]],
        subdirectories: List[str] = None,
    ) -> Optional[T]:
        """
        Reads the requested file, by name, returning its contents as a Python object for the given class handle.
        If no hash for the file is present, if the file doesn't match its known hash, or if the JSON deserialization
        process fails, then `None` will be returned and an error will be logged. If the `file_name`
        is actually a path-like object, the subdirectories are ignored.

        :param subdirectories: Path elements to be introduced between `root_dir` and the file; empty-list means
          no subdirectory. Ignored if the file_name is a path-like object.
        :param file_name: Name of the file, including any suffix, or a path-like object.
        :param class_handle: The class, itself, that we're trying to deserialize to (if None, then you get back
          whatever the JSON becomes, e.g., a dict).
        :returns: The contents of the file, or `None` if there was an error.
        """

        # this loads the file and verifies the hashes
        file_contents = self.read_file(file_name, subdirectories)
        return flatmap_optional(
            file_contents,
            lambda f: decode_json_file_contents(f, class_handle))
コード例 #4
0
def elgamal_reencrypt(
    public_key: ElementModP, nonce: ElementModQ, ciphertext: ElGamalCiphertext
) -> Optional[ElGamalCiphertext]:
    return flatmap_optional(
        elgamal_encrypt(0, nonce, public_key),
        lambda zero: elgamal_add(zero, ciphertext),
    )
コード例 #5
0
def make_existing_manifest(root_dir: str) -> Optional[Manifest]:
    """
    Constructs a `Manifest` instance from a directory that contains a `MANIFEST.json` file.
    If the file is missing or something else goes wrong, you could get `None` as a result.
    :param root_dir: a name for the directory containing `MANIFEST.json` and other files.
    """
    manifest_ex: Optional[ManifestExternal] = load_json_helper(
        root_dir=root_dir,
        file_name="MANIFEST.json",
        class_handle=ManifestExternal)
    return flatmap_optional(manifest_ex, lambda m: m.to_manifest(root_dir))