Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli.py): adding new option --ignore-path to ignore certian paths #9

Merged
merged 1 commit into from
May 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions confluence_utils/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,20 @@ def list_pages(url: str, space: str, token: str) -> None:
@cli.command()
@client_options
@click.argument("path", type=click.Path(exists=True, resolve_path=True))
def publish(path: str, url: str, space: str, token: str) -> None:
@click.option(
"--ignore-path",
"ignore_paths",
multiple=True,
help="Paths to ignore when converting markdown",
type=click.Path(exists=True, resolve_path=True),
)
def publish(
path: str,
url: str,
space: str,
token: str,
ignore_paths: List[str] = [],
) -> None:
try:
confluence = Confluence(
url=url,
Expand All @@ -82,12 +95,19 @@ def publish(path: str, url: str, space: str, token: str) -> None:
if homepage is not None:
homepage_id = homepage.get("id")

if os.path.isfile(path) and path.endswith(".md"):
if (
os.path.isfile(path)
and path.endswith(".md")
and not subpath_in_paths(ignore_paths, path)
):
publish_files([path], space, confluence, homepage_id)

elif os.path.isdir(path):
publish_files(
get_files(path, ".md"), space, confluence, homepage_id
get_files(path, ".md", ignore_paths),
space,
confluence,
homepage_id,
)

except click.ClickException as e:
Expand Down Expand Up @@ -203,12 +223,28 @@ def publish_files(
raise click.FileError(path, hint=str(e))


def get_files(path: str, extension_filter: Optional[str] = None) -> List[str]:
def get_files(
path: str,
extension_filter: Optional[str] = None,
ignore_paths: List[str] = [],
) -> List[str]:
files = []
for root, d_names, f_names in os.walk(path):
for f in f_names:
files.append(os.path.join(root, f))
current_path = os.path.join(root, f)
if subpath_in_paths(ignore_paths, current_path):
continue
else:
files.append(current_path)

if extension_filter:
return [s for s in files if s.endswith(extension_filter)]
else:
return files


def subpath_in_paths(paths: List[str], subpath: str) -> bool:
for path in paths:
if os.path.commonpath([path]) == os.path.commonpath([path, subpath]):
return True
return False