-
Notifications
You must be signed in to change notification settings - Fork 45
Code dup #33
base: master
Are you sure you want to change the base?
Code dup #33
Conversation
Thank you for refactoring the code! |
Thanks, please note that this pr has |
If I have any problems with that on Mikrotik I think I change it only for Mikrotik. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello!
Some notes about the code
@selfuryon please have a look at the updated pr. |
Yeah, I switched to use poetry for building the project and for dependency management. You can add it to |
Thanks for new commits! I'll check it more deeply tomorrow! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some notes.
Today I'm going to perform full testing for all devices what I have.
self._conn.close() | ||
await self._conn.wait_closed() | ||
|
||
def send(self, cmd): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have some differences here.
Code here is def send(self, cmd)
, but in parent class is async def send(self, cmd)
. We need to do the same for removing the ambiguous.
I think that we can leave it a regular function like asyncssh's authors and asyncio's authors made it
netdev/connections/telnet.py
Outdated
|
||
def __check_session(self): | ||
if not self._stdin: | ||
raise RuntimeError("SSH session not started") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to correct it from "SSH session" to "Telnet Session"
netdev/connections/telnet.py
Outdated
""" Establish Telnet Connection """ | ||
self._logger.info("Host {}: telnet: Establishing Telnet Connection on port {}".format(self._host, self._port)) | ||
try: | ||
self._stdout, self._stdin = await asyncio.open_connection(self._host, self._port, family=0, flags=0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In SSH we have this code:
fut = asyncssh.connect(**self._conn_dict)
try:
self._conn = await asyncio.wait_for(fut, self._timeout)
So we can manage the timeout for this operation. But when we use telnet we can't. I think we should use the same approach like in SSH
self._conn.close() | ||
await self._conn.wait_closed() | ||
|
||
def send(self, cmd): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same thing like in here.
I'd prefer to leave it as a regular function, not a coroutine.
@@ -30,7 +29,7 @@ class ArubaAOS6(IOSLikeDevice): | |||
|
|||
For Aruba AOS 6 devices base_pattern is "(prompt) (\(.*?\))?\s?[#|>] | |||
""" | |||
logger.info("Host {}: Setting base prompt".format(self._host)) | |||
self._logger.info("Host {}: Setting base prompt".format(self.host)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is an interesting thing here. In BaseConnection
we use self._host
format but in BaseDevice
we use self.host
format. Maybe we should unify it?
If we want to use self.host
should we use the same format for port
, protocol
and so on?
|
||
self.current_terminal = None # State Machine for the current Terminal mode of the session | ||
|
||
self.enable_mode = EnableMode( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should transfer the default parameters like enter_command
, _priv_enter
and so on to EnableMode
and ConfigMode
classes.
This class shouldn't contain any related commands for different terminal modes.
I don't think that the user needs to correct this because they are constants for particular devices.
|
||
async def _session_preparation(self): | ||
await super()._session_preparation() | ||
await self.enable_mode() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be self.enable_mode.enter()
?
I saw that you use this code for BaseTerminalMode
:
async def __call__(self):
""" callable terminal to enter """
return await self.enter()
But I think it too ambiguously: I tried to search a function but I know that it's a class. it may confuse others
return "" | ||
|
||
# Send config commands | ||
output = await self.config_mode() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be self.config_mode.enter()
?
The same thing like here
output += await super().send_config_set(config_commands=config_commands) | ||
|
||
if exit_config_mode: | ||
output += await self.config_mode.exit() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See, we use directly self.config_mode.exit()
, but for entering we use self.config_mode()
instead of self.config_mode.enter()
.
It confuses.
netdev/vendors/devices/junos_like.py
Outdated
|
||
if exit_config_mode: | ||
output += await self.exit_config_mode() | ||
output += await self.config_mode.exit() | ||
|
||
output = self._normalize_linefeeds(output) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should transfer the normalize_linefeeds
function to BaseConnection
?
It's a very common function and it's related to a connection not to a device
please check updated pr |
I have some problems with HP Comware in testing. Need to go deeper |
Hello! As example:
So I decided to get your idea, take some techniques and class and make a refactor for the module in a slight different another way (see #34) |
make sense, looking forward to see the new structure. |
is there any way to get structured output using asyncssh netdev library? |
Description
code refactoring:
added new module
connection
:which has the protocol connection classes like
ssh
,telnet
andserial
.this approach is needed to split the business logic from external libraries , so we can easily change libraries without changing our code. also this is needed to add different connection types.
added
terminal_modes
module:objects that handle entering and exiting from different terminal modes like
config_mode
,enable_mode
... etc.this approach is to eliminate code duplication in the
Devices
classes.those are the major changes,
there are some changes on the device level, like adding methods, removing methods ... etc.
a new main method added to
BaseDevice
is_session_preparation
, which will be called to prepare each device session.new features: