Skip to content

Commit

Permalink
Add python logging to CLI. (#38)
Browse files Browse the repository at this point in the history
Signed-off-by: Slendi <[email protected]>
  • Loading branch information
xslendix authored Aug 21, 2023
1 parent 25ebceb commit b819dc3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 34 deletions.
49 changes: 25 additions & 24 deletions src/nexus/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,34 +136,35 @@ def main():
with open(args.freq_log_path, "a"):
pass
except OSError as e:
print(f"Error: {e}")
logging.error(e)
exit_code = 4
if args.new_word_threshold <= 0:
print("Error: New word threshold must be greater than 0")
logging.error("New word threshold must be greater than 0")
exit_code = 3
if args.chord_char_threshold <= 0:
print("Error: Chord character threshold must be greater than 0")
logging.error("Chord character threshold must be greater than 0")
exit_code = 3
if len(args.allowed_keys_in_chord) == 0:
print("Error: Must allow at least one key in chord")
logging.error("Must allow at least one key in chord")
exit_code = 3
case "words":
if args.num < 0:
print("Error: Number of words must be >= 0")
logging.error("Number of words must be >= 0")
exit_code = 3
case "chords":
if args.num < 0:
print("Error: Number of chords must be >= 0")
logging.error("Number of chords must be >= 0")
exit_code = 3
case "banlist":
if args.num < 0:
print("Error: Number of words must be >= 0")
logging.error("Number of words must be >= 0")
exit_code = 3

# Parse commands
if args.command == "stoplog": # stop freqlogging
# Kill freqlogging process
print("This feature hasn't been implemented. To stop freqlogging gracefully, simply kill the process (Ctrl-c)")
logging.warning("This feature hasn't been implemented." +
"To stop freqlogging gracefully, simply kill the process (Ctrl-c)")
exit_code = 100
# TODO: implement

Expand All @@ -184,10 +185,10 @@ def main():
case "checkword": # check if word is banned
for word in args.word:
if freqlog.check_banned(word, CaseSensitivity[args.case]):
print(f"'{word}' is banned")
logging.info(f"'{word}' is banned")
exit_code = 1
else:
print(f"'{word}' is not banned")
logging.info(f"'{word}' is not banned")
case "banword": # ban word
for word in args.word:
if not freqlog.ban_word(word, CaseSensitivity[args.case]):
Expand All @@ -204,65 +205,65 @@ def main():
res = freqlog.list_words(args.num, WordMetadataAttr[args.sort_by], args.order == Order.DESCENDING,
CaseSensitivity[args.case])
if len(res) == 0:
print("No words in freqlog. Start typing!")
logging.info("No words in freqlog. Start typing!")
else:
for word in res:
print(word)
logging.info(word)
else: # specific words
if args.num:
print("Warning: -n/--num argument ignored when specific words are given")
logging.warning("-n/--num argument ignored when specific words are given")
words: list[WordMetadata] = []
for word in args.word:
res = freqlog.get_word_metadata(word, CaseSensitivity[args.case])
if res is None:
print(f"Word '{word}' not found")
logging.warning(f"Word '{word}' not found")
exit_code = 5
else:
words.append(res)
if len(words) > 0:
for word in sorted(words, key=lambda x: getattr(x, args.sort_by),
reverse=(args.order == Order.DESCENDING)):
print(word)
logging.info(word)
case "chords": # get 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:
print("No chords in freqlog. Start chording!")
logging.info("No chords in freqlog. Start chording!")
else:
for chord in res:
print(chord)
logging.info(chord)
else: # specific chords
if args.num:
print("Warning: -n/--num argument ignored when specific chords are given")
logging.warning("-n/--num argument ignored when specific chords are given")
chords: list[ChordMetadata] = []
for chord in args.chord:
res = freqlog.get_chord_metadata(chord)
if res is None:
print(f"Chord '{chord}' not found")
logging.warning(f"Chord '{chord}' not found")
exit_code = 5
else:
chords.append(res)
if len(chords) > 0:
for chord in sorted(chords, key=lambda x: getattr(x, args.sort_by),
reverse=(args.order == Order.DESCENDING)):
print(chord)
logging.info(chord)
case "banlist": # get banned words
banlist_case, banlist_caseless = freqlog.list_banned_words(args.num, BanlistAttr[args.sort_by],
args.order == Order.DESCENDING)
if len(banlist_case) == 0 and len(banlist_caseless) == 0:
print("No banned words")
logging.info("No banned words")
else:
for entry in banlist_caseless:
entry.word += "*"
print("Banned words (* denotes case-insensitive entries):")
logging.info("Banned words (* denotes case-insensitive entries):")
for entry in sorted(banlist_case | banlist_caseless, key=lambda x: getattr(x, args.sort_by),
reverse=(args.order == Order.DESCENDING)):
print(entry)
logging.info(entry)
except NotImplementedError:
print(f"Error: The '{args.command}' command has not been implemented yet")
logging.error(f"The '{args.command}' command has not been implemented yet")
exit_code = 100
sys.exit(exit_code)

Expand Down
19 changes: 9 additions & 10 deletions translations/i18n_en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,56 +83,55 @@
<context>
<name>GUI</name>
<message>
<location filename="../src/nexus/GUI.py" line="108"/>
<location filename="../src/nexus/GUI.py" line="140"/>
<location filename="../src/nexus/GUI.py" line="142"/>
<source>GUI</source>
<comment>Start logging</comment>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/nexus/GUI.py" line="110"/>
<location filename="../src/nexus/GUI.py" line="111"/>
<source>GUI</source>
<comment>Starting...</comment>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/nexus/GUI.py" line="121"/>
<location filename="../src/nexus/GUI.py" line="122"/>
<source>GUI</source>
<comment>Stop logging</comment>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/nexus/GUI.py" line="125"/>
<location filename="../src/nexus/GUI.py" line="127"/>
<source>GUI</source>
<comment>Logging started</comment>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/nexus/GUI.py" line="144"/>
<location filename="../src/nexus/GUI.py" line="147"/>
<source>GUI</source>
<comment>Logging stopped</comment>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/nexus/GUI.py" line="177"/>
<location filename="../src/nexus/GUI.py" line="180"/>
<source>GUI</source>
<comment>Loaded {} freqlogged words</comment>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/nexus/GUI.py" line="240"/>
<location filename="../src/nexus/GUI.py" line="243"/>
<source>GUI</source>
<comment>Unban {} words</comment>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/nexus/GUI.py" line="242"/>
<location filename="../src/nexus/GUI.py" line="245"/>
<source>GUI</source>
<comment>Unban one word</comment>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/nexus/GUI.py" line="259"/>
<location filename="../src/nexus/GUI.py" line="262"/>
<source>GUI</source>
<comment>Exported {} words to {}</comment>
<translation type="unfinished"></translation>
Expand Down

0 comments on commit b819dc3

Please sign in to comment.