Commits

Tuk Bredsdorff committed 3310043

Added constant completions. Closes #2.

Comments (0)

Files changed (2)

Support/moai_helper.py

 from plumbum import cli
 
 
+def get_root(path_to_html):
+	'''
+		Returns an lxml root node given a path to a html file.
+	'''
+	
+	try:
+		doc = lxml.html.parse(path_to_html)
+		return doc.getroot()
+	except IOError as e:
+		print e
+		exit(1)
+
+
 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()
+	root = get_root(html_docs_path)
 	return tuple(a.text for a in root.cssselect('a.el'))
 
 
 		to documentation for the member.
 	'''
 	
-	doc = lxml.html.parse(members_file_path)
-	root = doc.getroot()
+	root = get_root(members_file_path)
 	list_items = root.cssselect('.contents li')
 	
 	members = {}
 	return members
 
 
+def strip_class(s):
+	'''
+		Strips class name prefix from a string.
+		
+		'MOAILogMgr.LOG_STATUS' -> 'LOG_STATUS'
+	'''
+	
+	
+	dot_pos = s.find('.')
+	
+	return s[dot_pos+1:]
+
+
+def find_constants(constants_file_path):
+	'''
+		Returns a tuple with all the Moai constants.
+	'''
+	
+	root = get_root(constants_file_path)
+	
+	constants = (strip_class(td.text) for td in
+		root.cssselect('td.indexvalue table td td:first-child')
+		if td.text != None
+	)
+	
+	# Remove duplicated items by converting to a set
+	constants = set(constants)
+	
+	return tuple(constants)
+
 def generate_tmpreferences(completions):
 	'''
 		Generates a string with the completions in .tmPreferences format.
 		classes_path = '{0}/{1}'.format(html_docs_dir, classes_file)
 		functions_file = 'functions.html'
 		functions_path = '{0}/{1}'.format(html_docs_dir, functions_file)
+		constants_file = 'annotated.html'
+		constants_path = '{0}/{1}'.format(html_docs_dir, constants_file)
 		
 		class_names = find_class_names(classes_path)
 		function_info = find_members(functions_path)
 		function_names = function_info.keys()
-		tmpreferences = generate_tmpreferences(class_names + tuple(function_names))
+		constant_names = find_constants(constants_path)
+		
+		tmpreferences = generate_tmpreferences(class_names + tuple(function_names) + constant_names)
 		
 		print tmpreferences
 		

Support/test_moai_helper.py

 from __future__ import unicode_literals
 from os import environ as env
 
-from moai_helper import find_class_names, generate_tmpreferences, find_members
+from moai_helper import find_class_names, generate_tmpreferences, find_members, find_constants, strip_class
 
 html_docs_dir = '{0}/docs/html-lua/html'.format(env['TM_MOAI_SDK'])
 
 class_members_file = 'functions.html'
 classes_file = 'classes.html'
+constants_file = 'annotated.html'
 
 html_classes_path = '{0}/{1}'.format(html_docs_dir, classes_file)
 class_members_path = '{0}/{1}'.format(html_docs_dir, class_members_file)
+constants_path = '{0}/{1}'.format(html_docs_dir, constants_file)
 
 
 def test_find_class_names():
 	assert member_info['down']['MOAITouchSensor'] == 'class_m_o_a_i_touch_sensor.html#ad45b7fa98205d676476088e184f55d47'
 
 
+def test_find_constants():
+	assert strip_class('MOAILogMgr.LOG_STATUS') == 'LOG_STATUS'
+	constants = find_constants(constants_path)
+	assert 'EVENT_STOP' in constants
+	assert 'ATTR_ROTATE_QUAT' in constants
+	assert 'ATTR_ROTATE_QUAT' in constants
+	assert 'SHOULD_START_LOAD_WITH_REQUEST' in constants
+	assert 'REMOTE_NOTIFICATION_REGISTRATION_COMPLETE' in constants
+	assert constants.count(None) == 0
+
 def test_generate_tmpreferences():
 	'''
 		A very basic test. Checks if a class name is found in the rendered plist.