Skip to content

Commit

Permalink
Fix console error with compressed indexes, closes #347
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmezzetti committed Sep 27, 2022
1 parent fee71cf commit fa5ad86
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 41 deletions.
96 changes: 58 additions & 38 deletions src/python/txtai/console/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,24 +99,6 @@ def config(self):

self.console.print(self.app.config)

def load(self, path):
"""
Processes .load command.
Args:
path: path to configuration
"""

if os.path.isfile(path):
self.console.print(f"Loading application {path}")
self.app = Application(path)
else:
self.console.print(f"Loading index {path}")

# Load embeddings index
self.app = Embeddings()
self.app.load(path)

def highlight(self, command):
"""
Processes .highlight command.
Expand All @@ -141,32 +123,23 @@ def limit(self, command):
self.vlimit = int(action)
self.console.print(f"Set limit to {self.vlimit}")

def workflow(self, command):
def load(self, path):
"""
Processes .workflow command.
Processes .load command.
Args:
command: command line
"""

command = shlex.split(command)
if isinstance(self.app, Application):
self.console.print(list(self.app.workflow(command[1], command[2:])))

def split(self, command, default=None):
path: path to configuration
"""
Splits command by whitespace.

Args:
command: command line
default: default command action
Returns:
command action
"""
if self.isyaml(path):
self.console.print(f"Loading application {path}")
self.app = Application(path)
else:
self.console.print(f"Loading index {path}")

values = command.split(" ", 1)
return values if len(values) > 1 else (command, default)
# Load embeddings index
self.app = Embeddings()
self.app.load(path)

def search(self, query):
"""
Expand Down Expand Up @@ -205,6 +178,53 @@ def search(self, query):
# Print table to console
self.console.print(table)

def workflow(self, command):
"""
Processes .workflow command.
Args:
command: command line
"""

command = shlex.split(command)
if isinstance(self.app, Application):
self.console.print(list(self.app.workflow(command[1], command[2:])))

def isyaml(self, path):
"""
Checks if file at path is a valid YAML file.
Args:
path: file to check
Returns:
True if file is valid YAML, False otherwise
"""

if os.path.exists(path) and os.path.isfile(path):
try:
return Application.read(path)
# pylint: disable=W0702
except:
pass

return False

def split(self, command, default=None):
"""
Splits command by whitespace.
Args:
command: command line
default: default command action
Returns:
command action
"""

values = command.split(" ", 1)
return values if len(values) > 1 else (command, default)

def render(self, result, column, value):
"""
Renders a search result column value.
Expand Down
8 changes: 5 additions & 3 deletions test/python/testconsole.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ def setUpClass(cls):
with open(cls.apppath, "w", encoding="utf-8") as out:
out.write(APPLICATION % cls.embedpath)

# Save index
# Save index as uncompressed and compressed
cls.embeddings.save(cls.embedpath)
cls.embeddings.save(f"{cls.embedpath}.tar.gz")

# Create console
cls.console = Console(cls.embedpath)
Expand All @@ -66,7 +67,7 @@ def testApplication(self):
Test application
"""

self.assertIn("console.yml", self.command(f".load {self.apppath}"))
self.assertNotIn("Traceback", self.command(f".load {self.apppath}"))
self.assertIn("1", self.command(".limit 1"))
self.assertIn("Maine man wins", self.command("feel good story"))

Expand All @@ -82,7 +83,8 @@ def testEmbeddings(self):
Test embeddings index
"""

self.assertIn("embeddings", self.command(f".load {self.embedpath}"))
self.assertNotIn("Traceback", self.command(f".load {self.embedpath}.tar.gz"))
self.assertNotIn("Traceback", self.command(f".load {self.embedpath}"))
self.assertIn("1", self.command(".limit 1"))
self.assertIn("Maine man wins", self.command("feel good story"))

Expand Down

0 comments on commit fa5ad86

Please sign in to comment.