Source

pida-poelzi-main / pida / services / project / project.py

Full commit
  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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
# -*- coding: utf-8 -*- 
"""
    Service project
    ~~~~~~~~~~~~~~~

    This service handles manging Projects

    :copyright: 2005-2008 by The PIDA Project
    :license: GPL 2 or later
"""
from __future__ import with_statement
import os, sys

import gtk

from kiwi.ui.objectlist import Column

from pida.core.service import Service
from pida.core.features import FeaturesConfig
from pida.core.commands import CommandsConfig
from pida.core.options import OptionsConfig
from pida.core.events import EventsConfig
from pida.core.actions import ActionsConfig, TYPE_NORMAL, TYPE_MENUTOOL, \
    TYPE_TOGGLE
from pida.core.projects import Project
from pida.ui.views import PidaGladeView, PidaView
from pida.ui.objectlist import AttrSortCombo
from pida.core.pdbus import DbusConfig, EXPORT
from pida.core import environment

from pida.utils.puilder.view import PuilderView

# locale
from pida.core.locale import Locale
locale = Locale('project')
_ = locale.gettext

LEXPORT = EXPORT(suffix='project')

def open_directory_dialog(parent, title, folder=''):
    filechooser = gtk.FileChooserDialog(title,
                                        parent,
                                        gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER,
                                        (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                         gtk.STOCK_OPEN, gtk.RESPONSE_OK))
    filechooser.set_default_response(gtk.RESPONSE_OK)

    if folder:
        filechooser.set_current_folder(folder)

    response = filechooser.run()
    if response != gtk.RESPONSE_OK:
        filechooser.destroy()
        return

    path = filechooser.get_filename()
    if path and os.access(path, os.R_OK):
        filechooser.destroy()
        return path


class ProjectListView(PidaGladeView):

    key = 'project.list'

    gladefile = 'project_list'
    locale = locale
    label_text = _('Projects')

    icon_name = 'package_utilities'

    def create_ui(self):
        self.project_ol.set_headers_visible(False)
        self.project_ol.set_columns([Column('markup', use_markup=True)])
        self._sort_combo = AttrSortCombo(self.project_ol,
            [
                ('display_name', 'Name'),
                ('source_directory', 'Full Path'),
                ('name', 'Directory Name'),
            ],
            'display_name'
        )
        self._sort_combo.show()
        self.main_vbox.pack_start(self._sort_combo, expand=False)

    def on_project_ol__selection_changed(self, ol, project):
        self.svc.set_current_project(project)

    def on_project_ol__double_click(self, ol, project):
        self.svc.boss.cmd('filemanager', 'browse', new_path=project.source_directory)
        self.svc.boss.cmd('filemanager', 'present_view')

    def on_project_ol__right_click(self, ol, project, event):
        self.svc.boss.cmd('contexts', 'popup_menu', context='dir-menu',
            dir_name=project.source_directory, event=event,
            project=project)

    def set_current_project(self, project):
        self.project_ol.select(project)

    def update_project(self, project):
        self.project_ol.update(project)

    def can_be_closed(self):
        self.svc.get_action('project_properties').set_active(False)

class ProjectSetupView(PidaView):

    key = 'project.editor'

    label_text = _('Project Properties')

    def create_ui(self):
        self.script_view = PuilderView()
        self.script_view.show()
        self.script_view.set_execute_method(self.test_execute)
        self.script_view.connect('cancel-request',
                                 self._on_script_view__cancel_request)
        self.add_main_widget(self.script_view.get_toplevel())

    def test_execute(self, target, project):
        self.svc.execute_target(None, target, project)

    def set_project(self, project):
        #XXX: should we have more than one project viev ?
        #     for different projects each
        #XXX: ask on case of unsaved changes?
        self.project = project
        #self.script_view.load_script(
        #        os.path.join(
        #            project.source_directory,
        #            'build.vel'
        #            )
        #        )
        self.script_view.set_build(project.build)
        self.script_view.set_project(project)

    def _on_script_view__cancel_request(self, script_view):
        self.svc.get_action('project_properties').set_active(False)

    def can_be_closed(self):
        self.svc.get_action('project_properties').set_active(False)


class ProjectEventsConfig(EventsConfig):

    def create(self):
        self.publish('project_switched', 'loaded')

    def subscribe_all_foreign(self):
        self.subscribe_foreign('editor', 'started',
            self.editor_started)
        self.subscribe_foreign('contexts', 'show-menu', self.show_menu)
        self.subscribe_foreign('contexts', 'menu-deactivated',
            self.menu_deactivated)

    def editor_started(self):
        self.svc.set_last_project()

    def show_menu(self, menu, context, **kw):
        if (context == 'dir-menu'):
            is_project = 'project' in kw
            for a in ['project_properties', 'project_add',
                      'project_remove_directory']:
                self.svc.get_action(a).set_visible(is_project)
            for a in ['project_add_directory']:
                self.svc.get_action(a).set_visible(not is_project)

    def menu_deactivated(self, menu, context, **kw):
        if (context == 'dir-menu'):
            for a in ['project_properties', 'project_add', 'project_remove',
                      'project_add_directory']:
                self.svc.get_action(a).set_visible(True)


class ProjectActionsConfig(ActionsConfig):

    def create_actions(self):
        self.create_action(
            'project_add',
            TYPE_NORMAL,
            _('_Add New Project'),
            _('Adds a new project'),
            gtk.STOCK_ADD,
            self.on_project_add,
        )

        self.create_action(
            'project_execute',
            TYPE_MENUTOOL,
            _('_Execute Default'),
            _('Execute the project'),
            'package_utilities',
            self.on_project_execute,
            ''
        )

        self.create_action(
            'project_execute_last',
            TYPE_MENUTOOL,
            _('Execute _last Controller'),
            _('Execute last Controller'),
            'package_utilities',
            self.on_project_execute_last,
            ''
        )

        self.create_action(
            'project_remove',
            TYPE_NORMAL,
            _('Remove from workspace'),
            _('Remove the current project from the workspace'),
            gtk.STOCK_DELETE,
            self.on_project_remove,
        )

        self.create_action(
            'project_properties',
            TYPE_TOGGLE,
            _('Project _Properties'),
            _('Show the project property editor'),
            'settings',
            self.on_project_properties,
        )

        self.create_action(
            'project_execution_menu',
            TYPE_NORMAL,
            _('Execution Controllers'),
            _('Configurations with which to execute the project'),
            gtk.STOCK_EXECUTE,
            self.on_project_execution_menu,
        )

        self.create_action(
            'project_add_directory',
            TYPE_NORMAL,
            _('Add Directory as Project'),
            _('Add this directory as a project in the workspace'),
            gtk.STOCK_ADD,
            self.on_project_add_directory,
        )

        self.create_action(
            'project_remove_directory',
            TYPE_NORMAL,
            _('Remove from workspace'),
            _('Remove the project from the workspace'),
            gtk.STOCK_DELETE,
            self.on_project_remove_directory,
        )

    def on_project_remove(self, action):
        self.svc.remove_current_project()

    def on_project_add(self, action):
        path = open_directory_dialog(
            self.svc.window,
            _('Select a directory to add')
        )
        if path:
            self.svc.add_directory(path)

    def on_project_execute(self, action):
        default = self.svc._current.script.options.get('default')
        if default is not None:
            self.svc.execute_target(None, default)
        else:
            self.svc.error_dlg(
                _('This project has no default controller'))

    def on_project_execute_last(self, action):
        self.svc.execute_last()

    def on_project_properties(self, action):
        self.svc.show_properties(action.get_active())

    def on_project_execution_menu(self, action):
        menuitem = action.get_proxies()[0]
        menuitem.remove_submenu()
        menuitem.set_submenu(self.svc.create_menu())

    def on_project_add_directory(self, action):
        path = action.contexts_kw.get('dir_name')
        self.svc.add_directory(path)

    def on_project_remove_directory(self, action):
        project = action.contexts_kw.get('project')
        self.svc.remove_project(project)

class ProjectFeaturesConfig(FeaturesConfig):

    def subscribe_all_foreign(self):
        self.subscribe_foreign('contexts', 'dir-menu',
            (self.svc.get_action_group(), 'project-dir-menu.xml'))


class ProjectOptions(OptionsConfig):

    def create_options(self):
        self.create_option(
            'project_dirs',
            _('Project Directories'),
            list,
            [],
            _('The current directories in the workspace'),
            safe=False
        )

        self.create_option(
            'last_project',
            _('Last Project'),
            file,
            '',
            (_('The last project selected. ') +
            _('(Do not change this unless you know what you are doing)')),
            safe=False,
            workspace=True
        )



class ProjectCommandsConfig(CommandsConfig):

    def add_directory(self, project_directory):
        self.svc.add_directory(project_directory)

    def get_view(self):
        return self.svc.get_view()

    def get_current_project(self):
        return self.svc._current

    def get_project_for_document(self, document):
        return self.svc.get_project_for_document(document)

class ProjectDbusConfig(DbusConfig):

    @LEXPORT(in_signature='s')
    def add_directory(self, project_directory):
        self.svc.add_directory(project_directory)
        
    @LEXPORT(out_signature='s')
    def get_current_project_name(self):
        if self.svc._current:
            return self.svc._current.name
        return None
        
    @LEXPORT(out_signature='s')
    def get_current_project_source_directory(self):
        if self.svc._current:
            return self.svc._current.source_directory
        return None


# Service class
class ProjectService(Service):
    """The project manager service"""

    features_config = ProjectFeaturesConfig
    commands_config = ProjectCommandsConfig
    events_config = ProjectEventsConfig
    actions_config = ProjectActionsConfig
    options_config = ProjectOptions
    dbus_config = ProjectDbusConfig

    def pre_start(self):
        self._current = None

    def start(self):
        self._projects = []
        self.set_current_project(None)
        ###
        self.project_list = ProjectListView(self)
        self.project_properties_view = ProjectSetupView(self)
        self._read_options()

        acts = self.boss.get_service('window').actions

        acts.register_window(self.project_list.key,
                             self.project_list.label_text)


    def _read_options(self):
        for dirname in self.opt('project_dirs'):
            dirname = os.path.realpath(dirname)
            if not os.path.exists(dirname):
                self.log("%s does not exist", dirname)
                continue
            try:
                self._load_project(dirname)
            except Exception, e: #XXX: specific?!
                self.log("couldn't load project from %s", dirname)
                self.log.exception(e)

    def _save_options(self):
        self.set_opt('project_dirs', [p.source_directory for p in self._projects])

    def get_view(self):
        return self.project_list

    def add_directory(self, project_directory):
        # Add a directory to the project list
        project_directory = os.path.realpath(project_directory)
        project_file = Project.data_dir_path(project_directory, 'project.json')
        if not os.path.exists(project_file):

        #XXX: this should ask for the project name
        #     and a way to figure the branch name
            if self.boss.window.yesno_dlg(
                _('The directory does not contain a project file, ') +
                _('do you want to create one?')
            ):
                self.create_project_file(project_directory)
            else:
                return
        self.load_and_set_project(project_directory)
        self._save_options()

    def create_project_file(self, project_directory):
        project_name = os.path.basename(project_directory)
        Project.create_blank_project_file(project_name, project_directory)

    def set_current_project(self, project):
        self._current = project
        self.get_action('project_remove').set_sensitive(project is not None)
        self.get_action('project_execute').set_sensitive(project is not None)
        self.get_action('project_properties').set_sensitive(project is not None)
        self.get_action('project_execution_menu').set_sensitive(project is not None)
        if project is not None:
            project.reload()
            self.emit('project_switched', project=project)
            toolitem = self.get_action('project_execute').get_proxies()[0]
            toolitem.set_menu(self.create_menu())
            self.project_properties_view.set_project(project)
            self.get_action('project_execute').set_sensitive(bool(project.targets))
            self.set_opt('last_project', project.source_directory)
            self.boss.editor.set_path(project.source_directory)
            self._target_last = project.options.get('default')
            self.actions.get_action('project_execute_last').props.label = \
                _('Execute Last Controller')

    def set_last_project(self):
        last = self.opt('last_project')
        if last:
            for project in self._projects:
                if project.source_directory == last:
                    self.set_current_project(project)
                    self.project_list.set_current_project(project)

    def load_and_set_project(self, project_file):
        self.set_current_project(self._load_project(project_file))

    def _load_project(self, project_path):
        if not os.path.isdir(project_path):
            self.log(_("Can't load project. Path does not exist: %s") %project_path)
            return None
        project = Project(project_path)
        if project not in self._projects:
            self._projects.append(project)
            self.project_list.project_ol.append(project)
        self.emit('loaded', project=project)
        return project

    def remove_current_project(self):
        if self.remove_project(self._current):
            self.set_current_project(None)

    def remove_project(self, project):
        if self.boss.window.yesno_dlg(
            _('Are you sure you want to remove project "%s" from the workspace?')
            % project.name
        ):
            self._projects.remove(project)
            self.project_list.project_ol.remove(project, select=True)
            self._save_options()
            return True

    def execute_target(self, action, target, project=None):

        if project is None:
            project = self._current

        self.actions.get_action('project_execute_last').props.label = \
            _('Execute: %s') %target.name
        self._target_last = target

        script = environment.get_data_path('project_execute.py')

        env = ['PYTHONPATH=%s%s%s' %(environment.pida_root_path ,os.pathsep,
                                    os.environ.get('PYTHONPATH', sys.path[0]))]

        self.boss.cmd('commander', 'execute',
                commandargs=[
                    'python', script,
                    '--directory', project.source_directory,
                    '--target', target.name
                ],
                cwd=project.source_directory,
                title=_('%s:%s') % (project.name, target.name),
                env=env,
                )

    def execute_last(self):
        if self._target_last:
            self.execute_target(None, self._target_last)

    def create_menu(self):
        if self._current is not None:
            menu = gtk.Menu()
            for target in self._current.targets:
                act = gtk.Action(target,
                    target,
                    target, gtk.STOCK_EXECUTE)
                act.connect('activate', self.execute_target, target)
                mi = act.create_menu_item()
                menu.add(mi)
            menu.show_all()
            return menu

    def show_properties(self, visible):
        if visible:
            self.boss.cmd('window', 'add_detached_view', paned='Plugin',
                view=self.project_properties_view)
        else:
            self.boss.cmd('window', 'remove_view',
                view=self.project_properties_view)

    def get_project_for_document(self, document):
        matches = []
        match = None
        for project in self._projects:
            match = project.get_relative_path_for(document.filename)
            if match is not None:
                matches.append((project, match))
        if not matches:
            return match
        else:
            shortest = min(matches, key=lambda x:len(x[1]))
            return shortest[0], os.sep.join(shortest[1][-3:-1])
    
    def get_project_name(self):
        if self._current:
            return self._current.name
        return None
        


# Required Service attribute for service loading
Service = ProjectService



# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: