~k-nielsen81/translated-documentation-screenshot-viewer/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import os
try:
    from icu import Locale
except ImportError:
    from PyICU import Locale
from PIL import Image as PILImage

class ProjectPaths(object):
    """ Class that forms the paths for a specific project """

    def __init__(self, project):
        """ Form the paths for and retrieve information for the images on the
        site.

        The directory structure for this site is that the archives that contain
        the images are checked out into the
        image_project/static/source directory
        E.g:
        /home/kenneth/code/ul10ndocs/image_projects/static/source/ubuntu-docs
        /home/kenneth/code/ul10ndocs/image_projects/static/source/oneiric

        The source directory is made a source for static files, and
        therefore figure paths for the html are relative to this
        dir. Meanwhile, we also need to keep track of the absolute
        paths for internal handling in python. Below both sets of the
        paths are formed.

        Comments below are mostly variable content (paths) examples,
        made on the same machine as the paths above, to make it easier
        to follow. Commented right after assignment
        """

        ### Form the basepath of the documentation
        basepath = os.path.dirname(os.path.abspath(__file__))
        # /home/kenneth/code/view_localized_doc_images/image_projects/

        docpath = os.path.join(basepath, 'static', 'source',
                                project.codepath.replace('/', os.sep))
        # /home/kenneth/code/code/translated-documentation-screenshot-viewer/
        # image_projects/static/source/ubuntu-docs/ubuntu-help

        # Form the list of languages
        self.languages = []
        for name in os.listdir(docpath):
            # language name matching ll or ll_CC
            if len(name) == 2 or (len(name) == 5 and name[2] == '_'):
                self.languages.append(name)

        # Form dictionaries for language code to common language an reverse
        self.lang_code_to_common, self.lang_common_to_code = {},{}
        for lang in self.languages:
            try:
                name = Locale(lang.decode('UTF-8').encode('ASCII')).\
                    getDisplayName().title()
            except UnknownLocaleError:
                name = lang

            self.lang_common_to_code[name] = lang
            self.lang_code_to_common[lang] = name

        # Form a list of common names and sort it
        self.common_lang_names_sorted = self.lang_common_to_code.keys()
        self.common_lang_names_sorted.sort()

        ### Get the original figures
        figurespath = os.path.join(docpath, project.original,
                                   project.language_sub)
        self.C = ImageCollection(project, figurespath, 'C', 'Original')

        ### Get the localized figures
        for lang in self.languages:
            figurespath = os.path.join(docpath, lang, project.language_sub)
            # /home/kenneth/code/translated-documentation-screenshot-viewer/
            # image_projects/static/source/ubuntu-docs/ubuntu-help/my/figures
            self.__dict__[lang] =\
                ImageCollection(project, figurespath, lang,
                                self.lang_code_to_common[lang])

    def __getitem__(self, key):
        return self.__dict__[key]

    def get_lang_code_from_common_name(self, key):
        return self.lang_common_to_code[key]

    def get_common_names_sorted(self):
        return self.common_lang_names_sorted

    def get_item_from_common_name(self, key):
        lang_code = self.get_lang_code_from_common_name(key)
        return self.__dict__[lang_code]

    def get_item_from_lang_code(self, key):
        return self.__dict__[key]

class ImageCollection(object):
    """ Represents a set of images for a language """
    def __init__(self, project, figures_path, code, common):
        self.code = code
        self.common = common

        # Form the image collection
        self.collection = {}
        if os.path.isdir(figures_path):
            self.names = os.listdir(figures_path)
            for fig in self.names:
                self.collection[fig] = Image(project, fig, figures_path, code)
        else:
            self.names = []

    def __getitem__(self, key):
        return self.collection[key]

    def items(self):
        return self.collection.items()
    
    def get_file_names(self):
        return self.collection.keys()

    def get_count(self):
        return len(self.names)

    def get_code(self):
        return self.code

    def get_common(self):
        return self.common

class Image(object):
    def __init__(self, project, fig, figurespath, code):
        self.fig = fig
        self.relative = os.path.join('source',
                                     project.codepath,
                                     code,
                                     project.language_sub,
                                     fig)
        self.absolute = os.path.join(figurespath, fig)

        self.x, self.y = PILImage.open(self.absolute).size
        # The table is 900 pixels wide, and the images have a 10 pix padding
        self.max_img_width = 430

    def __getitem__(self, key):
        return self.__dict__[key]

    def reduced_width(self):
        """ Return self.max_img_width if the width of the image is larger, else
        return the empty string which means that the img width will not be set
        in the html
        """
        return self.max_img_width if self.x > self.max_img_width else ''