From 97c32c82c257363de1c20e7d716cc54a510e5aa7 Mon Sep 17 00:00:00 2001 From: asdf Date: Fri, 29 Nov 2019 13:05:10 +1100 Subject: [PATCH] Proposed update use config module and pathlib --- config.py | 22 +++++++++++++++------- linkulator | 26 ++++++++++++++++---------- posts.py | 21 +++++++++++++-------- 3 files changed, 44 insertions(+), 25 deletions(-) diff --git a/config.py b/config.py index 147fe63..1c797b1 100644 --- a/config.py +++ b/config.py @@ -1,17 +1,25 @@ """stores configuration settings for linkulator""" from pathlib import Path -from glob import glob class DefaultConfigOptions: """data class for configuration options""" - all_data_dirs: glob - data_dir: Path - data_file: Path + all_homedir_pattern: str + datadir: str + datafile: str + ignorefile: str + my_datadir: Path + my_datafile: Path + my_ignorefile: Path CONFIG = DefaultConfigOptions() -CONFIG.all_data_dirs = glob("/home/*/.linkulator/linkulator.data") -CONFIG.data_dir = Path(Path.home() / ".linkulator") -CONFIG.data_file = Path(CONFIG.data_dir / "linkulator.data") +CONFIG.all_homedir_pattern = "/home/*/" +CONFIG.datadir = ".linkulator" +CONFIG.datafile = "linkulator.data" +CONFIG.ignorefile = "ignore" + +CONFIG.my_datadir = Path(Path.home() / CONFIG.datadir) +CONFIG.my_datafile = Path(CONFIG.my_datadir / CONFIG.datafile) +CONFIG.my_ignorefile = Path(CONFIG.my_datadir / CONFIG.ignorefile) diff --git a/linkulator b/linkulator index be89dd6..3109490 100755 --- a/linkulator +++ b/linkulator @@ -2,18 +2,20 @@ ## If this script contains bugs, blame cmccabe. -from datetime import datetime import getpass -import glob import os import signal import stat import subprocess import sys -from pathlib import Path -from shutil import which -import posts import time +from datetime import datetime +from glob import glob +from pathlib import Path, PurePath +from shutil import which + +import posts +from config import CONFIG as config username = getpass.getuser() @@ -54,9 +56,8 @@ ignore_names = [] # IGNORE NAMES. def parse_ignore_file(): global ignore_names - p = Path(Path.home(), ".linkulator/ignore") - if p.exists(): - s = p.read_text() + if config.my_ignorefile.exists(): + s = config.my_ignorefile.read_text() l = s.splitlines() for line in l: name = line.split(" ")[0] @@ -67,7 +68,10 @@ def build_menu(): ## WHENEVER THIS FUNCTION IS CALLED, THE DATA IS REFRESHED FROM FILES. SINCE ## DISK IO IS PROBABLY THE HEAVIEST PART OF THIS SCRIPT, DON'T DO THIS OFTEN. - linkulator_files = glob.glob(homedir_filepaths + ".linkulator/linkulator.data") + files_pattern = str( + PurePath(config.all_homedir_pattern).joinpath(config.datadir, config.datafile) + ) + linkulator_files = glob(files_pattern) if len(linkulator_files) == 0: print("It looks link there are no links yet. Run 'linkulator -p' to add one.") @@ -79,7 +83,9 @@ def build_menu(): linkulator_lines = [] for filename in linkulator_files: with open(filename) as f: - file_owner = filename.split("/")[-3] # EXTRACT USERNAME FROM PATH. + file_owner = PurePath( + filename + ).parent.parent.name # EXTRACT USERNAME FROM PATH. if file_owner in ignore_names: continue ## IGNORE NAMES IN ignore_file for line in f: diff --git a/posts.py b/posts.py index 21ff920..af45fdb 100644 --- a/posts.py +++ b/posts.py @@ -1,10 +1,11 @@ """handle the linkulator post process""" -import time -import readline -import os import getpass +import os +import readline import sys +import time + from config import CONFIG as config USERNAME = getpass.getuser() @@ -19,7 +20,9 @@ class Link: timestamp: str -def rlinput(prompt, prefill=""): +def rlinput(prompt, prefill="") -> str: + """Creates an input prompt with a prefilled entry, using the specified + prompt text and prefill text""" readline.set_startup_hook(lambda: readline.insert_text(prefill)) try: return input(prompt) @@ -52,12 +55,13 @@ def get_input(item: str, prefill) -> str: print('Pipes, "|", are illegal characters in Linkulator. Please try again.') -def save_link(link): - if os.path.exists(config.data_file): +def save_link(link) -> None: + """Saves the specified link data to the user's data file""" + if os.path.exists(config.my_datafile): append_write = "a" # append if already exists else: append_write = "w+" # make a new file if not - with open(config.data_file, append_write) as file: + with open(config.my_datafile, append_write) as file: file.write( link.timestamp + "||" @@ -71,7 +75,8 @@ def save_link(link): print("Link added!") -def post_link(prefill: str = ""): +def post_link(prefill: str = "") -> None: + """High-level """ print("Enter link information here. Leaving any field blank aborts post.") link = Link()