Skip to content

Commit

Permalink
Add export feature, fix naming scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
Raymo111 committed Aug 18, 2023
1 parent 0a6985c commit 0b4c826
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 16 deletions.
38 changes: 38 additions & 0 deletions src/nexus/Freqlog/Freqlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,25 @@ def list_words(self, limit: int = -1, sort_by: WordMetadataAttr = WordMetadataAt
logging.info(f"Listing words, limit {limit}, sort_by {sort_by}, reverse {reverse}, case {case.name}")
return self.backend.list_words(limit, sort_by, reverse, case)

def export_words_to_csv(self, export_path: str, limit: int = -1, sort_by: WordMetadataAttr = WordMetadataAttr.word,
reverse: bool = False, case: CaseSensitivity = CaseSensitivity.INSENSITIVE) -> int:
"""
Export words in the store
:param export_path: Path to csv file to export to
:param limit: Maximum number of words to return
:param sort_by: Attribute to sort by: word, frequency, last_used, average_speed
:param reverse: Reverse sort order
:param case: Case sensitivity
:return: Number of words exported
"""
logging.info(f"Exporting words, limit {limit}, sort_by {sort_by}, reverse {reverse}, case {case.name}")
words = self.backend.list_words(limit, sort_by, reverse, case)
with open(export_path, "w") as f:
f.write(",".join(filter(lambda k: not k.startswith("_"), WordMetadataAttr.__dict__.keys())) + "\n")
f.write("\n".join(map(lambda w: ",".join(map(str, w.__dict__.values())), words)))
logging.info(f"Exported {len(words)} words to {export_path}")
return len(words)

def list_chords(self, limit: int, sort_by: ChordMetadataAttr,
reverse: bool, case: CaseSensitivity) -> list[ChordMetadata]:
"""
Expand All @@ -283,6 +302,25 @@ def list_chords(self, limit: int, sort_by: ChordMetadataAttr,
logging.info(f"Listing chords, limit {limit}, sort_by {sort_by}, reverse {reverse}, case {case.name}")
return self.backend.list_chords(limit, sort_by, reverse, case)

def export_chords_to_csv(self, export_path: str, limit: int, sort_by: ChordMetadataAttr,
reverse: bool, case: CaseSensitivity) -> int:
"""
Export chords in the store
:param export_path: Path to csv file to export to
:param limit: Maximum number of chords to return
:param sort_by: Attribute to sort by: chord, frequency, last_used, average_speed
:param reverse: Reverse sort order
:param case: Case sensitivity
:return: Number of chords exported
"""
logging.info(f"Exporting chords, limit {limit}, sort_by {sort_by}, reverse {reverse}, case {case.name}")
chords = self.backend.list_chords(limit, sort_by, reverse, case)
with open(export_path, "w") as f:
f.write(",".join(ChordMetadataAttr.__dict__.keys()) + "\n")
f.write("\n".join(map(lambda c: ",".join(c.__dict__.values()), chords)))
logging.info(f"Exported {len(chords)} chords to {export_path}")
return len(chords)

def list_banned_words(self, limit: int = -1, sort_by: BanlistAttr = BanlistAttr.word, reverse: bool = False) \
-> tuple[set[BanlistEntry], set[BanlistEntry]]:
"""
Expand Down
28 changes: 20 additions & 8 deletions src/nexus/GUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from PySide6.QtCore import Qt
from PySide6.QtWidgets import QApplication, QPushButton, QStatusBar, QTableWidget, QTableWidgetItem, QMainWindow, \
QDialog
QDialog, QFileDialog

from nexus.Freqlog import Freqlog
from nexus.ui.BanlistDialog import Ui_BanlistDialog
Expand Down Expand Up @@ -55,9 +55,10 @@ def __init__(self, args: argparse.Namespace):
self.window = MainWindow()

# Components
self.start_stop_button: QPushButton = self.window.startStop
self.refresh_button: QPushButton = self.window.refresh
self.banlist_button: QPushButton = self.window.banlist
self.start_stop_button: QPushButton = self.window.startStopButton
self.refresh_button: QPushButton = self.window.refreshButton
self.banlist_button: QPushButton = self.window.banlistButton
self.export_button: QPushButton = self.window.exportButton
self.chentry_table: QTableWidget = self.window.chentryTable
self.chord_table: QTableWidget = self.window.chordTable
self.statusbar: QStatusBar = self.window.statusbar
Expand All @@ -66,6 +67,7 @@ def __init__(self, args: argparse.Namespace):
self.start_stop_button.clicked.connect(self.start_stop)
self.refresh_button.clicked.connect(self.refresh)
self.banlist_button.clicked.connect(self.show_banlist)
self.export_button.clicked.connect(self.export)

self.freqlog: Freqlog | None = None # for logging
self.temp_freqlog: Freqlog = Freqlog(args.freq_log_path, loggable=False) # for other operations
Expand Down Expand Up @@ -214,16 +216,26 @@ def remove_banword():
bl_dialog.banlistTable.item(row.row(), 0).text()
] = (CaseSensitivity.SENSITIVE if bl_dialog.banlistTable.item(row.row(), 2).text() == "Sensitive"
else CaseSensitivity.INSENSITIVE)
confDialog = ConfirmDialog()
confDialog.confirmText.setText(f"Unban {len(selected_words)} word{'s' if len(selected_words) > 1 else ''}?")
confDialog.buttonBox.accepted.connect(lambda: self.temp_freqlog.unban_words(selected_words))
confDialog.exec()
conf_dialog = ConfirmDialog()
conf_dialog.confirmText.setText(
f"Unban {len(selected_words)} word{'s' if len(selected_words) > 1 else ''}?")
conf_dialog.buttonBox.accepted.connect(lambda: self.temp_freqlog.unban_words(selected_words))
conf_dialog.exec()
refresh_banlist()

bl_dialog.addButton.clicked.connect(banword)
bl_dialog.removeButton.clicked.connect(remove_banword)
bl_dialog.exec()

def export(self):
"""Controller for export button"""
filename = QFileDialog.getSaveFileName(self.window, "Export to CSV", "", "CSV (*.csv)")[0]
if filename:
if not filename.endswith(".csv"):
filename += ".csv"
num_exported = self.temp_freqlog.export_words_to_csv(filename)
self.statusbar.showMessage(f"Exported {num_exported} words to {filename}")

def exec(self):
"""Start the GUI"""
self.window.show()
Expand Down
12 changes: 10 additions & 2 deletions src/nexus/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ def main():
parser_words = subparsers.add_parser("words", help="Get list of freqlogged words",
parents=[log_arg, path_arg, case_arg, num_arg])
parser_words.add_argument("word", help="Word(s) to get data of", nargs="*")
parser_words.add_argument("-e", "--export", help="Export all freqlogged words as csv to file"
"(ignores word args)", required=False)
parser_words.add_argument("-s", "--sort-by", default=WordMetadataAttr.frequency.name,
help=f"Sort by (default: {WordMetadataAttr.frequency.name})",
choices={attr.name for attr in WordMetadataAttr})
Expand All @@ -80,6 +82,8 @@ def main():
# parser_chords = subparsers.add_parser("chords", help="Get list of stored freqlogged chords",
# parents=[log_arg, path_arg, num_arg])
# parser_chords.add_argument("chord", help="Chord(s) to get data of", nargs="*")
# parser_chords.add_argument("-e", "--export", help="Export freqlogged chords as csv to file"
# "(ignores chord args)", required=False)
# parser_chords.add_argument("-s", "--sort-by", default=ChordMetadataAttr.frequency.name,
# help=f"Sort by (default: {ChordMetadataAttr.frequency.name})")
# choices={attr.name for attr in ChordMetadataAttr})
Expand Down Expand Up @@ -194,7 +198,9 @@ def main():
exit_code = 6
# TODO: pretty print
case "words": # get words
if len(args.word) == 0: # all words
if args.export: # export words
freqlog.export_words_to_csv(args.export)
elif len(args.word) == 0: # all words
res = freqlog.list_words(args.num, WordMetadataAttr[args.sort_by], args.order == Order.DESCENDING,
CaseSensitivity[args.case])
if len(res) == 0:
Expand All @@ -218,7 +224,9 @@ def main():
reverse=(args.order == Order.DESCENDING)):
print(word)
case "chords": # get chords
if len(args.chord) == 0: # all chords
if args.export: # export chords
freqlog.export_chords_to_csv(args.export)
elif len(args.chord) == 0: # all chords
res = freqlog.list_chords(args.num, ChordMetadataAttr[args.sort_by], args.order == Order.DESCENDING,
CaseSensitivity[args.case])
if len(res) == 0:
Expand Down
19 changes: 13 additions & 6 deletions ui/main.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>834</width>
<height>515</height>
<width>816</width>
<height>519</height>
</rect>
</property>
<property name="windowTitle">
Expand All @@ -22,9 +22,9 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout" stretch="0,1">
<item>
<layout class="QHBoxLayout" name="toolbar" stretch="1,0,0">
<layout class="QHBoxLayout" name="toolbar" stretch="1,0,0,0">
<item>
<widget class="QPushButton" name="startStop">
<widget class="QPushButton" name="startStopButton">
<property name="styleSheet">
<string notr="true">background-color: green</string>
</property>
Expand All @@ -34,19 +34,26 @@
</widget>
</item>
<item>
<widget class="QPushButton" name="refresh">
<widget class="QPushButton" name="refreshButton">
<property name="text">
<string>Refresh</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="banlist">
<widget class="QPushButton" name="banlistButton">
<property name="text">
<string>Banlist</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="exportButton">
<property name="text">
<string>Export</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
Expand Down

1 comment on commit 0b4c826

@Raymo111
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implements #28

Please sign in to comment.