Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[enhancement] Refactored parts of CommandCompleter #67

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions smbclientng/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,6 @@ def main():
if options.debug:
print("[debug] Exiting the console.")


if __name__ == "__main__":
main()
75 changes: 29 additions & 46 deletions smbclientng/core/CommandCompleter.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,52 +389,42 @@ def complete(self, text, state):
if '\\' in remainder.strip() or '/' in remainder.strip():
path = remainder.strip().replace(ntpath.sep, '/')
path = '/'.join(path.split('/')[:-1])

# Get remote directory contents
directory_contents = self.smbSession.list_contents(path=path).items()

#
matching_entries = []
for _, entry in directory_contents:
if entry.is_directory() and entry.get_longname() not in [".",".."]:
if len(path) != 0:
matching_entries.append(path + '/' + entry.get_longname() + '/')
else:
matching_entries.append(entry.get_longname() + '/')

# Add quoting for shlex
matching_entries = [shlex.quote(s) for s in matching_entries]

self.matches += [
command + " " + s
for s in matching_entries
if s.lower().startswith(remainder.lower())
]
#
for s in matching_entries:
if s.lower().startswith(remainder.lower()) or shlex.quote(s).lower().startswith(remainder.lower()):
self.matches.append(command + " " + shlex.quote(s))

# Autocomplete file
if "remote_file" in self.commands[command]["autocomplete"]:
# Choose remote file
path = ""
if '\\' in remainder.strip() or '/' in remainder.strip():
path = remainder.strip().replace(ntpath.sep, '/')
path = '/'.join(path.split('/')[:-1])

path = '/'.join(path.split('/')[:-1])
# Get remote directory contents
directory_contents = self.smbSession.list_contents(path=path).items()

#
matching_entries = []
for _, entry in directory_contents:
if not entry.is_directory() and entry.get_longname() not in [".",".."]:
if (not entry.is_directory()) and entry.get_longname() not in [".",".."]:
if len(path) != 0:
matching_entries.append(path + '/' + entry.get_longname())
else:
matching_entries.append(entry.get_longname())

# Add quoting for shlex
matching_entries = [shlex.quote(s) for s in matching_entries]

self.matches += [
command + " " + s
for s in matching_entries
if s.lower().startswith(remainder.lower())
]
#
for s in matching_entries:
if s.lower().startswith(remainder.lower()) or shlex.quote(s).lower().startswith(remainder.lower()):
self.matches.append(command + " " + shlex.quote(s))

# Autocomplete local_directory
if "local_directory" in self.commands[command]["autocomplete"]:
Expand All @@ -443,24 +433,21 @@ def complete(self, text, state):
if os.path.sep in remainder.strip():
path = path.split(os.path.sep)[:-1]
path = os.path.sep.join(path)

# Current dir
if len(path.strip()) == 0:
path = "."

#
directory_contents = os.listdir(path=path + os.path.sep)
matching_entries = []
for entry in directory_contents:
if entry not in [".",".."]:
entry_path = path + os.path.sep + entry
if os.path.isdir(entry_path):
matching_entries.append(entry_path + os.path.sep)

self.matches += [
command + " " + s
for s in matching_entries
if s.startswith(remainder)
]
#
for s in matching_entries:
if s.lower().startswith(remainder.lower()) or shlex.quote(s).lower().startswith(remainder.lower()):
self.matches.append(command + " " + shlex.quote(s))

# Autocomplete local_file
if "local_file" in self.commands[command]["autocomplete"]:
Expand All @@ -469,32 +456,28 @@ def complete(self, text, state):
if os.path.sep in remainder.strip():
path = path.split(os.path.sep)[:-1]
path = os.path.sep.join(path)

# Current dir
if len(path.strip()) == 0:
path = "."

directory_contents = os.listdir(path=path + os.path.sep)
#
directory_contents = os.listdir(path=(path + os.path.sep))
matching_entries = []
for entry in directory_contents:
if entry not in [".",".."]:
entry_path = path + os.path.sep + entry
if not os.path.isdir(entry_path):
matching_entries.append(entry_path)

self.matches += [
command + " " + s
for s in matching_entries
if s.startswith(remainder)
]
#
for s in matching_entries:
if s.lower().startswith(remainder.lower()) or shlex.quote(s).lower().startswith(remainder.lower()):
self.matches.append(command + " " + shlex.quote(s))

else:
# Generic case for subcommands
self.matches += [
command + " " + s
for s in self.commands[command]["subcommands"]
if s.startswith(remainder)
]
for s in self.commands[command]["subcommands"]:
if s.startswith(remainder):
self.matches.append(command + " " + s)

else:
# Unknown subcommand, skipping autocomplete
pass
Expand Down
Loading