16
16
# You should have received a copy of the GNU General Public License
17
17
# along with this program. If not, see http://www.gnu.org/licenses/
22
if sys.version_info[0] != 2:
23
sys.exit('Sorry, Phatch is only compatible with Python 2.x!\n')
25
from data.info import INFO
26
from core import config
32
from lib import system
34
from core.info import INFO
35
from lib.context import join
36
from core.context import CONTEXT, check_user_paths
28
38
VERSION = "%(name)s %(version)s" % INFO
32
#TODO: move me to lib/system.py
33
if path.startswith('file://'):
34
return urllib.unquote(path[7:])
38
def parse_locale(config_paths):
40
index = sys.argv.index('-l') + 1
41
if index >= len(sys.argv):
42
sys.exit('Please specify locale language after "-l".')
43
canonical = sys.argv[index]
46
config.load_locale('phatch', config_paths["PHATCH_LOCALE_PATH"], canonical)
42
"""Only sets font cache global variables, does not execute anything."""
43
from lib.fonts import set_font_cache
45
CONTEXT['user_fonts_path'],
46
CONTEXT['app_fonts_path'],
47
CONTEXT['user_fonts_cache_path'],
48
CONTEXT['app_fonts_cache_path']
52
def apply_pil_patches():
53
"""Patches for pil <= 1.1.6"""
55
if Image.VERSION < '1.1.7':
57
join(CONTEXT['app_source_path'], 'other', 'pil_1_1_6'))
49
60
def parse_options():
125
136
default=logging.WARN,
126
137
help=_("Logging level"))
127
138
options, paths = parser.parse_args()
128
paths = [fix_path(path) for path in paths if path and path[0] != '%']
139
paths = [system.fix_path(path) for path in paths
140
if path and path[0] != '%']
129
141
return options, paths
132
def reexec_with_pythonw(f=None):
133
"""'pythonw' needs to be called for any wxPython app
134
to run from the command line on Mac Os X."""
135
if sys.version.split(' ')[0] < '2.5' and sys.platform == 'darwin' and\
136
not (sys.executable.endswith('/Python') or hasattr(sys, 'frozen')):
137
sys.stderr.write('re-executing using pythonw')
140
os.execvp('pythonw', ['pythonw', f] + sys.argv[1:])
143
def console(config_paths):
144
main(config_paths, app_file=None, gui=True)
146
index = sys.argv.index('-l') + 1
147
if index >= len(sys.argv):
148
sys.exit('Please specify locale language after "-l".')
149
canonical = sys.argv[index]
151
canonical = 'default'
152
i18n.load_locale('phatch', CONTEXT["app_locale_path"], canonical)
147
155
PYWX_ERROR = """\
161
def _gui(app_file, paths, settings):
162
reexec_with_pythonw(app_file) # ensure pythonw for mac
165
actionlist = paths[0]
168
gui.main(settings, actionlist)
172
config.verify_app_user_paths()
173
170
from lib.fonts import font_dictionary
174
171
font_dictionary(force=True)
177
def _init_log(level):
178
175
logger = logging.getLogger()
179
176
logger.setLevel(level)
182
def _inspect(app_file, paths):
183
reexec_with_pythonw(app_file) # ensure pythonw for mac
188
def _droplet(app_file, paths, settings):
189
reexec_with_pythonw(app_file) # ensure pythonw for mac
191
gui.drop(actionlist=paths[0], paths=paths[1:], settings=settings)
194
def has_ext(path, ext):
195
return path.lower().endswith(ext)
198
def _console(paths, settings):
179
def start_gui(paths, settings):
180
system.reexec_with_pythonw() # ensure pythonw for mac
182
actionlist = paths[0]
185
get_gui().main(settings, actionlist)
188
def start_inspector(paths):
189
system.reexec_with_pythonw() # ensure pythonw for mac
190
get_gui().inspect(paths)
193
def start_droplet(paths, settings):
194
system.reexec_with_pythonw() # ensure pythonw for mac
195
get_gui().drop(actionlist=paths[0], paths=paths[1:], settings=settings)
198
def start_console(paths, settings):
199
199
from core.api import init
201
201
from console import console
202
if paths and has_ext(paths[0], INFO['extension']):
202
if paths and system.has_ext(paths[0], INFO['extension']):
203
203
console.main(actionlist=paths[0], paths=paths[1:], settings=settings)
205
205
console.main(actionlist='', paths=paths, settings=settings)
208
def main(config_paths, app_file):
208
def create_settings(options=None):
213
'stop_for_errors': True,
214
'overwrite_existing_images': True,
216
'check_images_first': True,
217
'always_show_status_dialog': True,
224
'interactive': False,
228
'tag_actions': _('All'),
230
'collapse_automatic': False,
232
'droplet_path': CONTEXT['user_path'],
234
'image_inspector': False,
235
'paths': CONTEXT['user_path'],
237
'overwrite_existing_images_forced': False,
240
for attr in settings:
241
if hasattr(options, attr):
242
settings[attr] = getattr(options, attr)
209
247
"""init should be called first!"""
210
parse_locale(config_paths)
211
250
options, paths = parse_options()
212
from core.settings import create_settings
213
settings = create_settings(config_paths, options)
214
_init_log(options.log_level)
251
settings = create_settings(options)
252
init_log(options.log_level)
215
253
if settings['verbose']:
216
from lib import system
217
254
system.VERBOSE = True
255
# safe can only be set by the command line or the ui, and is not permanent
218
256
if 'safe' in settings:
219
257
from lib import formField
220
258
formField.set_safe(settings['safe'])
221
259
del settings['safe']
260
# initialize font.cache -> can be done at root level
262
if settings['init_fonts']:
222
268
if settings['image_inspector']:
223
_inspect(app_file, paths)
225
if settings['init_fonts']:
269
start_inspector(paths)
271
# check for font.cache, geek.txt
273
# force to choose from recent action lists if no action list is given
230
274
if paths and not (paths[0] == 'recent' or \
231
has_ext(paths[0], INFO['extension'])):
275
system.has_ext(paths[0], INFO['extension'])):
232
276
settings['droplet'] = True
233
277
paths.insert(0, 'recent')
278
# start droplet, console or gui
234
279
if settings['droplet']:
236
281
paths = ['recent']
237
_droplet(app_file, paths, settings)
282
start_droplet(paths, settings)
238
283
elif len(paths) > 1 or settings['console'] or settings['interactive']:
239
_console(paths, settings)
284
start_console(paths, settings)
241
_gui(app_file, paths, settings)
286
start_gui(paths, settings)
243
288
if __name__ == '__main__':