Optimization: Optimize file handling

This commit is contained in:
Dylan Araps 2017-06-19 13:33:56 +10:00
parent 9355441d81
commit 40a3d48d80

45
wal.py
View File

@ -82,6 +82,7 @@ def process_args(args):
# -c # -c
if args.c: if args.c:
shutil.rmtree(SCHEME_DIR) shutil.rmtree(SCHEME_DIR)
quit()
# -r # -r
if args.r: if args.r:
@ -166,11 +167,13 @@ def gen_colors(img):
COLOR_COUNT + index) COLOR_COUNT + index)
# Create a list of hex colors. # Create a list of hex colors.
search = re.search
append = colors.append
for color in magic_output: for color in magic_output:
hex_color = re.search('#.{6}', str(color)) hex_color = search('#.{6}', str(color))
if hex_color: if hex_color:
colors.append(hex_color.group(0)) append(hex_color.group(0))
# Remove the first element, which isn't a color. # Remove the first element, which isn't a color.
del colors[0] del colors[0]
@ -184,27 +187,22 @@ def get_colors(img):
cache_file = "%s%s" % (SCHEME_DIR, img.replace('/', '_')) cache_file = "%s%s" % (SCHEME_DIR, img.replace('/', '_'))
cache_file = Path(cache_file) cache_file = Path(cache_file)
# Cache the wallpaper name.
with open(WAL_FILE, 'w') as file:
file.write("%s\n" % (img))
if cache_file.is_file(): if cache_file.is_file():
with open(cache_file) as file: with open(cache_file) as file:
colors = file.readlines() colors = file.readlines()
colors = [x.strip() for x in colors] colors = [x.strip() for x in colors]
else: else:
# Cache the wallpaper name.
wal = open(WAL_FILE, 'w')
wal.write(img)
wal.write("\n")
wal.close()
# Generate the colors. # Generate the colors.
colors = gen_colors(img) colors = gen_colors(img)
# Cache the colorscheme. # Cache the colorscheme.
scheme = open(cache_file, 'w') with open(cache_file, 'w') as file:
for color in colors: file.write("\n".join(colors))
scheme.write(color)
scheme.write("\n")
scheme.close()
print("colors: Generated colorscheme") print("colors: Generated colorscheme")
return colors return colors
@ -289,14 +287,12 @@ def send_sequences(colors, vte):
# Send the sequences to all open terminals. # Send the sequences to all open terminals.
for term in glob.glob("/dev/pts/[0-9]*"): for term in glob.glob("/dev/pts/[0-9]*"):
term_file = open(term, 'w') with open(term, 'w') as file:
term_file.write(sequences) file.write(sequences)
term_file.close()
# Cache the sequences. # Cache the sequences.
sequence_file = open(SEQUENCE_FILE, 'w') with open(SEQUENCE_FILE, 'w') as file:
sequence_file.write(sequences) file.write(sequences)
sequence_file.close()
print("colors: Set terminal colors") print("colors: Set terminal colors")
@ -344,11 +340,8 @@ def set_wallpaper(img):
def export_plain(colors): def export_plain(colors):
"""Export colors to a plain text file.""" """Export colors to a plain text file."""
file = open(PLAIN_FILE, 'w') with open(PLAIN_FILE, 'w') as file:
for color in colors: file.write('\n'.join(colors))
file.write(color)
file.write("\n")
file.close()
def export_xrdb(colors): def export_xrdb(colors):
@ -399,9 +392,9 @@ def export_xrdb(colors):
colors[14], colors[14],
colors[15]) colors[15])
file = open(XRDB_FILE, 'w') # Write the colors to the file.
with open(XRDB_FILE, 'w') as file:
file.write(x_colors) file.write(x_colors)
file.close()
# Merge the colors into the X db so new terminals use them. # Merge the colors into the X db so new terminals use them.
call(["xrdb", "-merge", XRDB_FILE]) call(["xrdb", "-merge", XRDB_FILE])