Cleaned up code for pylint

This commit is contained in:
Amit Prasad 2019-12-20 11:34:57 -05:00
parent f06edfa7a2
commit d4bd389b43
2 changed files with 45 additions and 42 deletions

View File

@ -5,53 +5,53 @@ import logging
import os import os
import re import re
from .settings import CACHE_DIR, MODULE_DIR, CONF_DIR
from . import util from . import util
from .settings import CACHE_DIR, CONF_DIR, MODULE_DIR
def template(colors, input_file, output_file=None): def template(colors, input_file, output_file=None):
"""Read template file, substitute markers and """Read template file, substitute markers and
save the file elsewhere.""" save the file elsewhere."""
template_data = util.read_file_raw(input_file) template_data = util.read_file_raw(input_file)
for i in range(len(template_data)): for i, l in enumerate(template_data):
line = template_data[i] for match in re.finditer(r"(?<=(?<!\{))(\{([^{}]+)\})(?=(?!\}))", l):
matches = re.finditer(r"(?<=(?<!\{))(\{([^{}]+)\})(?=(?!\}))", line)
for match in matches:
# Get the color, and the functions associated with it # Get the color, and the functions associated with it
color, _, funcs = match.group(2).partition(".") cname, _, funcs = match.group(2).partition(".")
# Check that functions are needed for this color # Check that functions are needed for this color
if len(funcs) != 0: if len(funcs) == 0:
# Build up a string which will be replaced when the color is done processing continue
replace_str = color # Build up a string which will be replaced with the new color
# The modified color replace_str = cname
new_color = colors[color] # Color to be modified copied into new one
# Execute each function to be done new_color = util.Color(colors[cname].hex_color)
for func in filter(None, funcs.split(")")): # Execute each function to be done
# Get function name and arguments for func in filter(None, funcs.split(")")):
func_split = func.split("(") # Get function name and arguments
args = [] func = func.split("(")
if len(func_split) > 1: fname = func[0]
args = func_split[1].split(",") if fname[0] == '.':
fname = func_split[0] fname = fname[1:]
if fname[0] == '.': if not hasattr(new_color, fname):
fname = fname[1:] logging.error(
if not hasattr(new_color, fname): "Syntax error in template file '%s' on line '%s'",
logging.error( input_file, i)
"Syntax error in template file '%s' on line '%s'", input_file, i) function = getattr(new_color, fname)
f = getattr(new_color, fname)
# If the function is callable, call it # If the function is callable, call it
if callable(f): if callable(function):
new_color = f(*args) if len(func) > 1:
# add to the string that will replace the function calls with the generated function. new_color = function(*func[1].split(","))
if func[0] != '.': else:
replace_str += "." new_color = function()
replace_str += func + ")" # string to replace generated colors
# If the color was changed, replace the template with a unique identifier for the new color. if func[0] != '.':
if not new_color is colors[color]: replace_str += "."
cname = "color" + new_color.strip replace_str += "(".join(func) + ")"
template_data[i] = line.replace(replace_str, cname) # If the color was changed, replace with a unique identifier.
colors[cname] = new_color if new_color is not colors[cname]:
template_data[i] = l.replace(
replace_str, "color" + new_color.strip)
colors["color" + new_color.strip] = new_color
try: try:
template_data = "".join(template_data).format(**colors) template_data = "".join(template_data).format(**colors)
except ValueError: except ValueError:

View File

@ -5,11 +5,11 @@ import colorsys
import json import json
import logging import logging
import os import os
import platform
import re
import shutil import shutil
import subprocess import subprocess
import sys import sys
import platform
import re
class Color: class Color:
@ -60,15 +60,18 @@ class Color:
def lighten(self, percent): def lighten(self, percent):
"""Lighten color by percent""" """Lighten color by percent"""
return Color(lighten_color(self.hex_color, float(re.sub(r'[\D\.]', '', percent)) / 100)) percent = float(re.sub(r'[\D\.]', '', str(percent)))
return Color(lighten_color(self.hex_color, percent / 100))
def darken(self, percent): def darken(self, percent):
"""Darken color by percent""" """Darken color by percent"""
return Color(darken_color(self.hex_color, float(re.sub(r'[\D\.]', '', percent)) / 100)) percent = float(re.sub(r'[\D\.]', '', str(percent)))
return Color(darken_color(self.hex_color, percent / 100))
def saturate(self, percent): def saturate(self, percent):
"""Saturate a color""" """Saturate a color"""
return Color(saturate_color(self.hex_color, float(re.sub(r'[\D\.]', '', percent)) / 100)) percent = float(re.sub(r'[\D\.]', '', str(percent)))
return Color(saturate_color(self.hex_color, percent / 100))
def read_file(input_file): def read_file(input_file):