src.fairreckitlib.core.io.io_create

This module contains IO functions that create something on the disk and dispatch an IO event.

Functions:

create_dir: create a directory on the disk with IO event dispatching.
create_json: create a json file on the disk with IO event dispatching.
create_yml: create a yml file on the disk with event dispatching.

This program has been developed by students from the bachelor Computer Science at Utrecht University within the Software Project course. © Copyright Utrecht University (Department of Information and Computing Sciences)

 1"""This module contains IO functions that create something on the disk and dispatch an IO event.
 2
 3Functions:
 4
 5    create_dir: create a directory on the disk with IO event dispatching.
 6    create_json: create a json file on the disk with IO event dispatching.
 7    create_yml: create a yml file on the disk with event dispatching.
 8
 9This program has been developed by students from the bachelor Computer Science at
10Utrecht University within the Software Project course.
11© Copyright Utrecht University (Department of Information and Computing Sciences)
12"""
13
14import os
15from typing import Any, Dict, List, Union
16
17from ..events.event_dispatcher import EventDispatcher
18from .event_io import ON_CREATE_FILE, ON_MAKE_DIR, DirEventArgs, FileEventArgs
19from .io_utility import save_json, save_yml
20
21
22def create_dir(directory: str, event_dispatcher: EventDispatcher) -> str:
23    """Create the specified directory.
24
25    This functions checks whether the directory exists and the
26    event is only dispatched when the directory did not exist yet.
27
28    Args:
29        directory: the directory to create on the disk.
30        event_dispatcher: used to dispatch the IO event.
31
32    Returns:
33        the directory path.
34    """
35    if not os.path.isdir(directory):
36        os.mkdir(directory)
37        event_dispatcher.dispatch(DirEventArgs(ON_MAKE_DIR, directory))
38
39    return directory
40
41
42def create_json(
43        file_path: str,
44        data: Union[Dict[str, Any], List],
45        event_dispatcher: EventDispatcher,
46        *,
47        encoding: str='utf-8',
48        indent=1) -> None:
49    """Create a JSON file with the specified data.
50
51    The IO event is dispatched after the file is created.
52
53    Args:
54        file_path: path to where the json file will be stored.
55        data: the source dictionary to save in the file.
56        event_dispatcher: used to dispatch the IO event.
57        encoding: the encoding to use for writing the file contents.
58        indent: the indent level for pretty printing JSON array elements and object members.
59    """
60    save_json(file_path, data, encoding=encoding, indent=indent)
61    event_dispatcher.dispatch(FileEventArgs(ON_CREATE_FILE, file_path))
62
63
64def create_yml(
65        file_path: str,
66        data: Union[Dict[str, Any], List],
67        event_dispatcher: EventDispatcher,
68        *,
69        encoding: str='utf-8') -> None:
70    """Create a YML file with the specified data.
71
72    The IO event is dispatched after the file is created.
73
74    Args:
75        file_path: path to where the json file will be stored.
76        data: the source dictionary to save in the file.
77        event_dispatcher: used to dispatch the IO event.
78        encoding: the encoding to use for writing the file contents.
79    """
80    save_yml(file_path, data, encoding=encoding)
81    event_dispatcher.dispatch(FileEventArgs(ON_CREATE_FILE, file_path))
def create_dir( directory: str, event_dispatcher: src.fairreckitlib.core.events.event_dispatcher.EventDispatcher) -> str:
23def create_dir(directory: str, event_dispatcher: EventDispatcher) -> str:
24    """Create the specified directory.
25
26    This functions checks whether the directory exists and the
27    event is only dispatched when the directory did not exist yet.
28
29    Args:
30        directory: the directory to create on the disk.
31        event_dispatcher: used to dispatch the IO event.
32
33    Returns:
34        the directory path.
35    """
36    if not os.path.isdir(directory):
37        os.mkdir(directory)
38        event_dispatcher.dispatch(DirEventArgs(ON_MAKE_DIR, directory))
39
40    return directory

Create the specified directory.

This functions checks whether the directory exists and the event is only dispatched when the directory did not exist yet.

Args: directory: the directory to create on the disk. event_dispatcher: used to dispatch the IO event.

Returns: the directory path.

def create_json( file_path: str, data: Union[Dict[str, Any], List], event_dispatcher: src.fairreckitlib.core.events.event_dispatcher.EventDispatcher, *, encoding: str = 'utf-8', indent=1) -> None:
43def create_json(
44        file_path: str,
45        data: Union[Dict[str, Any], List],
46        event_dispatcher: EventDispatcher,
47        *,
48        encoding: str='utf-8',
49        indent=1) -> None:
50    """Create a JSON file with the specified data.
51
52    The IO event is dispatched after the file is created.
53
54    Args:
55        file_path: path to where the json file will be stored.
56        data: the source dictionary to save in the file.
57        event_dispatcher: used to dispatch the IO event.
58        encoding: the encoding to use for writing the file contents.
59        indent: the indent level for pretty printing JSON array elements and object members.
60    """
61    save_json(file_path, data, encoding=encoding, indent=indent)
62    event_dispatcher.dispatch(FileEventArgs(ON_CREATE_FILE, file_path))

Create a JSON file with the specified data.

The IO event is dispatched after the file is created.

Args: file_path: path to where the json file will be stored. data: the source dictionary to save in the file. event_dispatcher: used to dispatch the IO event. encoding: the encoding to use for writing the file contents. indent: the indent level for pretty printing JSON array elements and object members.

def create_yml( file_path: str, data: Union[Dict[str, Any], List], event_dispatcher: src.fairreckitlib.core.events.event_dispatcher.EventDispatcher, *, encoding: str = 'utf-8') -> None:
65def create_yml(
66        file_path: str,
67        data: Union[Dict[str, Any], List],
68        event_dispatcher: EventDispatcher,
69        *,
70        encoding: str='utf-8') -> None:
71    """Create a YML file with the specified data.
72
73    The IO event is dispatched after the file is created.
74
75    Args:
76        file_path: path to where the json file will be stored.
77        data: the source dictionary to save in the file.
78        event_dispatcher: used to dispatch the IO event.
79        encoding: the encoding to use for writing the file contents.
80    """
81    save_yml(file_path, data, encoding=encoding)
82    event_dispatcher.dispatch(FileEventArgs(ON_CREATE_FILE, file_path))

Create a YML file with the specified data.

The IO event is dispatched after the file is created.

Args: file_path: path to where the json file will be stored. data: the source dictionary to save in the file. event_dispatcher: used to dispatch the IO event. encoding: the encoding to use for writing the file contents.