#!/usr/bin/env python3
################################
# Development tool
# Auto-compiles style.less to style.css
#
# Requires lessc and less clean css to be installed:
#	npm install -g less
#	npm install -g less-plugin-clean-css
################################

import os, time
from os import path
from math import floor
from _helper import *


# Main application
class Main:
	style_less = "style.less"
	style_css = "style.css"
	
	def __init__(self):
		clear()
		os.chdir("../")
		header("Watching style.less for changes\nctrl+c to exit")
		print()
		
		while True:
			if not os.path.exists(self.style_less):
				print(self.style_less + " does not exist. Exiting.")
				return
				
			if not os.path.exists(self.style_css):
				self.compile()
			elif path.getmtime(self.style_less) > path.getmtime(self.style_css):
				self.compile()
				
			time.sleep(.2)
				
	def compile(self):
		start = time.time()
		os.system("lessc " + self.style_less + " " + self.style_css + " --clean-css")
		touch(self.style_css, path.getmtime(self.style_less))
		print("Recompiled [" + str(floor((time.time() - start) * 100)) + " ms]")
		print()
	
	
# Run application
if __name__ == "__main__":
	try:
		app = Main()
	except KeyboardInterrupt:
		print("Exiting")
[((24, 2, 24, 17), 'os.chdir', 'os.chdir', ({(24, 11, 24, 16): '"""../"""'}, {}), "('../')", False, 'import os, time\n'), ((41, 10, 41, 21), 'time.time', 'time.time', ({}, {}), '()', False, 'import os, time\n'), ((42, 2, 42, 79), 'os.system', 'os.system', ({(42, 12, 42, 78): "('lessc ' + self.style_less + ' ' + self.style_css + ' --clean-css')"}, {}), "('lessc ' + self.style_less + ' ' + self.style_css + ' --clean-css')", False, 'import os, time\n'), ((38, 3, 38, 17), 'time.sleep', 'time.sleep', ({(38, 14, 38, 16): '(0.2)'}, {}), '(0.2)', False, 'import os, time\n'), ((43, 24, 43, 54), 'os.path.getmtime', 'path.getmtime', ({(43, 38, 43, 53): 'self.style_less'}, {}), '(self.style_less)', False, 'from os import path\n'), ((29, 10, 29, 41), 'os.path.exists', 'os.path.exists', ({(29, 25, 29, 40): 'self.style_less'}, {}), '(self.style_less)', False, 'import os, time\n'), ((33, 10, 33, 40), 'os.path.exists', 'os.path.exists', ({(33, 25, 33, 39): 'self.style_css'}, {}), '(self.style_css)', False, 'import os, time\n'), ((35, 8, 35, 38), 'os.path.getmtime', 'path.getmtime', ({(35, 22, 35, 37): 'self.style_less'}, {}), '(self.style_less)', False, 'from os import path\n'), ((35, 41, 35, 70), 'os.path.getmtime', 'path.getmtime', ({(35, 55, 35, 69): 'self.style_css'}, {}), '(self.style_css)', False, 'from os import path\n'), ((44, 36, 44, 47), 'time.time', 'time.time', ({}, {}), '()', False, 'import os, time\n')]