def add_entry(self, pack: BasePack, entry: Entry):
        """
        The component can manually call this function to add the entry into
        the data pack immediately. Otherwise, the system will add the entries
        automatically when this component finishes.

        Args:
            pack (BasePack): The pack to add the entry into.
            entry (Entry):  The entry to be added.

        Returns:

        """
        pack.add_entry(entry, self.name)
Exemple #2
0
def write_pack(
    input_pack: BasePack,
    output_dir: str,
    sub_path: str,
    indent: Optional[int] = None,
    zip_pack: bool = False,
    overwrite: bool = False,
    drop_record: bool = False,
    serialize_method: str = "jsonpickle",
) -> str:
    """
    Write a pack to a path.

    Args:
        input_pack: A Pack to be written.
        output_dir: The output directory.
        sub_path: The file name for this pack.
        indent: Whether to format JSON with an indent.
        zip_pack: Whether to zip the output JSON.
        overwrite: Whether to overwrite the file if already exists.
        drop_record: Whether to drop the creation records in the serialization.
        serialize_method: The method used to serialize the data. Current
          available options are "jsonpickle" and "pickle".
          Default is "jsonpickle".

    Returns:
        If successfully written, will return the path of the output file.
        otherwise, will return None.

    """
    output_path = os.path.join(output_dir, sub_path)

    if overwrite or not os.path.exists(output_path):
        ensure_dir(output_path)
        input_pack.serialize(
            output_path,
            zip_pack=zip_pack,
            drop_record=drop_record,
            serialize_method=serialize_method,
            indent=indent,
        )
    else:
        logging.info("Will not overwrite existing path %s", output_path)

    logging.info("Writing a pack to %s", output_path)
    return output_path
Exemple #3
0
def write_pack(input_pack: BasePack,
               output_dir: str,
               sub_path: str,
               indent: Optional[int] = None,
               zip_pack: bool = False,
               overwrite: bool = False,
               drop_record: bool = False) -> str:
    """
    Write a pack to a path.

    Args:
        input_pack: A Pack to be written.
        output_dir: The output directory.
        sub_path: The file name for this pack.
        indent: Whether to format JSON with an indent.
        zip_pack: Whether to zip the output JSON.
        overwrite: Whether to overwrite the file if already exists.
        drop_record: Whether to drop the creation records in the serialization.

    Returns:
        If successfully written, will return the path of the output file.
        otherwise, will return None.

    """
    output_path = os.path.join(output_dir, sub_path) + '.json'
    if overwrite or not os.path.exists(output_path):
        if zip_pack:
            output_path = output_path + '.gz'

        ensure_dir(output_path)

        out_str: str = input_pack.serialize(drop_record)

        if indent:
            out_str = json.dumps(json.loads(out_str), indent=indent)

        if zip_pack:
            with gzip.open(output_path, 'wt') as out:
                out.write(out_str)
        else:
            with open(output_path, 'w') as out:
                out.write(out_str)
    else:
        logging.info("Will not overwrite existing path %s", output_path)

    logging.info("Writing a pack to %s", output_path)
    return output_path
Exemple #4
0
 def _parse_multi_pack(self, multi_pack_source: str) -> MultiPack:
     return BasePack.from_string(multi_pack_source)  # type: ignore