Commits

Anonymous committed b34e5e9

Adding new package dialog

  • Participants
  • Parent commits 6d77609

Comments (0)

Files changed (4)

docs/workingon.txt

 * Make a new package @ 1
   Should ask the source folder.
 
+- Create package makes __init__.py in the package
+- create_package returns the folder
+- Add to core
 
-- Where does this belong to Project or another class: Project
-- Add Folder.create_file, Folder.create_folder, Folder.get_child
-- Should ask the source folder and the package
-- Add to UICore
-- Open module after creating
+- Remove duplication from core
 
 ? Use Dialogs instead
 ? GUI Testing Problems? again!
         fileMenu.add_command(label='New File ...', command=self._create_new_file_dialog, underline=0)
         fileMenu.add_command(label='New Folder ...', command=self._create_new_folder_dialog, underline=0)
         fileMenu.add_command(label='New Module ...', command=self._create_module_dialog, underline=4)
+        fileMenu.add_command(label='New Package ...', command=self._create_package_dialog, underline=4)
         fileMenu.add_separator()
         fileMenu.add_command(label='Open File ...', command=self._open_file_dialog, underline=0)
         fileMenu.add_command(label='Find File ...', command=self._find_file_dialog, underline=0)
         source_entry.bind('<Escape>', lambda event: cancel())
         module_entry.bind('<Return>', lambda event: do_create_module())
         module_entry.bind('<Escape>', lambda event: cancel())
-
-#        ok_button = Button(create_dialog, text='Create', command=do_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)
         if event:
             return 'break'
 
+    def _create_package_dialog(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 Package')
+        create_dialog = Frame(toplevel)
+        source_label = Label(create_dialog, text='Source Folder')
+        source_entry = Entry(create_dialog)
+        package_label = Label(create_dialog, text='Package')
+        package_entry = Entry(create_dialog)
+        
+        def do_create_package():
+            source_folder = self.project.get_resource(source_entry.get())
+            new_package = self.project.create_package(source_folder,
+                                                      package_entry.get())
+            self._open_file_resource(new_package.get_child('__init__.py'))
+            toplevel.destroy()
+        def cancel():
+            toplevel.destroy()
+        source_entry.bind('<Return>', lambda event: do_create_package())
+        source_entry.bind('<Escape>', lambda event: cancel())
+        package_entry.bind('<Return>', lambda event: do_create_package())
+        package_entry.bind('<Escape>', lambda event: cancel())
+        source_label.grid(row=0, column=0)
+        source_entry.grid(row=0, column=1)
+        package_label.grid(row=1, column=0)
+        package_entry.grid(row=1, column=1)
+        create_dialog.grid()
+        package_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 get_source_folders(self):
         return self._find_source_folders(self.get_root_folder())
 
-    def create_module(self, src_folder, module):
-        packages = module.split('.')
+    def create_module(self, src_folder, new_module):
+        packages = new_module.split('.')
         parent = src_folder
         for package in packages[:-1]:
             parent = parent.get_child(package)
         return parent.create_file(packages[-1] + '.py')
 
+    def create_package(self, src_folder, new_package):
+        packages = new_package.split('.')
+        parent = src_folder
+        for package in packages[:-1]:
+            parent = parent.get_child(package)
+        created_package = parent.create_folder(packages[-1])
+        created_package.create_file('__init__.py')
+        return created_package
+
     @staticmethod
     def remove_recursively(file):
         for root, dirs, files in os.walk(file, topdown=False):

ropetest/projecttest.py

 
     def test_module_creation(self):
         new_module = self.project.create_module(self.project.get_root_folder(), 'module')
+        self.assertFalse(new_module.is_folder())
         self.assertEquals(self.project.get_resource('module.py'), new_module)
 
     def test_packaged_module_creation(self):
         new_module = self.project.create_module(src, 'pkg.mod')
         self.assertEquals(self.project.get_resource('src/pkg/mod.py'), new_module)
 
+    def test_package_creation(self):
+        new_package = self.project.create_package(self.project.get_root_folder(), 'pkg')
+        self.assertTrue(new_package.is_folder())
+        self.assertEquals(self.project.get_resource('pkg'), new_package)
+        self.assertEquals(self.project.get_resource('pkg/__init__.py'), 
+                          new_package.get_child('__init__.py'));
+
+    def test_nested_package_creation(self):
+        package = self.project.create_package(self.project.get_root_folder(), 'pkg1')
+        nested_package = self.project.create_package(self.project.get_root_folder(), 'pkg1.pkg2')
+        self.assertEquals(self.project.get_resource('pkg1/pkg2'), nested_package)
+
+    def test_packaged_package_creation_with_nested_src(self):
+        src = self.project.get_root_folder().create_folder('src')
+        package = self.project.create_package(src, 'pkg1')
+        nested_package = self.project.create_package(src, 'pkg1.pkg2')
+        self.assertEquals(self.project.get_resource('src/pkg1/pkg2'), nested_package)
+
 
 class FileFinderTest(unittest.TestCase):
     def setUp(self):