7
from locale import gettext as _
9
config_dir = os.path.expanduser("~/.config/xubuntu/")
10
config_file = "xdg-xubuntu-templates.cfg"
12
source_directory = "/usr/share/xubuntu/templates/"
14
"OpenDocument Text.odt": _("OpenDocument Text"),
15
"OpenDocument Spreadsheet.ods": _("OpenDocument Spreadsheet"),
16
"Plain Text.txt": _("Plain Text"),
20
def get_template_directory():
21
templates = subprocess.check_output(["xdg-user-dir", "TEMPLATES"]).strip()
22
templates = templates.decode("utf-8")
23
home = os.path.expanduser("~")
26
if os.path.exists(templates):
35
filename = os.path.join(config_dir, config_file)
38
if os.path.exists(filename):
39
with open(filename, 'r') as config:
40
for line in config.readlines():
41
created.append(line.strip())
43
print("Already created: " + ", ".join(created))
47
def write_config(created):
51
if not os.path.exists(config_dir):
52
os.makedirs(config_dir)
54
filename = os.path.join(config_dir, config_file)
56
with open(filename, 'w') as config:
57
for template in created:
58
config.write(template + "\n")
61
previous = read_config()
64
template_dir = get_template_directory()
66
print("Found template directory: '%s'" % template_dir)
67
for filename in templates.keys():
68
if filename in previous:
69
print("'%s' already created" % filename)
72
print("Copying template file: '%s'" % filename)
74
extension = os.path.splitext(filename)[1]
76
source_filename = os.path.join(source_directory, filename)
77
if not os.path.exists(source_filename):
78
print("'%s' does not exist" % source_filename)
81
target_filename = templates[filename] + extension
82
target_filename = os.path.join(template_dir, target_filename)
83
if os.path.exists(target_filename):
84
print("'%s' already exists" % target_filename)
87
print("%s => %s" % (source_filename, target_filename))
88
shutil.copy2(source_filename, target_filename)
89
created.append(filename)