Moai.tmbundle / Support / moai_helper.py

#!/usr/bin/env python
# Encoding: UTF-8

from __future__ import unicode_literals
from textwrap import dedent

import lxml.html
from plumbum import cli


def find_class_names(html_docs_path):
	'''
		Returns a list of comepletions passed from the html docs in the moai-sdk.
		
		Finds all Moai classnames.
	'''
	
	try:
		doc = lxml.html.parse(html_docs_path)
	except IOError as e:
		print e
	
	root = doc.getroot()
	return tuple(a.text for a in root.cssselect('a.el'))


def find_members(members_file_path):
	'''
		Returns a dictionary with members from all Moai classes.
		Each key is a member name and its value is a dictionary which keys are
		names of the classes that has the member in question. The keys are links
		to documentation for the member.
	'''
	
	doc = lxml.html.parse(members_file_path)
	root = doc.getroot()
	list_items = root.cssselect('.contents li')
	
	members = {}
	
	for li in list_items:
		name = li.text[:-5]  # Remove trailing whitespace, () and colon.
		members[name] = {}
		
		for a in li.getchildren():
			members[name][a.text] = a.attrib['href']
	
	return members


def generate_tmpreferences(completions):
	'''
		Generates a string with the completions in .tmPreferences format.
	'''
	
	# Sort completions alphabetically
	completions = sorted(completions)
	
	# Add single quotes and commas
	completions_str = ','.join("'{0}'".format(name) for name in completions)
	
	# Render in Apple's Old Style ASCII Plist format
	tmPreferences = dedent('''
			{{
				completions = ({0});
			}}
		''').format(completions_str)
	
	return tmPreferences


class CLIApp(cli.Application):
	'''
		This might not be strictly necessary but aren't cli toolkits lovely?
	'''
	
	PROGNAME = 'moai_helper'
	VERSION = '0.1'
	DESCRIPTION = 'Generates completions in .tmPreferences format for TextMate.'
	
	def main(self, html_docs_dir):
		classes_file = 'classes.html'
		classes_path = '{0}/{1}'.format(html_docs_dir, classes_file)
		functions_file = 'functions.html'
		functions_path = '{0}/{1}'.format(html_docs_dir, functions_file)
		
		class_names = find_class_names(classes_path)
		function_info = find_members(functions_path)
		tmpreferences = generate_tmpreferences(class_names)
		print tmpreferences
		from pprint import pprint
		pprint(function_info)


if __name__ == '__main__':
	CLIApp.run()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.