-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
50 lines (42 loc) · 1.31 KB
/
Copy pathcli.py
File metadata and controls
50 lines (42 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import re
import click
from graph import build_graph
from plot import build_plot
def get_valid_filename(s):
"""
Return the given string converted to a string that can be used for a clean
filename. Remove leading and trailing spaces; convert other spaces to
underscores; and remove anything that is not an alphanumeric, dash,
underscore, or dot.
>>> get_valid_filename("john's portrait in 2004.jpg")
'johns_portrait_in_2004.jpg'
"""
s = str(s).strip().replace(" ", "_")
return re.sub(r"(?u)[^-\w.]", "", s)
@click.command(name="vs-graph")
@click.argument("term")
@click.option(
"--radius",
"-r",
type=click.IntRange(min=1, max=10, clamp=False),
required=False,
default=3,
)
@click.option(
"--trim-leaves",
"-t",
is_flag=True
)
@click.option("--fpath", "-f", type=click.Path(), required=False, default=None)
def vs_graph(term, radius, fpath, trim_leaves=False):
"""Build a neighborhood graph of the search term
\b
Args:
term: Word to search for
radius: Maximum distance away from the central node to keep in the graph
fpath: Where to save the html output.
"""
if not fpath:
fpath = get_valid_filename(term) + ".html"
g = build_graph(term, radius=radius, trim_leaves=trim_leaves)
build_plot(term, g, fpath)