Commits

Anonymous committed 1dcde7c

Adding new module dialog

  • Participants
  • Parent commits d254ec7

Comments (0)

Files changed (5)

docs/workingon.txt

 - Where does this belong to Project or another class: Project
 - Add Folder.create_file, Folder.create_folder, Folder.get_child
 
+? Use Dialogs instead
+? Testing GUI? again!
 * Should ask the source folder and the package
-* Add to CoreUI
+* Add to UICore
 * Open module after creating
 
 
+? Separation of UI Wizards
+? Better multi-sequence key event handling
 ? Iterator for accessing editor text or LineOrientedOutput
 ? Should workingon.txt be under version control?
 ? Consider using Tix or Pmw or idlelib
         fileMenu.add_separator()
         fileMenu.add_command(label='New File ...', command=self._create_new_file, underline=0)
         fileMenu.add_command(label='New Folder ...', command=self._create_new_folder, underline=0)
+        fileMenu.add_command(label='New Module ...', command=self._create_module, underline=4)
         fileMenu.add_separator()
         fileMenu.add_command(label='Open File ...', command=self._open_file, underline=0)
         fileMenu.add_command(label='Find File ...', command=self._find_file, underline=0)
         if event:
             return 'break'
 
+    def _create_module(self, event=None):
+        if not self.project:
+            tkMessageBox.showerror(parent=self.root, title='No Open Project',
+                                   message='No project is open')
+            return
+        toplevel = Toplevel()
+        toplevel.title('New Module')
+        create_dialog = Frame(toplevel)
+        source_label = Label(create_dialog, text='Source Folder')
+        source_entry = Entry(create_dialog)
+        module_label = Label(create_dialog, text='Module')
+        module_entry = Entry(create_dialog)
+        
+        def create_module():
+            source_folder = self.project.get_resource(source_entry.get())
+            new_module = self.project.create_module(source_folder,
+                                                    module_entry.get())
+            toplevel.destroy()
+        def cancel():
+            toplevel.destroy()
+        source_entry.bind('<Return>', lambda event: create_module())
+        source_entry.bind('<Escape>', lambda event: cancel())
+        module_entry.bind('<Return>', lambda event: create_module())
+        module_entry.bind('<Escape>', lambda event: cancel())
+
+#        ok_button = Button(create_dialog, text='Create', command=create_module)
+#        cancel_button = Button(create_dialog, text='Cancel', command=cancel)
+        source_label.grid(row=0, column=0)
+        source_entry.grid(row=0, column=1)
+        module_label.grid(row=1, column=0)
+        module_entry.grid(row=1, column=1)
+        create_dialog.grid()
+        module_entry.focus_set()
+        toplevel.grab_set()
+        self.root.wait_window(toplevel)
+        if event:
+            return 'break'
+
     def _run_active_editor(self, event=None):
         if not self.get_active_editor():
             tkMessageBox.showerror(parent=self.root, title='No Open Editor',
     def search(self, keyword, start, case=True, forwards=True):
         pass
 
+
 class TextIndex(object):
     '''A class for pointing to a position in a text'''
 
             if self.get_searcher().is_searching():
                 self.get_searcher().cancel_searching()
         self.text.bind('<Control-g>', escape)
-        self.text.bind('<Control-x><Control-x>', lambda event: self.swapMarkAndInsert())
+        def do_swap_mark_and_insert(event):
+            self.swapMarkAndInsert()
+            return 'break'
+        self.text.bind('<Control-x><Control-x>', do_swap_mark_and_insert)
         def goNextPage(event):
             self.nextPage()
             return 'break'
         if self.editor.text.compare(self.index, '>', index.index):
             return +1
         return 0
-    
+
     def _getIndex(self):
         return self.index
 
             os.mkdir(self.root)
         elif not os.path.isdir(self.root):
             raise rope.core.RopeException('Project root exists and is not a directory')
-    
+
     def get_root_folder(self):
         return Folder(self, '')
 
     def get_source_folders(self):
         return self._find_source_folders(self.get_root_folder())
 
+    def create_module(self, src_folder, module):
+        packages = module.split('.')
+        parent = self.get_root_folder()
+        for package in packages[:-1]:
+            parent = parent.get_child(package)
+        return parent.create_file(packages[-1] + '.py')
+
     @staticmethod
     def remove_recursively(file):
         for root, dirs, files in os.walk(file, topdown=False):
         else:
             file_path = file_name
         self.project._create_file(file_path)
+        return self.get_child(file_name)
 
     def create_folder(self, folder_name):
         if self.get_path():
         else:
             folder_path = folder_name
         self.project._create_folder(folder_path)
+        return self.get_child(folder_name)
 
     def get_child(self, name):
         if self.get_path():

ropetest/projecttest.py

         folder_name = 'sampleFolder'
         file_name = 'sample2.txt'
         file_path = folder_name + '/' + file_name
-        self.project.get_root_folder().create_folder(folder_name)
-        parent_folder = self.project.get_resource(folder_name)
+        parent_folder = self.project.get_root_folder().create_folder(folder_name)
         parent_folder.create_file(file_name)
         file = self.project.get_resource(file_path)
         file.write('sample notes')
 
     def testNestedDirectories(self):
         folder_name = 'SampleFolder'
-        self.project.get_root_folder().create_folder(folder_name)
-        parent = self.project.get_resource(folder_name)
+        parent = self.project.get_root_folder().create_folder(folder_name)
         parent.create_folder(folder_name)
         folder_path = os.path.join(self.project.get_root_address(), folder_name, folder_name)
         self.assertTrue(os.path.exists(folder_path) and os.path.isdir(folder_path))
         self.assertTrue(self.project.get_resource(self.projectMaker.getSampleFolderName()) in folders)
 
     def test_nested_folder_get_files(self):
-        self.project.get_root_folder().create_folder('top')
-        parent = self.project.get_resource('top')
+        parent = self.project.get_root_folder().create_folder('top')
         parent.create_file('file1.txt')
         parent.create_file('file2.txt')
         files = parent.get_files()
         self.assertEquals(0, len(parent.get_folders()))
         
     def test_nested_folder_get_folders(self):
-        self.project.get_root_folder().create_folder('top')
-        parent = self.project.get_resource('top')
+        parent = self.project.get_root_folder().create_folder('top')
         parent.create_folder('dir1')
         parent.create_folder('dir2')
         folders = parent.get_folders()
         self.assertTrue(self.project.get_root_folder() in source_folders)
 
     def test_src_source_folder(self):
-        self.project.get_root_folder().create_folder('src')
-        src = self.project.get_resource('src')
+        src = self.project.get_root_folder().create_folder('src')
         src.create_file('sample.py')
         source_folders = self.project.get_source_folders()
         self.assertEquals(1, len(source_folders))
         self.assertTrue(self.project.get_resource('src') in source_folders)
 
     def test_packages(self):
-        self.project.get_root_folder().create_folder('src')
-        src = self.project.get_resource('src')
-        src.create_folder('package')
-        pkg = self.project.get_resource('src/package')
+        src = self.project.get_root_folder().create_folder('src')
+        pkg = src.create_folder('package')
         pkg.create_file('__init__.py')
         source_folders = self.project.get_source_folders()
         self.assertEquals(1, len(source_folders))
         self.assertTrue(src in source_folders)
 
     def test_multi_source_folders(self):
-        self.project.get_root_folder().create_folder('src')
-        src = self.project.get_resource('src')
-        src.create_folder('package')
-        package = self.project.get_resource('src/package')
+        src = self.project.get_root_folder().create_folder('src')
+        package = src.create_folder('package')
         package.create_file('__init__.py')
-        self.project.get_root_folder().create_folder('test')
-        test = self.project.get_resource('test')
+        test = self.project.get_root_folder().create_folder('test')
         test.create_file('alltests.py')
         source_folders = self.project.get_source_folders()
         self.assertEquals(2, len(source_folders))
 
     def test_folder_get_child_nested(self):
         root = self.project.get_root_folder()
-        root.create_folder('myfolder')
-        folder = root.get_child('myfolder')
+        folder = root.create_folder('myfolder')
         folder.create_file('myfile.txt')
         folder.create_folder('myfolder')
-        self.assertEquals(self.project.get_resource('myfolder/myfile.txt'), 
+        self.assertEquals(self.project.get_resource('myfolder/myfile.txt'),
                           folder.get_child('myfile.txt'))
-        self.assertEquals(self.project.get_resource('myfolder/myfolder'), 
+        self.assertEquals(self.project.get_resource('myfolder/myfolder'),
                           folder.get_child('myfolder'))
 
-        
+    def test_module_creation(self):
+        new_module = self.project.create_module(self.project.get_root_folder(), 'module')
+        self.assertEquals(self.project.get_resource('module.py'), new_module)
+
+    def test_packaged_module_creation(self):
+        package = self.project.get_root_folder().create_folder('package')
+        new_module = self.project.create_module(self.project.get_root_folder(), 'package.module')
+        self.assertEquals(self.project.get_resource('package/module.py'), new_module)
+
+
 class FileFinderTest(unittest.TestCase):
     def setUp(self):
         unittest.TestCase.setUp(self)
         self.assertEquals(1, len(result3))
         result4 = self.finder.find_files_starting_with('a')
         self.assertEquals(3, len(result4))
-        
+
 
 class TestPythonFileRunner(unittest.TestCase):
     def setUp(self):
             os.remove(temp_file_name)
 
     def test_setting_pythonpath(self):
-        self.project.get_root_folder().create_folder('src')
-        src = self.project.get_resource('src')
+        src = self.project.get_root_folder().create_folder('src')
         src.create_file('sample.py')
         src.get_child('sample.py').write('def f():\n    pass\n')
         self.project.get_root_folder().create_folder('test')