Skip to content

Instantly share code, notes, and snippets.

@rthompsonj
Created December 6, 2017 04:45
Show Gist options
  • Select an option

  • Save rthompsonj/5c3d05794da1c3097b713aeeb3f85555 to your computer and use it in GitHub Desktop.

Select an option

Save rthompsonj/5c3d05794da1c3097b713aeeb3f85555 to your computer and use it in GitHub Desktop.

Revisions

  1. rthompsonj created this gist Dec 6, 2017.
    91 changes: 91 additions & 0 deletions modify_config.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,91 @@
    import argparse

    #CFG = 'Admins.cfg'
    CFG = 'Output.cfg'
    OUTPUT = 'Output.cfg'

    ## add grouping class that handles add/modify/remove
    class Config(object):
    def __init__(self, currentPlayers, staticLines):
    self.currentPlayers = currentPlayers
    self.staticLines = staticLines

    def modify_player(self, remove, steamId, newAdminLevel=None, name=None):
    if remove:
    return self._remove_player(steamId)
    else:
    return self._add_or_modify(steamId, newAdminLevel, name)

    def _add_or_modify(self, steamId, newAdminLevel, name):
    if newAdminLevel is None or name is None:
    print('Missing admin level or name!')
    return False
    if steamId in self.currentPlayers:
    print('Updated {0}'.format(steamId))
    self.currentPlayers[steamId].adminlevel = newAdminLevel
    else:
    print('Added {0}'.format(steamId))
    self.currentPlayers[steamId] = Player(steamId, newAdminLevel, name)
    return True

    def _remove_player(self, steamId):
    if steamId not in self.currentPlayers:
    print('{0} not found!'.format(steamId))
    return False
    print('Removed {0}'.format(steamId))
    self.currentPlayers.pop(steamId)
    return True

    class Player(object):
    def __init__(self, steamId, adminLevel, name):
    self.steamId = steamId
    self.adminLevel = adminLevel
    self.name = name


    def get_current_file_data():
    with open(CFG, 'r') as f:
    data = f.read()
    lines = data.split('\n')

    currentPlayers = {}
    staticLines = []
    for line in lines:
    if not line.startswith('Admin='):
    staticLines.append(line)
    ## CHECK is this a Group= definition? if so, parse out group types for checking
    # if not a valid group was passed then print the list of valid groups
    continue

    person = line.strip('Admin=')

    data = person.split(':')
    steamId = data[0]

    intermediate = data[1].split('//')
    adminLevel = intermediate[0].strip(' ')
    name = intermediate[1].strip('\r').strip('\n').strip(' ')

    currentPlayers[steamId] = Player(steamId, adminLevel, name)

    return Config(currentPlayers, staticLines)

    def write_new_config(config):
    with open(OUTPUT, 'w') as f:
    for staticLine in config.staticLines:
    f.write(staticLine)
    for player in config.currentPlayers.values():
    f.write('Admin={0}:{1}\t\t\t//{2}\r\n'.format(player.steamId, player.adminLevel, player.name))

    if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('steamId', help='the steam id of the player')
    parser.add_argument('permission', help='permission level of the player')
    parser.add_argument('name', help='name of player')
    parser.add_argument('-r', dest='remove', action='store_true', help='Remove player')
    args = parser.parse_args()

    config = get_current_file_data()
    result = config.modify_player(args.remove, args.steamId, args.permission, args.name)
    if result:
    write_new_config(config)