def _onSelectCallback(self, binds, *args):

        editor = self.Notebook.SelectedFrame().editor
        bindFilePath = editor.FilePath
        options = {}
        options['initialfile'] = "keybinds.kst"
        options['title'] = "Select File Destination"
        options['filetypes'] = (("Keybind Export Files", "*.kst"),
                                ("All Files", "*.*"))
        options['defaultextension'] = "kst"
        self.update()
        filePath = filedialog.asksaveasfilename(**options)
        self.update()
        if (filePath == ''):
            return False

        bindFile = BindFile(binds, filePath=bindFilePath)
        boundFilesSource = self._getBoundFilesSource()
        bindFileCollection = BindFileCollection()
        bindFileCollection.Load(bindFilePath,
                                bindFile=bindFile,
                                boundFilesSource=boundFilesSource)
        bindFileCollection.Serialize(filePath)

        return True
 def test_New(self):
     target = BindFileCollection()
     target.New()
     self.assertEqual(None, target.File.Binds, "Unexpected empty bind length")
     target.New(defaults=True)
     self.assertEqual(99, len(target.File.Binds), "Unexpected default bind length")
     self.assertEqual("'", target.File.Binds[0].Key, "Unexpected default key")
     self.assertEqual("quickchat", target.File.Binds[0].Commands[0].Name, "Unexpected default command")
     self.assertEqual("", target.File.Binds[0].Commands[0].Text, "Unexpected default command text")
 def test_NonCircularChain(self):
     target = BindFileCollection()
     target.Load(".\\TestReferences\\Jock Tamson\\keybinds.txt")
     actual = target.KeyChains
     self.assertEqual(len(actual), 2)
     self.assertEqual(actual[0].Key, "MBUTTON")
     self.assertEqual(actual[0].Chord, "")
     self.assertEqual(len(actual[0].BoundFiles), 2)
     self.assertEqual(actual[1].Key, "MOUSECHORD")
     self.assertEqual(actual[1].Chord, "")
     self.assertEqual(len(actual[1].BoundFiles), 4)
 def test_Load(self):
     target = BindFileCollection()
     target.Load(".\\TestReferences\\Field Test\\keybinds.txt")
     actual = target.KeyChains
     self.assertEqual(len(actual), 3)
     self.assertEqual(actual[0].Key, "I")
     self.assertEqual(actual[0].Chord, "")
     self.assertEqual(len(actual[0].BoundFiles), 2)
     self.assertEqual(actual[1].Key, "MBUTTON")
     self.assertEqual(actual[1].Chord, "")
     self.assertEqual(len(actual[1].BoundFiles), 2)
     self.assertEqual(actual[2].Key, "NUMPAD0")
     self.assertEqual(actual[2].Chord, "SHIFT")
     self.assertEqual(len(actual[2].BoundFiles), 1)
 def GetCollection(self, includeUnbound=False):
     if (self.Dictionary == None):
         return None
     keyChains = []
     if (self.Dictionary[KEY_CHAINS] != NONE):
         if includeUnbound:
             chains = self.Dictionary[KEY_CHAINS]
         else:
             chains = [
                 p for p in self.Dictionary[KEY_CHAINS] if p[KEY] != UNBOUND
             ]
         for keyChain in chains:
             boundFiles = [
                 BindFile(repr=p[REPR], filePath=p[PATH])
                 for p in keyChain[BOUND_FILES]
             ]
             newChain = Keychain(key=keyChain[KEY],
                                 chord=keyChain[CHORD],
                                 boundFiles=boundFiles)
             keyChains.append(newChain)
     root = BindFile(filePath=self.Dictionary[PATH],
                     repr=self.Dictionary[ROOT])
     collection = BindFileCollection(filePath=root.FilePath,
                                     bindFile=root,
                                     keyChains=keyChains)
     return collection
Example #6
0
    def Open(self, fileName=None):
        if (fileName == None):
            options = {}
            filePath = self.FilePath
            if (filePath != None):
                options['initialfile'] = filePath
                options['initialdir'] = os.path.dirname(filePath)
            options['title'] = "Open Keybind File"
            options['filetypes'] = (("Text Files", "*.txt"), ("All Files",
                                                              "*.*"))
            options['defaultextension'] = "txt"
            options['multiple'] = False
            self.master.update()
            fileName = filedialog.askopenfilename(**options)
            self.master.update()

        if (fileName != ''):
            self.Reset()
            collection = BindFileCollection()
            collection.Load(fileName)
            self.Load(collection)
Example #7
0
    def ImportBinds(self, filePath):

        if (self.editor == None):
            return
        importCollection = BindFileCollection()
        importCollection.Deserialize(filePath)
        boundFiles = importCollection.GetBoundFiles()
        if (((self.editor.FilePath == None) or
             (self.editor.FilePath == NEW_FILE)) and (len(boundFiles) > 0)):
            response = messagebox.askokcancel(
                'Path Needed For Linked Files',
                'You must choose a target path for this file to set paths for linked files.\n'
                + 'The paths will be set, but no files will be saved yet.')
            if (not response):
                return
            options = {}
            options['initialfile'] = "keybinds.txt"
            options['title'] = "Select Target Destination for Linked Files"
            options['filetypes'] = (("Keybind Files", "*.txt"), ("All Files",
                                                                 "*.*"))
            options['defaultextension'] = "txt"
            self.master.update()
            pointPath = filedialog.asksaveasfilename(**options)
            self.master.update()
            if (pointPath == ''):
                return False
            self.FilePath = pointPath
        else:
            pointPath = self.editor.FilePath

        importCollection.RepointFilePaths(pointPath)
        if (pointPath != None):
            self.editor.FilePath = pointPath
            self.editor.PathLabel.configure(text=self.editor.FilePath)
            self.FilePath = self.editor.FilePath
            self.viewFrame.Directory = os.path.dirname(self.editor.FilePath)
            self.viewFrame.Dictionary[PATH] = self.editor.FilePath

        boundFiles = importCollection.GetBoundFiles()
        if (len(boundFiles) > 0):
            #put them in the orphange so refresh can find them
            orphans = [{
                PATH: boundFile.FilePath,
                REPR: boundFile.__repr__(),
                EDITOR: None,
                SELECTED_TAG: False
            } for boundFile in boundFiles]
            self.viewFrame.GetOrphanage(True, orphans)

        for bind in importCollection.File.Binds:
            self.editor.NewBindCallback(True, bind)
 def test_Serialization(self):
     filePath = "temp.json"
     collectionFilePath = os.path.abspath(".\\TestReferences\\Jock Tamson\\keybinds.txt")
     if (path.exists(filePath)):
         remove(filePath)
     try:
         target = BindFileCollection()
         target.Load(collectionFilePath)
         target.Serialize(filePath)
         target = BindFileCollection()
         target.Deserialize(filePath)
         expected = BindFileCollection()
         expected.Load(collectionFilePath)
         expected.RepointFilePaths("C:\\keybinds.txt", True)
         self.assertEqual(target.File.FilePath, expected.File.FilePath, "Unexpedted name following round trip")
         self.assertEqual(target.File.__repr__(), expected.File.__repr__(), "Unexpected repr following round trip")
         binds = target.GetBoundFiles()
         expectedBinds = expected.GetBoundFiles()
         self.assertEqual(len(binds), len(expectedBinds), "Did not find expected number of bound files") 
         for idx, expectedBind in enumerate(expectedBinds):
             self.assertEqual(binds[idx].FilePath, expectedBind.FilePath, "Did not find expected path for bound file " + str(idx))
             self.assertEqual(binds[idx].__repr__(), expectedBind.__repr__(), "Did not find expected repr for bound file " + str(idx))
     finally:
         if (path.exists(filePath)):
             remove(filePath)
    def test_RepointBindFileCollection(self):

        def _compare():
            for idx, entry in enumerate(expected):
                filePath = entry[0]
                boundFilePaths = entry[1]
                if (idx == 0):
                    self.assertEqual(target.FilePath, filePath)
                    bindFile = target.File
                    rootBoundFiles = target.GetBoundFiles()
                else:
                    bindFile = rootBoundFiles[idx - 1]
                self.assertEqual(bindFile.FilePath, filePath)
                actualBoundFilePaths = []
                for bind in bindFile.GetLoadFileBinds():
                    for command in bind.GetLoadFileCommands():
                        actualBoundFilePaths.append(command.GetTargetFile())
                for boundFileIndex, boundFilePath in enumerate(boundFilePaths):
                    self.assertEqual(actualBoundFilePaths[boundFileIndex], boundFilePath, "Did not find expected path for idx " + str(idx))

        target = BindFileCollection()
        target.Load(".\\TestReferences\\Field Test\\keybinds.txt")
        target.RepointFilePaths(".\\NewPath\\Field Test\\keybinds.txt")
        expected = [
            (os.path.abspath(".\\NewPath\\Field Test\\keybinds.txt" ) , [os.path.abspath(p) for p in [
            "./NewPath/Field Test/I1.txt", 
            "./NewPath/Field Test/MBUTTON1.txt", 
            "./NewPath/Field Test/keybinds.txt", 
            "./TestReferences/keybinds(1).txt"]]),
            (os.path.abspath(".\\NewPath\\Field Test\\I1.txt" ) , [os.path.abspath(p) for p in [
            "./NewPath/Field Test/I2.txt"]]),
            (os.path.abspath(".\\NewPath\\Field Test\\I2.txt" ) , [os.path.abspath(p) for p in [
            "./NewPath/Field Test/I1.txt"]]),
            (os.path.abspath(".\\NewPath\\Field Test\\MBUTTON1.txt" ) , [os.path.abspath(p) for p in [
            "./NewPath/Field Test/MBUTTON2.txt"]]),
            (os.path.abspath(".\\NewPath\\Field Test\\MBUTTON2.txt" ) , [os.path.abspath(p) for p in [
            "./NewPath/Field Test/MBUTTON1.txt"]])]
        _compare()
        
        target = BindFileCollection()
        target.Load(".\\TestReferences\\Field Test\\keybinds.txt")
        target.RepointFilePaths(".\\TestReferences\\Field Test\\new_keybinds.txt")
        expected = [
            (os.path.abspath(".\\TestReferences\\Field Test\\new_keybinds.txt" ) , [os.path.abspath(p) for p in [
            "./TestReferences/Field Test/I1(1).txt", 
            "./TestReferences/Field Test/MBUTTON1(1).txt", 
            "./TestReferences/Field Test/new_keybinds.txt", 
            "./TestReferences/keybinds(1).txt"]]),
            (os.path.abspath(".\\TestReferences\\Field Test\\I1(1).txt" ) , [os.path.abspath(p) for p in [
            "./TestReferences/Field Test/I2(1).txt"]]),
            (os.path.abspath(".\\TestReferences\\Field Test\\I2(1).txt" ) , [os.path.abspath(p) for p in [
            "./TestReferences/Field Test/I1(1).txt"]]),
            (os.path.abspath(".\\TestReferences\\Field Test\\MBUTTON1(1).txt" ) , [os.path.abspath(p) for p in [
            "./TestReferences/Field Test/MBUTTON2(1).txt"]]),
            (os.path.abspath(".\\TestReferences\\Field Test\\MBUTTON2(1).txt" ) , [os.path.abspath(p) for p in [
            "./TestReferences/Field Test/MBUTTON1(1).txt"]])]
        _compare()
        
        target = BindFileCollection()
        target.Load(".\\TestReferences\\Field Test\\keybinds.txt")
        target.RepointFilePaths("C:\\keybinds.txt")
        expected = [
            (os.path.abspath("C:\\keybinds.txt" ) , [os.path.abspath(p) for p in [
            "C:\\I1.txt", 
            "C:\\MBUTTON1.txt", 
            "C:\\keybinds.txt", 
            "./TestReferences/keybinds(1).txt"]]),
            (os.path.abspath("C:\\I1.txt" ) , [os.path.abspath(p) for p in [
            "C:\\I2.txt"]]),
            (os.path.abspath("C:\\I2.txt" ) , [os.path.abspath(p) for p in [
            "C:\\I1.txt"]]),
            (os.path.abspath("C:\\MBUTTON1.txt" ) , [os.path.abspath(p) for p in [
            "C:\\MBUTTON2.txt"]]),
            (os.path.abspath("C:\\MBUTTON2.txt" ) , [os.path.abspath(p) for p in [
            "C:\\MBUTTON1.txt"]])]
        _compare()
        self.rowconfigure(0, weight=0)

        self.FileName = tk.StringVar()
        self.NameEdit = KeystoneEntry(self, textvariable=self.FileName)
        self.NameLabel = KeystoneLabel(self, textvariable=self.FileName)
        name = GetDirPathFromRoot(self.RootPath, self.Link.FilePath)
        self.FileName.set(name)
        self.FileName.trace("w", self.OnSetDirty)
        self.NameLabel.grid(row=0, column=1, sticky='nsew')

        self.Button = KeystoneButton(self,
                                     text="...",
                                     width=3,
                                     command=self.OnEdit)
        self.Button.grid(row=0, column=0, sticky="nsew")

        self.Editing = False


if (__name__ == "__main__"):
    win = tk.Tk()
    refFilePath = "./TestReferences/Field Test/keybinds.txt"
    collection = BindFileCollection(refFilePath)
    keychain = Keychain(collection, "I")
    bindFile = ReadBindsFromFile("./TestReferences/Field Test/I1.txt")
    target = Keylink(bindFile, "I")
    editor = EditKeylink(win, target, keychain)
    editor.pack(anchor='n', fill='both', expand=True, side='left')

    tk.mainloop()
Example #11
0
 def New(self, defaults: bool = False):
     editor = EditBindFile(self.editFrame)
     editor.New(defaults)
     bindFile = editor.Get()
     collection = BindFileCollection(None, bindFile)
     self.Load(collection)