~xubuntu-dev/xubuntu-default-settings/trunk

« back to all changes in this revision

Viewing changes to usr/share/xubuntu/templates/xdg-xubuntu-templates

  • Committer: Sean Davis
  • Date: 2017-01-01 23:12:15 UTC
  • mto: This revision was merged to the branch mainline in revision 631.
  • Revision ID: smd.seandavis@gmail.com-20170101231215-9712dc68li85dd57
Add Xubuntu templates

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python3
 
2
 
 
3
import os
 
4
import subprocess
 
5
import shutil
 
6
 
 
7
from locale import gettext as _
 
8
 
 
9
config_dir = os.path.expanduser("~/.config/xubuntu/")
 
10
config_file = "xdg-xubuntu-templates.cfg"
 
11
 
 
12
source_directory = "/usr/share/xubuntu/templates/"
 
13
templates = {
 
14
    "OpenDocument Text.odt": _("OpenDocument Text"),
 
15
    "OpenDocument Spreadsheet.ods": _("OpenDocument Spreadsheet"),
 
16
    "Plain Text.txt": _("Plain Text"),
 
17
}
 
18
 
 
19
 
 
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("~")
 
24
    if home != templates:
 
25
        return templates
 
26
    if os.path.exists(templates):
 
27
        return templates
 
28
    return False
 
29
 
 
30
 
 
31
def read_config():
 
32
    global config_dir
 
33
    global config_file
 
34
 
 
35
    filename = os.path.join(config_dir, config_file)
 
36
 
 
37
    created = []
 
38
    if os.path.exists(filename):
 
39
        with open(filename, 'r') as config:
 
40
            for line in config.readlines():
 
41
                created.append(line.strip())
 
42
 
 
43
    print("Already created: " + ", ".join(created))
 
44
    return created
 
45
 
 
46
 
 
47
def write_config(created):
 
48
    global config_dir
 
49
    global config_file
 
50
 
 
51
    if not os.path.exists(config_dir):
 
52
        os.makedirs(config_dir)
 
53
 
 
54
    filename = os.path.join(config_dir, config_file)
 
55
 
 
56
    with open(filename, 'w') as config:
 
57
        for template in created:
 
58
            config.write(template + "\n")
 
59
 
 
60
 
 
61
previous = read_config()
 
62
created = previous
 
63
 
 
64
template_dir = get_template_directory()
 
65
if template_dir:
 
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)
 
70
            continue
 
71
 
 
72
        print("Copying template file: '%s'" % filename)
 
73
 
 
74
        extension = os.path.splitext(filename)[1]
 
75
 
 
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)
 
79
            continue
 
80
 
 
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)
 
85
            continue
 
86
 
 
87
        print("%s => %s" % (source_filename, target_filename))
 
88
        shutil.copy2(source_filename, target_filename)
 
89
        created.append(filename)
 
90
 
 
91
write_config(created)