From 86650ccd6cdf297085ab5f5962bbf3910132e47e Mon Sep 17 00:00:00 2001 From: Igor Chubin Date: Tue, 30 Apr 2019 22:34:35 +0200 Subject: [PATCH] refactored adapter/cheat_sheets.py --- lib/adapter/cheat_sheets.py | 119 ++++++++++++++++++++---------------- 1 file changed, 65 insertions(+), 54 deletions(-) diff --git a/lib/adapter/cheat_sheets.py b/lib/adapter/cheat_sheets.py index 3bfdf7e..a472609 100644 --- a/lib/adapter/cheat_sheets.py +++ b/lib/adapter/cheat_sheets.py @@ -1,10 +1,14 @@ -import sys +""" +Implementation of the adapter for the native cheat.sh cheat sheets repository, +cheat.sheets. The cheat sheets repository is hierarchically structured: cheat +sheets covering programming languages are are located in subdirectories. +""" + +# pylint: disable=relative-import + import os import glob -sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) -from globals import PATH_CHEAT_SHEETS - from git_adapter import GitRepositoryAdapter def _remove_initial_underscore(filename): @@ -12,73 +16,80 @@ def _remove_initial_underscore(filename): filename = filename[1:] return filename -def _sanitize_dirname(dirname): - dirname = os.path.basename(dirname) - 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(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]).rstrip('/')+'/' - for topic in topics if _isdir(topic)] - answers = [os.path.split(topic)[1] for topic in topics if not _isdir(topic)] - return answers, answer_dirs - class CheatSheets(GitRepositoryAdapter): + """ + Adapter for the cheat.sheets cheat sheets. + """ + _adapter_name = "cheat.sheets" _output_format = "code" _repository_url = "https://github.com/chubin/cheat.sheets" - - def __init__(self): - self._answers = [] - self._cheatsheet_answers = [] - self._cheatsheet_dirs = [] - GitRepositoryAdapter.__init__(self) - - def _update_cheat_sheets_topics(self): - self._answers = _get_answer_files_from_folder() - self._cheatsheet_answers, self._cheatsheet_dirs = _get_answers_and_dirs() - return self._answers + self._cheatsheet_answers, self._cheatsheet_dirs + _cheatsheet_files_prefix = "sheets/" def _get_list(self, prefix=None): - return self._update_cheat_sheets_topics()[0] + """ + Return all files on the first and the second level, + excluding directories and hidden files + """ - def _get_page(self, topic, request_options=None): - """ - Get the cheat sheet topic from the own repository (cheat.sheets). - It's possible that topic directory starts with omitted underscore - """ - filename = PATH_CHEAT_SHEETS + "%s" % topic - if not os.path.exists(filename): - filename = PATH_CHEAT_SHEETS + "_%s" % topic - if os.path.isdir(filename): - return "" - return open(filename, "r").read().decode('utf-8') + hidden_files = ["_info.yaml"] + answer = [] + for mask in ['*', '*/*']: + template = os.path.join( + self.local_repository_location(), + self._cheatsheet_files_prefix, + mask) + + answer += [ + os.path.basename(f_name) + for f_name in glob.glob(template) + if not os.path.isdir(f_name) and f_name not in hidden_files] + + return sorted(answer) class CheatSheetsDir(CheatSheets): + """ + Adapter for the cheat sheets directories. + Provides pages named according to subdirectories: + _dir => dir/ + + (currently only _get_list() is used; _get_page is shadowed + by the CheatSheets adapter) + """ + _adapter_name = "cheat.sheets dir" _output_format = "text" def _get_list(self, prefix=None): - return self._update_cheat_sheets_topics()[1] + + template = os.path.join( + self.local_repository_location(), + self._cheatsheet_files_prefix, + '*') + + answer = sorted([ + _remove_initial_underscore(os.path.basename(f_name)) + "/" + for f_name in glob.glob(template) + if os.path.isdir(f_name)]) + + return answer def _get_page(self, topic, request_options=None): - answer = [] - for f_name in glob.glob(PATH_CHEAT_SHEETS + "%s/*" % topic.rstrip('/')): - answer.append(os.path.basename(f_name)) - topics = sorted(answer) - return "\n".join(topics) + "\n" + """ + Content of the `topic` dir is the list of the pages in the dir + """ + + template = os.path.join( + self.local_repository_location(), + self._cheatsheet_files_prefix, + topic.rstrip('/'), + '*') + + answer = sorted([ + os.path.basename(f_name) for f_name in glob.glob(template)]) + return "\n".join(answer) + "\n" def is_found(self, topic): return CheatSheets.is_found(self, topic.rstrip('/'))