twc-cli 1.1.0__py3-none-any.whl → 1.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of twc-cli might be problematic. Click here for more details.

twc/commands/config.py CHANGED
@@ -3,36 +3,65 @@
3
3
  import os
4
4
  import sys
5
5
  import ctypes
6
+ import json
6
7
 
8
+ import yaml
7
9
  import toml
8
10
  import click
11
+ from click_default_group import DefaultGroup
9
12
 
10
- from . import options, get_default_config_file, GLOBAL_OPTIONS, DEFAULT_CONFIG
13
+ from twc import fmt
14
+ from twc import utils
15
+ from twc.vars import DEFAULT_CONFIG
16
+ from . import (
17
+ options,
18
+ load_config,
19
+ get_default_config_file,
20
+ GLOBAL_OPTIONS,
21
+ )
22
+
23
+
24
+ def write_to_file(data: str, filepath: str):
25
+ try:
26
+ with open(filepath, "w", encoding="utf-8") as file:
27
+ toml.dump(data, file)
28
+ if os.name == "nt":
29
+ hidden_file_attr = 0x02
30
+ ctypes.windll.kernel32.SetFileAttributesW(
31
+ filepath, hidden_file_attr
32
+ )
33
+ click.echo(f"Done! Configuration is saved in {filepath}")
34
+ sys.exit(0)
35
+ except OSError as error:
36
+ sys.exit(f"Error: {error}")
11
37
 
12
38
 
13
39
  def make_config(filepath: str = get_default_config_file()):
14
40
  """Create new configuration file."""
15
41
  if os.path.exists(filepath):
16
- sys.exit(f"File '{filepath}' already exists.")
17
- else:
18
- click.echo("Create new configuration file. Enter your API token.")
19
- while True:
20
- token = input("Token: ")
21
- if token:
22
- DEFAULT_CONFIG.update({"default": {"token": token}})
23
- break
24
- click.echo("Please enter token. Press ^C to cancel.")
25
- try:
26
- with open(filepath, "w", encoding="utf-8") as file:
27
- toml.dump(DEFAULT_CONFIG, file)
28
- if os.name == "nt":
29
- hidden_file_attr = 0x02
30
- ctypes.windll.kernel32.SetFileAttributesW(
31
- filepath, hidden_file_attr
32
- )
33
- click.echo(f"Success! Configuration is saved in {filepath}")
34
- except OSError as error:
35
- sys.exit(f"Error: {error}")
42
+ if click.confirm(
43
+ "You already have TWC CLI configured, continue?",
44
+ default=False,
45
+ ):
46
+ current_config = load_config()
47
+ profile = click.prompt("Enter profile name", default="default")
48
+ token = click.prompt(f"Enter API token for '{profile}'")
49
+ current_config[profile] = utils.merge_dicts(
50
+ current_config[profile],
51
+ {"token": token},
52
+ )
53
+ write_to_file(current_config, filepath)
54
+ else:
55
+ sys.exit("Aborted!")
56
+
57
+ click.echo("Create new configuration file. Enter your API token.")
58
+ while True:
59
+ token = input("Token: ")
60
+ if token:
61
+ DEFAULT_CONFIG.update({"default": {"token": token}})
62
+ break
63
+ click.echo("Please enter token. Press ^C to cancel.")
64
+ write_to_file(DEFAULT_CONFIG, filepath)
36
65
 
37
66
 
38
67
  # ------------------------------------------------------------- #
@@ -40,7 +69,97 @@ def make_config(filepath: str = get_default_config_file()):
40
69
  # ------------------------------------------------------------- #
41
70
 
42
71
 
43
- @click.command("config", help="Make new configuration file.")
72
+ @click.group(
73
+ "config", cls=DefaultGroup, default="init", default_if_no_args=True
74
+ )
44
75
  @options(GLOBAL_OPTIONS[:2])
45
76
  def config():
77
+ """Manage CLI configuration."""
78
+
79
+
80
+ # ------------------------------------------------------------- #
81
+ # $ twc config init #
82
+ # ------------------------------------------------------------- #
83
+
84
+
85
+ @config.command("init", help="Create confiration file and profile.")
86
+ @options(GLOBAL_OPTIONS[:2])
87
+ def config_init():
46
88
  make_config()
89
+
90
+
91
+ # ------------------------------------------------------------- #
92
+ # $ twc config file #
93
+ # ------------------------------------------------------------- #
94
+
95
+
96
+ @config.command("file", help="Display path to configuration file.")
97
+ @options(GLOBAL_OPTIONS[:4])
98
+ def config_file(config, verbose):
99
+ click.echo(click.format_filename(config.encode()))
100
+
101
+
102
+ # ------------------------------------------------------------- #
103
+ # $ twc config dump #
104
+ # ------------------------------------------------------------- #
105
+
106
+
107
+ @config.command("dump", help="Dump configuration.")
108
+ @options(GLOBAL_OPTIONS)
109
+ @click.option(
110
+ "--full", "full_dump", is_flag=True, help="Dump full configuration."
111
+ )
112
+ @click.option(
113
+ "--output",
114
+ "-o",
115
+ "output_format",
116
+ type=click.Choice(["toml", "yaml", "json"]),
117
+ default="toml",
118
+ show_default=True,
119
+ help="Output format.",
120
+ )
121
+ def config_dump(config, profile, verbose, full_dump, output_format):
122
+ if full_dump:
123
+ config_dict = load_config(config)
124
+ else:
125
+ config_dict = load_config(config)[profile]
126
+
127
+ if output_format == "toml":
128
+ fmt.print_colored(toml.dumps(config_dict), lang="toml")
129
+ elif output_format == "yaml":
130
+ fmt.print_colored(yaml.dump(config_dict), lang="yaml")
131
+ elif output_format == "json":
132
+ fmt.print_colored(json.dumps(config_dict), lang="json")
133
+
134
+
135
+ # ------------------------------------------------------------- #
136
+ # $ twc config set #
137
+ # ------------------------------------------------------------- #
138
+
139
+
140
+ @config.command("set", help="Set config parameter.")
141
+ @options(GLOBAL_OPTIONS)
142
+ @click.argument("params", nargs=-1, metavar="PARAM=VALUE")
143
+ def config_set(config, profile, verbose, params):
144
+ if not params:
145
+ raise click.UsageError("Nothing to do.")
146
+ config_dict = load_config(config)
147
+ params_dict = {}
148
+ for param in params:
149
+ key, value = param.split("=")
150
+ if value.isdigit():
151
+ value = int(value)
152
+ params_dict[key] = value
153
+ config_dict[profile] = utils.merge_dicts(config_dict[profile], params_dict)
154
+ write_to_file(config_dict, config)
155
+
156
+
157
+ # ------------------------------------------------------------- #
158
+ # $ twc config edit #
159
+ # ------------------------------------------------------------- #
160
+
161
+
162
+ @config.command("edit", help="Open config file in default editor.")
163
+ @options(GLOBAL_OPTIONS[:4])
164
+ def config_edit(config, verbose):
165
+ click.launch(config)