From 251256d418562b5f23220408e6828b7a14949a44 Mon Sep 17 00:00:00 2001 From: Per Persson Date: Fri, 13 Jul 2018 23:59:01 +0200 Subject: [PATCH 1/7] Fix incorrect MYDIR --- bin/srv.py | 2 +- lib/beautifier.py | 2 +- lib/cheat_wrapper.py | 2 +- lib/globals.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bin/srv.py b/bin/srv.py index c7bde53..fe3e7bc 100644 --- a/bin/srv.py +++ b/bin/srv.py @@ -20,7 +20,7 @@ import requests import jinja2 from flask import Flask, request, send_from_directory, redirect, Response -MYDIR = os.path.abspath(os.path.dirname(os.path.dirname('__file__'))) +MYDIR = os.path.abspath(os.path.join(__file__, '..', '..')) sys.path.append("%s/lib/" % MYDIR) from globals import FILE_QUERIES_LOG, LOG_FILE, TEMPLATES, STATIC, MALFORMED_RESPONSE_HTML_PAGE diff --git a/lib/beautifier.py b/lib/beautifier.py index 48491fd..83a7e6d 100644 --- a/lib/beautifier.py +++ b/lib/beautifier.py @@ -33,7 +33,7 @@ from tempfile import NamedTemporaryFile import redis -MYDIR = os.path.abspath(os.path.dirname(os.path.dirname('__file__'))) +MYDIR = os.path.abspath(os.path.join(__file__, '..', '..')) sys.path.append("%s/lib/" % MYDIR) from languages_data import VIM_NAME from globals import PATH_VIM_ENVIRONMENT diff --git a/lib/cheat_wrapper.py b/lib/cheat_wrapper.py index 49f5d9c..8552ada 100644 --- a/lib/cheat_wrapper.py +++ b/lib/cheat_wrapper.py @@ -17,7 +17,7 @@ import colored from pygments import highlight as pygments_highlight from pygments.formatters import Terminal256Formatter # pylint: disable=no-name-in-module -MYDIR = os.path.abspath(os.path.dirname(os.path.dirname('__file__'))) +MYDIR = os.path.abspath(os.path.join(__file__, '..', '..')) sys.path.append("%s/lib/" % MYDIR) from globals import error, ANSI2HTML, COLOR_STYLES from buttons import TWITTER_BUTTON, GITHUB_BUTTON, GITHUB_BUTTON_FOOTER diff --git a/lib/globals.py b/lib/globals.py index a726320..5443e09 100644 --- a/lib/globals.py +++ b/lib/globals.py @@ -8,7 +8,7 @@ import logging import os from pygments.styles import get_all_styles -MYDIR = os.path.abspath(os.path.dirname(os.path.dirname('__file__'))) +MYDIR = os.path.abspath(os.path.join(__file__, '..', '..')) ANSI2HTML = os.path.join(MYDIR, "share/ansi2html.sh") From 575054760f31670127cb3a0d8043200676fcacaa Mon Sep 17 00:00:00 2001 From: Luciano Strika Date: Sat, 14 Jul 2018 00:42:30 -0300 Subject: [PATCH 2/7] refactor tldr_update so it fits in two lines. abrstracted logic away into a function. --- lib/get_answer.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/get_answer.py b/lib/get_answer.py index 2fc4a9a..d9d9499 100644 --- a/lib/get_answer.py +++ b/lib/get_answer.py @@ -56,13 +56,13 @@ INTERNAL_TOPICS = [ ':share', ] +def _get_filenames(): + return [os.path.split(topic)[1] for topic in glob.glob(PATH_TLDR_PAGES)] + + def _update_tldr_topics(): - answer = [] - for topic in glob.glob(PATH_TLDR_PAGES): - _, filename = os.path.split(topic) - if filename.endswith('.md'): - answer.append(filename[:-3]) - return answer + return [ filename[:-3] for filename in _get_filenames() if filename.endswith('.md') ] + TLDR_TOPICS = _update_tldr_topics() def _update_cheat_topics(): From ec6f7d133753eba81ce015945ff25b49fa2294d4 Mon Sep 17 00:00:00 2001 From: Luciano Strika Date: Sat, 14 Jul 2018 00:45:35 -0300 Subject: [PATCH 3/7] saw a common pattern and cleaned the code for cheat topics updater function --- lib/get_answer.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/lib/get_answer.py b/lib/get_answer.py index d9d9499..2d8d05e 100644 --- a/lib/get_answer.py +++ b/lib/get_answer.py @@ -56,21 +56,18 @@ INTERNAL_TOPICS = [ ':share', ] -def _get_filenames(): - return [os.path.split(topic)[1] for topic in glob.glob(PATH_TLDR_PAGES)] +def _get_filenames(path): + return [os.path.split(topic)[1] for topic in glob.glob(path)] def _update_tldr_topics(): - return [ filename[:-3] for filename in _get_filenames() if filename.endswith('.md') ] + return [ filename[:-3] for filename in _get_filenames(PATH_TLDR_PAGES) if filename.endswith('.md') ] TLDR_TOPICS = _update_tldr_topics() def _update_cheat_topics(): - answer = [] - for topic in glob.glob(PATH_CHEAT_PAGES): - _, filename = os.path.split(topic) - answer.append(filename) - return answer + return _get_filenames(PATH_CHEAT_PAGES) + CHEAT_TOPICS = _update_cheat_topics() def _update_cheat_sheets_topics(): From c47c7eead4e58d6d39572a88af5ef3f292b70fb2 Mon Sep 17 00:00:00 2001 From: Luciano Strika Date: Sat, 14 Jul 2018 01:08:09 -0300 Subject: [PATCH 4/7] refactor first half of update_cheat_sheets_topics for clarity and maintainability --- lib/get_answer.py | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/lib/get_answer.py b/lib/get_answer.py index 2d8d05e..fe832fa 100644 --- a/lib/get_answer.py +++ b/lib/get_answer.py @@ -59,29 +59,35 @@ INTERNAL_TOPICS = [ def _get_filenames(path): return [os.path.split(topic)[1] for topic in glob.glob(path)] - def _update_tldr_topics(): return [ filename[:-3] for filename in _get_filenames(PATH_TLDR_PAGES) if filename.endswith('.md') ] -TLDR_TOPICS = _update_tldr_topics() - def _update_cheat_topics(): return _get_filenames(PATH_CHEAT_PAGES) + + +TLDR_TOPICS = _update_tldr_topics() CHEAT_TOPICS = _update_cheat_topics() +def _sanitize_dirname(dirname): + dirname = os.path.basename(dirname) + if dirname.startswith('_'): + dirname = dirname[1:] + return dirname + +def _format_answer(dirname,filename): + return "%s/%s" % ( _sanitize_dirname(dirname), filename ) + +def _get_answer_files_from_folder(): + topics = map(glob.glob(PATH_CHEAT_SHEETS + "*/*"), os.path.split) + return [_format_answer(dirname,filename) for dirname, filename in topics if filename not in ['_info.yaml'] ] + def _update_cheat_sheets_topics(): - answer = [] + answers = [] answer_dirs = [] - for topic in glob.glob(PATH_CHEAT_SHEETS + "*/*"): - dirname, filename = os.path.split(topic) - if filename in ['_info.yaml']: - continue - dirname = os.path.basename(dirname) - if dirname.startswith('_'): - dirname = dirname[1:] - answer.append("%s/%s" % (dirname, filename)) + answers = _get_answer_files_from_folder() for topic in glob.glob(PATH_CHEAT_SHEETS + "*"): _, filename = os.path.split(topic) @@ -92,9 +98,11 @@ def _update_cheat_sheets_topics(): else: answer.append(filename) return answer, answer_dirs + CHEAT_SHEETS_TOPICS, CHEAT_SHEETS_DIRS = _update_cheat_sheets_topics() CACHED_TOPICS_LIST = [[]] + def get_topics_list(skip_dirs=False, skip_internal=False): """ List of topics returned on /:list From 3331608e98bfa2f13cbf6f6c6b23ea0df49f8a07 Mon Sep 17 00:00:00 2001 From: Luciano Strika Date: Sat, 14 Jul 2018 01:31:36 -0300 Subject: [PATCH 5/7] big refactor of update_cheat_sheets_topics to make it more readable and maintainable. --- lib/get_answer.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/get_answer.py b/lib/get_answer.py index fe832fa..f8ae349 100644 --- a/lib/get_answer.py +++ b/lib/get_answer.py @@ -70,34 +70,34 @@ def _update_cheat_topics(): TLDR_TOPICS = _update_tldr_topics() CHEAT_TOPICS = _update_cheat_topics() +def _remove_initial_underscore(filename): + if filename.startswith('_'): + filename = filename[1:] + return filename + def _sanitize_dirname(dirname): dirname = os.path.basename(dirname) - if dirname.startswith('_'): - dirname = dirname[1:] + dirname = remove_initial_underscore(dirname) return dirname def _format_answer(dirname,filename): return "%s/%s" % ( _sanitize_dirname(dirname), filename ) def _get_answer_files_from_folder(): - topics = map(glob.glob(PATH_CHEAT_SHEETS + "*/*"), os.path.split) + topics = map(os.path.split, glob.glob(PATH_CHEAT_SHEETS + "*/*")) return [_format_answer(dirname,filename) for dirname, filename in topics if filename not in ['_info.yaml'] ] +def isdir(topic): + return os.path.isdir(topic) +def _get_answers_and_dirs(): + topics = glob.glob(PATH_CHEAT_SHEETS + "*") + answer_dirs = [_remove_initial_underscore(os.path.split(topic)[1]) for topic in topics if isdir(topic)] + answers = [ os.path.split(topic)[1] for topic in topics if not isdir(topic)] + return answer_dirs, answers def _update_cheat_sheets_topics(): - answers = [] - answer_dirs = [] - answers = _get_answer_files_from_folder() - - for topic in glob.glob(PATH_CHEAT_SHEETS + "*"): - _, filename = os.path.split(topic) - if os.path.isdir(topic): - if filename.startswith('_'): - filename = filename[1:] - answer_dirs.append(filename+'/') - else: - answer.append(filename) - return answer, answer_dirs + cheatsheet_answers, cheatsheet_dirs = _get_answers_and_dirs() + return answers+cheatsheet_answers, cheatsheet_dirs CHEAT_SHEETS_TOPICS, CHEAT_SHEETS_DIRS = _update_cheat_sheets_topics() From 1b7c80f2294a8894a3a466dda52780f0c985a6b4 Mon Sep 17 00:00:00 2001 From: Luciano Strika Date: Sat, 14 Jul 2018 01:32:29 -0300 Subject: [PATCH 6/7] rename function after privacy convention. --- lib/get_answer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/get_answer.py b/lib/get_answer.py index f8ae349..d896d10 100644 --- a/lib/get_answer.py +++ b/lib/get_answer.py @@ -86,11 +86,11 @@ def _format_answer(dirname,filename): def _get_answer_files_from_folder(): topics = map(os.path.split, glob.glob(PATH_CHEAT_SHEETS + "*/*")) return [_format_answer(dirname,filename) for dirname, filename in topics if filename not in ['_info.yaml'] ] -def isdir(topic): +def _isdir(topic): return os.path.isdir(topic) def _get_answers_and_dirs(): topics = glob.glob(PATH_CHEAT_SHEETS + "*") - answer_dirs = [_remove_initial_underscore(os.path.split(topic)[1]) for topic in topics if isdir(topic)] + answer_dirs = [_remove_initial_underscore(os.path.split(topic)[1]) for topic in topics if _isdir(topic)] answers = [ os.path.split(topic)[1] for topic in topics if not isdir(topic)] return answer_dirs, answers From e38f68f8bd863d7c333e59f593147f70e0c88034 Mon Sep 17 00:00:00 2001 From: Luciano Strika Date: Sat, 14 Jul 2018 01:39:00 -0300 Subject: [PATCH 7/7] end refactor of isdir --- lib/get_answer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/get_answer.py b/lib/get_answer.py index d896d10..59e47a5 100644 --- a/lib/get_answer.py +++ b/lib/get_answer.py @@ -91,7 +91,7 @@ def _isdir(topic): def _get_answers_and_dirs(): topics = glob.glob(PATH_CHEAT_SHEETS + "*") answer_dirs = [_remove_initial_underscore(os.path.split(topic)[1]) for topic in topics if _isdir(topic)] - answers = [ os.path.split(topic)[1] for topic in topics if not isdir(topic)] + answers = [ os.path.split(topic)[1] for topic in topics if not _isdir(topic)] return answer_dirs, answers def _update_cheat_sheets_topics():