#!/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")
[((466, 481), 'os.chdir', 'os.chdir', (['"""../"""'], {}), "('../')\n", (474, 481), False, 'import os, time\n'), ((895, 906), 'time.time', 'time.time', ([], {}), '()\n', (904, 906), False, 'import os, time\n'), ((909, 986), 'os.system', 'os.system', (["('lessc ' + self.style_less + ' ' + self.style_css + ' --clean-css')"], {}), "('lessc ' + self.style_less + ' ' + self.style_css + ' --clean-css')\n", (918, 986), False, 'import os, time\n'), ((845, 860), 'time.sleep', 'time.sleep', (['(0.2)'], {}), '(0.2)\n', (855, 860), False, 'import os, time\n'), ((1011, 1041), 'os.path.getmtime', 'path.getmtime', (['self.style_less'], {}), '(self.style_less)\n', (1024, 1041), False, 'from os import path\n'), ((579, 610), 'os.path.exists', 'os.path.exists', (['self.style_less'], {}), '(self.style_less)\n', (593, 610), False, 'import os, time\n'), ((695, 725), 'os.path.exists', 'os.path.exists', (['self.style_css'], {}), '(self.style_css)\n', (709, 725), False, 'import os, time\n'), ((754, 784), 'os.path.getmtime', 'path.getmtime', (['self.style_less'], {}), '(self.style_less)\n', (767, 784), False, 'from os import path\n'), ((787, 816), 'os.path.getmtime', 'path.getmtime', (['self.style_css'], {}), '(self.style_css)\n', (800, 816), False, 'from os import path\n'), ((1079, 1090), 'time.time', 'time.time', ([], {}), '()\n', (1088, 1090), False, 'import os, time\n')]