cli-ih 0.6.2__tar.gz → 0.6.2.1__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cli_ih
3
- Version: 0.6.2
3
+ Version: 0.6.2.1
4
4
  Summary: A background command handler for python's command-line interface.
5
5
  Author-email: Hotment <michatchuplay@gmail.com>
6
6
  Requires-Python: >=3.8
@@ -57,6 +57,27 @@ handler.start()
57
57
  # > exit
58
58
  ```
59
59
 
60
+ ## New Async client
61
+ ```python
62
+ import asyncio
63
+ from cli_ih import AsyncInputHandler
64
+
65
+ print(cli_ih.__version__)
66
+
67
+ handler = AsyncInputHandler(cursor="> ")
68
+
69
+ @handler.command(name="greet", description="Greets the user. Usage: greet [name]")
70
+ async def greet(name, *args):
71
+ await asyncio.sleep(1)
72
+ print(f"Hello, {name}{" " if args else ""}{' '.join(args)}!")
73
+ # NEW
74
+ @handler.command(name="add", description="Performs the `+` operator on the first 2 arguments.")
75
+ async def add(a, b):
76
+ print(a+b)
77
+
78
+ asyncio.run(handler.start())
79
+ ```
80
+
60
81
  ## Additional Info
61
82
 
62
83
  - You can provide a valid logger `logger=logger` to the `InputHandler` to enable logging (this will be removed soon)
@@ -49,6 +49,27 @@ handler.start()
49
49
  # > exit
50
50
  ```
51
51
 
52
+ ## New Async client
53
+ ```python
54
+ import asyncio
55
+ from cli_ih import AsyncInputHandler
56
+
57
+ print(cli_ih.__version__)
58
+
59
+ handler = AsyncInputHandler(cursor="> ")
60
+
61
+ @handler.command(name="greet", description="Greets the user. Usage: greet [name]")
62
+ async def greet(name, *args):
63
+ await asyncio.sleep(1)
64
+ print(f"Hello, {name}{" " if args else ""}{' '.join(args)}!")
65
+ # NEW
66
+ @handler.command(name="add", description="Performs the `+` operator on the first 2 arguments.")
67
+ async def add(a, b):
68
+ print(a+b)
69
+
70
+ asyncio.run(handler.start())
71
+ ```
72
+
52
73
  ## Additional Info
53
74
 
54
75
  - You can provide a valid logger `logger=logger` to the `InputHandler` to enable logging (this will be removed soon)
@@ -1,4 +1,4 @@
1
- from typing import Callable
1
+ from typing import Coroutine
2
2
  from .exceptions import HandlerClosed
3
3
  import logging, warnings, asyncio, inspect
4
4
 
@@ -48,7 +48,7 @@ class AsyncInputHandler:
48
48
  else:
49
49
  print(f"[EXEPTION]: {msg}: {e}")
50
50
 
51
- def __register_cmd(self, name: str, func: Callable, description: str = "", legacy=False):
51
+ def __register_cmd(self, name: str, func: Coroutine, description: str = "", legacy=False):
52
52
  name = name.lower()
53
53
  if not description:
54
54
  description = "A command"
@@ -58,14 +58,14 @@ class AsyncInputHandler:
58
58
  raise SyntaxError(f"Command '{name}' is already registered. If theese commands have a different case and they need to stay the same, downgrade the package version to 0.5.x")
59
59
  self.commands[name] = {"cmd": func, "description": description, "legacy": legacy, "is_async": inspect.iscoroutinefunction(func)}
60
60
 
61
- def register_command(self, name: str, func: Callable, description: str = ""):
61
+ def register_command(self, name: str, func: Coroutine, description: str = ""):
62
62
  """(DEPRECATED) Registers a command with its associated function."""
63
63
  warnings.warn("Registering commands with `register_command` is deprecated, and should not be used.", DeprecationWarning, 2)
64
64
  self.__register_cmd(name, func, description, legacy=True)
65
65
 
66
66
  def command(self, *, name: str = "", description: str = ""):
67
67
  """Registers a command with its associated function as a decorator."""
68
- def decorator(func: Callable):
68
+ def decorator(func: Coroutine):
69
69
  lname = name or func.__name__
70
70
  self.__register_cmd(lname, func, description)
71
71
  return func
@@ -75,30 +75,6 @@ class AsyncInputHandler:
75
75
  """Starts the input handler loop in a separate thread if thread mode is enabled."""
76
76
  self.is_running = True
77
77
 
78
- while self.is_running:
79
- try:
80
- user_input = await asyncio.to_thread(input, self.cursor)
81
- if not user_input:
82
- continue
83
-
84
- cmdargs = user_input.split(' ')
85
- command_name = cmdargs[0].lower()
86
- args = cmdargs[1:]
87
- if command_name in self.commands:
88
- await _run_command(self.commands, command_name, args)
89
- else:
90
- self.__warning(f"Unknown command: '{command_name}'")
91
- except EOFError:
92
- self.__error("Input ended unexpectedly.")
93
- break
94
- except KeyboardInterrupt:
95
- self.__error("Input interrupted.")
96
- break
97
- except HandlerClosed:
98
- self.__info("Input Handler exited.")
99
- break
100
- self.is_running = False
101
-
102
78
  async def _run_command(commands: dict, name: str, args: list):
103
79
  """Executes a command from the command dictionary if it exists."""
104
80
  command = commands.get(name)
@@ -125,17 +101,45 @@ class AsyncInputHandler:
125
101
  try:
126
102
  if is_legacy:
127
103
  warnings.warn("This way of running commands id Deprecated. And should be changed to the new decorator way.", DeprecationWarning, 2)
128
- await asyncio.to_thread(func, args)
129
- elif is_async:
130
- await func(*args)
104
+ if is_async:
105
+ await func(args)
106
+ else:
107
+ await asyncio.to_thread(func, args)
131
108
  else:
132
- await asyncio.to_thread(func, *args)
109
+ if is_async:
110
+ await func(*args)
111
+ else:
112
+ await asyncio.to_thread(func, *args)
133
113
 
134
114
  except HandlerClosed as e:
135
115
  raise e
136
116
  except Exception as e:
137
117
  self.__exeption(f"An error occurred in command '{name}'", e)
138
118
 
119
+ while self.is_running:
120
+ try:
121
+ user_input = await asyncio.to_thread(input, self.cursor)
122
+ if not user_input:
123
+ continue
124
+
125
+ cmdargs = user_input.split(' ')
126
+ command_name = cmdargs[0].lower()
127
+ args = cmdargs[1:]
128
+ if command_name in self.commands:
129
+ await _run_command(self.commands, command_name, args)
130
+ else:
131
+ self.__warning(f"Unknown command: '{command_name}'")
132
+ except EOFError:
133
+ self.__error("Input ended unexpectedly.")
134
+ break
135
+ except KeyboardInterrupt:
136
+ self.__error("Input interrupted.")
137
+ break
138
+ except HandlerClosed:
139
+ self.__info("Input Handler exited.")
140
+ break
141
+ self.is_running = False
142
+
139
143
  def register_default_commands(self):
140
144
  @self.command(name="help", description="Displays all the available commands")
141
145
  def help():
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cli_ih
3
- Version: 0.6.2
3
+ Version: 0.6.2.1
4
4
  Summary: A background command handler for python's command-line interface.
5
5
  Author-email: Hotment <michatchuplay@gmail.com>
6
6
  Requires-Python: >=3.8
@@ -57,6 +57,27 @@ handler.start()
57
57
  # > exit
58
58
  ```
59
59
 
60
+ ## New Async client
61
+ ```python
62
+ import asyncio
63
+ from cli_ih import AsyncInputHandler
64
+
65
+ print(cli_ih.__version__)
66
+
67
+ handler = AsyncInputHandler(cursor="> ")
68
+
69
+ @handler.command(name="greet", description="Greets the user. Usage: greet [name]")
70
+ async def greet(name, *args):
71
+ await asyncio.sleep(1)
72
+ print(f"Hello, {name}{" " if args else ""}{' '.join(args)}!")
73
+ # NEW
74
+ @handler.command(name="add", description="Performs the `+` operator on the first 2 arguments.")
75
+ async def add(a, b):
76
+ print(a+b)
77
+
78
+ asyncio.run(handler.start())
79
+ ```
80
+
60
81
  ## Additional Info
61
82
 
62
83
  - You can provide a valid logger `logger=logger` to the `InputHandler` to enable logging (this will be removed soon)
@@ -1,2 +1,3 @@
1
+ build
1
2
  cli_ih
2
3
  dist
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "cli_ih"
7
- version = "0.6.2"
7
+ version = "0.6.2.1"
8
8
  description = "A background command handler for python's command-line interface."
9
9
  readme = "README.md"
10
10
  requires-python = ">= 3.8"
File without changes
File without changes
File without changes
File without changes