Jump to content
The Dark Mod Forums

Help with Python Script


Recommended Posts

Hi all,

some time ago SteveL gave me a python script, that can search files in the TDM installation (also inside pk4-files), which is very handy for playing around with definitions, materials, etc. Unfortunately, the script does not work with the latest Python distribution. Could someone, who in contrast to myself knows python, be so kind to correct the script, so it works? Idle gives an error that the print part is wrong; I am not sure if they have changed anything in their syntax. Any help is appreciated!

"""Search text files in FMs and TDM assets"""
import re, os, fnmatch, zipfile

PATHS = [ r"E:\Spiele\Stealth\TDM" ] #, r"E:\dm-dev\campaign"]  
FILE_EXTS = ['.mtr'] # '.mtr', '.prt', ,'.skin','.fx','.md5mesh','.pfb', '.anim', '.md5anim','.gui','.map','.script','.gui','.def'
SEARCH_PTN = r'arm_leg' # regular expression

def searchPk4(pk4, exts):
    """Look in a pk4 archive and yield files with any of the extensions
    in exts. Returns ( filename, filecontents ). filename includes any
    directory path internal to the archive.
    
    pk4: full path, e.g. r'E:\darkmod\tdm_defs01.pk4'
    exts: list or tuple of bare extensions, e.g. ['def', 'mtr']
    """
    f = zipfile.ZipFile(pk4, 'r')
    for member in f.infolist():
        ext = (os.path.splitext(member.filename)[1]).lower()
        if ext in exts:
            yield member.filename, f.read(member)

def findTDMFiles(paths, exts):
    """Yield tuples: (filepath, filecontents) of all files
    found in paths with any of the extensions in exts. Looks inside
    pk4 archives, but not nested archives.
    
    paths, exts are sequences containing one or more paths / extensions.
    """
    for path in set(paths):
        for root, dirnames, filenames in os.walk(path):
            for ext in set( ['.pk4'] + exts):
                for filename in fnmatch.filter(filenames, '*'+ext):
                    fpath = os.path.join(root, filename)
                    if ext == '.pk4':
                        for internalname, content in searchPk4(fpath, exts):
                            yield ( fpath + ' -> ' + internalname,   content )
                    else:
                         yield fpath, file(fpath).read()

if __name__ == '__main__':
    search_regex = re.compile(SEARCH_PTN, re.IGNORECASE)
    context_regex = re.compile("[^\n]{0,250}(" + SEARCH_PTN + ")[^\n]{0,250}", re.IGNORECASE)

    result = '\n~~~~~ RESULT:\n'
    for fname, text in findTDMFiles(PATHS, FILE_EXTS):
        print "Checking " + fname
        refs = search_regex.findall(text)
        if refs:
            print '***** %s *****' % fname
            result += '***** %s *****\n' % fname
            for r in context_regex.finditer(text):
                print r.group().strip()
                result += r.group().strip() + '\n'
    print result

Link to post
Share on other sites

In Python 3 you can no longer write:

print "something"

it has to be

print("something")

as if you are calling a function.

 

I have no idea if that's the only problem with the script, but it would be the first thing to try if you are getting errors about print statements.

 

Also be aware that you are not forced to run scripts with Python 3. You should be able to install and run Python 2 quite happily alongside Python 3, and most Linux distributions will do this by default (because there are a lot of existing scripts which have not been updated).

  • Like 2
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...