Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

Auto Detect File Language (Shebang &...

juntalis
2011-10-03
2013-01-25
  • juntalis
    juntalis
    2011-10-03

    Still a work in progress, but I've begun writing a script to auto-detect the language of a file when Notepad++ is unable to by default. (Extension-less files, etc) At the moment, it has a few simple detections, all of which are configurable in the config file generated at config\py_autolang.cfg. I'll start putting together some documentation for the different options, but for now, you can learn more about each option by checking the method LanguageAutoDetector.__default_config. So far, it does the following detections:

    • Filename matching - Files named configure, for instance are automatically set to the bash language by default.

    • Partial filename matching - By default, Makefile\..+ (so Makefile.win32 for instance) will have their language set to makefile.

    • Shebang - Checks the first line for the familiar #!/usr/bin/env  or #!/usr/bin/. It will match windows paths, linux paths, or even situations where there is no path, such as #!python.exe

    • Try_Shebang - If a premapped shebang does not exist, try using the program name as the language.

    • Contains_String - If a file contains a regular expression, set the language accordingly.

    I still need to write code for some of the configuration values, such as the file_load_event options. (Right now, you'll need to run the script to add the callback, and then it will automatically try to detect on any file load. I'll change that soon to reflect the options set) I'm also going to integrate some caching so that the load time ends up a lot quicker.

    Anyways, any feedback would be appreciated.

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598
    599
    600
    601
    602
    603
    604
    605
    606
    607
    608
    609
    610
    611
    612
    613
    614
    615
    616
    617
    618
    619
    620
    621
    622
    623
    624
    625
    626
    627
    628
    629
    630
    631
    632
    633
    634
    635
    636
    637
    638
    639
    640
    641
    642
    643
    644
    645
    646
    647
    648
    649
    650
    651
    652
    653
    654
    655
    656
    657
    658
    659
    import pickle, re
    from os import path
    __author__ = 'Charles Grunwald (Juntalis) <cgrunwald@gmail.com>'
    def __setup_configobj__(deps, info):
        """ Setup configobj either in the lib folder or the local folder """
        from os import unlink, mkdir
        import shutil
    
        # Download and unzip
        tmpfile = deps.download(info['url'], info['filename'])
        tmpfolder = path.splitext(tmpfile)[0]
        deps.unzip(tmpfile, tmpfolder)
        unlink(tmpfile)
    
        modfolder = path.join(tmpfolder, 'configobj-4.7.2')
        modpath = [path.join(modfolder, 'configobj.py'), path.join(modfolder, 'validate.py')]
    
        (modpath, modconfigobj) = deps.install(modpath, 'configobj')
        shutil.rmtree(tmpfolder, True)
        return (modconfigobj, modpath)
    """ Our dependencies """
    __dependencies__ = {
        'configobj':
            (__setup_configobj__, {
                'filename' : 'configobj.zip',
                'url' : 'http://www.voidspace.org.uk/downloads/configobj-4.7.2.zip'
            })
    }
    class ScriptDeps:
        """ Simple class to install any script dependencies we don't have at startup. """
        __modules__ = {}
    
        def __init__(self):
            deps = __dependencies__
            for dep in deps.keys():
                (setup, info) = deps[dep]
                self.__modules__[dep] = setup(self, info)
        def get(self, name):
            if self.__modules__.has_key(name):
                return self.__modules__[name]
            return None
        def unzip(self, filename, dir):
            """ Extract zip file: filename to folder: dir. """
            import zipfile, os
            from cStringIO import StringIO
            zf = zipfile.ZipFile( filename )
            namelist = zf.namelist()
            dirlist = filter( lambda x: x.endswith( '/' ), namelist )
            filelist = filter( lambda x: not x.endswith( '/' ), namelist )
            # make base
            pushd = os.getcwd()
            if not path.isdir( dir ):
                os.mkdir( dir )
            os.chdir( dir )
            # create directory structure
            dirlist.sort()
            for dirs in dirlist:
                dirs = dirs.split( '/' )
                prefix = ''
                for dir in dirs:
                    dirname = path.join( prefix, dir )
                    if dir and not path.isdir( dirname ):
                        os.mkdir( dirname )
                    prefix = dirname
            # extract files
            for fn in filelist:
                try:
                    out = open( fn, 'wb' )
                    buffer = StringIO( zf.read( fn ))
                    buflen = 2 ** 20
                    datum = buffer.read( buflen )
                    while datum:
                        out.write( datum )
                        datum = buffer.read( buflen )
                    out.close()
                except:
                    import sys
                    sys.stderr.write('Error while unzipping %s..\n' % filename)
            os.chdir( pushd )
    
        def download(self, url, filename):
            """ Download dependencies specified by dictionary __dependencies__"""
            from urllib import urlretrieve as download
            from tempfile import gettempdir as tempdir
            # Iterate through dependencies, downloading and moving.
            result = path.join(tempdir(), filename)
            download(url, result)
            return result
    
        def install(self, modpath, modname):
            libdir = path.join(notepad.getNppDir(), 'plugins', 'PythonScript', 'lib')
            modconfigobj = None
            try:
                # install to pythonscript lib folder.
                for modfile in modpath:
                    shutil.move(modfile, libdir)
            except:
                # install to scripts/lib
                libdir = path.join(path.abspath(path.dirname(__file__)), 'lib')
                if not path.exists(libdir) or not path.isdir(libdir):
                    mkdir(libdir)
                for modfile in modpath:
                    shutil.move(modfile, libdir)
                initfile = path.join(libdir, '__init__.py')
                if not path.exists(initfile):
                    initfile = open(initfile,'w')
                    initfile.write('# Stub')
                    initfile.close()
                modconfigobj = __import__('lib.%s' % modname)
            else:
                modconfigobj = __import__(modname)
            finally:
                modpath = path.join(libdir, '%s.py' % modname)
    
            return (modpath, modconfigobj)
    class SimpleCache(dict):
        """
        Simple local cache.
        It saves local data in singleton dictionary with convenient interface
    
        Downloaded from http://code.activestate.com/recipes/577492-simple-local-cache-and-cache-decorator/
    
        Author: Andrey Nikishaev
        License: GPL
        Copyright 2010, http://creotiv.in.ua
        """
    
        def __new__(cls,*args):
            if not hasattr(cls,'_instance'):
                cls._instance = dict.__new__(cls)
            else:
                raise Exception('SimpleCache already initialized')
            return cls._instance
    
        @classmethod
        def getInstance(cls):
            if not hasattr(cls,'_instance'):
                cls._instance = dict.__new__(cls)
            return cls._instance
        def get(self,name,default=None):
            """Multilevel get function.
            Code:       
            Config().get('opt.opt_level2.key','default_value')
            """
            if not name: 
                return default
            levels = name.split('.')
            data = self         
            for level in levels:
                try:            
                    data = data[level]
                except:
                    return default
            return data
    
        def set(self,name,value):
            """Multilevel set function
            Code:       
            Config().set('opt.opt_level2.key','default_value')
            """
            levels = name.split('.')
            arr = self      
            for name in levels[:-1]:
                if not arr.has_key(name):        
                    arr[name] = {}   
                arr = arr[name]
            arr[levels[-1]] = value
    
        def getset(self,name,value):
            """Get cache, if not exists set it and return set value
            Code:       
            Config().getset('opt.opt_level2.key','default_value')
            """
            g = self.get(name)
            if not g:
                g = value
                self.set(name,g)
            return g
    def scache(func):
        def wrapper(*args, **kwargs):
            cache = SimpleCache.getInstance()
            fn = "scache." + func.__module__ + func.__class__.__name__ + \
                 func.__name__ + str(args) + str(kwargs)        
            val = cache.get(fn)
            if not val:
                res = func(*args, **kwargs)
                cache.set(fn,res)
                return res
            return val
        return wrapper
    # Try to import configobj module. If we cant, download it and set it up.
    try:
        import configobj
        has_configobj = True
    except ImportError:
        notepad.messageBox('Could not find module "configobj". Downloading and setting it up now..', 'Dependencies')
        deps = ScriptDeps()
        (configobj, configobj_path) = deps.get('configobj')
        has_configobj = configobj is not None
        if has_configobj:
            notepad.messageBox('Module "configobj" setup successfully.You can find it at:\n\n%s' % configobj_path, 'Setup Successful')
        else:
            notepad.messageBox('Error: Could not import configobj.\nDownload at: http://www.voidspace.org.uk/python/configobj.html', 'Import Error')
            exit()
    class LanguageAutoDetector:
        """ Main class """
        __log = None
        __config = None
        __config_path = None
        __cache = None
        __detections = [
            'filename',
            'partial_filename',
            'xml',
            'shebang',
            'try_shebang',
            'contains_string'
            #'regex'
        ]
        __cache_ignore = {
            'contains_string' : None,
            'try_shebang' : 'shebang',
            'partial_filename' : 'filename',
            'regex' : None
        }
    
        def __init__(self, config_file=None):
            config = self.__load_config(config_file)
            if config['cache']['enabled']:
                self.__load_cache()
            #self.__log = logger.FileLogger()
    
        def __new__(cls,*args):
            if not hasattr(cls,'_instance'):
                cls._instance = dict.__init__(cls, args)
            else:
                raise Exception('LanguageAutoDetector already initialized')
            return cls._instance
    
        @classmethod
        def getInstance(cls):
            if not hasattr(cls,'_instance'):
                cls._instance = dict.__new__(cls)
            return cls._instance
        @scache
        def config(self, key=None, default=None):
            if self.__config is None:
                config = self.__load_config()
            else:
                config = self.__config
    
            if key is None:
                return config
    
            return self.__getdict(config, key, default)
        @scache
        def cache(self, key=None, default=None):
            if self.__cache is None:
                cache = self.__load_cache()
            else:
                cache = self.__cache
    
            if key is None:
                return cache
    
            return self.__getdict(cache, key, default)
    
        def set_lang(self, result, bufferID):
            ret = False
            lang = self.__test_language(result)
            if lang is None:
                if self.config('errors.invalid_lang'):
                    notepad.messageBox('Error: Specified language %s invalid.' % result, 'Config Error')
            else:
                notepad.setLangType(lang, bufferID)
                ret = True
            return ret
    
        def detect(self, args):
            detections = self.config('detections.order')
            bufferID = args['bufferID']
            args['filename'] = path.basename(notepad.getBufferFilename(bufferID))
            notepad.activateBufferID(bufferID)
            for detection in detections:
                func = getattr(self, 'detection_%s' % detection.lower())
                result = func(args)
                console.write('Detection %s: %s\n' % (detection.lower(),result))
                if result is not None:
                    if self.set_lang(result, bufferID):
                        break
        # filename
        # partial_filename
        # xml
        # shebang
        # try_shebang
        # contains_string
    
        def detection_filename(self, args):
            filename = args['filename']
            config = self.config('detections.filename')
            for lang in config.keys():
                if filename in config[lang]:
                    return lang
            return None
    
        @scache
        def detection_partial_filename(self, args):
            filename = args['filename']
            config = self.config('detections.partial_filename')
            for lang in config.keys():
                for pattern in config[lang]:
                    rgx = re.compile("^%s$" % pattern, re.IGNORECASE)
                    if rgx.match(filename):
                        # TODO: Cache here filename -> lang
                        return lang
            return None
    
        def detection_xml(self, args):
            # Check for xml stuff.
            xml_config = self.config('detections.xml')
            xml_filename = args['filename']
            self.xml_lang = None
            def check(contents, lineNumber, totalLines):
                val = contents.strip().lower()
                if len(val) == 0:
                    return 0
                else:
                    for pattern in xml_config:
                        pattern = pattern.lower()
                        if val.startswith(pattern):
                            self.xml_lang = 'xml'
                            ext = path.splitext(xml_filename)[1]
                            if len(ext) > 0:
                                xml_cache = { ext : 'xml' } # TODO: Cache here
                return totalLines - lineNumber
            editor.forEachLine(check)
            return self.xml_lang
    
        def detection_shebang(self, args):
            shebang = self.__getshebang()
            if shebang is None:
                return None
            config = self.config('detections.shebang')
            for lang in config.keys():
                for pattern in config[lang]:
                    rgx = re.compile(r"(?:^#!((?:/[^\s]+/env(?:\.[a-z]+) |/[^\s]+/|[A-Z]:[^\s]*\\|[A-Z]:[^\s]*\\env\.[a-z]+ ?)?%s[\d.-_]*(?:\.[0-9a-z-_.])*)\b)\Z" % pattern, re.IGNORECASE)
                    if rgx.match(shebang):
                        # TODO: Cache here shebang -> lang
                        return lang
            return None
    
        def detection_try_shebang(self, args):
            shebang = self.__getshebang()
            if shebang is None:
                return None
            rgx = re.compile(r"^#!(?:/[^\s]+/env(?:\.[a-z]+) |/[^\s]+/|[A-Z]:[^\s]*\\|[A-Z]:[^\s]*\\env\.[a-z]+ ?)?([^\s]+)[\d.-_]*(?:\.[0-9a-z-_.])*\b", re.IGNORECASE)
            match = rgx.search(shebang)
            if match:
                result = match.group(1)
                lang = self.__test_language(result)
                if lang is None:
                    return None
                return result
            return None
    
        def detection_contains_string(self, args):
            text = editor.getText()
            config = self.config('detections.contains_string')
            for lang in config.keys():
                for pattern in config[lang]:
                    rgx = re.compile("%s" % pattern, re.IGNORECASE | re.MULTILINE)
                    if rgx.search(text):
                        # TODO: Cache here shebang -> lang
                        return lang
            return None
        def detection_regex(self, args):
            pass
    
        def __getshebang(self):
            line = editor.getLine(0)
            if line[0:2] == '#!':
                return line
            return None
    
        def __getdict(self, dict, name, default=None):
            """Multilevel get function.
            Code:       
            Config().get('opt.opt_level2.key','default_value')
            """
            if not name: 
                return default
            levels = name.split('.')
            data = dict
            for level in levels:
                try:            
                    data = data[level]
                except:
                    return default
            return data
    
        def __load_config(self, cfg=None):
            """ Load configuration for script. If it doesn't exist, write the default
                configuration to file. """
    
            # Figure out config path.
            if cfg is None:
                cfg = path.join(notepad.getPluginConfigDir(), 'py_autolang.cfg')
            self.__config_path = cfg
            if path.exists(cfg):
                self.__config = configobj.ConfigObj(cfg)
            else:
                self.__config = self.__default_config()
                self.__save_config()
            return self.__config
    
        def __default_config(self, cfg=None):
            """ Default configuration for script. """
    
            # Figure out config path.
            if cfg is None and self.__config_path is None:
                cfg = path.join(notepad.getPluginConfigDir(), 'py_autolang.cfg')
            elif cfg is None and self.__config_path is not None:
                cfg = self.__config_path
    
            config = configobj.ConfigObj()
            config.filename = cfg
    
            # Main config
            config['script'] = {}
            config['script']['enabled'] = True
            config['script']['autoload'] = True
    
            # Errors
            config['errors'] = {}
            config['errors']['invalid_lang'] = True # Message box on invalid lexer specified.
            config['errors']['invalid_lexer'] = True # Message box on invalid lang specified.
    
            # Script cache
            config['cache'] = {}
            config['cache']['enabled'] = True
            cache_folder = path.abspath(path.join(path.dirname(cfg), 'cache'))
            valid_folder = False
            while not valid_folder:
                if path.exists(cache_folder):
                    if not path.isdir(cache_folder):
                        import string, random
                        cache_folder += '-' + ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(3))
                    else:
                        valid_folder = True
                else:
                    from os import mkdir
                    mkdir(cache_folder)
            config['cache']['folder'] = cache_folder
    
            # Logging
            config['logging'] = {}
            config['logging']['console'] = False
            config['logging']['console_auto_open'] = False
            config['logging']['file'] = False
    
            # Loading event
            config['file_load_event'] = {}
            config['file_load_event']['no_extension'] = True
            config['file_load_event']['default_lexer'] = True
            config['file_load_event']['always'] = False
    
            """
                Detection methods order - This tells what order to use detection methods.
                So if filename is executed before shebang, and filename matches a detection,
                the shebang line wont be run. To disable a detection method, set to 0. If
                any two methods have the same number, an error will be thrown.
    
                Possible detections:
                    shebang
                    try_shebang
                    xml
                    filename
                    partial_filename
                    contains_string
                    regex - Not yet implemented
            """
            order = ['filename', 'partial_filename', 'xml', 'shebang', 'try_shebang', 'contains_string']
            # Default Detections
            ## Shebangs
            ## These all match the follow regex pattern:
            ### r"^#!(?:/[^\s]+/env(?:\.[a-z]+) |/[^\s]+/|[A-Z]:[^\s]*\\|[A-Z]:[^\s]*\\env\.[a-z]+ ?)?%s[\d.-_]*(?:\.[0-9a-z-_.])*\b" % key
            shebang = {
    
                # Shell
                ## Bash/Korn Shell/C Shell/Z Shell/etc
                'bash' : ['(?:[czk]|ba)?sh'],
    
                # Python
                ## CPython - http://python.org/
                ## Cross Twine Linker (xtpython) - http://crosstwine.com/linker/python.html
                ## Unpython Python to C compiler (unpython) - http://code.google.com/p/unpython/
                ## IPython (ipython) - http://ipython.org/
                ## PyPy - http://pypy.org/
                ## Iron Python - http://ironpython.net/
                ## Mozilla Embedded Python Console - http://www.thomas-schilz.de/MozPython/
                ## TinyPy - http://www.tinypy.org/
                ## Snipy - Personal project, you can remove if you want.
                ## Enthought SciPy distribution - http://www.enthought.com/
                ## Jython (jython) - http://www.jython.org/
                ## Cython (Optimizing Python to C Compiler) - http://cython.org/
                ## Typhon (typhon) - https://github.com/vic/typhon
                ## Mython (mython) - http://mython.org/
    
                # Languages Close Enough to Python """
                ## Nimrod  - http://force7.de/nimrod/download.html
                ## Serpent - http://sourceforge.net/projects/serpent/
                ## Boo - http://boo.codehaus.org/
    
                'python' : [
                    '(?:xt|un|i)?pythonw?',
                    '(?:py|i|moz|tiny|sni)pyw?(?:-c)?',
                    'epdw?',
                    '[jctm]ythonw?',
                    '(?:nimrod|ser?pent)',
                    'boo(?:c|i|ish)?'
                ],
                # Perl
                ## Perl - http://www.perl.org/
                ## Parrot - http://parrot.org/
                ### Hm, this one might be hard. Leaving parrot as perl for now
                'perl' : [ 'w?perl', 'parrot' ],
    
                # Ruby
                ## Ruby - http://www.ruby-lang.org/en/
                ## Iron Ruby (ir, etc) - http://ironruby.net/
                ## JRuby - http://jruby.org/
                ## Ruby on Rails - http://rubyonrails.org/
                'ruby' : [
                    '[ji]?[ie]r[wbi]{0,2}?(?:_swing)?',
                    '[ej]?ruby[wc]?',
                    'rake'
                ],
                # Javascript
                ## Node.Js - http://nodejs.org/
                ## Narwhal - https://github.com/tlrobinson/narwhal
                ## JSDB - http://www.jsdb.org/
                ## Ringo Javascript - http://ringojs.org/
                ## GlueScript - http://gluescript.sourceforge.net/
                ## Rhino - http://www.mozilla.org/rhino/
                'javascript' : [
                    '(?:node|npm)',
                    '(?:narwhal|tusk)',
                    '(?:jsdb|ringo|gluew?)',
                    '(?:rhino|js)'
                ],
    
                # PHP
                'php' : [
                    '(?:i?php(?:-cgi|-cli|-win)?|pharc?)'
                ]
            }
    
            filename = {
                'bash' : ['configure'],
                'makefile' : ['Makefile']
            }
    
            partial_filename = {
                'makefile' : ['Makefile\..+']
            }
    
            contains_string = {
                'bash' : ['^mk_add_options', '^ac_add_options']
            }
    
            config['detections'] = {
                'order' : order,
                'filename' : filename,
                'partial_filename' : partial_filename,
                'xml' : ['<?xml ', '<!DOCTYPE'],
                'shebang' : shebang,
                'contains_string' : contains_string
            }
    
            return config
    
        def __save_config(self):
            self.__config.write()
    
        def __load_cache(self):
            config = self.config()
            folder = config['cache']['folder']
    
            cache = {}
            for detection in config['detections']['order']:
                if detection in self.__cache_ignore:
                    if self.__cache_ignore[detection] is not None:
                        detection = self.__cache_ignore[detection]
                        if cache.has_key(detection):
                            continue
                    else:
                        continue
                f = path.join(folder, detection)
                if path.exists(f) and path.isfile(f):
                    input = open(f, 'rb')
                    cache[detection] = {
                        'changed' : False,
                        'value' : pickle.load(input)
                    }
                    input.close()
                else:
                    cache[detection] = {
                        'changed' : False,
                        'value' : None
                    }
            self.__cache = cache
            return self.__cache
    
        def __save_cache(self):
            config = self.config()
            folder = config['cache']['folder']
            saved = []
            for detection in config['detections']['order']:
                if detection in self.__cache_ignore:
                    if self.__cache_ignore[detection] is not None:
                        detection = self.__cache_ignore[detection]
                        if detection in saved:
                            continue
                    else:
                        continue
                f = path.join(folder, detection)
                cache = self.cache(detection)
                if cache['changed'] and cache['value'] is not None:
                    output = open(f, 'wb')
                    pickle.dump(cache['value'], output)
                    output.close()
                    self.__cache[detection]['changed'] = False
                saved.append(detection)
    
        def __test_lexer(self, lexer):
            result = True
            old_lexer = editor.getLexerLanguage()
            editor.setLexerLanguage(lexer)
            if editor.getLexerLanguage() == 'null':
                result = False
            editor.setLexerLanguage(old_lexer)
            return result
        def __test_language(self, lang):
            import Npp
            lang = lang.upper()
            result = None
            try:
                result = getattr(Npp.LANGTYPE, lang)
            except AttributeError:
                result = None
            return result
    import sys
    sys.stdout = console
    def detect_test(args):
        console.write('Language: %s\n' % notepad.getLangType(args["bufferID"]).__str__().lower())
    
    detector = LanguageAutoDetector()
    notepad.clearCallbacks([NOTIFICATION.FILEOPENED])
    notepad.callback(detector.detect, [NOTIFICATION.FILEOPENED])
    
     
  • juntalis
    juntalis
    2011-10-03

    Eh, cant seem to edit my post. Just realized I left a few lines in. You may want to get rid of the lines:

    import sys
    sys.stdout = console
    

    I also noticed that trying to copy the script and paste it into a file seems to mess up the newlines, so I threw it up on pastebin with the above fix:

    http://pastebin.com/SWG3JuAT

     
  • I also made a plugin for automatic language selection based on shebang, mode line ("vi" style), filename/filepath, file content (XML header…).  It supports user defined languages, setting tabstops and indentation modes, etc.  You can find it here https://sourceforge.net/projects/npppythonplugsq/files/Modeline%20Parser/.