Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #88 from SGSSGene/bug/manpath
Browse files Browse the repository at this point in the history
moving PATHS and make them dependent on XDG_* env variables
  • Loading branch information
aitjcize committed Nov 13, 2018
2 parents 6856cfb + 9f3cb90 commit 0f3d999
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 31 deletions.
8 changes: 3 additions & 5 deletions bin/cppman
Expand Up @@ -124,8 +124,7 @@ def main():
options.source)
else:
config.Source = options.source
if config.UpdateManPath:
update_man3_link()
update_man3_link()
print("Source set to `%s'." % options.source)
sys.exit(0)

Expand All @@ -143,9 +142,8 @@ def main():
raise Exception("invalid value `%s' for option `--use-mandb'" %
options.mandb)
config.UpdateManPath = config.parse_bool(options.mandb)
if config.UpdateManPath:
update_mandb_path()
update_man3_link()
update_mandb_path()
update_man3_link()
sys.exit(0)

if options.rebuild_index:
Expand Down
17 changes: 12 additions & 5 deletions cppman/environ.py
Expand Up @@ -27,20 +27,27 @@
from cppman import get_lib_path
from cppman.config import Config

HOME = os.path.expanduser('~')
HOME = os.environ["HOME"];

man_dir = HOME + '/.local/share/man/'
config_dir = HOME + '/.config/cppman/'
config_file = config_dir + 'cppman.cfg'
XDG_CACHE_HOME = os.getenv("XDG_CACHE_HOME", os.path.join(HOME, ".cache"))
XDG_CONFIG_HOME = os.getenv("XDG_CONFIG_HOME", os.path.join(HOME, ".config"))

cache_dir = os.path.join(XDG_CACHE_HOME, 'cppman')
manindex_dir = os.path.join(cache_dir, 'manindex')
config_dir = os.path.join(XDG_CONFIG_HOME, 'cppman')
config_file = os.path.join(config_dir, 'cppman.cfg')

config = Config(config_file)

try:
os.makedirs(cache_dir)
os.makedirs(manindex_dir)
os.makedirs(config_dir)
update_man3_link()
except:
pass

index_db_re = os.path.normpath(os.path.join(config_dir, 'index.db'))
index_db_re = os.path.join(cache_dir, 'index.db')

index_db = index_db_re if os.path.exists(index_db_re) \
else get_lib_path('index.db')
Expand Down
12 changes: 6 additions & 6 deletions cppman/main.py
Expand Up @@ -163,7 +163,7 @@ def cache_all(self):
raise KeyboardInterrupt

try:
os.makedirs(environ.man_dir)
os.makedirs(environ.cache_dir)
except:
pass

Expand Down Expand Up @@ -210,7 +210,7 @@ def cache_man_page(self, source, url, name):
return

try:
os.makedirs(os.path.join(environ.man_dir, source))
os.makedirs(os.path.join(environ.cache_dir, source))
except OSError:
pass

Expand All @@ -225,13 +225,13 @@ def cache_man_page(self, source, url, name):
f.write(groff_text.encode('utf-8'))

def clear_cache(self):
"""Clear all cache in man3"""
shutil.rmtree(environ.man_dir)
"""Clear all cache in man"""
shutil.rmtree(environ.cache_dir)

def man(self, pattern):
"""Call viewer.sh to view man page"""
try:
avail = os.listdir(os.path.join(environ.man_dir, environ.source))
avail = os.listdir(os.path.join(environ.cache_dir, environ.source))
except OSError:
avail = []

Expand Down Expand Up @@ -319,4 +319,4 @@ def get_normalized_page_name(self, name):

def get_page_path(self, source, name):
name = self.get_normalized_page_name(name)
return os.path.join(environ.man_dir, source, name + '.3.gz')
return os.path.join(environ.cache_dir, source, name + '.3.gz')
28 changes: 14 additions & 14 deletions cppman/util.py
Expand Up @@ -35,33 +35,33 @@


def update_mandb_path():
"""Add ~/.local/share/man to $HOME/.manpath"""
HOME = os.path.expanduser('~')
manpath_file = os.path.normpath(os.path.join(HOME, '.manpath'))
manpath = '.local/share/man'
"""Add $XDG_CACHE_HOME/cppman/man to $HOME/.manpath"""
manpath_file = os.path.join(environ.HOME, ".manpath")
man_dir = environ.cache_dir
manindex_dir = environ.manindex_dirl

lines = []

""" read all lines """
try:
with open(manpath_file, 'r') as f:
lines = f.readlines()
except IOError:
if not environ.config.UpdateManPath:
return
return

has_path = any([manpath in l for l in lines])
""" remove MANDATORY_MANPATH and MANDB_MAP entry """
lines = [line for line in lines if man_dir not in line]

with open(manpath_file, 'w') as f:
if environ.config.UpdateManPath:
if not has_path:
lines.append('MANDATORY_MANPATH\t%s\n' %
os.path.normpath(os.path.join(HOME, manpath)))
else:
lines = [line for line in lines if manpath not in line]
lines.append('MANDATORY_MANPATH\t%s\n' % man_dir)
lines.append('MANDB_MAP\t\t\t%s\t%s\n' % (man_dir, manindex_dir))

f.writelines(lines)


def update_man3_link():
man3_path = os.path.join(environ.man_dir, 'man3')
man3_path = os.path.join(environ.cache_dir, 'man3')

if os.path.lexists(man3_path):
if os.path.islink(man3_path):
Expand All @@ -73,7 +73,7 @@ def update_man3_link():
raise RuntimeError("Can't create link since `%s' already exists" %
man3_path)
try:
os.makedirs(os.path.join(environ.man_dir, environ.config.Source))
os.makedirs(os.path.join(environ.cache_dir, environ.config.Source))
except Exception:
pass

Expand Down
2 changes: 1 addition & 1 deletion misc/cppman.1
Expand Up @@ -15,7 +15,7 @@ cppman uses Vi Improved as a pager.
Press 'q' to leave pager.
Press 'K' on an entry like 'vector::insert(3)' links you to the manual page of vector::insert, like a hyperlink.
.SS man compatibility
cppman automatically adds '~/.local/share/man' to '~/.manpath', so the cached man pages can also be viewed with 'man' command. Note that to view uncached man pages, you still need to run 'cppman'.
cppman automatically adds '$XDG_CACHE_HOME/cppman/man' to '~/.manpath', so the cached man pages can also be viewed with 'man' command. Note that to view uncached man pages, you still need to run 'cppman'.
.SH OPTIONS
.IP "\-s SOURCE, \-\-source=SOURCE"
Select source, either 'cppreference.com' or 'cplusplus.com'. Default is 'cplusplus.com'.
Expand Down

0 comments on commit 0f3d999

Please sign in to comment.