~osomon/phatch/extract-all-metadata

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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# -*- coding: UTF-8 -*-

# Phatch - Photo Batch Processor
# Copyright (C) 2007-2008  www.stani.be
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see http://www.gnu.org/licenses/

import gettext, glob, locale, os, sys
from lib.unicoding import ensure_unicode

USER_PATH       = os.path.expanduser('~')
try:
    USER_PATH   = USER_PATH.decode(sys.getfilesystemencoding())
except:
    pass

def env(var,*paths):
    paths   = (USER_PATH,)+paths
    return os.environ.get(var,os.path.join(*paths))

if sys.platform.startswith('linux'):
    USER_DATA_PATH  = env('XDG_DATA_HOME', 
                            USER_PATH,'.local','share','phatch')
    USER_CONFIG_PATH= env('XDG_CONFIG_HOME','.config','phatch')
    USER_CACHE_PATH = env('XDG_CACHE_HOME','.cache','phatch')
else:
    #TODO: what would be the best user path for these platforms?
    USER_DATA_PATH  = USER_CONFIG_PATH = USER_CACHE_PATH =\
                            os.path.join(USER_PATH,'phatch')
    
USER_ACTIONS_PATH       = os.path.join(USER_DATA_PATH,'actions')
USER_ACTIONLISTS_PATH   = os.path.join(USER_DATA_PATH,'actionlists')
USER_BIN_PATH           = os.path.join(USER_DATA_PATH,'bin')
USER_FONTS_PATH         = os.path.join(USER_DATA_PATH,'fonts')
USER_FONTS_CACHE_PATH   = os.path.join(USER_CACHE_PATH,'fonts')
USER_LOG_PATH           = os.path.join(USER_CACHE_PATH,'log')
USER_MASKS_PATH         = os.path.join(USER_DATA_PATH,'masks')
USER_HIGHLIGHTS_PATH    = os.path.join(USER_DATA_PATH,'highlights')
USER_PREVIEW_PATH       = os.path.join(USER_CACHE_PATH,'preview')
USER_SETTINGS_PATH      = os.path.join(USER_CONFIG_PATH,'settings.cPickle')
USER_WATERMARKS_PATH    = os.path.join(USER_DATA_PATH,'watermarks')

def check_config_paths(config_paths):
    global SYSTEM_INSTALL
    global PHATCH_FONTS_PATH
    global PHATCH_FONTS_CACHE_PATH
    if config_paths: 
        SYSTEM_INSTALL      = False
        PHATCH_FONTS_PATH   = config_paths['PHATCH_FONTS_PATH']
        PHATCH_FONTS_CACHE_PATH= config_paths['PHATCH_FONTS_CACHE_PATH']
        return config_paths
    SYSTEM_INSTALL      = True
    ROOT_SHARE_PATH     = os.path.join(sys.prefix, "share")#for win?
    PHATCH_SHARE_PATH   = os.path.join(ROOT_SHARE_PATH,"phatch")
    PHATCH_DATA_PATH    = os.path.join(PHATCH_SHARE_PATH,"data")
    PHATCH_FONTS_PATH   = os.path.join(PHATCH_DATA_PATH,"fonts")
    PHATCH_FONTS_CACHE_PATH   = os.path.join(PHATCH_SHARE_PATH,
                            "cache","fonts")

    if sys.platform.startswith('win'):
        sys.stderr.write(
            'Sorry your platform is not yet supported.\n' \
            + 'The instructions for Windows are on the Phatch website.'
        )
        sys.exit()
    else:
        return {
            'PHATCH_IMAGE_PATH' : os.path.join(PHATCH_SHARE_PATH,
                                    'images'),
            'PHATCH_LOCALE_PATH': os.path.join(PHATCH_SHARE_PATH,
                                    'locale'),
            'PHATCH_DOCS_PATH'  : os.path.join(PHATCH_SHARE_PATH,
                                    'phatch','docs'),
            #cache
            'PHATCH_FONTS_CACHE_PATH' : PHATCH_FONTS_CACHE_PATH,
            #data
            'PHATCH_ACTIONLISTS_PATH': os.path.join(PHATCH_DATA_PATH,
                                    'actionlists'),
            'PHATCH_FONTS_PATH'      : PHATCH_FONTS_PATH,
            'PHATCH_HIGHLIGHTS_PATH' : os.path.join(PHATCH_DATA_PATH,
                                    'highlights'),
            'PHATCH_MASKS_PATH' : os.path.join(PHATCH_DATA_PATH,
                                    'masks'),
        }
        
def add_user_paths(config_paths):
    config_paths.update({
        'USER_PATH'             : USER_PATH,
        'USER_ACTIONS_PATH'     : USER_ACTIONS_PATH,
        'USER_BIN_PATH'         : USER_BIN_PATH,
        'USER_FONTS_PATH'       : USER_FONTS_PATH,
        'USER_FONTS_CACHE_PATH' : USER_FONTS_CACHE_PATH,
        'USER_MASKS_PATH'       : USER_MASKS_PATH,
        'USER_HIGHLIGHTS_PATH'  : USER_HIGHLIGHTS_PATH,
        'USER_PREVIEW_PATH'     : USER_PREVIEW_PATH,
        'USER_SETTINGS_PATH'    : USER_SETTINGS_PATH,
        'USER_WATERMARKS_PATH'  : USER_WATERMARKS_PATH,
    })

def fix_python_path(phatch_python_path=None):
    if not phatch_python_path:
        phatch_python_path = os.path.dirname(
            os.path.dirname(os.path.abspath(__file__)))
    if not(phatch_python_path in [ensure_unicode(x) for x in sys.path]):
        sys.path.insert(0,phatch_python_path)
    return phatch_python_path

def load_locale(app,path,canonical='default',unicode=True):
    locale.setlocale(locale.LC_ALL, '')
    #get default canonical if necessary
    if canonical == 'default':
        canonical  = locale.getdefaultlocale(envvars=('LC_ALL', 'LANG'))[0]#canonical
        if canonical is None:
            #for mac
            canonical   = 'en'
    #canonical = 'zh' #to test unicode languages
    #expand with similar translations
    languages   = [os.path.basename(x) for 
                    x in glob.glob(os.path.join(path,canonical[:2]+'*'))]
    if canonical in languages:
        languages.remove(canonical)
    languages.insert(0,canonical)
    #install
    i18n = gettext.translation(app, path, languages=languages,fallback=1)
    i18n.install(unicode=unicode)

def init_config_paths(config_paths=None):
    if config_paths is None:
        config_paths = {}
    #check paths
    config_paths    = check_config_paths(config_paths)
    add_user_paths(config_paths)
    #configure sys.path
    phatch_path     = fix_python_path(
                        config_paths.get('PHATCH_PYTHON_PATH',None))
    fix_python_path(os.path.join(phatch_path,'core','lib'))
    #only when the gui is involved
    pyWxLib         = os.path.join(phatch_path,'pyWx','lib')
    if os.path.isdir(pyWxLib): fix_python_path(pyWxLib)
    #other people module 
    fix_python_path(os.path.join(phatch_path,'other'))
    #patches for pil < 1.1.6 (ImportError=skip during build process)
    try:
        import Image
        if Image.VERSION < '1.1.6':
            fix_python_path(os.path.join(phatch_path,'other','pil-1.1.6'))
    except ImportError:
        pass
    #user actions
    fix_python_path(USER_ACTIONS_PATH)
    #load locale
    #load_locale('phatch',config_paths['PHATCH_LOCALE_PATH'])
    #set font cache
    from fonts import set_font_cache
    set_font_cache(USER_FONTS_PATH, PHATCH_FONTS_PATH,
        USER_FONTS_CACHE_PATH, PHATCH_FONTS_CACHE_PATH)
    #register paths
    global PATHS
    PATHS   = config_paths
    #return values
    return config_paths
    
def load_locale_only(config_paths=None):
    if config_paths is None:
        config_paths = {}
    config_paths    = check_config_paths(config_paths)
    load_locale('phatch',config_paths['PHATCH_LOCALE_PATH'])