Merge branch 'homePaths' of cmccabe/linkulator2 into master
This commit is contained in:
commit
75540dc78b
22
config.py
22
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)
|
||||
|
|
26
linkulator
26
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:
|
||||
|
|
21
posts.py
21
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()
|
||||
|
|
Loading…
Reference in New Issue