Skip to content

Commit

Permalink
Remove top-level __init__.py global imports
Browse files Browse the repository at this point in the history
  • Loading branch information
bergercookie committed Jan 21, 2024
1 parent a336a74 commit c2395f3
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 353 deletions.
205 changes: 3 additions & 202 deletions syncall/__init__.py
Original file line number Diff line number Diff line change
@@ -1,206 +1,7 @@
"""__init__"""

# global imports ------------------------------------------------------------------------------
from syncall.__version__ import __version__, __version_tuple__
from syncall.aggregator import Aggregator
from syncall.app_utils import (
app_name,
cache_or_reuse_cached_combination,
fetch_app_configuration,
fetch_from_pass_manager,
get_config_name_for_args,
get_resolution_strategy,
inform_about_app_extras,
inform_about_combination_name_usage,
list_named_combinations,
name_to_resolution_strategy_type,
report_toplevel_exception,
)
from syncall.cli import (
opt_asana_task_gid,
opt_asana_token_pass_path,
opt_asana_workspace_gid,
opt_asana_workspace_name,
opt_combination,
opt_custom_combination_savename,
opt_filesystem_root,
opt_gcal_calendar,
opt_gkeep_labels,
opt_gkeep_note,
opt_gkeep_passwd_pass_path,
opt_gkeep_user_pass_path,
opt_google_oauth_port,
opt_google_secret_override,
opt_gtasks_list,
opt_list_asana_workspaces,
opt_list_combinations,
opt_notion_page_id,
opt_notion_token_pass_path,
opt_resolution_strategy,
opt_tw_project,
opt_tw_tags,
)
from syncall.sync_side import ItemType, SyncSide
from syncall.__version__ import __version__ as version
from syncall.__version__ import __version_tuple__ as version_tuple

__all__ = [
"__version__",
"__version_tuple__",
"Aggregator",
"ItemType",
"SyncSide",
"app_name",
"cache_or_reuse_cached_combination",
"fetch_app_configuration",
"fetch_from_pass_manager",
"get_config_name_for_args",
"inform_about_app_extras",
"inform_about_combination_name_usage",
"list_named_combinations",
"get_resolution_strategy",
"name_to_resolution_strategy_type",
"opt_asana_task_gid",
"opt_asana_token_pass_path",
"opt_asana_workspace_gid",
"opt_asana_workspace_name",
"opt_combination",
"opt_custom_combination_savename",
"opt_gcal_calendar",
"opt_gtasks_list",
"opt_gkeep_note",
"opt_gkeep_passwd_pass_path",
"opt_gkeep_user_pass_path",
"opt_google_oauth_port",
"opt_google_secret_override",
"opt_list_asana_workspaces",
"opt_list_combinations",
"opt_notion_page_id",
"opt_notion_token_pass_path",
"opt_resolution_strategy",
"opt_tw_project",
"opt_filesystem_root",
"opt_tw_tags",
"opt_gkeep_labels",
"report_toplevel_exception",
]

# asana ----------------------------------------------------------------------------------------
try:
from syncall.asana.asana_side import AsanaSide
from syncall.asana.utils import list_asana_workspaces
from syncall.tw_asana_utils import convert_asana_to_tw, convert_tw_to_asana

__all__.extend(
["AsanaSide", "convert_asana_to_tw", "convert_tw_to_asana", "list_asana_workspaces"]
)
except ImportError:
pass

# tw ------------------------------------------------------------------------------------------
try:
from syncall.taskwarrior.taskwarrior_side import TaskWarriorSide

__all__.extend(
[
"TaskWarriorSide",
]
)
except ImportError:
pass

# notion --------------------------------------------------------------------------------------
try:
from syncall.notion.notion_side import NotionSide

__all__.extend(["NotionSide"])
except ImportError:
pass

# notion <> tw --------------------------------------------------------------------------------
try:
from syncall.tw_notion_utils import convert_notion_to_tw, convert_tw_to_notion

__all__.extend(["convert_notion_to_tw", "convert_tw_to_notion"])
except ImportError:
pass

# gcal ----------------------------------------------------------------------------------------
try:
from syncall.google.gcal_side import GCalSide
from syncall.tw_gcal_utils import convert_gcal_to_tw, convert_tw_to_gcal
except ImportError:
__all__.extend(
[
"GCalSide",
"convert_gcal_to_tw",
"convert_tw_to_gcal",
]
)

# gtasks --------------------------------------------------------------------------------------
try:
from syncall.google.gtasks_side import GTasksSide
from syncall.tw_gtasks_utils import convert_gtask_to_tw, convert_tw_to_gtask
except ImportError:
__all__.extend(
[
"GTasksSide",
"convert_gtask_to_tw",
"convert_tw_to_gtask",
]
)

# gkeep ---------------------------------------------------------------------------------------
try:
from syncall.google.gkeep_note import GKeepNote
from syncall.google.gkeep_note_side import GKeepNoteSide
from syncall.google.gkeep_todo_item import GKeepTodoItem
from syncall.google.gkeep_todo_side import GKeepTodoSide

__all__.extend(
[
"GKeepNote",
"GKeepNoteSide",
"GKeepTodoItem",
"GKeepTodoSide",
"convert_gkeep_todo_to_tw",
"convert_tw_to_gkeep_todo",
]
)
except ImportError:
pass

# gkeep <> tw
try:
from syncall.tw_gkeep_utils import convert_gkeep_todo_to_tw, convert_tw_to_gkeep_todo

__all__.extend(
[
"convert_gkeep_todo_to_tw",
"convert_tw_to_gkeep_todo",
]
)
except ImportError:
pass

# filesytem -----------------------------------------------------------------------------------
try:
from syncall.filesystem.filesystem_file import FilesystemFile
from syncall.filesystem.filesystem_side import FilesystemSide

__all__.extend(["FilesystemFile", "FilesystemSide"])
except ImportError:
pass


# filesytem <> gkeep --------------------------------------------------------------------------
try:
from syncall.filesystem_gkeep_utils import (
convert_filesystem_file_to_gkeep_note,
convert_gkeep_note_to_filesystem_file,
)

__all__.extend(
["convert_filesystem_file_to_gkeep_note", "convert_gkeep_note_to_filesystem_file"]
)
except ImportError:
pass
__all__ = ["version", "version_tuple"]
33 changes: 19 additions & 14 deletions syncall/app_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,20 +130,9 @@ def get_named_combinations(config_fname: str) -> Sequence[str]:
return list(prefs_manager.keys())


def list_named_combinations(config_fname: str) -> None:
"""List the named configurations currently available for the given configuration name.
Mainly used by the top-level synchronization apps.
"""
logger.success(
format_list(
header="\n\nNamed configurations currently available",
items=get_named_combinations(config_fname=config_fname),
)
)


def fetch_app_configuration(config_fname: str, combination: str) -> Mapping[str, Any]:
def fetch_app_configuration(
side_A_name: str, side_B_name: str, combination: str
) -> Mapping[str, Any]:
"""
Fetch the configuration of a top-level synchronization app.
Expand All @@ -155,6 +144,7 @@ def fetch_app_configuration(config_fname: str, combination: str) -> Mapping[str,
It will check whether the configuration file at hand exist and will also give meaningful
errors to the user if the configuration file does not contain the said combination.
"""
config_fname = determine_app_config_fname(side_A_name, side_B_name)
with PrefsManager(app_name=app_name(), config_fname=config_fname) as prefs_manager:
if combination not in prefs_manager:
# config not found ----------------------------------------------------------------
Expand Down Expand Up @@ -382,3 +372,18 @@ def teardown():
atexit.register(teardown)

return hooks


def determine_app_config_fname(side_A_name: str, side_B_name: str):
"""
Get the configuration name for the app at hand given the names of the sides involved.
>>> assert determine_app_config_fname("TW", "Google Tasks") == 'tw__google_tasks__configs'
>>> assert determine_app_config_fname("TW", "Google Calendar") == 'tw__google_calendar__configs'
"""
config_fname = (
f'{side_A_name.replace(" ", "_").lower()}'
"__"
f'{side_B_name.replace(" ", "_").lower()}__configs'
)
return config_fname
43 changes: 34 additions & 9 deletions syncall/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import sys

import click
from bubop import logger
from bubop import format_list, logger

from syncall import __version__
from syncall.app_utils import (
determine_app_config_fname,
error_and_exit,
fetch_from_pass_manager,
get_named_combinations,
name_to_resolution_strategy_type,
)
from syncall.constants import COMBINATION_FLAGS
Expand Down Expand Up @@ -484,7 +486,7 @@ def decorator(f):
return decorator


def _opt_default_duration_event_mins():
def opt_default_duration_event_mins():
return click.option(
"--default-event-duration-mins",
"default_event_duration_mins",
Expand All @@ -494,12 +496,35 @@ def _opt_default_duration_event_mins():
)


def _opt_list_combinations(name_A: str, name_B: str):
def _list_named_combinations(config_fname: str) -> None:
"""List the named configurations currently available for the given configuration name.
Mainly used by the top-level synchronization apps.
"""
logger.success(
format_list(
header="\n\nNamed configurations currently available",
items=get_named_combinations(config_fname=config_fname),
)
)


def _opt_list_combinations(side_A_name: str, side_B_name: str):
def callback(ctx, param, value):
_list_named_combinations(
config_fname=determine_app_config_fname(
side_A_name=side_A_name, side_B_name=side_B_name
)
)
sys.exit(0)

return click.option(
"--list-combinations",
"do_list_combinations",
is_flag=True,
help=f"List the available named {name_A}<->{name_B} combinations",
expose_value=False,
help=f"List the available named {side_A_name}<->{side_B_name} combinations",
callback=callback,
)


Expand Down Expand Up @@ -531,24 +556,24 @@ def _opt_list_resolution_strategies():
)


def _opt_combination(name_A: str, name_B: str):
def _opt_combination(side_A_name: str, side_B_name: str):
return click.option(
COMBINATION_FLAGS[0],
COMBINATION_FLAGS[1],
"combination_name",
type=str,
help=f"Name of an already saved {name_A}<->{name_B} combination",
help=f"Name of an already saved {side_A_name}<->{side_B_name} combination",
)


def _opt_custom_combination_savename(name_A: str, name_B: str):
def _opt_custom_combination_savename(side_A_name: str, side_B_name: str):
return click.option(
"-s",
"--save-as",
"custom_combination_savename",
type=str,
help=(
f"Save the given {name_A}<->{name_B} filters combination using a specified custom"
" name."
f"Save the given {side_A_name}<->{side_B_name} filters combination using a"
" specified custom name."
),
)
Loading

0 comments on commit c2395f3

Please sign in to comment.