Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Lyse
tt
Commits
4fc899fd
Commit
4fc899fd
authored
Sep 19, 2021
by
Lysander Trischler
Browse files
Load config file
parent
124e494b
Changes
3
Hide whitespace changes
Inline
Side-by-side
README.rst
View file @
4fc899fd
...
...
@@ -39,8 +39,15 @@ conversations, a `twtxt.net <https://twtxt.net/>`_ 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
...
...
tt
View file @
4fc899fd
...
...
@@ -16,7 +16,8 @@ except ImportError:
pyperclip
=
None
manager
=
twtxtmanager
.
TwtxtManager
()
config
=
twtxtmanager
.
Config
.
load_file
()
manager
=
twtxtmanager
.
TwtxtManager
(
config
)
manager
.
load_twts
()
class
TwtsListBox
(
widgets
.
VimListBox
):
...
...
twtxtmanager.py
View file @
4fc899fd
...
...
@@ -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
MAX_TWEETS_LIMIT
=
2000
self
.
_cache
=
shelve
.
open
(
'/home/lyse/.config/twtxt/
cache
'
)
own_source
=
twtxt
.
models
.
Source
(
nick
=
"lyse"
,
url
=
"https://lyse.isobeef.org/twtxt.txt"
)
with
open
(
self
.
TWTXT_TXT
,
'r'
,
encoding
=
'utf-8'
)
as
fd
:
self
.
_cache
=
shelve
.
open
(
self
.
config
.
cache
file
)
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
=
shelve
.
open
(
'/home/lyse/.config/twtxt/cache2'
)
os
.
makedirs
(
os
.
path
.
dirname
(
self
.
config
.
datafile
),
exist_ok
=
True
)
self
.
_data
=
shelve
.
open
(
self
.
config
.
datafile
)
for
url
,
feed
in
self
.
_cache
.
items
():
if
url
==
"last_update"
:
continue
...
...
@@ -65,10 +70,10 @@ class TwtxtManager:
known_tweet_hashes
.
add
(
twt
.
hash
)
known_tweet_hashes
.
add
(
twt
.
old_hash
)
twt
.
replies
=
[]
entry
=
self
.
_
cache2
.
get
(
twt
.
hash
)
entry
=
self
.
_
data
.
get
(
twt
.
hash
)
twt
.
read
=
bool
(
entry
and
entry
.
get
(
"read"
,
False
))
if
not
twt
.
read
:
entry
=
self
.
_
cache2
.
get
(
twt
.
old_hash
)
entry
=
self
.
_
data
.
get
(
twt
.
old_hash
)
twt
.
read
=
bool
(
entry
and
entry
.
get
(
"read"
,
False
))
if
twt
.
read
:
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
)
twt
.
read
=
entry
and
entry
.
get
(
"read"
,
False
)
twt
.
missing
=
True
conversation_tree
.
append
(
twt
)
...
...
@@ -138,10 +143,10 @@ class TwtxtManager:
"""
twt
.
read
=
not
twt
.
read
entry
=
self
.
_
cache2
.
get
(
twt
.
hash
,
{})
entry
=
self
.
_
data
.
get
(
twt
.
hash
,
{})
entry
[
"read"
]
=
twt
.
read
self
.
_
cache2
[
twt
.
hash
]
=
entry
self
.
_
cache2
.
sync
()
self
.
_
data
[
twt
.
hash
]
=
entry
self
.
_
data
.
sync
()
self
.
read_twts_count
+=
1
if
twt
.
read
else
-
1
self
.
unread_twts_count
-=
1
if
twt
.
read
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
@
classmethod
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
()
cfg
.
read
(
Config
.
_CONFIG_FILE
)
return
cls
(
cfg
)
@
property
def
nick
(
self
):
return
self
.
_cfg
.
get
(
"twtxt"
,
"nick"
,
fallback
=
os
.
environ
.
get
(
"USER"
,
""
).
lower
())
@
property
def
twtfile
(
self
):
return
self
.
_make_abs
(
Config
.
_CONFIG_DIR
,
self
.
_cfg
.
get
(
"twtxt"
,
"twtfile"
,
fallback
=
"twtxt.txt"
))
@
property
def
twturl
(
self
):
return
self
.
_cfg
.
get
(
"twtxt"
,
"twturl"
,
fallback
=
None
)
@
property
def
cachefile
(
self
):
return
self
.
_make_abs
(
Config
.
_CONFIG_DIR
,
self
.
_cfg
.
get
(
"twtxt"
,
"cachefile"
,
fallback
=
"cache"
))
@
property
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
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment