"""Various utilities and language enhancements."""


from functools import partial
from pathlib import Path

import requests
import yaml
import xdg.BaseDirectory

try:
    from yaml import CLoader as YAMLLoader, CDumper as YAMLDumper
except ImportError:
    from yaml import Loader as YAMLLoader, Dumper as YAMLDumper


# Consistent names definitions
RESOURCE_NAME = __package__


# Filesystem standard directories
def default_cache_dir(directory: Path = None) -> Path:
    """Get default cache dir if directory is None,
    else return directory as it is.
    """

    if directory is None:
        return Path(xdg.BaseDirectory.save_cache_path(RESOURCE_NAME))
    else:
        return directory


def default_data_dir(directory: Path = None) -> Path:
    """Get default data dir if directory is None,
    else return directory as it is.
    """

    if directory is None:
        return Path(xdg.BaseDirectory.save_data_path(RESOURCE_NAME))
    else:
        return directory


def default_new_session(session: requests.Session = None) -> requests.Session:
    """Create new Requests' Session, if none is provided.
    Otherwise, return session as it is.
    """

    if session is None:
        return requests.Session()
    else:
        return session


yamlload = partial(yaml.load, Loader=YAMLLoader)
yamlload.__doc__ = """Load and parse an YAML document."""


yamldump = partial(yaml.dump, Dumper=YAMLDumper, default_flow_style=False)
yamldump.__doc__ = """Dump and store an YAML document."""

@@ -46,16 +46,46 @@
 s.%22%22%22%0A%0A%0A
+from datetime import datetime%0A
 from fun
@@ -184,16 +184,47 @@
 irectory
+%0Afrom iso8601 import parse_date
 %0A%0Atry:%0A 
@@ -1313,16 +1313,574 @@
 ssion%0A%0A%0A
+# YAML tweaks%0A%0A# Force YAML to use ISO-8601 standard for timestamps%0Adef yaml_timestamp_representer(dumper, data):%0A    %22%22%22Custom representer for datetime objects in YAML.%22%22%22%0A    return dumper.represent_scalar('!!timestamp', data.isoformat())%0AYAMLDumper.add_representer(datetime, yaml_timestamp_representer)%0A%0A%0Adef yaml_timestamp_constructor(loader, node):%0A    %22%22%22Custom constructor for datetime objects from YAML.%22%22%22%0A    value = loader.construct_scalar(node)%0A    return parse_date(value)%0AYAMLLoader.add_constructor('!!timestamp', yaml_timestamp_constructor)%0A%0A%0A
 yamlload

Make PyYAML use ISO-8601 format for timestamps