Commit 4fc899fd authored by Lysander Trischler's avatar Lysander Trischler
Browse files

Load config file

parent 124e494b
......@@ -39,8 +39,15 @@ conversations, a ` <>`_ extension. It is also
possible to post twts or replies. The heavy lifting is still done by the
reference implementation, especially fetching and storing the *twtxt.txt* files
in a local cache. ``tt`` just uses ``twtxt``'s *~/.config/twtxt/cache.db* for
all the twts to show. In a second cache file next to it – highly creatively
named *~/.config/twtxt/cache2.db* – the read status of each twt is recorded.
all the twts to show. In a data file *~/.local/share/twtxt/data.db* – the read
status of each twt is recorded. Both files can be changed in the configuration
file *~/.config/twtxt/config* with `twtxt.cachefile` and `twtxt.datafile`. The
data file used to be at *~/.config/twtxt/cache2.db*. The following settings
from the original twtxt configuration file are supported:
* `twtxt.nick`
* `twtxt.twtfile`
* `twtxt.twturl`
Hell no, you say!? Oh yes, you're absolutely right, it's a terrible mess. But
the good news is, it will all change in the future™. I plan on refactoring it
......@@ -16,7 +16,8 @@ except ImportError:
pyperclip = None
manager = twtxtmanager.TwtxtManager()
config = twtxtmanager.Config.load_file()
manager = twtxtmanager.TwtxtManager(config)
class TwtsListBox(widgets.VimListBox):
......@@ -3,19 +3,23 @@ Manage twts.
import collections
import configparser
import os
import os.path
import shelve
import twtxt.models
import twtxt.parser
import twtxthash
import twtxtparser
import xdg
class TwtxtManager:
TWTXT_TXT = '/home/lyse/.config/twtxt/twtxt.txt'
def __init__(self):
def __init__(self, config):
self.config = config
self._cache = None
self._cache2 = None
self._data = None
self.conversation_tree = []
self.missing_conversation_root_count = 0
self.twts_count = 0
......@@ -39,12 +43,13 @@ class TwtxtManager:
self.unread_twts_count = 0
self._cache ='/home/lyse/.config/twtxt/cache')
own_source = twtxt.models.Source(nick="lyse", url="")
with open(self.TWTXT_TXT, 'r', encoding='utf-8') as fd:
self._cache =
own_source = twtxt.models.Source(nick=self.config.nick, url=self.config.twturl)
with open(self.config.twtfile, 'r', encoding='utf-8') as fd:
own_twts = twtxt.parser.parse_tweets(fd.readlines(), own_source)
self._cache[own_source.url] = {'tweets': own_twts}
self._cache2 ='/home/lyse/.config/twtxt/cache2')
os.makedirs(os.path.dirname(self.config.datafile), exist_ok=True)
self._data =
for url, feed in self._cache.items():
if url == "last_update":
......@@ -65,10 +70,10 @@ class TwtxtManager:
twt.replies = []
entry = self._cache2.get(twt.hash)
entry = self._data.get(twt.hash) = bool(entry and entry.get("read", False))
if not
entry = self._cache2.get(twt.old_hash)
entry = self._data.get(twt.old_hash) = bool(entry and entry.get("read", False))
self.read_twts_count += 1
......@@ -110,7 +115,7 @@ class TwtxtManager:
twt.subject = None
twt.tokens = []
twt.replies = replies
entry = self._cache2.get(hash)
entry = self._data.get(hash) = entry and entry.get("read", False)
twt.missing = True
......@@ -138,10 +143,10 @@ class TwtxtManager:
""" = not
entry = self._cache2.get(twt.hash, {})
entry = self._data.get(twt.hash, {})
entry["read"] =
self._cache2[twt.hash] = entry
self._data[twt.hash] = entry
self.read_twts_count += 1 if else -1
self.unread_twts_count -= 1 if else -1
......@@ -193,7 +198,64 @@ class TwtxtManager:
local twtxt.txt file.
with open(self.TWTXT_TXT, 'a', encoding='utf-8') as fd:
with open(self.config.twtfile, 'a', encoding='utf-8') as fd:
fd.write("%s\t%s\n" % (created_at.isoformat(), text))
class Config:
_CONFIG_DIR = os.path.join(xdg.xdg_config_home(), 'twtxt')
_CONFIG_FILE = os.path.join(_CONFIG_DIR, 'config')
_CACHE_DIR = os.path.join(xdg.xdg_cache_home(), 'twtxt')
# The original twtxt implementation actually places its 'cache' file in the
# config directory, which is wrong. The XDG Base Directory Specification
# defines a dedicated cache directory. To be backwards-compatible, we cannot
# fix this, though. :-(
_CACHE_FILE = os.path.join(_CONFIG_DIR, 'cache')
_DATA_DIR = os.path.join(xdg.xdg_data_home(), 'twtxt')
_DATA_FILE = os.path.join(_DATA_DIR, 'data')
def __init__(self, cfg):
self._cfg = cfg
def load_file(cls):
if not os.path.exists(Config._CONFIG_FILE):
raise ValueError("Config file '%s' does not exist." % Config._CONFIG_FILE)
cfg = configparser.ConfigParser()
return cls(cfg)
def nick(self):
return self._cfg.get("twtxt", "nick", fallback=os.environ.get("USER", "").lower())
def twtfile(self):
return self._make_abs(Config._CONFIG_DIR,
self._cfg.get("twtxt", "twtfile", fallback="twtxt.txt"))
def twturl(self):
return self._cfg.get("twtxt", "twturl", fallback=None)
def cachefile(self):
return self._make_abs(Config._CONFIG_DIR,
self._cfg.get("twtxt", "cachefile", fallback="cache"))
def datafile(self):
return self._make_abs(Config._CONFIG_DIR,
self._cfg.get("twtxt", "datafile", fallback=Config._DATA_FILE))
def _make_abs(self, base_path, path):
Expand user homes and environment variables in the given `path` and
make it absolute with the given `base_path` in case it is relative.
path = os.path.expanduser(os.path.expandvars(path))
if os.path.isabs(path):
return path
return os.path.join(base_path, path)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment