Skip to content

Instantly share code, notes, and snippets.

@TheEnigmaBlade
Created April 8, 2016 01:35
Show Gist options
  • Save TheEnigmaBlade/f13ff26218c969a5c4e2fadc94427b65 to your computer and use it in GitHub Desktop.
Save TheEnigmaBlade/f13ff26218c969a5c4e2fadc94427b65 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# Config
watch_dir_path = "D:\\Documents\\LoL Subreddit\\CSS\\Main v2.0\\"
main_file = "theme.less"
include_ext = ".less"
exclude_files = ["style-darkmode.less", "style.less"]
extend_css_file = "css/champion-flair.css"
output_file = "css/stylesheet.css"
use_command = True
command = "lessc \"{file}\""
remote_update = True
user_agent = "script:CSS dev updater:v1.0 (by /u/TheEnigmaBlade)"
subreddit = "lolcsstest"
# Main
import sys, time, subprocess, re
import reddit, praw.errors
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
r = None
def init_reddit():
global r
if remote_update:
if r:
reddit.destroy_reddit(r)
r = reddit.initialize_reddit(user_agent, oauth_scopes={"modconfig"})
last_time = 0
wait_len = 10
def update_css():
global last_time
curr_time = time.time()
if curr_time - last_time < wait_len:
last_time = curr_time
return
print("Updating CSS...", end=" ")
path = watch_dir_path + main_file
p = subprocess.Popen(command.format(file=path), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = p.stdout.read()
error = p.stderr.read()
if error and len(error) > 0:
error = error.decode("utf-8")
print("lessc failed!", file=sys.stderr)
print(error, file=sys.stderr)
return
output = output.decode("utf-8")
output = clean_css(output)
if extend_css_file:
with open(extend_css_file, "r") as f:
output += f.read()
with open(output_file, "w") as f:
f.write(output)
if remote_update:
update_reddit(output)
def clean_css(css):
css = css.replace(" ", "\t")
css = css.replace(" 0px", " 0")
return css
def update_reddit(css):
print("reddit...", end=" ")
try:
response = r.set_stylesheet(subreddit, css)
if "errors" in response and len(response["errors"]) > 0:
errors = response["errors"]
print("failed!")
print("Error when updating stylesheet", file=sys.stderr)
print(errors, file=sys.stderr)
else:
print("done!")
except praw.errors.OAuthInvalidToken:
init_reddit()
update_reddit(css)
class FileSaveWatcher(FileSystemEventHandler):
ignores = [watch_dir_path+p for p in exclude_files]
locked = False
def on_any_event(self, event):
#Check if ignored
if event.src_path in self.ignores:
return
if not event.src_path.endswith(include_ext):
return
# I hope boolean assignment is atomic
if not self.locked:
self.locked = True
update_css()
self.locked = False
if __name__ == "__main__":
init_reddit()
import sys
if len(sys.argv) == 2 and sys.argv[1] == "--no-watch":
update_css()
else:
observer = Observer()
observer.schedule(FileSaveWatcher(), watch_dir_path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment