예제 #1
0
def addraws(pydwarf, dfraws, rawsdir, entities, extratokens=None):
    # Get the entities that need to have permitted things added to them.
    entitytokens = getentities(dfraws, entities)
    if len(entitytokens) != len(entities):
        if len(entitytokens):
            pydwarf.log.error('Of entities %s passed by argument, only %s were found.' % (entities, entitytokens))
        else:
            return pydwarf.failure('Found none of entities %s to which to add permitted buildings and reactions.' % entities)
            
    # Read the raws
    try:
        shukaroraws = raws.dir(path=rawsdir, log=pydwarf.log)
    except:
        pydwarf.log.exception('Failed to load raws from %s.' % rawsdir)
        return pydwarf.failure('Failed to load raws from %s.' % rawsdir)
        
    # Add new buildings and reactions to entities, and whatever else needs adding
    buildings = shukaroraws.all(exact_value='BUILDING_WORKSHOP', args_count=1)
    reactions = shukaroraws.all(exact_value='REACTION', args_count=1)
    for entity in entitytokens:
        if extratokens: entity.add(extratokens)
        for building in buildings: entity.add(raws.token(value='PERMITTED_BUILDING', args=(building.args[0],)))
        for reaction in reactions: entity.add(raws.token(value='PERMITTED_REACTION', args=(reaction.args[0],)))
    pydwarf.log.info('Added %d permitted buildings and %d permitted reactions to %d entities.' % (len(buildings), len(reactions), len(entitytokens)))
    
    # Add new files
    for filename, rfile in shukaroraws.files.iteritems():
        if filename not in dfraws: dfraws.addfile(rfile=rfile)
        
    # All done!
    return pydwarf.success()
예제 #2
0
def __main__(args=None):
    conf = getconf(args)
    pydwarf.log.debug('Proceeding with configuration: %s.' % conf)
    
    # Report versions
    pydwarf.log.info('Running PyDwarf manager version %s.' % __version__)
    pydwarf.log.debug('With PyDwarf version %s.' % pydwarf.__version__)
    pydwarf.log.debug('With raws version %s.' % raws.__version__)
    
    # Handle flags that completely change behavior
    if args.list:
        pydwarf.urist.list()
        exit(0)
    elif args.meta is not None:
        pydwarf.urist.doclist(args.meta)
        exit(0)
    
    # Verify that input directory exists
    if not os.path.exists(conf.input):
        pydwarf.log.error('Specified raws directory %s does not exist.' % conf.input)
        exit(1)
    
    # Make backup
    if conf.backup is not None:
        pydwarf.log.info('Backing up raws to %s.' % conf.backup)
        try:
            copytree(conf.input, conf.backup)
        except:
            pydwarf.log.error('Failed to create backup.')
            exit(1)
    else:
        pydwarf.log.warning('Proceeding without backing up raws.')
    
    # Read input raws
    pydwarf.log.info('Reading raws from input directory %s.' % conf.input)
    pydwarf.urist.session.dfraws = raws.dir(path=conf.input, log=pydwarf.log)
    
    # Run each script
    pydwarf.log.info('Running scripts.')
    pydwarf.urist.session.handleall(conf.scripts)
    
    # Get the output directory, remove old raws if present
    outputdir = conf.output if conf.output else conf.input
    if os.path.exists(outputdir):
        pydwarf.log.info('Removing obsolete raws from %s.' % outputdir)
        for removefile in [os.path.join(outputdir, f) for f in os.listdir(outputdir)]:
            pydwarf.log.debug('Removing file %s.' % removefile)
            if removefile.endswith('.txt'): os.remove(removefile)
    else:
        pydwarf.log.info('Creating raws output directory %s.' % outputdir)
        os.makedirs(outputdir)
    
    # Write the output
    pydwarf.log.info('Writing changes to raws to %s.' % outputdir)
    pydwarf.urist.session.dfraws.write(outputdir, pydwarf.log)
    
    # All done!
    pydwarf.log.info('All done!')
예제 #3
0
파일: manager.py 프로젝트: lethosor/PyDwarf
def __main__():
    
    # Get configuration
    conf = config.export
    if not conf:
        pydwarf.log.error('No configuration specified. Imported config package must contain an export variable.')
        exit(1)
    
    # Things to do with versions
    pydwarf.log.info('Running PyDwarf %s.' % pydwarf.__version__)
    if conf.version is not None:
        pydwarf.log.info('Managing Dwarf Fortress version %s.' % conf.version)
        pydwarf.urist.session.dfversion = conf.version
    else:
        pydwarf.log.error('No Dwarf Fortress version was specified in conf. Scripts will be run regardless of their indicated compatibility.')
    
    # Verify that input directory exists
    if not os.path.exists(conf.input):
        pydwarf.log.error('Specified raws directory %s does not exist.' % conf.input)
        exit(1)
    
    # Make backup
    if conf.backup is not None:
        pydwarf.log.info('Backing up raws to %s...' % conf.backup)
        try:
            copytree(conf.input, conf.backup)
        except:
            pydwarf.log.error('Failed to create backup.')
            exit(1)
    else:
        pydwarf.log.warning('Proceeding without backing up raws.')
    
    # Read input raws
    pydwarf.log.info('Reading raws from input directory %s...' % conf.input)
    pydwarf.urist.session.dfraws = raws.dir(path=conf.input, log=pydwarf.log)
    
    # Run each script
    pydwarf.log.info('Running scripts...')
    pydwarf.urist.session.handleall(conf.scripts)
    
    # Get the output directory, remove old raws if present
    outputdir = conf.output if conf.output else conf.input
    if os.path.exists(outputdir):
        pydwarf.log.info('Removing obsolete raws from %s...' % outputdir)
        for removefile in [os.path.join(outputdir, f) for f in os.listdir(outputdir)]:
            pydwarf.log.debug('Removing file %s...' % removefile)
            if removefile.endswith('.txt'): os.remove(removefile)
    else:
        pydwarf.log.info('Creating raws output directory %s...' % outputdir)
        os.makedirs(outputdir)
    
    # Write the output
    pydwarf.log.info('Writing changes to raws to %s...' % outputdir)
    pydwarf.urist.session.dfraws.write(outputdir, pydwarf.log)
    
    # All done!
    pydwarf.log.info('All done!')
예제 #4
0
def armourypack(dfraws, remove_entity_items=True, remove_attacks=('SCRATCH', 'BITE'), remove_attacks_from=('DWARF', 'HUMAN', 'ELF')):
    try:
        armouryraws = raws.dir(path=armourydir, log=pydwarf.log)
    except:
        return pydwarf.failure('Unable to load armoury raws.')
    
    additemstoraws(dfraws, armouryraws)
    additemstoents(dfraws, armouryraws, remove_entity_items)
    addreactions(dfraws, armouryraws)
    removeattacks(dfraws, remove_attacks, remove_attacks_from)
    
    return pydwarf.success()
예제 #5
0
def armourypack(dfraws,
                remove_entity_items=True,
                remove_attacks=('SCRATCH', 'BITE'),
                remove_attacks_from=('DWARF', 'HUMAN', 'ELF')):
    try:
        armouryraws = raws.dir(path=armourydir, log=pydwarf.log)
    except:
        return pydwarf.failure('Unable to load armoury raws.')

    additemstoraws(dfraws, armouryraws)
    additemstoents(dfraws, armouryraws, remove_entity_items)
    addreactions(dfraws, armouryraws)
    removeattacks(dfraws, remove_attacks, remove_attacks_from)

    return pydwarf.success()
예제 #6
0
def addraws(pydwarf, dfraws, rawsdir, entities, extratokens=None):
    # Get the entities that need to have permitted things added to them.
    entitytokens = getentities(dfraws, entities)
    if len(entitytokens) != len(entities):
        if len(entitytokens):
            pydwarf.log.error(
                'Of entities %s passed by argument, only %s were found.' %
                (entities, entitytokens))
        else:
            return pydwarf.failure(
                'Found none of entities %s to which to add permitted buildings and reactions.'
                % entities)

    # Read the raws
    try:
        shukaroraws = raws.dir(path=rawsdir, log=pydwarf.log)
    except:
        pydwarf.log.exception('Failed to load raws from %s.' % rawsdir)
        return pydwarf.failure('Failed to load raws from %s.' % rawsdir)

    # Add new buildings and reactions to entities, and whatever else needs adding
    buildings = shukaroraws.all(exact_value='BUILDING_WORKSHOP', args_count=1)
    reactions = shukaroraws.all(exact_value='REACTION', args_count=1)
    for entity in entitytokens:
        if extratokens: entity.add(extratokens)
        for building in buildings:
            entity.add(
                raws.token(value='PERMITTED_BUILDING',
                           args=(building.args[0], )))
        for reaction in reactions:
            entity.add(
                raws.token(value='PERMITTED_REACTION',
                           args=(reaction.args[0], )))
    pydwarf.log.info(
        'Added %d permitted buildings and %d permitted reactions to %d entities.'
        % (len(buildings), len(reactions), len(entitytokens)))

    # Add new files
    for filename, rfile in shukaroraws.files.iteritems():
        if filename not in dfraws: dfraws.addfile(rfile=rfile)

    # All done!
    return pydwarf.success()
예제 #7
0
def armoury(df, remove_entity_items=True):
    try:
        pydwarf.log.debug('Loading armoury raws from %s.' % armoury_dir)
        armouryraws = raws.dir(root=armoury_dir, log=pydwarf.log)
    except:
        return pydwarf.failure('Unable to load armoury raws.')

    # Persist a list of armoury item tokens because we're going to be needing them for a few things
    armouryitemtokens = armouryraws.allobj(type_in=armoury_item_objects)

    # Rename items in the armoury raws in accordance with a manually compiled list of naming oddities
    for item in armouryitemtokens.all(arg_in=(0, weird_armoury_names)):
        item.args[0] = weird_armoury_names[item.args[0]]

    # Remove any existing items in the raws which share an id with the items about to be added
    pydwarf.log.debug('Removing obsolete items.')
    df.removeallobj(type_in=armoury_item_objects,
                    id_in=[token.arg() for token in armouryitemtokens])

    # Look for files made empty as a result (of which there should be a few) and remove them
    removedfiles = []
    for file in df.files.values():
        if isinstance(file, raws.rawfile) and len(file) <= 1:
            pydwarf.log.debug('Removing emptied file %s.' % file)
            file.remove()
            removedfiles.append(file)

    # Get a list of entity tokens corresponding to the relevant names
    entitytokens = [df.getobj('ENTITY', entity) for entity in armoury_entities]

    # If remove_entity_items is set to True, remove all existing tokens which permit relevant items
    # Otherwise, just remove the ones with conflict with those about to be added
    for entitytoken in entitytokens:
        pydwarf.log.debug('Removing permission tokens from %s.' % entitytoken)
        entitytoken.removeallprop(
            value_in=armoury_items,
            arg_in=(None if remove_entity_items else
                    (0, [token.args[0] for token in armouryitemtokens])))

    # Now add all the armoury raw files that have items in them
    try:
        for file in armouryraws.iterfiles():
            if file.kind == 'raw' and file.name.startswith('item_'):
                pydwarf.log.debug('Adding file %s to raws.' % file)
                copy = file.copy()
                copy.loc = 'raw/objects'
                copy.name += '_armoury_stal'
                df.add(copy)
    except:
        pydwarf.log.exception('Encountered exception.')
        return pydwarf.failure('Failed to add armoury item raws.')

    # Add new permitted item tokens
    try:
        for entitytoken in entitytokens:
            pydwarf.log.debug('Permitting items for %s.' % entitytoken)
            for itemtype, items in armoury_entities[
                    entitytoken.arg()].iteritems():
                for item in items:
                    entitytoken.add(
                        raws.token(
                            value=itemtype,
                            args=[weird_armoury_names.get(item[0], item[0])] +
                            item[1:]))
    except:
        pydwarf.log.exception('Encountered exception.')
        return pydwarf.failure('Failed to permit items for entities.')

    # And add the new reactions
    try:
        pydwarf.log.debug('Adding new reactions.')
        reactions = armouryraws['reaction_armoury'].copy()
        reactions.loc = 'raw/objects'
        reactions.name = 'reaction_armoury_stal'
        response = pydwarf.urist.getfn('pineapple.easypatch')(
            df, reactions, permit_entities=armoury_entities.keys())
        if not response: return response
    except:
        pydwarf.log.exception('Encountered exception.')
        return pydwarf.failure('Failed to add new reactions.')

    # All done!
    return pydwarf.success()
예제 #8
0
def armoury(df, remove_entity_items=True):
    try:
        pydwarf.log.debug('Loading armoury raws from %s.' % armoury_dir)
        armouryraws = raws.dir(root=armoury_dir, log=pydwarf.log)
    except:
        return pydwarf.failure('Unable to load armoury raws.')
        
    # Persist a list of armoury item tokens because we're going to be needing them for a few things
    armouryitemtokens = armouryraws.allobj(type_in=armoury_item_objects)
    
    # Rename items in the armoury raws in accordance with a manually compiled list of naming oddities
    for item in armouryitemtokens.all(arg_in=(0, weird_armoury_names)):
        item.args[0] = weird_armoury_names[item.args[0]]
        
    # Remove any existing items in the raws which share an id with the items about to be added
    pydwarf.log.debug('Removing obsolete items.')
    df.removeallobj(type_in=armoury_item_objects, id_in=[token.arg() for token in armouryitemtokens])
    
    # Look for files made empty as a result (of which there should be a few) and remove them
    removedfiles = []
    for file in df.files.values():
        if isinstance(file, raws.rawfile) and len(file) <= 1:
            pydwarf.log.debug('Removing emptied file %s.' % file)
            file.remove()
            removedfiles.append(file)
    
    # Get a list of entity tokens corresponding to the relevant names
    entitytokens = [df.getobj('ENTITY', entity) for entity in armoury_entities]
    
    # If remove_entity_items is set to True, remove all existing tokens which permit relevant items
    # Otherwise, just remove the ones with conflict with those about to be added
    for entitytoken in entitytokens:
        pydwarf.log.debug('Removing permission tokens from %s.' % entitytoken)
        entitytoken.removeallprop(
            value_in = armoury_items,
            arg_in = (
                None if remove_entity_items else (0, [token.args[0] for token in armouryitemtokens])
            )
        )
    
    # Now add all the armoury raw files that have items in them
    try:
        for file in armouryraws.iterfiles():
            if file.kind == 'raw' and file.name.startswith('item_'):
                pydwarf.log.debug('Adding file %s to raws.' % file)
                copy = file.copy()
                copy.loc = 'raw/objects'
                copy.name += '_armoury_stal'
                df.add(copy)
    except:
        pydwarf.log.exception('Encountered exception.')
        return pydwarf.failure('Failed to add armoury item raws.')
        
    # Add new permitted item tokens
    try:
        for entitytoken in entitytokens:
            pydwarf.log.debug('Permitting items for %s.' % entitytoken)
            for itemtype, items in armoury_entities[entitytoken.arg()].iteritems():
                for item in items:
                    entitytoken.add(
                        raws.token(
                            value = itemtype,
                            args = [weird_armoury_names.get(item[0], item[0])] + item[1:]
                        )
                    )
    except:
        pydwarf.log.exception('Encountered exception.')
        return pydwarf.failure('Failed to permit items for entities.')
            
    # And add the new reactions
    try:
        pydwarf.log.debug('Adding new reactions.')
        reactions = armouryraws['reaction_armoury'].copy()
        reactions.loc = 'raw/objects'
        reactions.name = 'reaction_armoury_stal'
        response = pydwarf.urist.getfn('pineapple.easypatch')(
            df,
            reactions,
            permit_entities = armoury_entities.keys()
        )
        if not response: return response
    except:
        pydwarf.log.exception('Encountered exception.')
        return pydwarf.failure('Failed to add new reactions.')
    
    # All done!
    return pydwarf.success()
예제 #9
0
# Helper script for building a json file representing which armoury items are allowed for which entities

import sys

sys.path.append('../../')

import json
import raws

print 'And so it begins.'

entities = raws.dir(path='StalsArmouryPackv1_8a_4024')['entity_default']

edict = {}

for entity in entities.all(exact_value='ENTITY'):
    print 'Entity: %s' % entity
    itemtypes = ('AMMO', 'DIGGER', 'TOOL', 'WEAPON', 'ARMOR', 'PANTS',
                 'GLOVES', 'SHOES', 'HELM', 'SHIELD')
    edict[entity.args[0]] = {}
    entitydict = edict[entity.args[0]]
    for item in entity.alluntil(value_in=itemtypes,
                                until_exact_value='ENTITY'):
        if item.value == 'AMMO':
            if item.value not in entitydict: entitydict[item.value] = {}
            forweapon = item.get(exact_value='WEAPON', reverse=True).args[0]
            if forweapon not in entitydict[item.value]:
                entitydict[item.value][forweapon] = []
            entitydict[item.value][forweapon].append(item.args[0])
        else:
            if item.value not in entitydict: entitydict[item.value] = []
def getsmallraws():
    if 'smallraws' not in globals():
        globals()['smallraws'] = raws.dir(root=small_dir, log=pydwarf.log)
    return smallraws
예제 #11
0
def diff(dfraws, paths):
    
    # Get all the files in the mods
    newfiles = []
    for path in paths:
        if os.path.isfile(path) and path.endswith('.txt'):
            with open(path, 'rb') as rfilestream:
                rfiles = (raws.file(rfile=rfilestream, path=path),)
        elif os.path.isdir(path):
            rfiles = raws.dir(path=path).files.values()
        else:
            return pydwarf.failure('Failed to load raws from path %s.' % path)
        newfiles.append(rfiles)
    
    operations = {}
    conflicts = 0
    
    currentfiletokensdict = {}
    for newfilelist in newfiles:
        for newfile in newfilelist:
            pydwarf.log.info('Handling diff for file %s...' % newfile.header)
            
            # Get list of tokens for current file (And don't do it for the same file twice)
            currentfiletokens = None
            if newfile.header in currentfiletokensdict:
                currentfiletokens = currentfiletokensdict[newfile.header]
            elif newfile.header in dfraws.files:
                currentfiletokens = list(dfraws.getfile(newfile.header))
                currentfiletokensdict[newfile.header] = currentfiletokens
            
            # Do a diff
            if currentfiletokens:
                newfiletokens = list(newfile.tokens())
                diff = difflib.SequenceMatcher(None, currentfiletokens, newfiletokens)
                if newfile.header not in operations: operations[newfile.header] = {'insert': [], 'delete': [], 'replace': [], 'equal': []}
                for item in diff.get_opcodes():
                    if item[0] != 'equals':
                        op = item[0]
                        operations[newfile.header][op].append(diffrecord(currentfiletokens, newfiletokens, newfile.path, *item))
                                
            # File doesn't exist yet, don't bother with a diff
            else:
                pydwarf.log.debug('File didn\'t exist yet, adding...')
                dfraws.addfile(rfile=newfile)
                
    for fileheader, fileops in operations.iteritems():
        # Do some handling for potentially conflicting replacements
        for i in xrange(0, len(fileops['replace'])):
            irecord = fileops['replace'][i]
            if not irecord.ignore:
                for j in xrange(i+1, len(fileops['replace'])):
                    jrecord = fileops['replace'][j]
                    # Replacements overlap?
                    if (jrecord.bpath is not irecord.bpath) and (irecord.afrom <= jrecord.auntil and jrecord.afrom <= irecord.auntil):
                        jrecord.ignore = True
                        if not raws.token.tokensequal(irecord.btokens(), jrecord.btokens()):
                            # Replacements aren't identical (this means there's a conflict)
                            if not irecord.conflicts: irecord.conflicts = []
                            irecord.conflicts.append(jrecord)
                            
        # Make replacements (insertions)
        for record in fileops['replace']:
            if not record.ignore:
                if record.conflicts is None:
                    tokens = record.btokens()
                else:
                    # Handle conflicts
                    pydwarf.log.error('Encountered potentially conflicting changes in %s, block replaced by %d input files.' % (fileheader, len(record.conflicts)+1))
                    tokens = []
                    lasttoken = None
                    for conflict in record.conflicts + [record]:
                        conflict.blower.prefix = '\n<<<diff from %s;%s' % (conflict.bpath, conflict.blower.prefix if conflict.blower.prefix else '')
                        for token in conflict.btokens():
                            lasttoken = token
                            tokens.append(token)
                        lasttoken.suffix = '%s\n>>>\n' % (lasttoken.suffix if lasttoken.suffix else '')
                    tokens[0].prefix = '\n<<<<<<diff potential conflict! block modified by %d files %s;\n%s' % (len(record.conflicts)+1, ', '.join([r.bpath for r in record.conflicts] + [record.bpath]), tokens[0].prefix if tokens[0].prefix else '')
                    lasttoken.suffix = '%s\n>>>>>>\n\n' % (lasttoken.suffix if lasttoken.suffix else '')
                    conflicts += 1
                tokens = record.alower.add(tokens=raws.token.copy(tokens=tokens))
                
        # Make insertions
        for record in fileops['insert']:
            record.alower.add(raws.token.copy(tokens=record.btokens()))
            
        # Make deletions
        for record in fileops['delete']:
            for token in record.atokens():
                token.remove()
                
        # Make replacements (deletions)
        for record in fileops['replace']:
            for token in record.atokens():
                token.remove()
        
    if conflicts == 0:
        return pydwarf.success('Merged %d mods without conflicts.' % len(paths))
    else:
        return pydwarf.failure('Merged %d mods with %d conflicts. Recommended you search in outputted raws for text like "<<<<<<diff potential conflict!" and resolve manually.' % (len(paths), conflicts))
예제 #12
0
파일: manager.py 프로젝트: lethosor/PyDwarf
def __main__():

    # Get configuration
    conf = config.export
    if not conf:
        pydwarf.log.error(
            'No configuration specified. Imported config package must contain an export variable.'
        )
        exit(1)

    # Things to do with versions
    pydwarf.log.info('Running PyDwarf %s.' % pydwarf.__version__)
    if conf.version is not None:
        pydwarf.log.info('Managing Dwarf Fortress version %s.' % conf.version)
        pydwarf.urist.session.dfversion = conf.version
    else:
        pydwarf.log.error(
            'No Dwarf Fortress version was specified in conf. Scripts will be run regardless of their indicated compatibility.'
        )

    # Verify that input directory exists
    if not os.path.exists(conf.input):
        pydwarf.log.error('Specified raws directory %s does not exist.' %
                          conf.input)
        exit(1)

    # Make backup
    if conf.backup is not None:
        pydwarf.log.info('Backing up raws to %s...' % conf.backup)
        try:
            copytree(conf.input, conf.backup)
        except:
            pydwarf.log.error('Failed to create backup.')
            exit(1)
    else:
        pydwarf.log.warning('Proceeding without backing up raws.')

    # Read input raws
    pydwarf.log.info('Reading raws from input directory %s...' % conf.input)
    pydwarf.urist.session.dfraws = raws.dir(path=conf.input, log=pydwarf.log)

    # Run each script
    pydwarf.log.info('Running scripts...')
    pydwarf.urist.session.handleall(conf.scripts)

    # Get the output directory, remove old raws if present
    outputdir = conf.output if conf.output else conf.input
    if os.path.exists(outputdir):
        pydwarf.log.info('Removing obsolete raws from %s...' % outputdir)
        for removefile in [
                os.path.join(outputdir, f) for f in os.listdir(outputdir)
        ]:
            pydwarf.log.debug('Removing file %s...' % removefile)
            if removefile.endswith('.txt'): os.remove(removefile)
    else:
        pydwarf.log.info('Creating raws output directory %s...' % outputdir)
        os.makedirs(outputdir)

    # Write the output
    pydwarf.log.info('Writing changes to raws to %s...' % outputdir)
    pydwarf.urist.session.dfraws.write(outputdir, pydwarf.log)

    # All done!
    pydwarf.log.info('All done!')
예제 #13
0
# Helper script for building a json file representing which armoury items are allowed for which entities

import sys
sys.path.append('../../')

import json
import raws

print 'And so it begins.'

entities = raws.dir(path='StalsArmouryPackv1_8a_4024')['entity_default']

edict = {}

for entity in entities.all(exact_value='ENTITY'):
    print 'Entity: %s' % entity
    itemtypes = ('AMMO', 'DIGGER', 'TOOL', 'WEAPON', 'ARMOR', 'PANTS', 'GLOVES', 'SHOES', 'HELM', 'SHIELD')
    edict[entity.args[0]] = {}
    entitydict = edict[entity.args[0]]
    for item in entity.alluntil(value_in=itemtypes, until_exact_value='ENTITY'):
        if item.value == 'AMMO':
            if item.value not in entitydict: entitydict[item.value] = {}
            forweapon = item.get(exact_value='WEAPON', reverse=True).args[0]
            if forweapon not in entitydict[item.value]: entitydict[item.value][forweapon] = []
            entitydict[item.value][forweapon].append(item.args[0])
        else:
            if item.value not in entitydict: entitydict[item.value] = []
            entitydict[item.value].append(item.args[0])

print edict
예제 #14
0
# Helper script for building a json file representing which armoury items are allowed for which entities

import sys
sys.path.append('../../')

import json
import raws

print('And so it begins.')

armouryraws = raws.dir(root='raw/armoury')

itemtypes = ('AMMO', 'DIGGER', 'TOOL', 'WEAPON', 'ARMOR', 'PANTS', 'GLOVES', 'SHOES', 'HELM', 'SHIELD')

edict = {}

for entity in armouryraws.allobj('ENTITY'):
    print('Entity: %s' % entity)
    edict[entity.args[0]] = {}
    entitydict = edict[entity.args[0]]
    for item in entity.allprop(value_in=itemtypes):
        if item.value not in entitydict: entitydict[item.value] = []
        entitydict[item.value].append(item.args)

print(edict)

with open('armouryentities.json', 'wb') as efile: json.dump(edict, efile, indent=4)

print('All done!')
예제 #15
0
This script looks for changes to creature tiles and colors and similar information
in files in gemset's raw/objects, then writes a more easily parseable file out
for the PyDwarf gemset script to use.
'''

import sys
import os

sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), '../..'))

import json
from pprint import pprint

import raws

gems = raws.dir('gemset/raw/objects')
outputpath = 'gemsetproperties.json'

# These are the propeties we care about for each object type
tileproperties = {
    'CREATURE': (
        'CREATURE_TILE',
        'ALTTILE',
    ),
    'INORGANIC': (
        'DISPLAY_COLOR',
        'TILE',
    ),
    'PLANT': (
        'PICKED_TILE',
        'PICKED_COLOR',
예제 #16
0
def diff(df, paths):

    # Get all the files in the mods
    newfiles = []
    for path in paths:
        if os.path.isdir(path):
            rfiles = [
                file for file in raws.dir(root=path).files.values()
                if isinstance(file, raws.rawfile)
            ]
        else:
            return pydwarf.failure('Failed to load raws from path %s.' % path)
        newfiles.append(rfiles)

    operations = {}
    conflicts = 0

    currentfiletokensdict = {}
    for newfilelist in newfiles:
        for newfile in newfilelist:
            pydwarf.log.info('Handling diff for file %s...' % newfile.path)

            # Get list of tokens for current file (And don't do it for the same file twice)
            currentfiletokens = None
            if str(newfile) in currentfiletokensdict:
                currentfiletokens = currentfiletokensdict[str(newfile)]
            elif str(newfile) in df.files:
                currentfiletokens = list(df[str(newfile)].tokens())
                currentfiletokensdict[str(newfile)] = currentfiletokens

            # Do a diff
            if currentfiletokens:
                newfiletokens = list(newfile.tokens())
                diff = difflib.SequenceMatcher(None, currentfiletokens,
                                               newfiletokens)
                if str(newfile) not in operations:
                    operations[str(newfile)] = {
                        'insert': [],
                        'delete': [],
                        'replace': [],
                        'equal': []
                    }
                for item in diff.get_opcodes():
                    if item[0] != 'equals':
                        op = item[0]
                        operations[str(newfile)][op].append(
                            diffrecord(currentfiletokens, newfiletokens,
                                       newfile.path, *item))

            # File doesn't exist yet, don't bother with a diff
            else:
                pydwarf.log.debug('File didn\'t exist yet, adding...')
                df.add(newfile.copy())

    for fileheader, fileops in operations.iteritems():
        # Do some handling for potentially conflicting replacements
        for i in xrange(0, len(fileops['replace'])):
            irecord = fileops['replace'][i]
            if not irecord.ignore:
                for j in xrange(i + 1, len(fileops['replace'])):
                    jrecord = fileops['replace'][j]
                    # Replacements overlap?
                    if (jrecord.bpath is not irecord.bpath) and (
                            irecord.afrom <= jrecord.auntil
                            and jrecord.afrom <= irecord.auntil):
                        jrecord.ignore = True
                        if not raws.helpers.tokensequal(
                                irecord.btokens(), jrecord.btokens()):
                            # Replacements aren't identical (this means there's a conflict)
                            if not irecord.conflicts: irecord.conflicts = []
                            irecord.conflicts.append(jrecord)

        # Make replacements (insertions)
        for record in fileops['replace']:
            if not record.ignore:
                if record.conflicts is None:
                    tokens = record.btokens()
                else:
                    # Handle conflicts
                    pydwarf.log.error(
                        'Encountered potentially conflicting changes in %s, block replaced by %d input files.'
                        % (fileheader, len(record.conflicts) + 1))
                    tokens = []
                    lasttoken = None
                    for conflict in record.conflicts + [record]:
                        conflict.blower.prefix = '\n<<<diff from %s;%s' % (
                            conflict.bpath, conflict.blower.prefix
                            if conflict.blower.prefix else '')
                        for token in conflict.btokens():
                            lasttoken = token
                            tokens.append(token)
                        lasttoken.suffix = '%s\n>>>\n' % (
                            lasttoken.suffix if lasttoken.suffix else '')
                    tokens[
                        0].prefix = '\n<<<<<<diff potential conflict! block modified by %d files %s;\n%s' % (
                            len(record.conflicts) + 1,
                            ', '.join([r.bpath for r in record.conflicts] +
                                      [record.bpath]),
                            tokens[0].prefix if tokens[0].prefix else '')
                    lasttoken.suffix = '%s\n>>>>>>\n\n' % (
                        lasttoken.suffix if lasttoken.suffix else '')
                    conflicts += 1
                tokens = record.alower.add(
                    tokens=raws.helpers.copytokens(tokens))

        # Make insertions
        for record in fileops['insert']:
            record.alower.add(raws.helpers.copytokens(tokens=record.btokens()))

        # Make deletions
        for record in fileops['delete']:
            for token in record.atokens():
                token.remove()

        # Make replacements (deletions)
        for record in fileops['replace']:
            for token in record.atokens():
                token.remove()

    if conflicts == 0:
        return pydwarf.success('Merged %d mods without conflicts.' %
                               len(paths))
    else:
        return pydwarf.success(
            'Merged %d mods with %d conflicts. Recommended you search in outputted raws for text like "<<<<<<diff potential conflict!" and resolve manually.'
            % (len(paths), conflicts))
예제 #17
0
def getsmallraws():
    if 'smallraws' not in globals():
        globals()['smallraws'] = raws.dir(root=small_dir, log=pydwarf.log)
    return smallraws
예제 #18
0
in files in gemset's raw/objects, then writes a more easily parseable file out
for the PyDwarf gemset script to use.
'''

import sys
import os

sys.path.append(
    os.path.join(os.path.dirname(os.path.abspath(__file__)), '../..'))

import json
from pprint import pprint

import raws

gems = raws.dir('gemset/raw/objects')
outputpath = 'gemsetproperties.json'

# These are the propeties we care about for each object type
tileproperties = {
    'CREATURE': (
        'CREATURE_TILE',
        'ALTTILE',
    ),
    'INORGANIC': (
        'DISPLAY_COLOR',
        'TILE',
    ),
    'PLANT': (
        'PICKED_TILE',
        'PICKED_COLOR',