Skip to content

Instantly share code, notes, and snippets.

@santiago-salas-v
Last active September 10, 2017 23:56
Show Gist options
  • Save santiago-salas-v/7eecf8e6779907aa21b7989ce4248c2a to your computer and use it in GitHub Desktop.
Save santiago-salas-v/7eecf8e6779907aa21b7989ce4248c2a to your computer and use it in GitHub Desktop.
Recursive directory scan for first file index.hmtl (python 3) + sublime text plugin
"""
Open documentation files contained in directory.
"""
import os
import webbrowser
index_found = False
def enter_node(current_dir, index_html_files):
"""
Recursively retrieves the first file called 'index.html' in subdirectories
of current_dir.
:param current_dir: a directory to scan for 'index.html' files.
:type current_dir: str
:param index_html_files: list of 'index.html' files found so far (full path)
:type index_html_files: list
:return: list of string
:rtype: list
"""
listed_files = os.listdir(current_dir)
sub_dirs = []
sub_files = []
print(current_dir)
for file_from_list in listed_files:
if os.path.isdir(
os.path.join(
os.path.abspath(current_dir), file_from_list
)
):
sub_dirs.append(file_from_list)
else:
sub_files.append(file_from_list)
if 'index.html' in sub_files:
index_html_files.append(
os.path.abspath(
os.path.join(
current_dir,
'index.html')))
print(index_html_files[-1])
print('index.html found! Returning...')
if len(sub_dirs) == 0 or 'index.html' in sub_files:
pass
# exit_dir = True
else:
# Only continue recursion if files below and index.html not found
# exit_dir = False
for new_dir in sub_dirs:
enter_node(os.path.join(
current_dir, new_dir), index_html_files
)
# exit_dir = True
return index_html_files
index_html_files_found = enter_node(os.getcwd(), [])
for file_from_final_list in index_html_files_found:
webbrowser.open(file_from_final_list)
import sublime
import sublime_plugin
import os
import sys
import webbrowser
import threading
libdocs_folder = 'C:\\Users\\ssv\\Documents\\LIBDOCS'
sys.path.append(libdocs_folder)
class OpendocsCommand(sublime_plugin.TextCommand):
def run(self, edit, filename=None):
th = OpendocsThread(filename)
th.start()
class OpendocsThread(threading.Thread):
def __init__(self, filename=None):
threading.Thread.__init__(self)
def run(self):
import opendocs_3
index_html_files_found = opendocs_3.enter_node(libdocs_folder, [])
for file_from_final_list in index_html_files_found:
webbrowser.open(file_from_final_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment