Community
    • Login

    Feature Request: Sort by IP Address (CIDR Notation)

    Scheduled Pinned Locked Moved General Discussion
    30 Posts 5 Posters 31.0k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • AvianographerA
      Avianographer
      last edited by

      Claudia, are you familiar with Python’s netaddr module? I seem to be having all sorts of trouble getting this to work. Here is what I have so far (including your code at the top).

      ipList = [] 												# used to save the ips and do sorting    
      
      def create_ip_list(line_content, line_number, total_lines): # function gets called for each line
          if line_content.find('/') > -1:                         # simple check
              ip, mask = line_content.split('/')                  # first split mask bits from ip
              o1, o2, o3, o4 = [int(x) for x in ip.split('.')]    # split ip to its octets
      
              if not (o1, o2, o3, o4, int(mask)) in ipList:       # looking for duplicates, check if ip is already in list
                  ipList.append((o1, o2, o3, o4, int(mask)))      # not found in list, so append to list
      
      editor.forEachLine(create_ip_list)                          # main function starts here
      ipList.sort()                                               # we have all ips, let's sort it
      
      editor.beginUndoAction()                                    # set an undo point, in case of there is a need to undo all
      editor.clearAll()                                           # clear editor content
         
      for ip in ipList:                                           # iterating over ip list previously saved and
          editor.appendText('{0}.{1}.{2}.{3}/{4}\n'.format(*ip))  # write sorted ips to the editor
          
      editor.endUndoAction()                                      # inform editor about action end
      
      ipRange = []
      	
      def create_range(line_content, line_number, total_lines):   # function gets called for each line
      	ipRange.append(IPNetwork(line_content))					# append to list
      
      editor.forEachLine(create_range)							# main function starts here
      
      cidr_merge(ipRange)											# use netaddr cidr_merge to merge CIDR ranges
      
      editor.beginUndoAction()									# set an undo point, in case there is a need to undo all
      editor.clearAll()											# clear editor content
      
      for ip in ipRange:											# iterating over ip list previously saved and
      	editor.appentText(ip)									# write merged CIDR ranges to the editor
      
      editor.endUndoAction()										# inform editor about action end
      

      I keep getting:

      raise AddrFormatError('invalid IPNetwork %s' % addr)
      netaddr.core.AddrFormatError: Invalid IPNetwork
      

      It’s like IPNetwork doesn’t realize ‘ip’ is a string in the format it wants for some reason.

      Claudia FrankC 1 Reply Last reply Reply Quote 0
      • Claudia FrankC
        Claudia Frank @Avianographer
        last edited by

        Hello Dustin,

        no, don’t have used it yet but could it be that it is strict about additional eols?
        As i converted ip and mask to int, any eol char has been stripped silently.
        Maybe give it a try with

        ipRange.append(IPNetwork(line_content.strip())) 
        

        Which netaddr version do you use?

        Cheers
        Claudia

        1 Reply Last reply Reply Quote 1
        • Claudia FrankC
          Claudia Frank
          last edited by

          Hello Dustin,

          I’ve downloaded netaddr (0.7.18) and it isn’t strict about the eol.
          But it is strict about getting nothing ;-)
          I assume you have an empty line, one reason why I used the simple check in my create_ip_list function ;-)

          Cheers
          Claudia

          1 Reply Last reply Reply Quote 1
          • AvianographerA
            Avianographer
            last edited by

            Alright, here is the final script that does everything I need it to.

            from netaddr import *										# import everything from the netaddr module
            
            ipList = [] 												# initialize the array to store the IPNetwork objects
            
            def createIpList(lineContents, lineNumber, totalLines): 	# function used to fill ipList with each line
                if lineContents.find('/') > -1:                         # verify that it is not a blank line by checking for the presence of the "/" in a CIDR range
            		ipList.append(IPNetwork(lineContents))				# append to ipList
            
            editor.forEachLine(createIpList)							# main function starts here
            
            result = cidr_merge(ipList)									# use netaddr cidr_merge to merge CIDR ranges. It auto sorts and de-duplicates.
            
            editor.beginUndoAction()									# set an undo point, in case there is a need to undo all
            editor.clearAll()											# clear editor content
            
            for ip in result:											# iterating over ipList previously saved and
            	editor.appendText(ip)									# write merged CIDR ranges to the editor
            editor.appendText("\n")									# add a newline since the IPNetwork object doesn't include one
            
            editor.endUndoAction()										# inform editor about action end
            

            Thanks to everyone, especially Claudia, I now have something that goes beyond my original intentions and fully automates my task. All I have to do is copy/paste/run. So happy!

            Also, it turns out cidr_merge sorts and de-duplicates, as well, so I only needed that one function.

            Claudia FrankC SuncatcherS 2 Replies Last reply Reply Quote 0
            • Claudia FrankC
              Claudia Frank @Avianographer
              last edited by

              Hello Dustin,

              nice to see that you did it and thank you for pointing me to the netaddr module.
              I have played a little with it and I can already see two dns tasks which can take usage
              of it.

              Cheers
              Claudia

              1 Reply Last reply Reply Quote 1
              • SuncatcherS
                Suncatcher
                last edited by Suncatcher

                I would greatly appreciate this feature too, and preferably out-of-the box, i.e. without any plugins. Or it’ll be good to built-in this functionality into a plugin.
                I noticed that TextFX plugin is dying, so is there any other perspective plugin that can implement this?
                guy038, it’s a cool method that was proposed by you, and it works out-of-the-box, which is an advantage over Pythonscript! Thanks for your effort! Brilliant!

                1 Reply Last reply Reply Quote 0
                • SuncatcherS
                  Suncatcher
                  last edited by

                  BTW, I noticed that last version of Pythonscript was published in 2014, and it worries me a lot. I wasn’t able to install it from plugin manager (it threw unknown exception) and had to do it manually.
                  So with the every new version of NPP compatibility issues will be bigger and bigger, and we should search for a replacement anyway.

                  Scott SumnerS 1 Reply Last reply Reply Quote 0
                  • Scott SumnerS
                    Scott Sumner @Suncatcher
                    last edited by

                    @Suncatcher

                    I don’t know that you should be “worried, a lot” about the Pythonscript plugin being last published in 2014. Perhaps that just means it is very stable and has few bugs in need of fixing. :-)

                    What does “search for a replacement” mean??

                    @Dave-Brotherstone is the author of the Pythonscript plugin, as well as the PluginManager plugin. He has recently been working a lot on updating the PluginManager and finding a new good site for hosting the plugins it manages. I think this work also includes heading toward a build of a 64-bit version…so he is busy, but I’m guessing that when that work is complete he will also strive to achieve a 64-bit build of Pythonscript, as well. So I think it is far from dead, even though it hasn’t been updated since 2014. Just guesses, but maybe educated guesses?

                    I can think of a few pieces of software that I use that haven’t had new releases since 2007-2009, so to me 2014 is relatively recent!

                    SuncatcherS Scott SumnerS 2 Replies Last reply Reply Quote 2
                    • SuncatcherS
                      Suncatcher @Scott Sumner
                      last edited by

                      @Scott-Sumner said:

                      Perhaps that just means it is very stable and has few bugs in need of fixing. :-)

                      Bold claim!)
                      For me it often means that project is simply abandoned. Abandoned and not maintained for a long time, which means that:

                      • the amount of bugs increased exponentially since then
                      • new features haven’t been implemented for a long time too, whereas technologies are moving forward.
                      • not to mention that in most cases those projects have poor community where it’s impossible to resolve any issue.

                      But this is a common case and maybe Pythonscipt is another thing.

                      P.S. Considering that author of Plugin Manager and author of Pythonscript is a same person, and he’s been working a lot on updating the PluginManager, it is especially strange to face issues while installing PS from Manager. I was unable to do it, and not only I.

                      Scott SumnerS 1 Reply Last reply Reply Quote 0
                      • Scott SumnerS
                        Scott Sumner @Suncatcher
                        last edited by

                        @Suncatcher

                        It IS rather strange that the Pythonscript plugin has trouble installing via PluginManager, given a common author, I’ll grant you that.

                        I don’t know the full explanation of the Pythonscript + PluginManager problems, but I haven’t let that discourage me. As you have discovered, installing Pythonscript another way works. I have been using Pythonscript deeply since my very first day of using Notepad++, and I can say I’ve found bugs, but not many. Workarounds are the mainstay of Notepad++ (power) users. Bugs exist in software. Features are lacking. These are just facts.

                        1 Reply Last reply Reply Quote 0
                        • SuncatcherS
                          Suncatcher @Avianographer
                          last edited by

                          @Dustin-Cook BTw, the script from Dustin doesn’t work for me (as well as the first ones), it just cleans the screen with the test data. Maybe I’m doing smth wrong?

                          1 Reply Last reply Reply Quote 0
                          • Scott SumnerS
                            Scott Sumner @Scott Sumner
                            last edited by

                            @Scott-Sumner said:

                            @Dave-Brotherstone is the author of the Pythonscript plugin, as well as the PluginManager plugin

                            It appears Dave may be @bruderstein on this forum…maybe if he sees this he could comment on the status of Pythonscript…I’m curious about the state of even the 32-bit version as I just got bit again by the notepad.getFiles() bug. :(

                            That bug is detailed here: https://github.com/bruderstein/PythonScript/issues/22

                            I’d fix the bug in a local build as a temporary measure, but thus far I have been unable to build Pythonscript from source (okay, I haven’t worked extensively on getting it to build, but Notepad++ is an easy one to build, and I was hoping for the same from Pythonscript).

                            Claudia FrankC 1 Reply Last reply Reply Quote 0
                            • Claudia FrankC
                              Claudia Frank @Scott Sumner
                              last edited by

                              @Scott-Sumner

                              Scott, what is the error you get? In the past I compiled it a couple of times
                              and can’t remember that there was some trouble doing it.

                              Cheers
                              Claudia

                              Scott SumnerS 1 Reply Last reply Reply Quote 0
                              • Scott SumnerS
                                Scott Sumner @Claudia Frank
                                last edited by

                                @Claudia-Frank

                                Since we’re already way off topic, can we take a discussion of building Pythonscript offline to email? If an offline discussion turns up something really interesting, I can post back here (under a new topic) to share with the Community.

                                Claudia FrankC 1 Reply Last reply Reply Quote 0
                                • Claudia FrankC
                                  Claudia Frank @Scott Sumner
                                  last edited by

                                  @Scott-Sumner

                                  sure, @guy038 as far as I remember you already in contact with Scott,
                                  would you be so kind and forward my email to him?

                                  Thanks
                                  Claudia

                                  1 Reply Last reply Reply Quote 1
                                  • guy038G
                                    guy038
                                    last edited by guy038

                                    Hello Claudia,

                                    No problem : I’ve just sent your e-mail address to Scott !

                                    Good Python discussions, with Scott ;-)

                                    Cheers,

                                    guy038

                                    Claudia FrankC 1 Reply Last reply Reply Quote 1
                                    • Claudia FrankC
                                      Claudia Frank @guy038
                                      last edited by

                                      @guy038 - Thank you very much.

                                      Cheers
                                      Claudia

                                      1 Reply Last reply Reply Quote 0
                                      • First post
                                        Last post
                                      The Community of users of the Notepad++ text editor.
                                      Powered by NodeBB | Contributors