Skip to content

Commit

Permalink
[D 0.2.8] implementing --infinite, closing 0.2.8
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentindiMeo committed Dec 31, 2023
1 parent 90c4dad commit 7fa2780
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 41 deletions.
3 changes: 2 additions & 1 deletion Exit.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ def gnUsage() -> None:
"\n -r, --allow-repetition Allow repetition of phrases if you ask for more than there are in the source file" \
"\n -o, --other-step Use the even-numbered phrase gaps as \"and\"s instead of commas (def: [odd-])" \
"\n -a, --alternate Alternate between \"and\"s, and emoji instead of commas (requires -e, def: False)" \
"\n -i, --ignore Ignore preferences (preferences.sav)" \
"\n -i, --infinite Toggle infinite mode" \
"\n --ignore Ignore preferences (preferences.sav)" \
"\n -S, --save Save preferences to file (preferences.sav)" \
"\n" \
"\n --verbose Toggle verbose mode" \
Expand Down
61 changes: 36 additions & 25 deletions Parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@
from random import randint as rand
from re import search as matches

from Utils import isIn, rremove, askConfirmation, askConfirmationNumber
from Utils import isIn, askConfirmation, askConfirmationNumber
from Types import Parameters
from Exit import exitCode, gnExit

FILE_AV: list[str] = ["--no-copy", "-n", "-e", "-s", "-w", "--allow-repetition", "--other-step"]
FILE_AV: list[str] = ["--no-copy", "-n", "-e", "-s", "-w", "-r", "-o", "-i"]
PAR_HAS_ARG: list[str] = ["b", "n", "s", "w"]
SAVE_FILEPATH: str = "preferences.sav"
DEF_NB_PHRASES: str = "?"
DEF_NB_DBOUND: str = "2,5"
DEF_NB_UBOUND: str = "999"
SAVE_FILEPATH: str = "preferences.sav"
DEF_COPY: bool = True
DEF_NB_PHRASES: str = "?"
DEF_NB_DBOUND: str = "2,5"
DEF_NB_UBOUND: str = "999"
DEF_EMOJI: bool = False
DEF_SOURCE: str = "./assets/source.log " # same as below
DEF_FOR_WHOM: str = " " # space to skip the CLI if the user used the default value as a parameter
DEF_SOURCE: str = "./assets/source.log " # same as below
DEF_FOR_WHOM: str = " " # space to skip the CLI if the user used the default value as a parameter
DEF_REPETITION: bool = False
DEF_STEP: bool = False
DEF_ALTERNATE: bool = False
DEF_COPY: bool = True
DEF_INFINITE: bool = False
DEF_VERBOSITY: bool = False
DEF_SAVE_PREF: bool = False
MAT_BOUNDED_INPUT: str = r"^[0-9]+,[0-9]+$"
Expand All @@ -46,14 +47,16 @@ def saveParameters(p: Parameters) -> None:

def fromCommandLine(p: Parameters, av: list[str] = []) -> Parameters:
copy: bool = p.copy
nbPhrases: str = p.nbPhrases
nbPhrases: str = p.nbPhrases
emoji: bool = p.emoji
source: str = p.source
forWhom: str = p.forWhom
source: str = p.source
forWhom: str = p.forWhom
allowRep: bool = p.allowRep
step: bool = p.step
alternate: bool = p.alternate
infinite: bool = p.infinite # TODO --infinite : infinite loop, continue with key press
times: str = p.times # TODO --times: play the goodnight x times (no infinite mode)
infinite: bool = p.infinite
delay: float = p.delay # TODO --delay: with --infinite, delay between loop iterations (in ms)
randomOrNumber: str = DEF_NB_PHRASES

if (copy == DEF_COPY and "--no-copy" not in av):
Expand Down Expand Up @@ -128,8 +131,14 @@ def fromCommandLine(p: Parameters, av: list[str] = []) -> Parameters:
alternate = True
if (p.verbose): print("\tAlternating set to {alternate}.")
else: print(MAT_DEFAULTING_N)
if (infinite == DEF_INFINITE and "-i" not in av and "--infinite" not in av):
confirmed: bool = askConfirmation("Put infinite mode on")
if (confirmed):
infinite = True
if (p.verbose): print("\tInfinite mode set to {infinite}.")
else: print(MAT_DEFAULTING_N)
newP = Parameters(c = copy, n = nbPhrases if nbPhrases != DEF_NB_PHRASES else DEF_NB_DBOUND, e = emoji, s = source.strip(), w = forWhom.strip(), \
r = allowRep, a = alternate, o = step, v = p.verbose, sav = p.saving)
r = allowRep, a = alternate, i = infinite, o = step, v = p.verbose, sav = p.saving)
if (p.saving): saveParameters(newP)
newP.pickNbPhrases()
return newP
Expand All @@ -138,7 +147,7 @@ def fromParameters(ac: int, av: list[str]) -> Parameters:
if (("-n" in av or "--nb-phrases" in av) and ("-b" in av or "--bounds" in av)):
print("Cannot use both -n/--nb-phrases and -b/--bounds at the same time."); gnExit(exitCode.ERR_INV_ARG)

def getSanitizedAv(ac: int, av: list[str]) -> (int, list[str]): # type: ignore
def getSanitizedAv(ac: int, av: list[str]) -> (int, list[str]):
newAv: list[str] = [av[0]]

def isMultiOptional(s: str) -> bool: return (len(s) > 2 and s[0] == '-' and s[1] != '-')
Expand All @@ -159,9 +168,8 @@ def isMultiOptional(s: str) -> bool: return (len(s) > 2 and s[0] == '-' and s[1]

if ("--default" in newAv): # --default ignores all other parameters
newAv.remove("--default"); newAv.insert(1, "--default")
# if newAv has -i or --ignore, move them to the end (because they instantly jump to CLI)
if ("--ignore" in newAv): newAv.remove("--ignore"); newAv.append("-i")
if ("-i" in newAv): newAv = rremove(newAv, "-i"); newAv.append("-i")
# if newAv has --ignore, move it to the end (because it instantly jumps to CLI)
if ("--ignore" in newAv): newAv.remove("--ignore"); newAv.append("--ignore")
return (len(newAv), newAv)
(ac, av) = getSanitizedAv(ac, av)

Expand All @@ -170,14 +178,16 @@ def isMultiOptional(s: str) -> bool: return (len(s) > 2 and s[0] == '-' and s[1]

extractP: Parameters = defaultParameters() if ("-i" in av) else fromFile(extraction = True)
copy: bool = extractP.copy
nbPhrases: str = extractP.nbPhrases
nbPhrases: str = extractP.nbPhrases
emoji: bool = extractP.emoji
source: str = extractP.source
forWhom: str = extractP.forWhom
source: str = extractP.source
forWhom: str = extractP.forWhom
allowRep: bool = extractP.allowRep
step: bool = extractP.step
alternate: bool = extractP.alternate
times: str = extractP.times
infinite: bool = extractP.infinite
delay: float = extractP.delay
saving: bool = extractP.saving

i: int = 1 # iterator needs tracking for jumping over PAR_HAS_ARG arguments
Expand Down Expand Up @@ -240,16 +250,17 @@ def isMultiOptional(s: str) -> bool: return (len(s) > 2 and s[0] == '-' and s[1]
case "-r" | "--allow-repetition": allowRep = True
case "-o" | "--other-step": step = True
case "-a" | "--alternate": alternate = True
case "-i" | "--ignore":
return fromCommandLine(Parameters(c=copy, n=nbPhrases, e=emoji, s=source, w=forWhom, r=allowRep, a=alternate, o=step, v=verbose, sav=saving))
case "-i" | "--infinite": infinite = True
case "--ignore":
return fromCommandLine(Parameters(c=copy, n=nbPhrases, e=emoji, s=source, w=forWhom, r=allowRep, o=step, a=alternate, i=infinite, v=verbose, sav=saving))
case "-S" | "--save": saving = True

case "--verbose": pass # still needs to be here to avoid an invalid parameter error
case "-h" | "--help": gnExit(exitCode.HELP)

case _: print(f"Invalid argument '{av[i]}'."); gnExit(exitCode.ERR_INV_ARG)
i += 1
return fromCommandLine(Parameters(c=copy, n=nbPhrases, e=emoji, s=source, w=forWhom, r=allowRep, a=alternate, o=step, v=verbose, sav=saving), av + FILE_AV)
return fromCommandLine(Parameters(c=copy, n=nbPhrases, e=emoji, s=source, w=forWhom, r=allowRep, o=step, a=alternate, i=infinite, v=verbose, sav=saving), av + FILE_AV)

def fromFile(savefile: str = SAVE_FILEPATH, extraction: bool = False, noParam: bool = False) -> Parameters:
p: Parameters = defaultParameters()
Expand Down Expand Up @@ -282,7 +293,7 @@ def fromFile(savefile: str = SAVE_FILEPATH, extraction: bool = False, noParam: b

def defaultParameters(fromParameter: bool = False) -> Parameters:
p = Parameters(c = DEF_COPY, n = DEF_NB_DBOUND if fromParameter else DEF_NB_PHRASES, e = DEF_EMOJI, s = DEF_SOURCE, w = DEF_FOR_WHOM, \
r = DEF_REPETITION, a = DEF_ALTERNATE, o = DEF_STEP, v = DEF_VERBOSITY, sav = DEF_SAVE_PREF)
r = DEF_REPETITION, o = DEF_STEP, a = DEF_ALTERNATE, i = DEF_INFINITE, v = DEF_VERBOSITY, sav = DEF_SAVE_PREF)
if (fromParameter): p.pickNbPhrases()
p.source = p.source.strip(); p.forWhom = p.forWhom.strip()
return p
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
 
<div align="center">
<a href="#top"><img alt="Python version" src="https://img.shields.io/badge/Python-3.10+-blue?logo=python" /></a>
<a href="#card_file_box-change-log"><img alt="Last version released" src="https://img.shields.io/badge/release-v0.2.7-blue?logo=windows-terminal" /></a>
<a href="#card_file_box-change-log"><img alt="Last version released" src="https://img.shields.io/badge/release-v0.2.8-blue?logo=windows-terminal" /></a>
<a href="https://github.com/QuentindiMeo/goodnight.py/commits/main"><img alt="GitHub last commit" src="https://img.shields.io/github/last-commit/QuentindiMeo/goodnight.py?color=blueviolet&logo=clarifai" /></a>
<a href="#top"><img alt="Lines of code" src="https://tokei.rs/b1/github/QuentindiMeo/goodnight.py?category=code" /></a>
<!-- <img alt="Lines of code" src="https://img.shields.io/tokei/lines/github/QuentindiMeo/goodnight.py?color=green&logo=haveibeenpwned" /> -->
Expand Down Expand Up @@ -72,7 +72,8 @@ python goodnight.py [OPTIONS]
-r, --allow-repetition Allow repetition of phrases if you ask for more than there are in the source file
-o, --other-step Use the even-numbered phrase gaps as "and"s instead of commas (def: odd-)
-a, --alternate Alternate between "and"s, and emoji instead of commas (requires -e, def: False)
-i, --ignore Ignore preferences (preferences.sav file)
-i, --infinite Toggle infinite mode
--ignore Ignore preferences (preferences.sav file)
-S, --save Save preferences to file (preferences.sav)
--verbose Toggle verbose mode
Expand Down Expand Up @@ -138,8 +139,9 @@ This program was tested on and designed for WSL2 and Ubuntu 22.04.
- ***[DEV 0.2.5]** Nov 13 2023* - Adding --other-step; updating usage/help; setting 999 as max r/n bound
- ***[DEV 0.2.6]** Nov 26 2023* - Adding --save; improving largely code efficiency and structure for maintainability
- ***[DEV 0.2.7]** Dec 13 2023* - Adding --no-copy, preparing --alternate; deprecating --isave
- ***[DEV 0.2.8]** Dec ?? 2023* - Adding --alternate, --infinite; creating Goodnight class
- ***[REL 1.0.0]** Dec ?? 2023* - First release; major README update
- ***[DEV 0.2.8]** Dec 31 2023* - Adding --alternate, --infinite; creating Goodnight class
- ***[DEV 0.2.9]** Jan ?? 2024* - Adding --delay, --times
- ***[REL 1.0.0]** Jan ?? 2024* - First release; major README update

<br />

Expand Down
27 changes: 16 additions & 11 deletions Types.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,25 @@
WeightedList: TypeAlias = (ElementList, int) # a list of P/E/N elements and its weight
UnweightedList: TypeAlias = list[ElementList] # an unweighted list of P/E/N elements

DEF_NB_PHRASES: str = "?"
DEF_NB_DBOUND: str = "2,5"
DEF_NB_UBOUND: str = "999"
DEF_COPY: bool = True
DEF_NB_PHRASES: str = "?"
DEF_NB_DBOUND: str = "2,5"
DEF_NB_UBOUND: str = "999"
DEF_EMOJI: bool = False
DEF_SOURCE: str = "./assets/source.log " # same as below
DEF_FOR_WHOM: str = " " # space to skip the CLI if the user used the default value as a parameter
DEF_SOURCE: str = "./assets/source.log " # same as below
DEF_FOR_WHOM: str = " " # space to skip the CLI if the user used the default value as a parameter
DEF_REPETITION: bool = False
DEF_STEP: bool = False
DEF_ALTERNATE: bool = False
DEF_COPY: bool = True
DEF_INFINITE: bool = False
DEF_VERBOSITY: bool = False
DEF_SAVE_PREF: bool = False

class Goodnight:
def __init__(self, txt: str) -> None:
def __init__(self, txt: str, t: int = 1) -> None:
self.txt = txt
self.step = False
self.times = t

class Parameters:
def pickNbPhrases(self) -> None:
Expand Down Expand Up @@ -61,9 +63,11 @@ def __str__(self) -> str:
f"\tverbose mode: {self.verbose}\n" \
f"\tsaving preferences: {self.saving}"

def __init__(self, n: str, e: bool = DEF_EMOJI, s: str = DEF_SOURCE, w: str = DEF_FOR_WHOM, \
r: bool = DEF_REPETITION, o: bool = DEF_STEP, a: bool = DEF_ALTERNATE, c: bool = DEF_COPY, \
def __init__(self, c: bool = DEF_COPY, n: str = DEF_NB_PHRASES, e: bool = DEF_EMOJI, s: str = DEF_SOURCE, w: str = DEF_FOR_WHOM, \
r: bool = DEF_REPETITION, o: bool = DEF_STEP, a: bool = DEF_ALTERNATE, i: bool = DEF_INFINITE, \
v: bool = DEF_VERBOSITY, sav: bool = DEF_SAVE_PREF) -> None:
self.copy = c

self.nbPhrases = n
self.emoji = e
self.source = s
Expand All @@ -72,12 +76,13 @@ def __init__(self, n: str, e: bool = DEF_EMOJI, s: str = DEF_SOURCE, w: str = DE
self.allowRep = r
self.step = o
self.alternate = a
self.copy = c
self.infinite = i

self.verbose = v
self.saving = sav

self.infinite = False
self.times = 1
self.delay = 0

class Contents:
def pickNick(self, p: Parameters) -> str:
Expand Down

0 comments on commit 7fa2780

Please # to comment.