I have a file containing a color table that sets environment variables that I use for zsh prompts. Currently have a python script that takes all the colors found between #color-begin and #color-end. However, I cannot pass python string variables into a python shell invocation and I'm unsure what the problem is.
Something like
pythonStr = "fg_blue"
pythonStr = "$"+fg_blue
os.system("echo + pythonStr")
So far I have found a few examples that pass variables into .sh files, but how does one print out a environment color variable without a .sh file? If this is not possible, why is this the case? Here is the current python code I have and the color codes I would like to print out.
Any help would be greatly appreciated.
printcolor.py
import os
import subprocess
def prcl():
with open("appearance") as f:
content = f.readlines()
foo = False
for line in content:
line = line.strip(' \n\t')
# Toggle boolean
if "color-" in line:
foo = not foo
if foo == True:
if line is not "" and "#" not in line:
# Use '=' as a delimiter
head, sep, tail = line.partition("=")
head='"$'+head+'"'
# prints out blank lines
os.system("echo "+head)
#prints literal string
#print(head)
environment variables for color
#color-begin
fg_black=%{$'\e[0;30m'%}
fg_red=%{$'\e[0;31m'%}
fg_green=%{$'\e[0;32m'%}
fg_brown=%{$'\e[0;33m'%}
fg_blue=%{$'\e[0;34m'%}
fg_purple=%{$'\e[0;35m'%}
fg_cyan=%{$'\e[0;36m'%}
fg_lgray=%{$'\e[0;37m'%}
fg_dgray=%{$'\e[1;30m'%}
fg_lred=%{$'\e[1;31m'%}
fg_lgreen=%{$'\e[1;32m'%}
fg_yellow=%{$'\e[1;33m'%}
fg_lblue=%{$'\e[1;34m'%}
fg_pink=%{$'\e[1;35m'%}
fg_lcyan=%{$'\e[1;36m'%}
fg_white=%{$'\e[1;37m'%}
fg_blue=%{$'\e[0;34m'%}
fg_purple=%{$'\e[0;35m'%}
fg_cyan=%{$'\e[0;36m'%}
fg_lgray=%{$'\e[0;37m'%}
fg_dgray=%{$'\e[1;30m'%}
fg_lred=%{$'\e[1;31m'%}
fg_lgreen=%{$'\e[1;32m'%}
fg_yellow=%{$'\e[1;33m'%}
fg_lblue=%{$'\e[1;34m'%}
fg_pink=%{$'\e[1;35m'%}
fg_lcyan=%{$'\e[1;36m'%}
fg_white=%{$'\e[1;37m'%}
#Text Background Colors
bg_red=%{$'\e[0;41m'%}
bg_green=%{$'\e[0;42m'%}
bg_brown=%{$'\e[0;43m'%}
bg_blue=%{$'\e[0;44m'%}
bg_purple=%{$'\e[0;45m'%}
bg_cyan=%{$'\e[0;46m'%}
bg_gray=%{$'\e[0;47m'%}
#Attributes
at_normal=%{$'\e[0m'%}
at_bold=%{$'\e[1m'%}
at_italics=%{$'\e[3m'%}
at_underl=%{$'\e[4m'%}
at_blink=%{$'\e[5m'%}
at_outline=%{$'\e[6m'%}
at_reverse=%{$'\e[7m'%}
at_nondisp=%{$'\e[8m'%}
at_strike=%{$'\e[9m'%}
at_boldoff=%{$'\e[22m'%}
at_italicsoff=%{$'\e[23m'%}
at_underloff=%{$'\e[24m'%}
at_blinkoff=%{$'\e[25m'%}
at_reverseoff=%{$'\e[27m'%}
at_strikeoff=%{$'\e[29m'%}
#color-end
There are several minor issues with your Python program, but the main reason why your variables are not expanded is because they are not present in the environment when the Python program is run.
Your appearance
file should look like:
export fg_black=$'\e[0;30m'
export fg_red=$'\e[0;31m'
export fg_green=$'\e[0;32m'
The %{..%}
is just unnecessary, but export
is crucial. Without export
, after you source appearance
in your shell, those variables are not passed onto a python process when called later from that shell with python printcolor.py
.
Another option in zsh
would be to use setopt allexport
and export all environment variables to a subprocess (subshell), but that can have many undesired effects, so you're safer with individual export
s.
After you modify your appearance
file, you'll have to tweak color name parsing in Python, perhaps like this (the inner if
only):
if line and not line.startswith("#"):
color = line.split("=")[0].split()[1]
subprocess.call('echo "${%s}"text' % color, shell=True)