Skip to content

Commit cbe304d

Browse files
committed
Fix cmd options (#66)
* fix cmd options, use path_type, IntRange * add change log and new version
1 parent 2f0aa54 commit cbe304d

File tree

9 files changed

+99
-55
lines changed

9 files changed

+99
-55
lines changed

.github/workflows/devcontainer-ci.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ name: Check Dev Container
33
on:
44
workflow_dispatch:
55
push:
6-
branches: [ main ]
6+
branches: [ main, dev ]
77
paths:
88
- ".devcontainer/**"
99
- ".github/workflows/devcontainer-ci.yaml"
1010
pull_request:
11-
branches: [ main ]
11+
branches: [ main, dev ]
1212
paths:
1313
- ".devcontainer/**"
1414
- ".github/workflows/devcontainer-ci.yaml"

.github/workflows/python-tests.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on:
44
workflow_dispatch:
55

66
pull_request:
7-
branches: [ main ]
7+
branches: [ main, dev ]
88
paths:
99
- '**.py'
1010
- ".github/workflows/python-tests.yaml"

.github/workflows/python-types-lint-format.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ name: Run Python type, lint and format checks
33
on:
44
workflow_dispatch:
55
push:
6-
branches: [ main ]
6+
branches: [ main, dev ]
77
paths:
88
- '**.py'
99
- ".github/workflows/python-types-lint-format.yaml"
1010

1111
pull_request:
12-
branches: [ main ]
12+
branches: [ main, dev ]
1313
paths:
1414
- '**.py'
1515
- ".github/workflows/python-types-lint-format.yaml"

.github/workflows/python-versions-tests.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Run Python Matrix Tests
33
on:
44
workflow_dispatch:
55
push:
6-
branches: [ main ]
6+
branches: [ main, dev ]
77

88
permissions:
99
contents: read

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ All notable changes to this project will be documented in this file.
1212

1313
### Other Changes
1414

15+
## [v0.2.1] 7 Aug 2024
16+
- Fix command line list[str] type issue and use Click.IntRange for retries and timeout.
17+
1518
## [v0.2.0] 7 Aug 2024
1619
- Redesign the package.
1720
- Port to using Click instead of arg_parser.

docs/source/advanced.md

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,47 @@ To further customize your experience with the Markdown Checker, you can utilize
99
- **Description**: Path to the root directory to check.
1010
- **Required**: Yes
1111

12+
### `-f`, `--func`
13+
- **Type**: `click.Choice`
14+
- **Description**: Function to be executed.
15+
- **Choices**:
16+
- `check_broken_paths`
17+
- `check_broken_urls`
18+
- `check_paths_tracking`
19+
- `check_urls_tracking`
20+
- `check_urls_locale`
21+
- **Required**: Yes
22+
1223
### `-ext`, `--extensions`
1324
- **Type**: `list[str]`
1425
- **Description**: File extensions to filter the files.
1526
- **Default**:
1627
- `.md`
1728
- `.ipynb`
18-
- **Required**: Yes
29+
- **Required**: No
1930

2031
### `-td`, `--tracking-domains`
2132
- **Type**: `list[str]`
22-
- **Description**: List of tracking domains to check if they have a tracking id or not.
33+
- **Description**: List of tracking domains to check.
2334
- **Default**:
2435
- `github.com`
2536
- `microsoft.com`
2637
- `visualstudio.com`
2738
- `aka.ms`
2839
- `azure.com`
29-
- **Required**: Yes
40+
- **Required**: No
3041

3142
### `-sf`, `--skip-files`
3243
- **Type**: `list[str]`
3344
- **Description**: List of file names to skip check.
3445
- **Default**:
3546
- `CODE_OF_CONDUCT.md`
3647
- `SECURITY.md`
37-
- **Required**: Yes
48+
- **Required**: No
3849

3950
### `-sd`, `--skip-domains`
4051
- **Type**: `list[str]`
41-
- **Description**: List of domains to skip checking if their urls are working or not.
52+
- **Description**: List of domains to skip checking.
4253
- **Default**: `[]`
4354
- **Required**: No
4455

@@ -53,26 +64,32 @@ To further customize your experience with the Markdown Checker, you can utilize
5364
### `-gu`, `--guide-url`
5465
- **Type**: `str`
5566
- **Description**: Full URL of your contributing guide.
56-
- **Required**: Yes
67+
- **Required**: No
5768

5869
### `-to`, `--timeout`
59-
- **Type**: `int`
70+
- **Type**: `Click.IntRange`
6071
- **Description**: Timeout in seconds for the requests before retrying.
6172
- **Default**: `10`
73+
- **Range**: `0-50`
6274
- **Required**: No
6375

6476
### `-rt`, `--retries`
65-
- **Type**: `int`
77+
- **Type**: `Click.IntRange`
6678
- **Description**: Number of retries for the requests before flagging a url as broken.
6779
- **Default**: `3`
80+
- **Range**: `0-10`
6881
- **Required**: No
6982

7083
### `-o`, `--output-file-name`
7184
- **Type**: `str`
7285
- **Description**: Name of the output file.
7386
- **Default**: `comment`
74-
- **Required**: Yes
87+
- **Required**: No
7588

89+
### `SRC ...`
90+
- **Type**: `click.Path`
91+
- **Description**: Source files or directories to check.
92+
- **Required**: No
7693

7794
## Other Options
7895

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[project]
22
name = "markdown-checker"
33
description= "A markdown link validation reporting tool."
4-
version = "0.2.0"
4+
version = "0.2.1"
55
authors = [{ name = "John Aziz", email = "[email protected]" }]
66
maintainers = [{ name = "John Aziz", email = "[email protected]" }]
77
license = {file = "LICENSE"}

src/markdown_checker/__init__.py

Lines changed: 58 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
from markdown_checker.utils.spinner import spinner
2323

2424

25-
def check_url(url: MarkdownURL, skip_domains: list[str], skip_urls_containing: list[str], timeout: int, retries: int):
25+
def check_url(
26+
url: MarkdownURL, skip_domains: list[str], skip_urls_containing: list[str], timeout: int, retries: int
27+
) -> Union[None, MarkdownURL]:
2628
if any(url.host_name().lower() in domain.lower() for domain in skip_domains) or any(
2729
url.link in substring for substring in skip_urls_containing
2830
):
@@ -150,44 +152,83 @@ def detect_issues(
150152
return detected_issues, links_count
151153

152154

155+
class ListOfStrings(click.Option):
156+
"""
157+
Helper class to parse a list of strings from the command line.
158+
159+
Ref: https://stackoverflow.com/questions/47631914/how-to-pass-several-list-of-arguments-to-click-option
160+
"""
161+
162+
def type_cast_value(self, ctx, value):
163+
try:
164+
if isinstance(value, str):
165+
# split the string by comma and remove empty strings
166+
return list(filter(None, value.split(",")))
167+
elif isinstance(value, list):
168+
return value
169+
else:
170+
raise Exception
171+
except Exception:
172+
raise click.BadParameter(value)
173+
174+
153175
@click.command()
154176
@click.option(
155177
"-d",
156178
"--dir",
157-
type=click.Path(exists=True, file_okay=False, dir_okay=True, readable=True),
179+
type=click.Path(path_type=Path, exists=True, file_okay=False, dir_okay=True, readable=True),
158180
help="Path to the root directory to check.",
159181
required=True,
160182
)
183+
@click.option(
184+
"-f",
185+
"--func",
186+
type=click.Choice(
187+
[
188+
"check_broken_paths",
189+
"check_broken_urls",
190+
"check_paths_tracking",
191+
"check_urls_tracking",
192+
"check_urls_locale",
193+
]
194+
),
195+
help="Function to be executed.",
196+
required=True,
197+
)
161198
@click.option(
162199
"-ext",
163200
"--extensions",
201+
cls=ListOfStrings,
164202
type=list[str],
165203
default=[".md", ".ipynb"],
166204
help="File extensions to filter the files.",
167-
required=True,
205+
required=False,
168206
)
169207
@click.option(
170208
"-td",
171209
"--tracking-domains",
210+
cls=ListOfStrings,
172211
type=list[str],
173212
default=["github.com", "microsoft.com", "visualstudio.com", "aka.ms", "azure.com"],
174213
help="List of tracking domains to check.",
175-
required=True,
214+
required=False,
176215
)
177216
@click.option(
178217
"-sf",
179218
"--skip-files",
219+
cls=ListOfStrings,
180220
type=list[str],
181221
default=[
182222
"CODE_OF_CONDUCT.md",
183223
"SECURITY.md",
184224
],
185225
help="List of file names to skip check.",
186-
required=True,
226+
required=False,
187227
)
188228
@click.option(
189229
"-sd",
190230
"--skip-domains",
231+
cls=ListOfStrings,
191232
type=list[str],
192233
default=[],
193234
help="List of domains to skip check.",
@@ -196,45 +237,31 @@ def detect_issues(
196237
@click.option(
197238
"-suc",
198239
"--skip-urls-containing",
240+
cls=ListOfStrings,
199241
type=list[str],
200242
default=["https://www.microsoft.com/en-us/security/blog", "video-embed.html"],
201243
help="List of urls to skip check.",
202244
required=False,
203245
)
204-
@click.option(
205-
"-f",
206-
"--func",
207-
type=click.Choice(
208-
[
209-
"check_broken_paths",
210-
"check_broken_urls",
211-
"check_paths_tracking",
212-
"check_urls_tracking",
213-
"check_urls_locale",
214-
]
215-
),
216-
help="Function to be executed.",
217-
required=True,
218-
)
219246
@click.option(
220247
"-gu",
221248
"--guide-url",
222249
type=str,
223250
help="Full url of your contributing guide.",
224-
required=True,
251+
required=False,
225252
)
226253
@click.option(
227254
"-to",
228255
"--timeout",
229-
type=int,
256+
type=click.IntRange(0, 50),
230257
default=10,
231258
help="Timeout in seconds for the requests.",
232259
required=False,
233260
)
234261
@click.option(
235262
"-rt",
236263
"--retries",
237-
type=int,
264+
type=click.IntRange(0, 10),
238265
default=3,
239266
help="Number of retries for the requests.",
240267
required=False,
@@ -245,12 +272,12 @@ def detect_issues(
245272
type=str,
246273
default="comment",
247274
help="Name of the output file.",
248-
required=True,
275+
required=False,
249276
)
250277
@click.argument(
251278
"src",
252279
nargs=-1,
253-
type=click.Path(exists=True, file_okay=True, dir_okay=True, readable=True),
280+
type=click.Path(path_type=Path, exists=True, file_okay=True, dir_okay=True, readable=True),
254281
is_eager=True,
255282
metavar="SRC ...",
256283
required=False,
@@ -259,23 +286,22 @@ def detect_issues(
259286
message=(f"%(prog)s, %(version)s\n" f"Python ({platform.python_implementation()}) {platform.python_version()}"),
260287
)
261288
def main(
262-
src: tuple[str, ...],
263-
dir: str,
289+
src: tuple[Path, ...],
290+
dir: Path,
264291
func: str,
265-
guide_url: str,
292+
guide_url: Union[str, None],
266293
extensions: list[str],
267294
skip_files: list[str],
268295
skip_domains: list[str],
269296
skip_urls_containing: list[str],
297+
tracking_domains: list[str],
270298
timeout: int,
271299
retries: int,
272-
tracking_domains: list[str],
273300
output_file_name: str,
274301
) -> None:
275302
"""A markdown link validation reporting tool."""
276-
_ = tuple(Path(item) for item in src) or (Path("./"),) # default to current directory
277-
278-
_, files_paths = get_files_paths_list(Path(dir), extensions)
303+
_ = src or (Path("./"),) # default to current directory
304+
_, files_paths = get_files_paths_list(dir, extensions)
279305

280306
# remove files from skip_files list
281307
files_paths = [file_path for file_path in files_paths if file_path.name not in skip_files]

src/markdown_checker/reports/md_reports/generator.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pathlib
2+
from typing import Union
23

34
from markdown_checker.reports.generator_base import GeneratorBase
45

@@ -20,12 +21,14 @@ class MarkdownGenerator(GeneratorBase):
2021

2122
def __init__(
2223
self,
24+
contributing_guide_url: Union[str, None] = None,
2325
output_file_name: str = "comment",
24-
contributing_guide_url: str = "https://github.com/john0isaac/markdown-checker/blob/main/CONTRIBUTING.md",
2526
) -> None:
2627
self.output_file_name = output_file_name
2728
self.contributing_guide_line = (
28-
f" For more details, check our [Contributing Guide]({contributing_guide_url}).\n\n"
29+
(f" For more details, check our [Contributing Guide]({contributing_guide_url}).\n\n")
30+
if contributing_guide_url
31+
else "\n\n"
2932
)
3033

3134
def _write_file(self, generated_text: str) -> None:
@@ -60,8 +63,3 @@ def generate(self, function_name: str, formatted_output: str) -> None:
6063
"""
6164
generated_text = self._generate_text(function_name=function_name, formatted_output=formatted_output)
6265
self._write_file(generated_text)
63-
64-
65-
if __name__ == "__main__":
66-
md_generator = MarkdownGenerator()
67-
md_generator.generate("check_broken_paths", "This is a test")

0 commit comments

Comments
 (0)