#! /usr/bin/env python

import os, re

def getvram():
	"acquires the amount of video ram from the x-server log file; fails silently and returns 0"
	xdpyinfo = os.popen("xdpyinfo 2>/dev/null", "r")
	vendor = re.compile("\nvendor string\:[ \t]*(.*)\n").search(xdpyinfo.read(), 1)
	xdpyinfo.close()
	if vendor == None:
		return 0
	if vendor.groups()[0] == "The XFree86 Project, Inc":
		log = "/var/log/XFree86.0.log"
	elif vendor.groups()[0] == "The X.Org Foundation":
		log = "/var/log/Xorg.0.log"
	else:
		return 0
	xlog = os.popen("grep -i videoram "+log+" 2>/dev/null", "r")
	vram = re.compile(".*\: videoram: ([0-9]*) kbytes?", re.I).match(xlog.read(), 1)
	xlog.close()
	if vram == None:
		return 0
	return int(vram.groups()[0])//1024

print getvram()
