# Path: picatrix/lib/framework.py
# class MagicProtocol(Protocol):
# class MagicArgument:
# class MagicArgumentParser(argparse.ArgumentParser):
# class _Magic:
#   def __call__(self, *args: Any) -> Any:
#   def __init__(self, *argc, **kwargs):
#   def __init__(self, *args, **kwargs):
#   def exit(self, status: Optional[int] = 0, message: Optional[Text] = ''):
#   def __init__(
#       self,
#       fn: MagicProtocol,
#       arguments: Optional[List[MagicArgument]] = None,
#       name_func: Optional[Callable[[Text], Text]] = None):
#   def __call__(self, line: Text, cell: Optional[Text] = None) -> Optional[Any]:
#   def __dir__(self):
#   def __getattribute__(self, name: Text):
# def _get_arguments_from_arg_lines(
#     arg_lines: List[Text]) -> List[Dict[Text, Text]]:
# def _get_argument_lines_from_docstring(lines: List[Text]) -> List[Text]:
# def _add_function_arguments_to_parser(
#     fn: MagicProtocol, parser: MagicArgumentParser):
# def _parse_docstring(function: MagicProtocol) -> List[Dict[Text, Text]]:
# def _parse_line_string(line: Text) -> List[Text]:
# def picatrix_helper(function: Callable[..., Any]) -> Callable[..., Any]:
# def picatrix_magic(
#     function: Optional[MagicProtocol] = None,
#     arguments: Optional[List[MagicArgument]] = None,
#     name_func: Optional[Callable[[Text], Text]] = None,
#     conditional: Optional[Callable[[None], bool]] = None) -> MagicProtocol:
#   def wrapper(func):
# 
# Path: picatrix/lib/state.py
# def state(refresh_state: bool = False):
#   """Property that returns a state object."""
#   # pylint: disable=global-statement
#   # Making sure we have only one state object.
#   global __state
# 
#   with _LOCK:
#     if refresh_state or __state is None:
#       __state = State()
# 
#     return __state
# 
# Path: picatrix/lib/utils.py
# def ask_question(question: Text, input_type: Optional[Any] = Text) -> Any:
# def clear_notebook_output():
# def ipython_bind_global(name: Text, value: Any) -> Any:
# def ipython_get_global(name: Text) -> Any:
# def ipython_remove_global(name: Text):


import datetime
import logging
import os
import dateutil.parser
import pandas as pd
from typing import Any, Dict, List, Optional, Text, Union
from dfdatetime import time_elements
from timesketch_api_client import aggregation as api_aggregation
from timesketch_api_client import client as api_client
from timesketch_api_client import config
from timesketch_api_client import search as api_search
from timesketch_api_client import sketch as api_sketch
from timesketch_api_client import story as api_story
from timesketch_api_client import timeline as api_timeline
from timesketch_import_client import helper, importer
from picatrix.lib import framework, state, utils
  state_obj = state.state()

  if data:
    sketch_id = data.strip()
    client = state_obj.get_from_cache('timesketch_client')
    sketch = client.get_sketch(sketch_id)
  else:
    sketch = state_obj.get_from_cache('timesketch_sketch')

  if not sketch:
    return []

  return {x.name: x for x in sketch.list_timelines()}


@framework.picatrix_magic
def timesketch_create_sketch(
    data: Text, description: Text, set_active: Optional[bool] = True) -> Text:
  """Magic to create a new sketch in Timesketch.

  Args:
    data (str): name of the new sketch.
    description (str): sketch description.
    set_active (bool): whether to set the newly created sketch as the active
        one. Defaults to True.

  Returns:
    str: response string with sketch IDs.
  """
  connect(ignore_sketch=True)

# Path: picatrix/lib/framework.py
# class MagicProtocol(Protocol):
# class MagicArgument:
# class MagicArgumentParser(argparse.ArgumentParser):
# class _Magic:
#   def __call__(self, *args: Any) -> Any:
#   def __init__(self, *argc, **kwargs):
#   def __init__(self, *args, **kwargs):
#   def exit(self, status: Optional[int] = 0, message: Optional[Text] = ''):
#   def __init__(
#       self,
#       fn: MagicProtocol,
#       arguments: Optional[List[MagicArgument]] = None,
#       name_func: Optional[Callable[[Text], Text]] = None):
#   def __call__(self, line: Text, cell: Optional[Text] = None) -> Optional[Any]:
#   def __dir__(self):
#   def __getattribute__(self, name: Text):
# def _get_arguments_from_arg_lines(
#     arg_lines: List[Text]) -> List[Dict[Text, Text]]:
# def _get_argument_lines_from_docstring(lines: List[Text]) -> List[Text]:
# def _add_function_arguments_to_parser(
#     fn: MagicProtocol, parser: MagicArgumentParser):
# def _parse_docstring(function: MagicProtocol) -> List[Dict[Text, Text]]:
# def _parse_line_string(line: Text) -> List[Text]:
# def picatrix_helper(function: Callable[..., Any]) -> Callable[..., Any]:
# def picatrix_magic(
#     function: Optional[MagicProtocol] = None,
#     arguments: Optional[List[MagicArgument]] = None,
#     name_func: Optional[Callable[[Text], Text]] = None,
#     conditional: Optional[Callable[[None], bool]] = None) -> MagicProtocol:
#   def wrapper(func):
# 
# Path: picatrix/lib/state.py
# def state(refresh_state: bool = False):
#   """Property that returns a state object."""
#   # pylint: disable=global-statement
#   # Making sure we have only one state object.
#   global __state
# 
#   with _LOCK:
#     if refresh_state or __state is None:
#       __state = State()
# 
#     return __state
# 
# Path: picatrix/lib/utils.py
# def ask_question(question: Text, input_type: Optional[Any] = Text) -> Any:
# def clear_notebook_output():
# def ipython_bind_global(name: Text, value: Any) -> Any:
# def ipython_get_global(name: Text) -> Any:
# def ipython_remove_global(name: Text):


# Path: picatrix/magics/timesketch.py
import datetime
import logging
import os
import dateutil.parser
import pandas as pd
from typing import Any, Dict, List, Optional, Text, Union
from dfdatetime import time_elements
from timesketch_api_client import aggregation as api_aggregation
from timesketch_api_client import client as api_client
from timesketch_api_client import config
from timesketch_api_client import search as api_search
from timesketch_api_client import sketch as api_sketch
from timesketch_api_client import story as api_story
from timesketch_api_client import timeline as api_timeline
from timesketch_import_client import helper, importer
from picatrix.lib import framework, state, utils

  state_obj = state.state()

  if data:
    sketch_id = data.strip()
    client = state_obj.get_from_cache('timesketch_client')
    sketch = client.get_sketch(sketch_id)
  else:
    sketch = state_obj.get_from_cache('timesketch_sketch')

  if not sketch:
    return []

  return {x.name: x for x in sketch.list_timelines()}


@framework.picatrix_magic
def timesketch_create_sketch(
    data: Text, description: Text, set_active: Optional[bool] = True) -> Text:
  """Magic to create a new sketch in Timesketch.

  Args:
    data (str): name of the new sketch.
    description (str): sketch description.
    set_active (bool): whether to set the newly created sketch as the active
        one. Defaults to True.

  Returns:
    str: response string with sketch IDs.
  """
  connect(ignore_sketch=True)

  state_obj = state.state()