def Redo(self):
     self.old_name = self.pattern['name']
     if not self.new_name:
         Notify(tm.main.empty_pattern_name)
         return False
     if self.old_name == self.new_name:
         return False
     if self.new_name in self.doc.patterns:
         Notify(tm.main.pattern_option_already_exist)
         return False
     self.pattern['name'] = self.new_name
     del self.doc.patterns[self.old_name]
     self.doc.patterns[self.new_name] = self.pattern
     wizard.W_PatternNameChanged(self.pattern)
     return True
Beispiel #2
0
 def Version2(data):
     from framework.project import ProjectView
     from framework.dialogs import Notify, SelectFiles, Choice
     projectdata = data.get('projectdata')
     if projectdata:
         Notify(
             _('Project data found in configuration. Please save it as project file.'
               ))
         del data['projectdata']
         while True:
             path = SelectFiles(tm.main.save_project_as,
                                save=True,
                                wildcard=ProjectView.msg_wildcard)
             if path:
                 try:
                     with open(path, 'w') as f:
                         json.dump(dict(data=projectdata),
                                   f,
                                   'utf-8',
                                   ensure_ascii=False)
                         data['projectfile'] = path
                         break
                 except:
                     if not Choice(
                             tm.main.config_convert_project_file_failed):
                         break
             else:
                 if Choice(tm.main.config_abort_project_convert):
                     break
Beispiel #3
0
    def SaveProjectToFile(self, filename):
        docs = set()
        for it in QTreeWidgetItemIterator(self):
            node = it.value()
            if isinstance(node, ProjectNode):
                if node.doc != None:
                    docs.add(node.doc)
        for doc in docs:
            doc.UpdatePropsState(True)

        try:
            projectdata = self.CurrentProjectData(filename)
            with open(filename, 'w') as f:
                json.dump(projectdata, f, 'utf-8', ensure_ascii=False)
            self.DeleteAutosave()
            if self.projectfile and self.projectfile != filename:
                self.UpdateRecentProjects()
            self.projectfile = filename
            self.projectdata = projectdata
        except:
            Notify(tm.main.project_not_accessible)
            self.projectfile = None
        finally:
            if appconfig.get('projectfile') != self.projectfile:
                appconfig['projectfile'] = self.projectfile
                appconfig.SaveSettings()
            self.RefreshProps()
            self.UpdateProject()
Beispiel #4
0
    def SaveMappingToFile(self):
        if self.patterns.currentIndex() != 0:
            pattern, option = self.options_list[self.patterns.itemData(
                self.patterns.currentIndex())][1:]
            pattern_name = pattern.name
            option_name = option.name

            mapping = self.GetRoleMapping()

            data = {
                'version': 0,
                'pattern': pattern_name,
                'option': option_name,
                'roles': mapping
            }

            path, wildcard = SelectFiles(tm.ext.patterns_save_mapping,
                                         save=True,
                                         wildcard='.json file (*.json)')
            if path:
                try:
                    with open(path, 'w') as f:
                        json.dump(data, f, 'utf-8', ensure_ascii=True)
                except:
                    Notify(tm.ext.patterns_err_save)
 def Redo(self):
     if self.delete:
         if self.pattern[
                 'name'] not in self.doc.patterns or self.doc.patterns[
                     self.pattern['name']] != self.pattern:
             return False
         del self.doc.patterns[self.pattern['name']]
         wizard.W_PatternDeleted(self.pattern)
     else:
         if not self.new:
             counter = 1
             name = self.pattern['name']
             while name:
                 for p in self.doc.patterns.iterkeys():
                     if name == p:
                         name = '%s%i' % (self.pattern['name'], counter)
                         counter += 1
                         break
                 else:
                     self.pattern['name'] = name
                     break
         if self.pattern['name'] in self.doc.patterns:
             Notify(tm.main.pattern_already_exist)
             return False
         self.doc.patterns[self.pattern['name']] = self.pattern
         wizard.W_PatternAdded(self.doc, self.pattern, new=self.new)
         self.new = False
     return True
Beispiel #6
0
 def VerifyEditablePaths(cls, paths):
     """Returns true if source already opened for editing."""
     for doc in appdata.documents:
         for p in doc.paths:
             if p in paths:
                 Notify(cls.msg_cantedit.format(p))
                 return False
     return True
Beispiel #7
0
 def VerifySavePath(self, path):
     """Is that path used by other opened documents?"""
     for doc in appdata.documents:
         for p in doc.paths:
             if doc != self and p == path:
                 Notify(self.msg_cantsave.format(p))
                 return False
     return True
Beispiel #8
0
    def SetPatternOption(self, name, mapping=None):
        if not name:
            return

        for i in xrange(self.patterns.count()):
            if self.patterns.itemText(i) == name:
                if self.patterns.currentIndex() != i:
                    self.patterns.setCurrentIndex(i)

                self.pattern, self.option = self.options_list[
                    self.patterns.itemData(i)][1:]

                self.roles.setRowCount(0)
                row_count = len(self.pattern.roles)
                self.roles.setRowCount(row_count)
                new = self.option.get_build_targets()
                for i, v in enumerate(sorted(self.pattern.roles.keys())):
                    if v in new:
                        check = QCheckBox()
                        check.clicked[bool].connect(self.OnMappingRoleChange)
                        self.roles.setCellWidget(i, 0, check)

                    item = QTableWidgetItem(v)
                    item.setFlags(item.flags() & ~Qt.ItemIsEditable)
                    self.roles.setItem(i, 1, item)

                if self.sheet:
                    option_name = name

                    @public.wth('', self.sheet_guard)
                    def f():
                        try:
                            self.xlApp.Interactive = False
                            for v in self.sheet.CustomProperties:
                                if v.Name == 'dot15926.pattern':
                                    v.Value = option_name
                                    break
                            else:
                                self.sheet.CustomProperties.Add(
                                    'dot15926.pattern', option_name)
                        except public.BreakTask:
                            pass
                        except pythoncom.com_error as e:
                            show_excel_error(e)
                        except:
                            log.exception()
                        finally:
                            try:
                                self.xlApp.Interactive = True
                            except:
                                pass

                self.FillRoles(mapping)
                return

        Notify(tm.ext.patterns_err_not_found % name)
    def Redo(self):
        items = self.g.grTemplateGetDependencies(self.template)
        for v in items:
            if not self.g.grIsTemplate(v):
                Notify(tm.main.crossfile_template_specializations_detected)
                return False

        self.index_old = None
        for v in items:
            index_old = self.g.grGetTemplateRoleIndex(v, self.role_name)
            if self.index_old == None:
                self.index_old = index_old
            elif self.index_old != index_old:
                Notify(tm.main.invalid_template_specializations_detected)
                return False

        if self.index_old and self.index_old != self.index:
            for v in items:
                self.g.grSetTemplateRoleIndex(v, self.role_name, self.index)
                wizard.W_TemplateRolesChanged(self.g, v)
            return True
        return False
Beispiel #10
0
 def LoadMappingFromFile(self):
     path, wildcard = SelectFiles(tm.ext.patterns_load_mapping,
                                  multi=False,
                                  wildcard='.json file (*.json)')
     if path:
         try:
             with open(path, 'r') as f:
                 data = json.load(f, 'utf-8')
                 pattern = '.'.join((data['pattern'], data['option']))
                 self.SetPatternOption(pattern, data['roles'])
         except:
             log.exception()
             Notify(tm.ext.patterns_err_load)
    def Redo(self):
        objs = set()
        subj = set()
        self.triples = []
        items = self.g.grTemplateGetDependencies(self.template)
        for v in items:
            if not self.g.grIsTemplate(v):
                Notify(tm.main.crossfile_template_specializations_detected)
                return False

        for v in items:
            triples = self.g.grCollectTemplateRoleTriples(v, self.role_name)
            self.index = self.g.grGetTemplateRoleIndex(v, self.role_name)

            for a in self.g.grSubjects(self.g.p7tm + 'hasTemplate', v):
                if self.g.grHas(a, kb.rdf_type,
                                self.g.p7tm + 'TemplateRoleDescription'):
                    for t in self.g.grTriplesForSubj(a):
                        if t.has_literal and t.p == self.g.p7tm + 'valRoleIndex':
                            other_index = int(t.l)
                            if self.index < other_index:
                                t.with_l(str(other_index - 1)).insertto(self.g)
                                t.deletefrom(self.g)
                                break

            for t in self.g.grTriplesForSubj(self.g.grTemplateGetDescUri(v)):
                if t.has_literal and t.p == self.g.p7tm + 'valNumberOfRoles':
                    t.with_l(str(int(t.l) - 1)).insertto(self.g)
                    t.deletefrom(self.g)
                    break

            for a in triples:
                a.deletefrom(self.g)
                subj.add(a.s)
                if a.has_object:
                    objs.add(a.o)

            self.triples += triples
            wizard.W_TemplateRolesChanged(self.g, v)
            wizard.W_EntityPropertiesChanged(self.g, v)

        for o in objs:
            wizard.W_EntityRelationshipsChanged(self.g, o)

        for s in subj:
            if not self.g.grTriplesForSubj(s):
                wizard.W_EntityDestroyed(self.g, s)

        return True
    def Redo(self):
        self.old_name = self.option.get('name')
        if self.old_name == self.new_name:
            return False
        if self.new_name:
            if self.new_name in (o.get('name')
                                 for o in self.pattern['options']):
                Notify(tm.main.pattern_option_already_exist)
                return False
            self.option['name'] = self.new_name
        elif self.old_name:
            del self.option['name']

        wizard.W_PatternOptionChanged(self.option)
        return True
    def Redo(self):
        self.triples = []
        items = self.g.grTemplateGetDependencies(self.template)
        for v in items:
            if not self.g.grIsTemplate(v):
                Notify(tm.main.crossfile_template_specializations_detected)
                return False

        for v in items:
            index = 0
            for a in self.g.grSubjects(self.g.p7tm + 'hasTemplate', v):
                if self.g.grHas(a, kb.rdf_type,
                                self.g.p7tm + 'TemplateRoleDescription'):
                    prop = self.g.grOneObj(a, self.g.p7tm + 'hasRole')
                    if 'name' in self.role and self.role[
                            'name'] == self.g.grLiteral(
                                prop, kb.ns_rdfs + 'label'):
                        return False
                    for t in self.g.grTriplesForSubj(a):
                        if t.has_literal and t.p == self.g.p7tm + 'valRoleIndex':
                            index = max(int(t.l), index)

            self.role['index'] = index + 1
            self.triples += [
                t for t in self.g.grTemplateRoleTriplesByDesc(v, self.role)
                if t.insertto(self.g)
            ]

            for t in self.g.grTriplesForSubj(self.g.grTemplateGetDescUri(v)):
                if t.has_literal and t.p == self.g.p7tm + 'valNumberOfRoles':
                    t.with_l(str(int(t.l) + 1)).insertto(self.g)
                    t.deletefrom(self.g)
                    break

            wizard.W_TemplateRolesChanged(self.g, v)
            wizard.W_EntityPropertiesChanged(self.g, v)

        return True
    def Redo(self):
        if self.role_name == self.new_name:
            return False
        items = self.g.grTemplateGetDependencies(self.template)
        for v in items:
            if not self.g.grIsTemplate(v):
                Notify(tm.main.crossfile_template_specializations_detected)
                return False

        found = set()
        for v in items:
            role_uri = self.g.grTemplateRoleUri(v, self.role_name)
            if role_uri and role_uri not in found and self.g.grTriplesForSubj(
                    role_uri):
                LiteralTriple.of(role_uri, kb.ns_rdfs + 'label',
                                 self.role_name).deletefrom(self.g)
                LiteralTriple.of(role_uri, kb.ns_rdfs + 'label',
                                 self.new_name).insertto(self.g)
                found.add(role_uri)
                wizard.W_EntityPropertiesChanged(self.g, role_uri)
            wizard.W_TemplateRolesChanged(self.g, v)
        if not found:
            return False
        return True
Beispiel #15
0
 def f():
     if e:
         enc = locale.getpreferredencoding()
         Notify(tm.ext.patterns_err_com.format(e.strerror.decode(enc)))
     else:
         Notify(tm.ext.patterns_err_not_avail)
Beispiel #16
0
    def LoadProjectFromFile(self, filename):
        autosavefile = None

        if filename:
            fdir, fname = os.path.split(filename)
            if os.path.exists(os.path.join(fdir, '.$' + fname)):
                if Choice(tm.main.project_autosave_found):
                    autosavefile = os.path.join(fdir, '.$' + fname)
                else:
                    os.remove(os.path.join(fdir, '.$' + fname))

            if not autosavefile and not os.path.exists(filename):
                Notify(tm.main.project_not_found.format(filename))
                self.projectfile = None
            else:
                self.projectfile = filename
        else:
            if os.path.exists(self.autosave_file):
                if Choice(tm.main.project_autosave_found):
                    autosavefile = self.autosave_file
                else:
                    os.remove(self.autosave_file)
            if not autosavefile:
                self.NewProject()
                return

        autosavefailed = False
        projectfailed = False

        if autosavefile:
            try:
                with open(autosavefile, 'r') as f:
                    projectdata = json.load(f,
                                            'utf-8',
                                            object_hook=DictDecoder())
                ProjectVersionConverter.Check(projectdata)
                self.LoadProject(projectdata)
                self.autosavedata = projectdata
            except:
                log.exception()
                autosavefailed = True

        if self.projectfile:
            try:
                with open(self.projectfile, 'r') as f:
                    projectdata = json.load(f,
                                            'utf-8',
                                            object_hook=DictDecoder())
                ProjectVersionConverter.Check(projectdata)
                if not autosavefile or autosavefailed:
                    self.LoadProject(projectdata)
                self.projectdata = projectdata
            except:
                log.exception()
                projectfailed = True

        if autosavefailed:
            if projectfailed:
                Notify(tm.main.project_autosave_failed)
                self.projectfile = None
            else:
                Notify('%s\n%s' % (tm.main.project_autosave_failed,
                                   tm.main.project_autosave_skipped))
        elif projectfailed:
            Notify(tm.main.project_load_failed)
            self.projectfile = None

        if appconfig.get('projectfile') != self.projectfile:
            appconfig['projectfile'] = self.projectfile
            appconfig.SaveSettings()
        self.RefreshProps()
        self.UpdateProject()
    def Redo(self):
        if self.action == ROLE_DELETE:
            if self.role not in self.part:
                return False

            self.value = self.part[self.role]
            del self.part[self.role]
            wizard.W_PatternOptionPartRoleDeleted(self.part, self.role)

        elif self.action == ROLE_ADD:
            if self.role in self.part:
                Notify(tm.main.pattern_option_part_role_already_exist)
                return False
            self.part[self.role] = self.value
            wizard.W_PatternOptionPartRoleAdded(self.part, self.role)

        elif self.action == ROLE_BIND:
            if self.value == self.part[self.role]:
                return False
            self.old_value = self.part[self.role]
            self.part[self.role] = self.value
            wizard.W_PatternOptionPartRoleChanged(self.part, self.role,
                                                  self.role)

        elif self.action == ROLE_RENAME:
            if self.value == self.role:
                return False
            if self.value in self.part:
                Notify(tm.main.pattern_option_part_role_already_exist)
                return False
            self.part[self.value] = self.part[self.role]
            del self.part[self.role]
            wizard.W_PatternOptionPartRoleChanged(self.part, self.role,
                                                  self.value)

        elif self.action == ROLE_MODIFY:
            if self.role not in self.part:
                return False
            self.old_value = self.part[self.role]
            if self.value[0] == self.role and self.value[1] == self.old_value:
                return False
            if self.value[0] != self.role:
                if self.value[0] in self.part:
                    Notify(tm.main.pattern_option_part_role_already_exist)
                    return False
                del self.part[self.role]
            self.part[self.value[0]] = self.value[1]
            wizard.W_PatternOptionPartRoleChanged(self.part, self.role,
                                                  self.value[0])

        if 'self' in self.part and 'type' in self.part and isinstance(
                self.part['type'],
                basestring) and self.part['type'].startswith('patterns.'):
            self.other = DocumentModifyPatternOptionPartRoles(
                self.option, self.part, 'self', action=ROLE_DELETE)
        elif 'self' not in self.part and (
                'type' not in self.part
                or not isinstance(self.part['type'], basestring)
                or not self.part['type'].startswith('patterns.')):
            self.other = DocumentModifyPatternOptionPartRoles(
                self.option,
                self.part,
                'self',
                value=GenPartName(self.option),
                action=ROLE_ADD)

        if self.other and not self.other.Redo():
            self.other = None

        return True