<commit_before># ggmath - ggame extensions for geometry and mathematics in the browser

from ggame import Color, LineStyle, LineAsset, Sprite, App
from abc import ABCMeta, abstractmethod

from math import sin, cos

class _MathObj(Sprite):

    __metaclass__ = ABCMeta

    @abstractmethod
    def overridethis(self):
        pass

class LineSegment(_MathObj):

    def __init__(self, start, end, style=LineStyle(1, Color(0,1))):
        self._start = start
        self._end = end
        self._style = style
        self._newSegment(self._start, self._end, self._style)

    def _updateAsset(self, asset):
        self.asset = asset
        self.GFX = self.asset.GFX
        self.GFX.visible = True
        App._add(self)

    def _newSegment(self, start, end, style):
        self._updateAsset(LineAsset(end[0]-start[0], end[1]-start[1], style))
        self.position = start

    def _refreshSegment(self, start, end, style):
        App._remove(self)
        self._newSegment(start, end, style)

    @property
    def start(self):
        return self._start

    @start.setter
    def start(self, val):
        self._start = val
        self._refreshSegment(self._start, self._end, self._style)

    @property
    def end(self):
        return self._end

    @end.setter
    def end(self, val):
        self._end = val
        self._refreshSegment(self._start, self._end, self._style)

    def step(self):
        self.start = (self.start[0]+1, self.start[1])

lines = [LineSegment((300*sin(x)+300,300*cos(x)+300), (-300*sin(x)+300,-300*cos(x)+300)) for x in range(50)]
#l = LineSegment((200,200), (500,500))
g = 0
class MathApp(App):
    def step(self):
        global g
        for spr in MathApp.getSpritesbyClass(LineSegment):
            g = g + 1
            #spr.step()
            #spr.start = (300*sin(g)+300,300*cos(g)+300)
            spr.start = (spr.start[0]+1, spr.start[1])
            pass

ap = MathApp()
ap.run()<commit_msg>Updated from Brython Server: 2/9/2017 1:24:08 PM<commit_after># ggmath - ggame extensions for geometry and mathematics in the browser

from ggame import Color, LineStyle, LineAsset, Sprite, App
from abc import ABCMeta, abstractmethod

from math import sin, cos

class _MathObj(Sprite, metaclass=ABCMeta):

    @abstractmethod
    def overridethis(self):
        pass

class LineSegment(_MathObj):

    def __init__(self, start, end, style=LineStyle(1, Color(0,1))):
        self._start = start
        self._end = end
        self._style = style
        self._newSegment(self._start, self._end, self._style)

    def _updateAsset(self, asset):
        self.asset = asset
        self.GFX = self.asset.GFX
        self.GFX.visible = True
        App._add(self)

    def _newSegment(self, start, end, style):
        self._updateAsset(LineAsset(end[0]-start[0], end[1]-start[1], style))
        self.position = start

    def _refreshSegment(self, start, end, style):
        App._remove(self)
        self._newSegment(start, end, style)

    @property
    def start(self):
        return self._start

    @start.setter
    def start(self, val):
        self._start = val
        self._refreshSegment(self._start, self._end, self._style)

    @property
    def end(self):
        return self._end

    @end.setter
    def end(self, val):
        self._end = val
        self._refreshSegment(self._start, self._end, self._style)

    def step(self):
        self.start = (self.start[0]+1, self.start[1])

lines = [LineSegment((300*sin(x)+300,300*cos(x)+300), (-300*sin(x)+300,-300*cos(x)+300)) for x in range(50)]
#l = LineSegment((200,200), (500,500))
g = 0
class MathApp(App):
    def step(self):
        global g
        for spr in MathApp.getSpritesbyClass(LineSegment):
            g = g + 1
            #spr.step()
            #spr.start = (300*sin(g)+300,300*cos(g)+300)
            spr.start = (spr.start[0]+1, spr.start[1])
            pass

ap = MathApp()
ap.run()<|endoftext|>


<commit_before># ggmath - ggame extensions for geometry and mathematics in the browser

from ggame import Color, LineStyle, LineAsset, Sprite, App
from abc import ABCMeta, abstractmethod

from math import sin, cos

class _MathDynamic(metaclass=ABCMeta):

    @abstractmethod
    def step():
        pass

    @classmethod
    def Eval(cls, val):
        print("ONE")
        if callable(val):
            print("TWO")
            return(val)
        else:
            print("THREE")
            return lambda : val

class _MathVisual(Sprite, _MathDynamic, metaclass=ABCMeta):

    def _updateAsset(self, asset):
        self.asset = asset
        self.GFX = self.asset.GFX
        self.GFX.visible = True
        App._add(self)

    @abstractmethod
    def _newAsset():
        pass

    @abstractmethod
    def _refreshAsset():
        pass

class LineSegment(_MathVisual):

    def __init__(self, start, end, style=LineStyle(1, Color(0,1))):
        self._start = self.Eval(start)  # save function
        self._end = self.Eval(end)
        self._style = style
        self._oldstart = None
        self._oldend = None
        self._newAsset(self._start(), self._end(), self._style)

    def _newAsset(self, start, end, style):
        # start and end are simple numerics
        if start != self._oldstart or end != self._oldend:
            self._oldstart = start
            self._oldend = end
            self._updateAsset(LineAsset(end[0]-start[0], end[1]-start[1], style))
            self.position = start

    def _refreshAsset(self, start, end, style):
        App._remove(self)
        self._newAsset(start, end, style)

    def _touchAsset(self):
        self._refreshAsset(self._start(), self._end(), self._style)

    @property
    def start(self):
        return self._start()

    @start.setter
    def start(self, val):
        newval = self.Eval(val)
        if newval != self._start:
            self._start = newval
            self._touchAsset()

    @property
    def end(self):
        return self._end()

    @end.setter
    def end(self, val):
        newval = self.Eval(val)
        if newval != self._end:
            self._end = newval
            self._touchAsset()

    def step(self):
        self._touchAsset()

#l = LineSegment((200,200), (500,500))

class MathApp(App):

    def __init__(self):
        super().__init__()

        def f():
            return 300*cos(self.g)+300

        self.g = 0
        f = lambda :300*cos(self.g)+300
        #f = 100
        self.lines = [LineSegment((300*sin(x)+300, f), (-300*sin(x)+300,-300*cos(x)+300)) for x in range(10)]

    def step(self):
        self.g = self.g + 1
        if self.g > 60:
            self.g = 0

ap = MathApp()
ap.run()<commit_msg>Updated from Brython Server: 2/9/2017 9:04:43 PM<commit_after># ggmath - ggame extensions for geometry and mathematics in the browser

from ggame import Color, LineStyle, LineAsset, Sprite, App
from abc import ABCMeta, abstractmethod

from math import sin, cos

class _MathDynamic(metaclass=ABCMeta):

    @abstractmethod
    def step():
        pass

    @classmethod
    def Eval(cls, val):
        print("ONE")
        if callable(val):
            print("TWO")
            return(val)
        else:
            print("THREE")
            return lambda : val

class _MathVisual(Sprite, _MathDynamic, metaclass=ABCMeta):

    def _updateAsset(self, asset):
        self.asset = asset
        self.GFX = self.asset.GFX
        self.GFX.visible = True
        App._add(self)

    @abstractmethod
    def _newAsset():
        pass

    @abstractmethod
    def _refreshAsset():
        pass

class LineSegment(_MathVisual):

    def __init__(self, start, end, style=LineStyle(1, Color(0,1))):
        self._start = self.Eval(start)  # save function
        self._end = self.Eval(end)
        self._style = style
        self._oldstart = None
        self._oldend = None
        self._newAsset(self._start(), self._end(), self._style)

    def _newAsset(self, start, end, style):
        # start and end are simple numerics
        if start != self._oldstart or end != self._oldend:
            self._oldstart = start
            self._oldend = end
            self._updateAsset(LineAsset(end[0]-start[0], end[1]-start[1], style))
            self.position = start

    def _refreshAsset(self, start, end, style):
        App._remove(self)
        self._newAsset(start, end, style)

    def _touchAsset(self):
        self._refreshAsset(self._start(), self._end(), self._style)

    @property
    def start(self):
        return self._start()

    @start.setter
    def start(self, val):
        newval = self.Eval(val)
        if newval != self._start:
            self._start = newval
            self._touchAsset()

    @property
    def end(self):
        return self._end()

    @end.setter
    def end(self, val):
        newval = self.Eval(val)
        if newval != self._end:
            self._end = newval
            self._touchAsset()

    def step(self):
        self._touchAsset()

#l = LineSegment((200,200), (500,500))

class MathApp(App):

    def __init__(self):
        super().__init__()

        def f():
            return 300*cos(self.g)+300

        self.g = 0
        self.lines = [LineSegment(lambda x = self.g:(300*sin(x)+300, 300*cos(x)+300), (-300*sin(x)+300,-300*cos(x)+300)) for x in range(10)]

    def step(self):
        self.g = self.g + 1
        if self.g > 60:
            self.g = 0

ap = MathApp()
ap.run()<|endoftext|>


<commit_before>import os
import sublime
import sublime_plugin
from threading import Thread

from sublime_haskell_common import is_enabled_haskell_command, get_haskell_command_window_view_file_project, call_ghcmod_and_wait
from parseoutput import parse_output_messages, show_output_result_text, format_output_messages, mark_messages_in_views, hide_output

def lint_as_hints(msgs):
    for m in msgs:
        if m[0] == 'lint':
            m[1].level = 'hint'

class SublimeHaskellGhcModCheck(sublime_plugin.WindowCommand):
    def run(self):
        run_ghcmod('check', 'Checking')

    def is_enabled(self):
        return is_enabled_haskell_command(False)

class SublimeHaskellGhcModLint(sublime_plugin.WindowCommand):
    def run(self):
        run_ghcmod('lint', 'Linting', lint_as_hints)

    def is_enabled(self):
        return is_enabled_haskell_command(False)

class SublimeHaskellGhcModCheckAndLint(sublime_plugin.WindowCommand):
    def run(self):
        run_ghcmods(['check', 'lint'], 'Checking and Linting', lint_as_hints)

    def is_enabled(self):
        return is_enabled_haskell_command(False)

def run_ghcmods(cmds, msg, alter_messages_cb=None):
    """
    Run several ghcmod commands, concats result messages with callback
    and show output.
    alter_messages_cb accepts dictionary (cmd => list of output messages)
    """
    window, view, file_shown_in_view = get_haskell_command_window_view_file_project()
    if not file_shown_in_view:
        return

    file_dir, file_name = os.path.split(file_shown_in_view)

    ghc_mod_args = []
    for cmd in cmds:
        ghc_mod_args.append((cmd, [cmd, file_shown_in_view]))

    def show_current_file_first_and_alter(msgs):
        if alter_messages_cb:
            alter_messages_cb(msgs)

        def compare(l, r):
            # sort by file equality to file_name
            res = cmp(l[1].filename != file_shown_in_view, r[1].filename != file_shown_in_view)
            if res == 0:
                # then by file
                res = cmp(l[1].filename, r[1].filename)
                if res == 0:
                    # then by line
                    res = cmp(l[1].line, r[1].line)
                    if res == 0:
                        # then by column
                        res = cmp(l[1].column, r[1].column)
            return res

        msgs.sort(compare)

    run_ghcmods_thread(view, file_shown_in_view, 'Ghc-Mod: ' + msg + ' ' + file_name, ghc_mod_args, show_current_file_first_and_alter)

def run_ghcmod(cmd, msg, alter_messages_cb=None):
    run_ghcmods([cmd], msg, alter_messages_cb)

def run_ghcmods_thread(view, filename, msg, cmds_with_args, alter_messages_cb):
    sublime.status_message(msg + '...')
    thread = Thread(
        target=wait_ghcmod_and_parse,
        args=(view, filename, msg, cmds_with_args, alter_messages_cb))
    thread.start()

def wait_ghcmod_and_parse(view, filename, msg, cmds_with_args, alter_messages_cb):
    sublime.set_timeout(lambda: hide_output(view), 0)

    parsed_messages = []

    file_dir = os.path.dirname(filename)

    all_cmds_successful = True

    for (cmd, args) in cmds_with_args:
        stdout = call_ghcmod_and_wait(args, filename)

        # stdout contains NULL as line endings within one message
        # error_output_regex using indents to determine one message scope
        # Replace NULLs to indents
        out = stdout.replace('\0', '\n  ').decode('utf-8')

        all_cmds_successful &= len(out) == 0

        parsed = parse_output_messages(file_dir, out)
        for p in parsed:
            parsed_messages.append((cmd, p))

    if alter_messages_cb:
        alter_messages_cb(parsed_messages)

    concated_messages = map(lambda m: m[1], parsed_messages)
    output_text = format_output_messages(concated_messages)

    exit_code = 0 if all_cmds_successful else 1

    show_output_result_text(view, msg, output_text, exit_code, file_dir)
    sublime.set_timeout(lambda: mark_messages_in_views(concated_messages), 0)
<commit_msg>Use list comp instead of map<commit_after>import os
import sublime
import sublime_plugin
from threading import Thread

from sublime_haskell_common import is_enabled_haskell_command, get_haskell_command_window_view_file_project, call_ghcmod_and_wait
from parseoutput import parse_output_messages, show_output_result_text, format_output_messages, mark_messages_in_views, hide_output

def lint_as_hints(msgs):
    for m in msgs:
        if m[0] == 'lint':
            m[1].level = 'hint'

class SublimeHaskellGhcModCheck(sublime_plugin.WindowCommand):
    def run(self):
        run_ghcmod('check', 'Checking')

    def is_enabled(self):
        return is_enabled_haskell_command(False)

class SublimeHaskellGhcModLint(sublime_plugin.WindowCommand):
    def run(self):
        run_ghcmod('lint', 'Linting', lint_as_hints)

    def is_enabled(self):
        return is_enabled_haskell_command(False)

class SublimeHaskellGhcModCheckAndLint(sublime_plugin.WindowCommand):
    def run(self):
        run_ghcmods(['check', 'lint'], 'Checking and Linting', lint_as_hints)

    def is_enabled(self):
        return is_enabled_haskell_command(False)

def run_ghcmods(cmds, msg, alter_messages_cb=None):
    """
    Run several ghcmod commands, concats result messages with callback
    and show output.
    alter_messages_cb accepts dictionary (cmd => list of output messages)
    """
    window, view, file_shown_in_view = get_haskell_command_window_view_file_project()
    if not file_shown_in_view:
        return

    file_dir, file_name = os.path.split(file_shown_in_view)

    ghc_mod_args = []
    for cmd in cmds:
        ghc_mod_args.append((cmd, [cmd, file_shown_in_view]))

    def show_current_file_first_and_alter(msgs):
        if alter_messages_cb:
            alter_messages_cb(msgs)

        def compare(l, r):
            # sort by file equality to file_name
            res = cmp(l[1].filename != file_shown_in_view, r[1].filename != file_shown_in_view)
            if res == 0:
                # then by file
                res = cmp(l[1].filename, r[1].filename)
                if res == 0:
                    # then by line
                    res = cmp(l[1].line, r[1].line)
                    if res == 0:
                        # then by column
                        res = cmp(l[1].column, r[1].column)
            return res

        msgs.sort(compare)

    run_ghcmods_thread(view, file_shown_in_view, 'Ghc-Mod: ' + msg + ' ' + file_name, ghc_mod_args, show_current_file_first_and_alter)

def run_ghcmod(cmd, msg, alter_messages_cb=None):
    run_ghcmods([cmd], msg, alter_messages_cb)

def run_ghcmods_thread(view, filename, msg, cmds_with_args, alter_messages_cb):
    sublime.status_message(msg + '...')
    thread = Thread(
        target=wait_ghcmod_and_parse,
        args=(view, filename, msg, cmds_with_args, alter_messages_cb))
    thread.start()

def wait_ghcmod_and_parse(view, filename, msg, cmds_with_args, alter_messages_cb):
    sublime.set_timeout(lambda: hide_output(view), 0)

    parsed_messages = []

    file_dir = os.path.dirname(filename)

    all_cmds_successful = True

    for (cmd, args) in cmds_with_args:
        stdout = call_ghcmod_and_wait(args, filename)

        # stdout contains NULL as line endings within one message
        # error_output_regex using indents to determine one message scope
        # Replace NULLs to indents
        out = stdout.replace('\0', '\n  ').decode('utf-8')

        all_cmds_successful &= len(out) == 0

        parsed = parse_output_messages(file_dir, out)
        for p in parsed:
            parsed_messages.append((cmd, p))

    if alter_messages_cb:
        alter_messages_cb(parsed_messages)

    concated_messages = [m[1] for m in parsed_messages]
    output_text = format_output_messages(concated_messages)

    exit_code = 0 if all_cmds_successful else 1

    show_output_result_text(view, msg, output_text, exit_code, file_dir)
    sublime.set_timeout(lambda: mark_messages_in_views(concated_messages), 0)
<|endoftext|>


<commit_before>import os, urllib, re

REG_PY_URL = 'https://cvs.khronos.org/svn/repos/ogl/trunk/doc/registry/public/api/reg.py';
GL_XML_URL = 'https://cvs.khronos.org/svn/repos/ogl/trunk/doc/registry/public/api/gl.xml';

if not os.path.isfile('reg.py'):
	print 'reg.py not found. Downloading...'
	urllib.urlretrieve(REG_PY_URL, 'reg.py')
else:
	print 'Found reg.py. Reusing it...'

if not os.path.isfile('gl.xml'):
	print 'gl.xml not found. Downloading...'
	urllib.urlretrieve(GL_XML_URL, 'gl.xml')
else:
	print 'Found gl.xml. Reusing it...'

if not os.path.isfile('reg.py') or not os.path.isfile('reg.py'):
	raise Exception('Either reg.py or gl.xml does not exist.')

print 'Began API generation...'
from reg import *

prefixHeaderString = '''#pragma once

#include <cinttypes>
#include <cstddef>

#if !defined(_WIN32) && (defined(__WIN32__) || defined(WIN32) || defined(__MINGW32__))
#	define _WIN32
#endif /* _WIN32 */
#ifndef APIENTRY
#   ifdef _WIN32
#	   define APIENTRY __stdcall
#   else
#	   define APIENTRY
#   endif
#endif /* APIENTRY */

#if defined _WIN32 || defined __CYGWIN__
#	define GLCOREDLL_IMPORT __declspec(dllimport)
#	define GLCOREDLL_EXPORT __declspec(dllexport)
#else
#	if __GNUC__ >= 4
#		define GLCOREDLL_IMPORT __attribute__ ((visibility ("default")))
#		define GLCOREDLL_EXPORT __attribute__ ((visibility ("default")))
#	else
#		define GLCOREDLL_IMPORT
#		define GLCOREDLL_EXPORT
#	endif /* __GNUC__ */
#endif /* _WIN32 */

#ifdef GLCORE_DLL // defined if GlCore is compiled as a DLL
#	ifdef GLCORE_DLL_EXPORTS // defined if we are building the GlCore DLL (instead of using it)
#		define GLCOREAPI GLCOREDLL_EXPORT
#	else
#		define GLCOREAPI GLCOREDLL_IMPORT
#	endif /* GLCORE_DLL_EXPORTS */
#else // GLCORE_DLL is not defined: this means GlCore is a static lib.
#	define GLCOREAPI
#endif /* GLCORE_DLL */
'''

class CppGeneratorOptions(GeneratorOptions):
	"""Represents options during C++ header generation"""
	def __init__(self,
				 filename = 'glcore.h',
				 apiname = 'gl',
				 profile = 'core',
				 versions = '.*',
				 emitversions = '.*',
				 defaultExtensions = 'glcore',
				 addExtensions = None,
				 removeExtensions = None,
				 sortProcedure = regSortFeatures,
				 apicall = 'GLCOREAPI ',
				 apientry = 'APIENTRY '):
		GeneratorOptions.__init__(self, filename, apiname, profile,
			versions, emitversions, defaultExtensions,
			addExtensions, removeExtensions, sortProcedure)

		self.apicall		 = apicall
		self.apientry		= apientry

from pprint import pprint

class CppOutputGenerator(OutputGenerator):
	"""Generate specified API interfaces as a C++ header"""
	def __init__(self,
				 errFile = None, #sys.stderr,
				 warnFile = None, #sys.stderr,
				 diagFile = None): #sys.stdout):
		OutputGenerator.__init__(self, errFile, warnFile, diagFile)
		#internal bookkeeping
		self.currentFeature = None
		self.currentNamespace = ''
		self.rootNamespace = ''
		self.dependentVersion = None #highest version available
		self.enumTypes = { 'i' : '', 'u': '', 'ull' : '' }
		self.groupDict = None
		self.prototypes = ''
		self.types = ''
		self.enums = ''

	def resetState(self):
		self.enums = ''
		self.types = ''
		self.prototypes = ''

	def isEmpty(self):
		return self.enums == '' and self.types == '' and self.prototypes == ''

	def makeGroupDictIfNotExist(self, elem):
		if self.groupDict == None:
			self.groupDict = {}
			# hack: to find all groups
			for group in elem.getparent().findall('groups/group'):
				group_name = group.get('name')
				for enum in group.findall('enum'):
					enum_name = enum.get('name')
					if enum_name not in self.groupDict:
						self.groupDict[enum_name] = []
					self.groupDict[enum_name].append(group_name)

	def newline(self):
		write('', file=self.outFile)

	def writeline(self, str):
		write(str, file=self.outFile)

	def beginFile(self, genOpts):
		OutputGenerator.beginFile(self, genOpts)
		self.rootNamespace = genOpts.apiname + genOpts.profile
		self.writeline(prefixHeaderString)
		self.writeline('namespace ' + self.rootNamespace)
		self.writeline('{')
		self.newline();

	def endFile(self):
		self.writeline('} //!namespace ' + self.rootNamespace)
		OutputGenerator.endFile(self)

	def beginFeature(self, interface, emit):
		self.makeGroupDictIfNotExist(interface)
		OutputGenerator.beginFeature(self, interface, emit)
		self.currentFeature = FeatureInfo(interface)
		self.resetState()

	def endFeature(self):
		if not self.isEmpty():
			self.genNamespaceBegin()
			self.genTypes()
			self.genEnums()
			self.genCmds()
			self.genNamespaceEnd()
		OutputGenerator.endFeature(self)

	def isFeatureApi(self):
		return self.currentFeature.category == 'VERSION'

	def genNamespaceBegin(self):
		if self.isFeatureApi():
			self.writeline('namespace api {')
			self.genApiNamespaceBegin()
			self.genDependentNamespace()
		else:
			self.writeline('namespace ' + self.currentFeature.category.lower() + ' {')
			self.genExtNamespaceBegin()
			self.genDependentNamespace()

	def genDependentNamespace(self):
		if self.dependentVersion != None:
			self.writeline('using namespace api::v' + self.dependentVersion + ';')
			self.newline()

	def genNamespaceEnd(self):
		if self.isFeatureApi():
			self.genApiNamespaceEnd()
			self.writeline('} //!namespace api')
			self.dependentVersion = self.currentNamespace
		else:
			self.genExtNamespaceEnd()
			self.writeline('} //!namespace ' + self.currentFeature.category.lower())
		self.newline()

	def genApiNamespaceBegin(self):
		self.currentNamespace = self.currentFeature.number.replace('.', '')
		self.writeline('namespace v' + self.currentNamespace + ' {')
		self.newline()

	def genApiNamespaceEnd(self):
		self.writeline('} //!namespace v' + self.currentNamespace)

	def genExtNamespaceBegin(self):
		self.currentNamespace = self.currentFeature.name.replace('GL_' + self.currentFeature.category + '_', '').lower()
		self.writeline('namespace ' + self.currentNamespace + ' {')
		self.newline()
		self.writeline(self.makeExtendionDocumentation(self.currentFeature.category, self.currentNamespace))
		self.newline()

	def genExtNamespaceEnd(self):
		self.writeline('} //!namespace ' + self.currentNamespace)

	def genType(self, typeinfo, name):
		OutputGenerator.genType(self, typeinfo, name)
		if name in ('inttypes', 'stddef'):
			return
		typeElem = typeinfo.elem
		s = noneStr(typeElem.text)
		for elem in typeElem:
			if (elem.tag == 'apientry'):
				s += self.genOpts.apientry + noneStr(elem.tail)
			else:
				s += noneStr(elem.tail)
		if name.startswith('struct'):
			s = name + s;
		else:
			s = s.replace(' ;', ';')
			s = s.replace('  *)', ' *)')
			s = s.replace('typedef', 'using ' + name + ' =')
			s += '\n'
		self.types += s

	def genTypes(self):
		if self.types != '':
			self.writeline(self.types)
		self.types = ''

	def genEnum(self, enuminfo, name):
		OutputGenerator.genEnum(self, enuminfo, name)
		t = enuminfo.elem.get('type')
		if t == '' or t == None:
			t = 'i' #hack: to force type to be int for non suffixed ones
		if t == 'i' or t == 'u' or t == 'ull':
			self.enumTypes[t] += name.ljust(47) + ' = ' + enuminfo.elem.get('value') + ( t if t != 'i' else '' )
			self.enumTypes[t] += ","
			if name in self.groupDict:
				self.enumTypes[t] += ' /* '
				for group in self.groupDict[name]:
					self.enumTypes[t] += (group + ' ')
				self.enumTypes[t] += '*/'
			self.enumTypes[t] += "\n"
		else:
			raise Exception('Unknown enum type found: ' + t)

	def genEnums(self):
		for enumType in self.enumTypes:
			enumBody = self.enumTypes[enumType]
			if enumBody != '':
				enumBody += '}; //!enum'
				cppType = ''
				if enumType == 'u':
					cppType = ' : unsigned'
				elif enumType == 'ull':
					cppType = ' : unsigned long long'
				self.enums += ('enum' + cppType + ' {\n')
				self.enums += enumBody
				self.enums += '\n'
				self.enumTypes[enumType] = ''
		self.writeline(self.enums)

	def genCmd(self, cmdinfo, name):
		OutputGenerator.genCmd(self, cmdinfo, name)
		self.prototypes += (self.makePrototype(cmdinfo.elem))

	def genCmds(self):
		if self.prototypes != '':
			self.writeline(self.prototypes)
		self.prototypes = ''

	def makePrototype(self, cmd):
		proto = cmd.find('proto')
		params = cmd.findall('param')
		pdecl = self.genOpts.apicall
		pdecl += noneStr(proto.text)
		for elem in proto:
			text = noneStr(elem.text)
			tail = noneStr(elem.tail)
			if (elem.tag == 'name'):
				pdecl += self.genOpts.apientry + text + tail
			else:
				pdecl += text + tail
		n = len(params)
		paramdecl = ' ('
		if n > 0:
			for i in range(0,n):
				paramdecl += ''.join([t for t in params[i].itertext()])
				if (i < n - 1):
					paramdecl += ', '
		else:
			paramdecl += 'void'
		paramdecl += ");\n";
		return pdecl + paramdecl

	def makeExtendionDocumentation(self, cat, name):
		print 'Generating documentation for ' + name + ' in category ' + cat
		url = 'https://www.opengl.org/registry/specs/' + cat + '/' + name + '.txt'
		doc = urllib.urlopen(url).read().decode('utf-8')
		regex = 'Overview\n\n((^ +\S.*\n)+)'
		overview = ''
		result = re.search(regex, doc, re.M)
		if result != None:
			overview = re.search(regex, doc, re.M).group(1)
			overview = re.sub('^ +', '', overview, 0, re.M)
		if overview != '':
			overview = ((overview[:300] + '...\n\n') if len(overview) > 300 else overview + '\n')
		overview += 'Full extension documentation available at:\n'
		overview += url
		return '/*\nExtension overview:\n' + (overview if overview != '' else 'not available.') + '\n*/'

# Load & parse registry
reg = Registry()
reg.loadElementTree(etree.parse('gl.xml'))
reg.setGenerator(CppOutputGenerator())
reg.apiGen(CppGeneratorOptions())

print 'Done.'
<commit_msg>First attempt at doing docs for core api<commit_after>import os, urllib, re

REG_PY_URL = 'https://cvs.khronos.org/svn/repos/ogl/trunk/doc/registry/public/api/reg.py';
GL_XML_URL = 'https://cvs.khronos.org/svn/repos/ogl/trunk/doc/registry/public/api/gl.xml';

if not os.path.isfile('reg.py'):
	print 'reg.py not found. Downloading...'
	urllib.urlretrieve(REG_PY_URL, 'reg.py')
else:
	print 'Found reg.py. Reusing it...'

if not os.path.isfile('gl.xml'):
	print 'gl.xml not found. Downloading...'
	urllib.urlretrieve(GL_XML_URL, 'gl.xml')
else:
	print 'Found gl.xml. Reusing it...'

if not os.path.isfile('reg.py') or not os.path.isfile('reg.py'):
	raise Exception('Either reg.py or gl.xml does not exist.')

print 'Began API generation...'
from reg import *

prefixHeaderString = '''#pragma once

#include <cinttypes>
#include <cstddef>

#if !defined(_WIN32) && (defined(__WIN32__) || defined(WIN32) || defined(__MINGW32__))
#	define _WIN32
#endif /* _WIN32 */
#ifndef APIENTRY
#   ifdef _WIN32
#	   define APIENTRY __stdcall
#   else
#	   define APIENTRY
#   endif
#endif /* APIENTRY */

#if defined _WIN32 || defined __CYGWIN__
#	define GLCOREDLL_IMPORT __declspec(dllimport)
#	define GLCOREDLL_EXPORT __declspec(dllexport)
#else
#	if __GNUC__ >= 4
#		define GLCOREDLL_IMPORT __attribute__ ((visibility ("default")))
#		define GLCOREDLL_EXPORT __attribute__ ((visibility ("default")))
#	else
#		define GLCOREDLL_IMPORT
#		define GLCOREDLL_EXPORT
#	endif /* __GNUC__ */
#endif /* _WIN32 */

#ifdef GLCORE_DLL // defined if GlCore is compiled as a DLL
#	ifdef GLCORE_DLL_EXPORTS // defined if we are building the GlCore DLL (instead of using it)
#		define GLCOREAPI GLCOREDLL_EXPORT
#	else
#		define GLCOREAPI GLCOREDLL_IMPORT
#	endif /* GLCORE_DLL_EXPORTS */
#else // GLCORE_DLL is not defined: this means GlCore is a static lib.
#	define GLCOREAPI
#endif /* GLCORE_DLL */
'''

class CppGeneratorOptions(GeneratorOptions):
	"""Represents options during C++ header generation"""
	def __init__(self,
				 filename = 'glcore.h',
				 apiname = 'gl',
				 profile = 'core',
				 versions = '.*',
				 emitversions = '.*',
				 defaultExtensions = 'glcore',
				 addExtensions = None,
				 removeExtensions = None,
				 sortProcedure = regSortFeatures,
				 apicall = 'GLCOREAPI ',
				 apientry = 'APIENTRY '):
		GeneratorOptions.__init__(self, filename, apiname, profile,
			versions, emitversions, defaultExtensions,
			addExtensions, removeExtensions, sortProcedure)

		self.apicall		 = apicall
		self.apientry		= apientry

from pprint import pprint

class CppOutputGenerator(OutputGenerator):
	"""Generate specified API interfaces as a C++ header"""
	def __init__(self,
				 errFile = None, #sys.stderr,
				 warnFile = None, #sys.stderr,
				 diagFile = None): #sys.stdout):
		OutputGenerator.__init__(self, errFile, warnFile, diagFile)
		#internal bookkeeping
		self.currentFeature = None
		self.currentNamespace = ''
		self.rootNamespace = ''
		self.dependentVersion = None #highest version available
		self.enumTypes = { 'i' : '', 'u': '', 'ull' : '' }
		self.groupDict = None
		self.prototypes = ''
		self.types = ''
		self.enums = ''

	def resetState(self):
		self.enums = ''
		self.types = ''
		self.prototypes = ''

	def isEmpty(self):
		return self.enums == '' and self.types == '' and self.prototypes == ''

	def makeGroupDictIfNotExist(self, elem):
		if self.groupDict == None:
			self.groupDict = {}
			# hack: to find all groups
			for group in elem.getparent().findall('groups/group'):
				group_name = group.get('name')
				for enum in group.findall('enum'):
					enum_name = enum.get('name')
					if enum_name not in self.groupDict:
						self.groupDict[enum_name] = []
					self.groupDict[enum_name].append(group_name)

	def newline(self):
		write('', file=self.outFile)

	def writeline(self, str):
		write(str, file=self.outFile)

	def beginFile(self, genOpts):
		OutputGenerator.beginFile(self, genOpts)
		self.rootNamespace = genOpts.apiname + genOpts.profile
		self.writeline(prefixHeaderString)
		self.writeline('namespace ' + self.rootNamespace)
		self.writeline('{')
		self.newline();

	def endFile(self):
		self.writeline('} //!namespace ' + self.rootNamespace)
		OutputGenerator.endFile(self)

	def beginFeature(self, interface, emit):
		self.makeGroupDictIfNotExist(interface)
		OutputGenerator.beginFeature(self, interface, emit)
		self.currentFeature = FeatureInfo(interface)
		self.resetState()

	def endFeature(self):
		if not self.isEmpty():
			self.genNamespaceBegin()
			self.genTypes()
			self.genEnums()
			self.genCmds()
			self.genNamespaceEnd()
		OutputGenerator.endFeature(self)

	def isFeatureApi(self):
		return self.currentFeature.category == 'VERSION'

	def genNamespaceBegin(self):
		if self.isFeatureApi():
			self.writeline('namespace api {')
			self.genApiNamespaceBegin()
			self.genDependentNamespace()
		else:
			self.writeline('namespace ' + self.currentFeature.category.lower() + ' {')
			self.genExtNamespaceBegin()
			self.genDependentNamespace()

	def genDependentNamespace(self):
		if self.dependentVersion != None:
			self.writeline('using namespace api::v' + self.dependentVersion + ';')
			self.newline()

	def genNamespaceEnd(self):
		if self.isFeatureApi():
			self.genApiNamespaceEnd()
			self.writeline('} //!namespace api')
			self.dependentVersion = self.currentNamespace
		else:
			self.genExtNamespaceEnd()
			self.writeline('} //!namespace ' + self.currentFeature.category.lower())
		self.newline()

	def genApiNamespaceBegin(self):
		self.currentNamespace = self.currentFeature.number.replace('.', '')
		self.writeline('namespace v' + self.currentNamespace + ' {')
		self.newline()

	def genApiNamespaceEnd(self):
		self.writeline('} //!namespace v' + self.currentNamespace)

	def genExtNamespaceBegin(self):
		self.currentNamespace = self.currentFeature.name.replace('GL_' + self.currentFeature.category + '_', '').lower()
		self.writeline('namespace ' + self.currentNamespace + ' {')
		self.newline()
		self.writeline(self.makeExtendionDocumentation(self.currentFeature.category, self.currentNamespace))
		self.newline()

	def genExtNamespaceEnd(self):
		self.writeline('} //!namespace ' + self.currentNamespace)

	def genType(self, typeinfo, name):
		OutputGenerator.genType(self, typeinfo, name)
		if name in ('inttypes', 'stddef'):
			return
		typeElem = typeinfo.elem
		s = noneStr(typeElem.text)
		for elem in typeElem:
			if (elem.tag == 'apientry'):
				s += self.genOpts.apientry + noneStr(elem.tail)
			else:
				s += noneStr(elem.tail)
		if name.startswith('struct'):
			s = name + s;
		else:
			s = s.replace(' ;', ';')
			s = s.replace('  *)', ' *)')
			s = s.replace('typedef', 'using ' + name + ' =')
			s += '\n'
		self.types += s

	def genTypes(self):
		if self.types != '':
			self.writeline(self.types)
		self.types = ''

	def genEnum(self, enuminfo, name):
		OutputGenerator.genEnum(self, enuminfo, name)
		t = enuminfo.elem.get('type')
		if t == '' or t == None:
			t = 'i' #hack: to force type to be int for non suffixed ones
		if t == 'i' or t == 'u' or t == 'ull':
			self.enumTypes[t] += name.ljust(47) + ' = ' + enuminfo.elem.get('value') + ( t if t != 'i' else '' )
			self.enumTypes[t] += ","
			if name in self.groupDict:
				self.enumTypes[t] += ' /* '
				for group in self.groupDict[name]:
					self.enumTypes[t] += (group + ' ')
				self.enumTypes[t] += '*/'
			self.enumTypes[t] += "\n"
		else:
			raise Exception('Unknown enum type found: ' + t)

	def genEnums(self):
		for enumType in self.enumTypes:
			enumBody = self.enumTypes[enumType]
			if enumBody != '':
				enumBody += '}; //!enum'
				cppType = ''
				if enumType == 'u':
					cppType = ' : unsigned'
				elif enumType == 'ull':
					cppType = ' : unsigned long long'
				self.enums += ('enum' + cppType + ' {\n')
				self.enums += enumBody
				self.enums += '\n'
				self.enumTypes[enumType] = ''
		self.writeline(self.enums)

	def genCmd(self, cmdinfo, name):
		OutputGenerator.genCmd(self, cmdinfo, name)
		self.prototypes += self.makeApiDocumentation(name) + '\n'
		self.prototypes += (self.makePrototype(cmdinfo.elem))

	def genCmds(self):
		if self.prototypes != '':
			self.writeline(self.prototypes)
		self.prototypes = ''

	def makePrototype(self, cmd):
		proto = cmd.find('proto')
		params = cmd.findall('param')
		pdecl = self.genOpts.apicall
		pdecl += noneStr(proto.text)
		for elem in proto:
			text = noneStr(elem.text)
			tail = noneStr(elem.tail)
			if (elem.tag == 'name'):
				pdecl += self.genOpts.apientry + text + tail
			else:
				pdecl += text + tail
		n = len(params)
		paramdecl = ' ('
		if n > 0:
			for i in range(0,n):
				paramdecl += ''.join([t for t in params[i].itertext()])
				if (i < n - 1):
					paramdecl += ', '
		else:
			paramdecl += 'void'
		paramdecl += ");\n";
		return pdecl + paramdecl

	def makeExtendionDocumentation(self, cat, name):
		print 'Generating documentation for ' + name + ' in category ' + cat
		url = 'https://www.opengl.org/registry/specs/' + cat + '/' + name + '.txt'
		doc = urllib.urlopen(url).read().decode('utf-8')
		regex = 'Overview\n\n((^ +\S.*\n)+)'
		overview = ''
		result = re.search(regex, doc, re.M)
		if result != None:
			overview = re.search(regex, doc, re.M).group(1)
			overview = re.sub('^ +', '', overview, 0, re.M)
		if overview != '':
			overview = ((overview[:300] + '...\n\n') if len(overview) > 300 else overview + '\n')
		overview += 'Full extension documentation available at:\n'
		overview += url
		return '/*\nExtension overview:\n' + (overview if overview != '' else 'not available.') + '\n*/'

	def makeApiDocumentation(self, name):
		print 'Generating documentation for ' + name
		url = 'https://cvs.khronos.org/svn/repos/ogl/trunk/ecosystem/public/sdk/docs/man4/' + name + '.xml'
		doc = urllib.urlopen(url).read().decode('utf-8')
		try:
			tree = etree.fromstring(doc)
		except:
			return '/* Documentation not available. */'
		if tree != None:
			elem = tree.find('{*}refnamediv/{*}refpurpose')
			if elem != None:
				return '/* ' + elem.text + ' */'
		return '/* Documentation not available. */'

# Load & parse registry
reg = Registry()
reg.loadElementTree(etree.parse('gl.xml'))
reg.setGenerator(CppOutputGenerator())
reg.apiGen(CppGeneratorOptions())

print 'Done.'
<|endoftext|>


<commit_before>import os, urllib

REG_PY_URL = 'https://cvs.khronos.org/svn/repos/ogl/trunk/doc/registry/public/api/reg.py';
GL_XML_URL = 'https://cvs.khronos.org/svn/repos/ogl/trunk/doc/registry/public/api/gl.xml';

if not os.path.isfile('reg.py'):
	print 'reg.py not found. Downloading...'
	urllib.urlretrieve(REG_PY_URL, 'reg.py')
else:
	print 'Found reg.py. Reusing it...'

if not os.path.isfile('gl.xml'):
	print 'gl.xml not found. Downloading...'
	urllib.urlretrieve(GL_XML_URL, 'gl.xml')
else:
	print 'Found gl.xml. Reusing it...'

if not os.path.isfile('reg.py') or not os.path.isfile('reg.py'):
	raise Exception('Either reg.py or gl.xml does not exist.')

print 'Began API generation...'
from reg import *

prefixHeaderString = '''#pragma once

#include <cinttypes>
#include <cstddef>

#if !defined(_WIN32) && (defined(__WIN32__) || defined(WIN32) || defined(__MINGW32__))
#	define _WIN32
#endif /* _WIN32 */
#ifndef APIENTRY
#   ifdef _WIN32
#	   define APIENTRY __stdcall
#   else
#	   define APIENTRY
#   endif
#endif /* APIENTRY */
'''

class CppGeneratorOptions(GeneratorOptions):
	"""Represents options during C++ header generation"""
	def __init__(self,
				 filename = 'glcore.h',
				 apiname = 'gl',
				 profile = 'core',
				 versions = '.*',
				 emitversions = '.*',
				 defaultExtensions = 'glcore',
				 addExtensions = None,
				 removeExtensions = None,
				 sortProcedure = regSortFeatures,
				 apicall = 'GLAPI ',
				 apientry = 'APIENTRY ',
				 apientryp = 'APIENTRYP '):
		GeneratorOptions.__init__(self, filename, apiname, profile,
			versions, emitversions, defaultExtensions,
			addExtensions, removeExtensions, sortProcedure)

		self.apicall		 = apicall
		self.apientry		= apientry
		self.apientryp	   = apientryp

from pprint import pprint

class CppOutputGenerator(OutputGenerator):
	"""Generate specified API interfaces as a C++ header"""
	def __init__(self,
				 errFile = None, #sys.stderr,
				 warnFile = None, #sys.stderr,
				 diagFile = None): #sys.stdout):
		OutputGenerator.__init__(self, errFile, warnFile, diagFile)
		#internal bookkeeping
		self.currentFeature = None
		self.currentNamespace = ''
		self.rootNamespace = ''
		self.dependentVersion = None #highest version available
		self.enumTypes = { 'i' : '', 'u': '', 'ull' : '' }
		self.groupDict = None

	def makeGroupDictIfNotExist(self, elem):
		if self.groupDict == None:
			self.groupDict = {}
			# hack: to find all groups
			for group in elem.getparent().findall('groups/group'):
				group_name = group.get('name')
				for enum in group.findall('enum'):
					enum_name = enum.get('name')
					if enum_name not in self.groupDict:
						self.groupDict[enum_name] = []
					self.groupDict[enum_name].append(group_name)

	def newline(self):
		write('', file=self.outFile)

	def writeline(self, str):
		write(str, file=self.outFile)

	def beginFile(self, genOpts):
		OutputGenerator.beginFile(self, genOpts)
		self.rootNamespace = genOpts.apiname + genOpts.profile
		self.writeline(prefixHeaderString)
		self.writeline('namespace ' + self.rootNamespace)
		self.writeline('{')
		self.newline();

	def endFile(self):
		self.writeline('} //!namespace ' + self.rootNamespace)
		OutputGenerator.endFile(self)

	def beginFeature(self, interface, emit):
		self.makeGroupDictIfNotExist(interface)
		OutputGenerator.beginFeature(self, interface, emit)
		self.currentFeature = FeatureInfo(interface)
		self.genNamespaceBegin()

	def endFeature(self):
		self.genEnums()
		self.genNamespaceEnd()
		self.currentFeature = None
		OutputGenerator.endFeature(self)

	def isFeatureApi(self):
		return self.currentFeature.category == 'VERSION'

	def genNamespaceBegin(self):
		if self.isFeatureApi():
			self.writeline('namespace api {')
			self.genApiNamespaceBegin()
			self.genDependentNamespace()
		else:
			self.writeline('namespace ' + self.currentFeature.category.lower() + ' {')
			self.genExtNamespaceBegin()
			self.genDependentNamespace()

	def genDependentNamespace(self):
		if self.dependentVersion != None:
			self.writeline('using namespace api::v' + self.dependentVersion + ';')
			self.newline()

	def genNamespaceEnd(self):
		if self.isFeatureApi():
			self.genApiNamespaceEnd()
			self.writeline('} //!namespace api')
			self.dependentVersion = self.currentNamespace
		else:
			self.genExtNamespaceEnd()
			self.writeline('} //!namespace ' + self.currentFeature.category.lower())
		self.newline()

	def genApiNamespaceBegin(self):
		self.currentNamespace = self.currentFeature.number.replace('.', '')
		self.writeline('namespace v' + self.currentNamespace + ' {')
		self.newline()

	def genApiNamespaceEnd(self):
		self.writeline('} //!namespace v' + self.currentNamespace)

	def genExtNamespaceBegin(self):
		self.currentNamespace = self.currentFeature.name.replace('GL_' + self.currentFeature.category + '_', '').lower()
		self.writeline('namespace ' + self.currentNamespace + ' {')
		self.newline()

	def genExtNamespaceEnd(self):
		self.writeline('} //!namespace ' + self.currentNamespace)

	def genType(self, typeinfo, name):
		OutputGenerator.genType(self, typeinfo, name)
		if name in ('inttypes', 'stddef'):
			return
		typeElem = typeinfo.elem
		s = noneStr(typeElem.text)
		for elem in typeElem:
			if (elem.tag == 'apientry'):
				s += self.genOpts.apientry + noneStr(elem.tail)
			else:
				s += noneStr(elem.tail)
		if name.startswith('struct'):
			s = name + s;
		else:
			s = s.replace(' ;', ';')
			s = s.replace('  *)', ' *)')
			s = s.replace('typedef', 'using ' + name + ' =')
		self.writeline(s)

	def genEnum(self, enuminfo, name):
		OutputGenerator.genEnum(self, enuminfo, name)
		t = enuminfo.elem.get('type')
		if t == '' or t == None:
			t = 'i' #hack: to force type to be int for non suffixed ones
		if t == 'i' or t == 'u' or t == 'ull':
			self.enumTypes[t] += name.ljust(47) + ' = ' + enuminfo.elem.get('value') + ( t if t != 'i' else '' )
			self.enumTypes[t] += ","
			if name in self.groupDict:
				self.enumTypes[t] += ' /* '
				for group in self.groupDict[name]:
					self.enumTypes[t] += (group + ' ')
				self.enumTypes[t] += '*/'
			self.enumTypes[t] += "\n"
		else:
			raise Exception('Unknown enum type found: ' + t)

	def genEnums(self):
		for enumType in self.enumTypes:
			enumBody = self.enumTypes[enumType]
			if enumBody != '':
				enumBody += '}; //!enum'
				cppType = ''
				if enumType == 'u':
					cppType = ' : unsigned'
				elif enumType == 'ull':
					cppType = ' : unsigned long long'
				self.writeline('enum' + cppType + ' {')
				self.writeline(enumBody)
				self.newline()
				self.enumTypes[enumType] = ''

	def genCmd(self, cmdinfo, name):
		OutputGenerator.genCmd(self, cmdinfo, name)

# Load & parse registry
reg = Registry()
reg.loadElementTree(etree.parse('gl.xml'))
reg.setGenerator(CppOutputGenerator())
reg.apiGen(CppGeneratorOptions())

print 'Done.'
<commit_msg>Added boilerplate to make function prototypes<commit_after>import os, urllib

REG_PY_URL = 'https://cvs.khronos.org/svn/repos/ogl/trunk/doc/registry/public/api/reg.py';
GL_XML_URL = 'https://cvs.khronos.org/svn/repos/ogl/trunk/doc/registry/public/api/gl.xml';

if not os.path.isfile('reg.py'):
	print 'reg.py not found. Downloading...'
	urllib.urlretrieve(REG_PY_URL, 'reg.py')
else:
	print 'Found reg.py. Reusing it...'

if not os.path.isfile('gl.xml'):
	print 'gl.xml not found. Downloading...'
	urllib.urlretrieve(GL_XML_URL, 'gl.xml')
else:
	print 'Found gl.xml. Reusing it...'

if not os.path.isfile('reg.py') or not os.path.isfile('reg.py'):
	raise Exception('Either reg.py or gl.xml does not exist.')

print 'Began API generation...'
from reg import *

prefixHeaderString = '''#pragma once

#include <cinttypes>
#include <cstddef>

#if !defined(_WIN32) && (defined(__WIN32__) || defined(WIN32) || defined(__MINGW32__))
#	define _WIN32
#endif /* _WIN32 */
#ifndef APIENTRY
#   ifdef _WIN32
#	   define APIENTRY __stdcall
#   else
#	   define APIENTRY
#   endif
#endif /* APIENTRY */

#if defined _WIN32 || defined __CYGWIN__
#	define GLCOREDLL_IMPORT __declspec(dllimport)
#	define GLCOREDLL_EXPORT __declspec(dllexport)
#else
#	if __GNUC__ >= 4
#		define GLCOREDLL_IMPORT __attribute__ ((visibility ("default")))
#		define GLCOREDLL_EXPORT __attribute__ ((visibility ("default")))
#	else
#		define GLCOREDLL_IMPORT
#		define GLCOREDLL_EXPORT
#	endif /* __GNUC__ */
#endif /* _WIN32 */

#ifdef GLCORE_DLL // defined if GlCore is compiled as a DLL
#	ifdef GLCORE_DLL_EXPORTS // defined if we are building the GlCore DLL (instead of using it)
#		define GLCOREAPI GLCOREDLL_EXPORT APIENTRY
#	else
#		define GLCOREAPI GLCOREDLL_IMPORT APIENTRY
#	endif /* GLCORE_DLL_EXPORTS */
#else // GLCORE_DLL is not defined: this means GlCore is a static lib.
#	define GLCOREAPI APIENTRY
#endif /* GLCORE_DLL */
'''

class CppGeneratorOptions(GeneratorOptions):
	"""Represents options during C++ header generation"""
	def __init__(self,
				 filename = 'glcore.h',
				 apiname = 'gl',
				 profile = 'core',
				 versions = '.*',
				 emitversions = '.*',
				 defaultExtensions = 'glcore',
				 addExtensions = None,
				 removeExtensions = None,
				 sortProcedure = regSortFeatures,
				 apicall = 'GLCOREAPI ',
				 apientry = 'APIENTRY ',
				 apientryp = 'APIENTRYP '):
		GeneratorOptions.__init__(self, filename, apiname, profile,
			versions, emitversions, defaultExtensions,
			addExtensions, removeExtensions, sortProcedure)

		self.apicall		 = apicall
		self.apientry		= apientry
		self.apientryp	   = apientryp

from pprint import pprint

class CppOutputGenerator(OutputGenerator):
	"""Generate specified API interfaces as a C++ header"""
	def __init__(self,
				 errFile = None, #sys.stderr,
				 warnFile = None, #sys.stderr,
				 diagFile = None): #sys.stdout):
		OutputGenerator.__init__(self, errFile, warnFile, diagFile)
		#internal bookkeeping
		self.currentFeature = None
		self.currentNamespace = ''
		self.rootNamespace = ''
		self.dependentVersion = None #highest version available
		self.enumTypes = { 'i' : '', 'u': '', 'ull' : '' }
		self.groupDict = None

	def makeGroupDictIfNotExist(self, elem):
		if self.groupDict == None:
			self.groupDict = {}
			# hack: to find all groups
			for group in elem.getparent().findall('groups/group'):
				group_name = group.get('name')
				for enum in group.findall('enum'):
					enum_name = enum.get('name')
					if enum_name not in self.groupDict:
						self.groupDict[enum_name] = []
					self.groupDict[enum_name].append(group_name)

	def newline(self):
		write('', file=self.outFile)

	def writeline(self, str):
		write(str, file=self.outFile)

	def beginFile(self, genOpts):
		OutputGenerator.beginFile(self, genOpts)
		self.rootNamespace = genOpts.apiname + genOpts.profile
		self.writeline(prefixHeaderString)
		self.writeline('namespace ' + self.rootNamespace)
		self.writeline('{')
		self.newline();

	def endFile(self):
		self.writeline('} //!namespace ' + self.rootNamespace)
		OutputGenerator.endFile(self)

	def beginFeature(self, interface, emit):
		self.makeGroupDictIfNotExist(interface)
		OutputGenerator.beginFeature(self, interface, emit)
		self.currentFeature = FeatureInfo(interface)
		self.genNamespaceBegin()

	def endFeature(self):
		self.genEnums()
		self.genNamespaceEnd()
		self.currentFeature = None
		OutputGenerator.endFeature(self)

	def isFeatureApi(self):
		return self.currentFeature.category == 'VERSION'

	def genNamespaceBegin(self):
		if self.isFeatureApi():
			self.writeline('namespace api {')
			self.genApiNamespaceBegin()
			self.genDependentNamespace()
		else:
			self.writeline('namespace ' + self.currentFeature.category.lower() + ' {')
			self.genExtNamespaceBegin()
			self.genDependentNamespace()

	def genDependentNamespace(self):
		if self.dependentVersion != None:
			self.writeline('using namespace api::v' + self.dependentVersion + ';')
			self.newline()

	def genNamespaceEnd(self):
		if self.isFeatureApi():
			self.genApiNamespaceEnd()
			self.writeline('} //!namespace api')
			self.dependentVersion = self.currentNamespace
		else:
			self.genExtNamespaceEnd()
			self.writeline('} //!namespace ' + self.currentFeature.category.lower())
		self.newline()

	def genApiNamespaceBegin(self):
		self.currentNamespace = self.currentFeature.number.replace('.', '')
		self.writeline('namespace v' + self.currentNamespace + ' {')
		self.newline()

	def genApiNamespaceEnd(self):
		self.writeline('} //!namespace v' + self.currentNamespace)

	def genExtNamespaceBegin(self):
		self.currentNamespace = self.currentFeature.name.replace('GL_' + self.currentFeature.category + '_', '').lower()
		self.writeline('namespace ' + self.currentNamespace + ' {')
		self.newline()

	def genExtNamespaceEnd(self):
		self.writeline('} //!namespace ' + self.currentNamespace)

	def genType(self, typeinfo, name):
		OutputGenerator.genType(self, typeinfo, name)
		if name in ('inttypes', 'stddef'):
			return
		typeElem = typeinfo.elem
		s = noneStr(typeElem.text)
		for elem in typeElem:
			if (elem.tag == 'apientry'):
				s += self.genOpts.apientry + noneStr(elem.tail)
			else:
				s += noneStr(elem.tail)
		if name.startswith('struct'):
			s = name + s;
		else:
			s = s.replace(' ;', ';')
			s = s.replace('  *)', ' *)')
			s = s.replace('typedef', 'using ' + name + ' =')
		self.writeline(s)

	def genEnum(self, enuminfo, name):
		OutputGenerator.genEnum(self, enuminfo, name)
		t = enuminfo.elem.get('type')
		if t == '' or t == None:
			t = 'i' #hack: to force type to be int for non suffixed ones
		if t == 'i' or t == 'u' or t == 'ull':
			self.enumTypes[t] += name.ljust(47) + ' = ' + enuminfo.elem.get('value') + ( t if t != 'i' else '' )
			self.enumTypes[t] += ","
			if name in self.groupDict:
				self.enumTypes[t] += ' /* '
				for group in self.groupDict[name]:
					self.enumTypes[t] += (group + ' ')
				self.enumTypes[t] += '*/'
			self.enumTypes[t] += "\n"
		else:
			raise Exception('Unknown enum type found: ' + t)

	def genEnums(self):
		for enumType in self.enumTypes:
			enumBody = self.enumTypes[enumType]
			if enumBody != '':
				enumBody += '}; //!enum'
				cppType = ''
				if enumType == 'u':
					cppType = ' : unsigned'
				elif enumType == 'ull':
					cppType = ' : unsigned long long'
				self.writeline('enum' + cppType + ' {')
				self.writeline(enumBody)
				self.newline()
				self.enumTypes[enumType] = ''

	def genCmd(self, cmdinfo, name):
		OutputGenerator.genCmd(self, cmdinfo, name)

	def makePrototype(self, cmd):
		proto = cmd.find('proto')
		params = cmd.findall('param')
		pdecl = self.genOpts.apicall
		pdecl += noneStr(proto.text)
		for elem in proto:
			text = noneStr(elem.text)
			tail = noneStr(elem.tail)
			pdecl += text + tail
		n = len(params)
		paramdecl = ' ('
		if n > 0:
			for i in range(0,n):
				paramdecl += ''.join([t for t in params[i].itertext()])
				if (i < n - 1):
					paramdecl += ', '
		else:
			paramdecl += 'void'
		paramdecl += ");\n";
		return pdecl + paramdecl

# Load & parse registry
reg = Registry()
reg.loadElementTree(etree.parse('gl.xml'))
reg.setGenerator(CppOutputGenerator())
reg.apiGen(CppGeneratorOptions())

print 'Done.'
<|endoftext|>


<commit_before>#!/usr/bin/env python
import subprocess, requests, json, tarfile, os, boto3, argparse , logging
import xml.etree.ElementTree as ET

def setup_logger():
    """
    Configure the logger
    :return: logger
    """
    logger = logging.getLogger()
    logging.basicConfig()
    logger.setLevel(logging.INFO)
    logging.getLogger('requests').setLevel(logging.WARNING)
    logging.getLogger('botocore').setLevel(logging.WARNING)
    logging.getLogger('urllib3').setLevel(logging.WARNING)
    return logger

def make_tarfile(output_filename, source_dir):
    with tarfile.open(output_filename, "w:gz") as tar:
        tar.add(source_dir, arcname=os.path.basename(source_dir))

def backup(config, s3_bucket_name=None, prefix=None):
    logger = setup_logger()
    backup_endpoint = "{}/go/api/backups".format(config["host"])
    config_endpoint = "{}/go/api/admin/config.xml".format(config["host"])

    if s3_bucket_name is None:
        s3_bucket_name = config['bucket']
    else :
        logger.info("Changed bucket to %s", s3_bucket_name)
    if prefix is None:
        prefix = config["prefix"]
    else :
        logger.info("Changed prefix to %s", prefix)
    headers = {'Confirm': 'true', 'Accept': 'application/vnd.go.cd.v1+json'}

    r = requests.post(backup_endpoint, auth=(config["username"], config["password"]), headers=headers)
    response_object = json.loads(r.text)

    if not r.status_code == 200:
        print 'Something is wrong'
        print r.text
        exit(2)

    backup_path = response_object['path']  # use this form for immediate fail
    backup_file = "{}/{}.tgz".format(config["backup_path"], os.path.basename(backup_path))
    if not os.path.exists(os.path.dirname(backup_file)):
        os.makedirs(os.path.dirname(backup_file))
    make_tarfile(backup_file, backup_path)
    os.chmod(backup_file, 0600)
    s3 = boto3.resource('s3')
    bucket = s3.Bucket(s3_bucket_name)
    bucket.upload_file(backup_file, '{}/config/{}'.format(prefix, os.path.basename(backup_file)))
    os.remove(backup_file)
    r = requests.get(config_endpoint, auth=(config["username"], config["password"]))
    if not r.status_code == 200:
        print 'Something went wrong when getting the xml'
        print r.text
        exit(2)
    artifacts_dir = None
    for node in ET.ElementTree(ET.fromstring(r.text)).getroot().findall('server'):
        artifacts_dir = node.get('artifactsdir')
    if artifacts_dir is None:
        print "Could not find artifacts dir"
        exit(2)
    command = ['service', 'go-server', 'stop']
    subprocess.call(command, shell=False)

    command = ['aws', 's3', 'sync', '{}/pipelines'.format(artifacts_dir),
               's3://{}/go_server/backups/pipelines'.format(s3_bucket_name)]
    subprocess.call(command, shell=False)

    command = ['service', 'go-server', 'start']
    subprocess.call(command, shell=False)

parser = argparse.ArgumentParser()

parser.add_argument("--config-file", help="Config file location", default="/etc/go-ctl.conf")

subparsers = parser.add_subparsers(help='commands')

set_parser = subparsers.add_parser("set", help="Sets config values")
set_parser.add_argument("--key", help="Key")
set_parser.add_argument("--value", help="Value")
set_parser.set_defaults(which='set')

get_parser = subparsers.add_parser("get", help="Get config value associated with key")
get_parser.set_defaults(which='get')
get_parser.add_argument('key', help='an integer for the accumulator')

backup_parser = subparsers.add_parser("backup", help="Backups the pipelines and config")
backup_parser.add_argument("--bucket", default=None, help="Bucket to use")
backup_parser.add_argument("--prefix", default=None, help="Bucket prefix")
backup_parser.set_defaults(which="backup")

args = parser.parse_args()

print args
config = {
    "backup_path": "/tmp/go-backups",
    "prefix": "go_server/backups",
    "host": "http://localhost:8153",
    "bucket": "rvbgo-s3bucket-10utfkgtekakz"
}
if os.path.isfile(args.config_file):
    with open(args.config_file) as data_file:
        config = json.load(data_file)

if args.which == "set":
    print "We now set {} = {}".format(args.key, args.value)
    config[args.key] = args.value
    with open(args.config_file, 'w') as outfile:
        json.dump(config, outfile)
    os.chmod(args.config_file, 0600)
elif args.which == "get":
    print "Now we get the value of {}".format(args.key)
    print config.get(args.key)
elif args.which == "backup":
    backup(config, s3_bucket_name=args.bucket, prefix=args.prefix)
<commit_msg>Logger<commit_after>#!/usr/bin/env python
import subprocess, requests, json, tarfile, os, boto3, argparse , logging
import xml.etree.ElementTree as ET

def setup_logger():
    """
    Configure the logger
    :return: logger
    """
    logger = logging.getLogger()
    logging.basicConfig()
    logger.setLevel(logging.INFO)
    logging.getLogger('requests').setLevel(logging.WARNING)
    logging.getLogger('botocore').setLevel(logging.WARNING)
    logging.getLogger('urllib3').setLevel(logging.WARNING)
    return logger

def make_tarfile(output_filename, source_dir):
    with tarfile.open(output_filename, "w:gz") as tar:
        tar.add(source_dir, arcname=os.path.basename(source_dir))

def backup(config, s3_bucket_name=None, prefix=None):
    logger = setup_logger()
    backup_endpoint = "{}/go/api/backups".format(config["host"])
    config_endpoint = "{}/go/api/admin/config.xml".format(config["host"])

    if s3_bucket_name is None:
        s3_bucket_name = config['bucket']
    else :
        logger.info("Changed bucket to %s", s3_bucket_name)
    if prefix is None:
        prefix = config["prefix"]
    else :
        logger.info("Changed prefix to %s", prefix)
    headers = {'Confirm': 'true', 'Accept': 'application/vnd.go.cd.v1+json'}

    r = requests.post(backup_endpoint, auth=(config["username"], config["password"]), headers=headers)
    response_object = json.loads(r.text)
    logger.debug("Json response")
    logger.debug(r.text)
    if not r.status_code == 200:
        logger.error("Non 200 response from server %s", r.text)
        exit(1)

    backup_path = response_object['path']
    backup_file = "{}/{}.tgz".format(config["backup_path"], os.path.basename(backup_path))
    if not os.path.exists(os.path.dirname(backup_file)):
        os.makedirs(os.path.dirname(backup_file))
        logger.info("Creating backup dir %s",os.path.dirname(backup_file))
    make_tarfile(backup_file, backup_path)
    logger.info("Created tarball from %s in %s", backup_path, backup_file)
    os.chmod(backup_file, 0600)
    s3 = boto3.resource('s3')
    bucket = s3.Bucket(s3_bucket_name)
    bucket.upload_file(backup_file, '{}/config/{}'.format(prefix, os.path.basename(backup_file)))
    logger.info("Uploaded %s to %s", backup_file, '{}/config/{}'.format(prefix, os.path.basename(backup_file)))
    os.remove(backup_file)
    r = requests.get(config_endpoint, auth=(config["username"], config["password"]))
    if not r.status_code == 200:
        logger.error("Non 200 response from server %s", r.text)
        exit(1)
    artifacts_dir = None
    for node in ET.ElementTree(ET.fromstring(r.text)).getroot().findall('server'):
        artifacts_dir = node.get('artifactsdir')
    if artifacts_dir is None:
        logger.error("Could not find artifacts dir %s", r.text)
        exit(1)
    command = ['service', 'go-server', 'stop']
    subprocess.call(command, shell=False)
    logger.info("Starting s3 sync")
    command = ['aws', 's3', 'sync', '{}/pipelines'.format(artifacts_dir),
               's3://{}/go_server/backups/pipelines'.format(s3_bucket_name)]
    subprocess.call(command, shell=False)
    logger.info("Done syncing %s to %s",'{}/pipelines'.format(artifacts_dir), 's3://{}/go_server/backups/pipelines'.format(s3_bucket_name))
    command = ['service', 'go-server', 'start']
    subprocess.call(command, shell=False)

parser = argparse.ArgumentParser()

parser.add_argument("--config-file", help="Config file location", default="/etc/go-ctl.conf")

subparsers = parser.add_subparsers(help='commands')

set_parser = subparsers.add_parser("set", help="Sets config values")
set_parser.add_argument("--key", help="Key")
set_parser.add_argument("--value", help="Value")
set_parser.set_defaults(which='set')

get_parser = subparsers.add_parser("get", help="Get config value associated with key")
get_parser.set_defaults(which='get')
get_parser.add_argument('key', help='an integer for the accumulator')

backup_parser = subparsers.add_parser("backup", help="Backups the pipelines and config")
backup_parser.add_argument("--bucket", default=None, help="Bucket to use")
backup_parser.add_argument("--prefix", default=None, help="Bucket prefix")
backup_parser.set_defaults(which="backup")

args = parser.parse_args()

print args
config = {
    "backup_path": "/tmp/go-backups",
    "prefix": "go_server/backups",
    "host": "http://localhost:8153",
    "bucket": "rvbgo-s3bucket-10utfkgtekakz"
}
if os.path.isfile(args.config_file):
    with open(args.config_file) as data_file:
        config = json.load(data_file)

if args.which == "set":
    print "We now set {} = {}".format(args.key, args.value)
    config[args.key] = args.value
    with open(args.config_file, 'w') as outfile:
        json.dump(config, outfile)
    os.chmod(args.config_file, 0600)
elif args.which == "get":
    print "Now we get the value of {}".format(args.key)
    print config.get(args.key)
elif args.which == "backup":
    backup(config, s3_bucket_name=args.bucket, prefix=args.prefix)
<|endoftext|>


<commit_before>#!/usr/bin/python
import os
import sys
from optparse import OptionParser
import subprocess

parser = OptionParser()
parser.add_option("-l", "--lab", dest="labname", help="assignment to grade", metavar="LAB")

#TODO Use these options instead of the hard code booleans (debug and printSrc)
#parser.add_option("-d", "--debug", dest="debug", help="Enable debug mode: prints report to the command line", metavar="DEBUG")
#parser.add_option("-is", "--includesource", dest="incSrc", help="Append the assignemnts source code at the end of the report", metavar="INCSRC")

(options, args) = parser.parse_args()

#TODO Use options for the next two settings.
# Enabling debug prints the report to the command line
debug = False

# Enabling printSrc appends the java source file
printSrc = True

# Class to represent the student's report file.
# methods: flag(), add(message), submit()
class Report:
	filename = "default.txt"
	flagged = False
	content = []

	# Set the assignment as flagged. The flag being on affects the report name
	def flag(self):
		self.flagged = True

	# construct with the report filename
	def __init__(self, filename):
		self.filename = filename
		self.content = []

	# add a line to the report
	def add(self, message):
		if debug:
			print(message)
		self.content.append(message + "\n")

	# submit the report and save the file
	def submit(self):
		if self.flagged:
			self.filename = "FLAGGED_" + self.filename
		currdir = os.getcwd()
		reportsdir = os.path.join(currdir,"reports")

		# If "reports" directory does not exist, create it
		if not os.path.exits(reportsdir):
			os.mkdir(reportsdir)

		with file(os.path.join(currdir,"reports", self.filename), "w") as f :
			f.writelines(self.content)

#TODO logic to parse code could could be moved to a CodeParser class

currdir = os.getcwd()

#TODO Create dirs and extract assignments programatically.

# directory where the extracted assignments reside
submissionsdir = os.path.join(currdir,"submissions")
if not os.path.exists(submissionsdir):
	sys.exit("could not find \"submissions\" directory")

# directory where the assignments will be compiled, run and unit tested 1 by 1.
javadir = os.path.join(currdir,"java")
if not os.path.exists(javadir):
	sys.exit("could not find \"java\" directory")

submissions = os.listdir(submissionsdir)
for submission in submissions:
	fname, fext = os.path.splitext(submission)
	studentName, javaName, x = fname.split("_")
	print(studentName)
	report = Report(studentName.lower().replace(" ","_") + ".txt")

	if not fext == ".java":
		report.add("ERROR: File extension: {0}, expected: .java".format(fext))
		report.flag()
		report.submit()
		continue
	lines = []
	comments = []
	mainClass = ""
	functions = []
	issues = []
	with file(os.path.join(submissionsdir, submission) ,"r") as f:
		lines = f.readlines()

	#
	# find the main class name
	#
	#TODO A better way to parse the file is to create a class tree but that seems overkill for this project
	for line in lines:
		line = line.strip()
		code = line.split("//")[0]
		if mainClass == "" and "class" in code:
			prefix = code.split("(")[0]
			words = prefix.split()
			idx = words.index("class")
			if idx >= 0:
				mainClass = words[idx+1]

	# Exit if the main class could not be determined
	if mainClass == "":
		report.add("ERROR: Could not determine main class name")
		report.flag()
		report.submit()
		continue

	# re-write the source file in a more script friendly way.
	newlines = []
	for line in lines:
		# Comment out the package name (everything will be in the default package for now)
		if line.startswith("package"):
			line = "//" + line
		# replace every reference of the main class with the lab name.
		newlines.append(line.replace(mainClass, options.labname))

	# rename the file to match the lab name.
	with file(os.path.join(javadir, options.labname + ".java"), "w") as f:
		f.writelines(newlines)

	#
	# Compile and run
	currdir = os.getcwd()
	os.chdir(javadir)
	# Right now, runner.sh must be modified to compile the lab name.
	# This needs to be done once for the entire assignment
	#TODO do this programatically.
	#TODO Should runner.sh be a python script? Keeping it outsite this script allows to re-compile an individual assignment without running the whole script.
	args = [os.path.join(javadir, "runner.sh"), ">>", "build.log"]
	subprocess.call(args)
	os.chdir(currdir)

	# Report the unit test results. These are stored in result.log in the "java" sub directory
	report.add("------ Functional Results -----")
	runlog = os.path.join(javadir,"result.log")
	if os.path.exists(runlog):
		with file(runlog, "r") as f:
			res = f.readlines()
		for line in res:
			report.add("\t" + line)
	report.add("")

	#
	# crawl for functions
	# Look for "{" in each file and the first line of text that preceeds the line with "{"
	# (if the curly brace is not on its own line, the function prototype is in that line)
	# skip lines that start with words in the "ignoredKeyworkds" list. (not exhaustive)
	ignoredKeywords = ["for", "while", "try", "catch", "if", "then", "while", "do", "class"]

	i = 0;
	while i < len(lines):
		line = lines[i].strip()
		if line.startswith("//") or not "{" in line:
			i = i + 1
			continue

		func = ""
		if line.startswith("{"):
			n = 1
			while i - n >= 0 and len(func) == 0:
				func = lines[i-n].strip()
				n = n + 1
		else:
			func = line.replace("{", "") # remove "{" for readability

		words = func.replace("("," ").split()
		if words[0] not in ignoredKeywords:
			functions.append(func)
		i = i + 1

	#
	# crawl for comments
	#TODO handle empty lines between comments and logic (basically skip them)
	#
	i = 0;
	while i < len(lines):
		line = lines[i].strip()
		# Single comments
		if "//" in line and not line.startswith("//"):
			comment = []
			if i > 0:
				comment.append("    " + lines[i-1])
			comment.append("--->" + lines[i])
			if i + 1 < len(lines):
				comment.append("    " + lines[i+1])
			comments.append(comment)
			i = i + 1
			continue

		# Block comments (//)
		comment = []
		while line.startswith("//"):
			comment.append(line)
			i = i + 1
			if i == len(lines):
				break
			line = lines[i].strip()
		if len(comment) > 0:
			if i + 1 < len(lines):
				comment.append(lines[i+1])
			comments.append(comment)
		#TODO handle /* */ comments
		i = i + 1

	# Issue checker: If source file name does not match the class
	if not mainClass == javaName:
		issues.append("Java file name: \"{0}\" does not match class name : \"{1}\"!".format(javaName, mainClass))

	# Report on the issues
	if len(issues) > 0:
		report.add("------ !! ISSUES !! -----")
		for issue in issues:
			report.add("\t" + issue)
	report.add("------ Main class -----")
	report.add("\tName = {0}".format(mainClass))

	# Report on the functions
	report.add("")
	report.add("------ FUNCTIONS ----- (count={0})".format(len(functions)))
	i = 1
	for function in functions:
		report.add("\t-function {0}:".format(i))
		report.add("\t\t"+function.strip())
		i = i + 1

	# Report on the comments
	report.add("")
	report.add("------ COMMENTS ----- (count={0})".format(len(comments)))
	i = 1
	for comment in comments:
		report.add("\t-Comment {0}:".format(i))
		for line in comment:
			report.add("\t\t"+line.strip())
		i = i + 1

	# If enabled, append the source code to the end of the report.
	if printSrc:
		report.add("")
		report.add("")
		report.add("-"*20)
		for line in lines:
			report.add(line)
	report.submit()
<commit_msg>Added command line options for verbose, appending source to report and inputing the path to a zip file to programatically unzip the submissions.<commit_after>#!/usr/bin/python
import os
import sys
from optparse import OptionParser
import subprocess

parser = OptionParser()
parser.add_option("-l", "--lab", dest="labname", help="assignment to grade", metavar="LAB")
parser.add_option("-z", "--zipfile", dest="zipfile", help="zip file to extract", metavar="ZIP")
parser.add_option("-v", "--verbose", dest="verbose", action="store_true", default=False, help="Enable vebose mode: prints report to the command line", metavar="DEBUG")
parser.add_option("-a", "--addsource", dest="addsource", action="store_true", default=False, help="Append the assignemnt's source code at the end of the report", metavar="ADDSRC")

(options, args) = parser.parse_args()

if options.labname is None:
	parser.error("labname not given")

currdir = os.getcwd()
submissionsdir = os.path.join(currdir,"submissions")

if not options.zipfile is None:
	zippath = options.zipfile
	if not os.path.isabs(options.zipfile):
		zippath = os.path.join(currdir, options.zipfile)
	if not os.path.exists(zippath):
		 parser.error("{0} does not exist".format(zippath))
	if not os.path.splitext(zippath)[1] == ".zip":
		parser.error("{0} not a zip file".format(zippath))
	if not os.path.exists(submissionsdir):
		os.mkdir(submissionsdir)
	args = ["unzip","-d", submissionsdir, zippath]
	subprocess.call(args)

# Class to represent the student's report file.
# methods: flag(), add(message), submit()
class Report:
	filename = "default.txt"
	flagged = False
	content = []

	# Set the assignment as flagged. The flag being on affects the report name
	def flag(self):
		self.flagged = True

	# construct with the report filename
	def __init__(self, filename):
		self.filename = filename
		self.content = []

	# add a line to the report
	def add(self, message):
		if options.verbose:
			print(message)
		self.content.append(message + "\n")

	# submit the report and save the file
	def submit(self):
		if self.flagged:
			self.filename = "FLAGGED_" + self.filename
		currdir = os.getcwd()
		reportsdir = os.path.join(currdir,"reports")

		# If "reports" directory does not exist, create it
		if not os.path.exits(reportsdir):
			os.mkdir(reportsdir)

		with file(os.path.join(currdir,"reports", self.filename), "w") as f :
			f.writelines(self.content)

#TODO logic to parse code be moved to a CodeParser class

# directory where the extracted assignments reside
submissionsdir = os.path.join(currdir,"submissions")
if not os.path.exists(submissionsdir):
	sys.exit("could not find \"submissions\" directory")

# directory where the assignments will be compiled, run and unit tested 1 by 1.
javadir = os.path.join(currdir,"java")
if not os.path.exists(javadir):
	sys.exit("could not find \"java\" directory")

submissions = os.listdir(submissionsdir)
for submission in submissions:
	fname, fext = os.path.splitext(submission)
	studentName, javaName, x = fname.split("_")
	print(studentName)
	report = Report(studentName.lower().replace(" ","_") + ".txt")

	if not fext == ".java":
		report.add("ERROR: File extension: {0}, expected: .java".format(fext))
		report.flag()
		report.submit()
		continue
	lines = []
	comments = []
	mainClass = ""
	functions = []
	issues = []
	with file(os.path.join(submissionsdir, submission) ,"r") as f:
		lines = f.readlines()

	#
	# find the main class name
	#
	#TODO A better way to parse the file is to create a class tree but that seems overkill for this project
	for line in lines:
		line = line.strip()
		code = line.split("//")[0]
		if mainClass == "" and "class" in code:
			prefix = code.split("(")[0]
			words = prefix.split()
			idx = words.index("class")
			if idx >= 0:
				mainClass = words[idx+1]

	# Exit if the main class could not be determined
	if mainClass == "":
		report.add("ERROR: Could not determine main class name")
		report.flag()
		report.submit()
		continue

	# re-write the source file in a more script friendly way.
	newlines = []
	for line in lines:
		# Comment out the package name (everything will be in the default package for now)
		if line.startswith("package"):
			line = "//" + line
		# replace every reference of the main class with the lab name.
		newlines.append(line.replace(mainClass, options.labname))

	# rename the file to match the lab name.
	with file(os.path.join(javadir, options.labname + ".java"), "w") as f:
		f.writelines(newlines)

	#
	# Compile and run
	currdir = os.getcwd()
	os.chdir(javadir)
	# Right now, runner.sh must be modified to compile the lab name.
	# This needs to be done once for the entire assignment
	#TODO do this programatically.
	#TODO Should runner.sh be a python script? Keeping it outsite this script allows to re-compile an individual assignment without running the whole script.
	args = [os.path.join(javadir, "runner.sh"), ">>", "build.log"]
	subprocess.call(args)
	os.chdir(currdir)

	# Report the unit test results. These are stored in result.log in the "java" sub directory
	report.add("------ Functional Results -----")
	runlog = os.path.join(javadir,"result.log")
	if os.path.exists(runlog):
		with file(runlog, "r") as f:
			res = f.readlines()
		for line in res:
			report.add("\t" + line)
	report.add("")

	#
	# crawl for functions
	# Look for "{" in each file and the first line of text that preceeds the line with "{"
	# (if the curly brace is not on its own line, the function prototype is in that line)
	# skip lines that start with words in the "ignoredKeyworkds" list. (not exhaustive)
	ignoredKeywords = ["for", "while", "try", "catch", "if", "then", "while", "do", "class"]

	i = 0;
	while i < len(lines):
		line = lines[i].strip()
		if line.startswith("//") or not "{" in line:
			i = i + 1
			continue

		func = ""
		if line.startswith("{"):
			n = 1
			while i - n >= 0 and len(func) == 0:
				func = lines[i-n].strip()
				n = n + 1
		else:
			func = line.replace("{", "") # remove "{" for readability

		words = func.replace("("," ").split()
		if words[0] not in ignoredKeywords:
			functions.append(func)
		i = i + 1

	#
	# crawl for comments
	#TODO handle empty lines between comments and logic (basically skip them)
	#
	i = 0;
	while i < len(lines):
		line = lines[i].strip()
		# Single comments
		if "//" in line and not line.startswith("//"):
			comment = []
			if i > 0:
				comment.append("    " + lines[i-1])
			comment.append("--->" + lines[i])
			if i + 1 < len(lines):
				comment.append("    " + lines[i+1])
			comments.append(comment)
			i = i + 1
			continue

		# Block comments (//)
		comment = []
		while line.startswith("//"):
			comment.append(line)
			i = i + 1
			if i == len(lines):
				break
			line = lines[i].strip()
		if len(comment) > 0:
			if i + 1 < len(lines):
				comment.append(lines[i+1])
			comments.append(comment)
		#TODO handle /* */ comments
		i = i + 1

	# Issue checker: If source file name does not match the class
	if not mainClass == javaName:
		issues.append("Java file name: \"{0}\" does not match class name : \"{1}\"!".format(javaName, mainClass))

	# Report on the issues
	if len(issues) > 0:
		report.add("------ !! ISSUES !! -----")
		for issue in issues:
			report.add("\t" + issue)
	report.add("------ Main class -----")
	report.add("\tName = {0}".format(mainClass))

	# Report on the functions
	report.add("")
	report.add("------ FUNCTIONS ----- (count={0})".format(len(functions)))
	i = 1
	for function in functions:
		report.add("\t-function {0}:".format(i))
		report.add("\t\t"+function.strip())
		i = i + 1

	# Report on the comments
	report.add("")
	report.add("------ COMMENTS ----- (count={0})".format(len(comments)))
	i = 1
	for comment in comments:
		report.add("\t-Comment {0}:".format(i))
		for line in comment:
			report.add("\t\t"+line.strip())
		i = i + 1

	# If enabled, append the source code to the end of the report.
	if options.addsource:
		report.add("")
		report.add("")
		report.add("-"*20)
		for line in lines:
			report.add(line)
	report.submit()
<|endoftext|>


<commit_before>#!/usr/bin/env python3

'''
A basic script that is used to fetch and update defined repositories(YAML format)
into an OpenGrok source location.

The main feature is (compared to running it in crontab) is that it provides an
easy-to-read and easy-to-use YAML configuration file that even non-operation
people or other services can use.

Author: Eric Ripa - eric@ripa.io (2014-12-08)
'''

import argparse
from plumbum import local
from plumbum.cmd import git
from plumbum.cmd import repo
from plumbum.commands.processes import ProcessExecutionError
import subprocess, shlex
import yaml
import os
import shutil

def parse_args():
    parser = argparse.ArgumentParser(description = "groker - {OpenGrok helper")
    parser.add_argument('-c', '--config-file',
                        help='Use a non-default configuration (default is repos.yml)',
                        default='repos.yml')
    parser.add_argument('-o', '--output-dir',
                        help='Where to put the repositories (usually under OpenGroks src directory, default=/var/opengrok/src)',
                        default='/var/opengrok/src')
    parser.add_argument('-g', '--opengrok',
                        help='Path to the OpenGrok tool, default=/opt/opengrok/bin/OpenGrok)',
                        default='/opt/opengrok/bin/OpenGrok')
    parser.add_argument('-p', '--python2',
                        help='*NOT IMPLEMENTED* Path to python2 (might be needed for Repo YMMV), default=/usr/bin/python2)',
                        default='/usr/bin/python2')
    return parser.parse_args()

def read_config(file_name):
    if not os.path.isfile(file_name):
        print('Could not find configuration file (%s), will copy the template and use that.' % (file_name))
        shutil.copyfile('repos.yml-template', file_name)
    with open(file_name, 'r') as f:
        return yaml.load(f)

def interesting_ref(ref, tags):
    if 'heads' or 'HEAD' in ref:
        return True
    else:
        ref_split = ref.split()
        if ref_split[-2] == 'tags' and ref_split[-1] in tags:
            print('OK!')
            return True
    return False

def target_name(name, target):
    return name + '-' + target

def git_clone(url, tag, target):
    clone_cmd = git['clone', '-b', tag, url, target]
    try:
        res = clone_cmd()
    except ProcessExecutionError as e:
        print('Error cloning %s with tag %s, message was:\n%s' % (url, tag, e.stderr))

def git_update(tag, target):
    update_command = git['pull']
    try:
        with local.cwd(target):
            res = update_command()
    except ProcessExecutionError as e:
        print('Error updating %s with tag %s, message was:\n%s' % (target, tag, e.stderr))

def repo_init(url, tag, target):
    if not os.path.isdir(target):
        os.makedirs(target)
    # use subprocess instead of plumbum as repo requires python2
    init_cmd = shlex.split('/usr/bin/python2 %s init -u %s  -b %s' % (repo.__str__(), url, tag))
    try:
        with local.cwd(target):
            res = subprocess.check_call(init_cmd, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
            repo_sync(target)
    except subprocess.CalledProcessError as e:
        print('Error initializing repo %s at %s with tag %s, message was:\n%s' % (url, target, tag, e.output))
        shutil.rmtree(target, ignore_errors=True)

def repo_sync(target):
    # use subprocess instead of plumbum as repo requires python2
    sync_cmd = shlex.split('/usr/bin/python2 %s sync -j8' % (repo.__str__()))
    try:
        with local.cwd(target):
            res = subprocess.check_call(sync_cmd, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError as e:
        print('Error syncing %s, message was:\n%s' % (target, e.output))

def fetch_repo(name, details, output_dir):
    print('processing %s source at %s' % (details['type'], details['url']))
    for tag in details['tags']:
        target_directory = os.path.join(output_dir, target_name(name, tag))
        if os.path.isdir(target_directory):
            print('  updating tag: %s, target: %s' % (tag, target_directory))
            if details['type'] == 'git':
                git_update(tag, target_directory)
            elif details['type'] == 'repo':
                repo_sync(target_directory)
        else:
            print('  fetching tag: %s, target: %s' % (tag, target_directory))
            if details['type'] == 'git':
                git_clone(details['url'], tag, target_directory)
            elif details['type'] == 'repo':
                repo_init(details['url'], tag, target_directory)

def fetch_repos(output_dir, config):
    for name, details in config.items():
        if details['type'] == 'repo' or details['type'] == 'git':
            fetch_repo(name, details, output_dir)
        else:
            print('WARNING: Selected type %s for %s is not yet implemented' % (details['type'], name))

def reindex_opengrok(opengrok):
    opengrok_cmd = shlex.split('%s index' % opengrok)
    try:
        subprocess.check_call(opengrok_cmd, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError as e:
        print('Could not index, error running OpenGrok index, message was:\n%s' % (e.output))
    except FileNotFoundError:
        print('Could not index, unable to find OpenGrok tool at %s, you can specify a path with -g/--opengroup <path>' % opengrok)

def main():
    args = parse_args()
    config = read_config(args.config_file)
    if not os.path.isdir(args.output_dir):
        print('Output directory did not exist, creating.. (%s)' % args.output_dir)
        os.makedirs(args.output_dir)
    if config:
        fetch_repos(args.output_dir, config)
        reindex_opengrok(args.opengrok)
    else:
        sys.exit('No repos found in %s, exiting...' % (args.config_file))

if __name__ == '__main__':
    main()
<commit_msg>added reindexing message<commit_after>#!/usr/bin/env python3

'''
A basic script that is used to fetch and update defined repositories(YAML format)
into an OpenGrok source location.

The main feature is (compared to running it in crontab) is that it provides an
easy-to-read and easy-to-use YAML configuration file that even non-operation
people or other services can use.

Author: Eric Ripa - eric@ripa.io (2014-12-08)
'''

import argparse
from plumbum import local
from plumbum.cmd import git
from plumbum.cmd import repo
from plumbum.commands.processes import ProcessExecutionError
import subprocess, shlex
import yaml
import os
import shutil

def parse_args():
    parser = argparse.ArgumentParser(description = "groker - {OpenGrok helper")
    parser.add_argument('-c', '--config-file',
                        help='Use a non-default configuration (default is repos.yml)',
                        default='repos.yml')
    parser.add_argument('-o', '--output-dir',
                        help='Where to put the repositories (usually under OpenGroks src directory, default=/var/opengrok/src)',
                        default='/var/opengrok/src')
    parser.add_argument('-g', '--opengrok',
                        help='Path to the OpenGrok tool, default=/opt/opengrok/bin/OpenGrok)',
                        default='/opt/opengrok/bin/OpenGrok')
    parser.add_argument('-p', '--python2',
                        help='*NOT IMPLEMENTED* Path to python2 (might be needed for Repo YMMV), default=/usr/bin/python2)',
                        default='/usr/bin/python2')
    return parser.parse_args()

def read_config(file_name):
    if not os.path.isfile(file_name):
        print('Could not find configuration file (%s), will copy the template and use that.' % (file_name))
        shutil.copyfile('repos.yml-template', file_name)
    with open(file_name, 'r') as f:
        return yaml.load(f)

def interesting_ref(ref, tags):
    if 'heads' or 'HEAD' in ref:
        return True
    else:
        ref_split = ref.split()
        if ref_split[-2] == 'tags' and ref_split[-1] in tags:
            print('OK!')
            return True
    return False

def target_name(name, target):
    return name + '-' + target

def git_clone(url, tag, target):
    clone_cmd = git['clone', '-b', tag, url, target]
    try:
        res = clone_cmd()
    except ProcessExecutionError as e:
        print('Error cloning %s with tag %s, message was:\n%s' % (url, tag, e.stderr))

def git_update(tag, target):
    update_command = git['pull']
    try:
        with local.cwd(target):
            res = update_command()
    except ProcessExecutionError as e:
        print('Error updating %s with tag %s, message was:\n%s' % (target, tag, e.stderr))

def repo_init(url, tag, target):
    if not os.path.isdir(target):
        os.makedirs(target)
    # use subprocess instead of plumbum as repo requires python2
    init_cmd = shlex.split('/usr/bin/python2 %s init -u %s  -b %s' % (repo.__str__(), url, tag))
    try:
        with local.cwd(target):
            res = subprocess.check_call(init_cmd, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
            repo_sync(target)
    except subprocess.CalledProcessError as e:
        print('Error initializing repo %s at %s with tag %s, message was:\n%s' % (url, target, tag, e.output))
        shutil.rmtree(target, ignore_errors=True)

def repo_sync(target):
    # use subprocess instead of plumbum as repo requires python2
    sync_cmd = shlex.split('/usr/bin/python2 %s sync -j8' % (repo.__str__()))
    try:
        with local.cwd(target):
            res = subprocess.check_call(sync_cmd, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError as e:
        print('Error syncing %s, message was:\n%s' % (target, e.output))

def fetch_repo(name, details, output_dir):
    print('processing %s source at %s' % (details['type'], details['url']))
    for tag in details['tags']:
        target_directory = os.path.join(output_dir, target_name(name, tag))
        if os.path.isdir(target_directory):
            print('  updating tag: %s, target: %s' % (tag, target_directory))
            if details['type'] == 'git':
                git_update(tag, target_directory)
            elif details['type'] == 'repo':
                repo_sync(target_directory)
        else:
            print('  fetching tag: %s, target: %s' % (tag, target_directory))
            if details['type'] == 'git':
                git_clone(details['url'], tag, target_directory)
            elif details['type'] == 'repo':
                repo_init(details['url'], tag, target_directory)

def fetch_repos(output_dir, config):
    for name, details in config.items():
        if details['type'] == 'repo' or details['type'] == 'git':
            fetch_repo(name, details, output_dir)
        else:
            print('WARNING: Selected type %s for %s is not yet implemented' % (details['type'], name))

def reindex_opengrok(opengrok):
    opengrok_cmd = shlex.split('%s index' % opengrok)
    print('Starting OpenGrok index operation...')
    try:
        subprocess.check_call(opengrok_cmd, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
        print('Done!')
    except subprocess.CalledProcessError as e:
        print('Could not index, error running OpenGrok index, message was:\n%s' % (e.output))
    except FileNotFoundError:
        print('Could not index, unable to find OpenGrok tool at %s, you can specify a path with -g/--opengroup <path>' % opengrok)

def main():
    args = parse_args()
    config = read_config(args.config_file)
    if not os.path.isdir(args.output_dir):
        print('Output directory did not exist, creating.. (%s)' % args.output_dir)
        os.makedirs(args.output_dir)
    if config:
        fetch_repos(args.output_dir, config)
        reindex_opengrok(args.opengrok)
    else:
        sys.exit('No repos found in %s, exiting...' % (args.config_file))

if __name__ == '__main__':
    main()
<|endoftext|>


<commit_before>"""
f = female
m = male
u = unknown
c = castrated male
"""
import csv
import numpy as np

age_groups = np.array([[5, 10], [11, 20], [21, 30], [31, 100], [101, 1000]])

def get_age_group(age):
    return 1 + (np.searchsorted(age_groups.ravel(), [age])[0] // 2)

def parse_row(row):
    return (row[0], np.int32(row[1]), np.int32(row[2]), row[3], row[4])

def read_group_info():
    group_info = np.empty(83,
                          dtype=[('dataset', 'a6'),
                                 ('number', 'i4'),
                                 ('age', 'i4'),
                                 ('sex', 'a1'),
                                 ('segment', 'a1')])

    with open('data-aorta/smc_orientation_tangential.csv', 'rb') as fd:
        reader = csv.reader(fd)
        reader.next()
        group_info[:] = [parse_row(row) for row in reader]

    return group_info

def map_group_names(group_info):
    gmap = {}
    for row in group_info:
        gmap[row['dataset']] = (row['segment'], get_age_group(row['age']))

    return gmap

def get_datasets_of_group(group_info, group):
    if isinstance(group, int):
        age = group_info['age']
        col = np.array([get_age_group(ii) for ii in age])

    else:
        col = group_info['segment']

    ii = np.where(col == group)[0]
    return group_info['dataset'][ii]
<commit_msg>small tweak in group definition (no influence on actual data)<commit_after>"""
f = female
m = male
u = unknown
c = castrated male
"""
import csv
import numpy as np

age_groups = np.array([[1, 10], [11, 20], [21, 30], [31, 100], [101, 1000]])

def get_age_group(age):
    return 1 + (np.searchsorted(age_groups.ravel(), [age])[0] // 2)

def parse_row(row):
    return (row[0], np.int32(row[1]), np.int32(row[2]), row[3], row[4])

def read_group_info():
    group_info = np.empty(83,
                          dtype=[('dataset', 'a6'),
                                 ('number', 'i4'),
                                 ('age', 'i4'),
                                 ('sex', 'a1'),
                                 ('segment', 'a1')])

    with open('data-aorta/smc_orientation_tangential.csv', 'rb') as fd:
        reader = csv.reader(fd)
        reader.next()
        group_info[:] = [parse_row(row) for row in reader]

    return group_info

def map_group_names(group_info):
    gmap = {}
    for row in group_info:
        gmap[row['dataset']] = (row['segment'], get_age_group(row['age']))

    return gmap

def get_datasets_of_group(group_info, group):
    if isinstance(group, int):
        age = group_info['age']
        col = np.array([get_age_group(ii) for ii in age])

    else:
        col = group_info['segment']

    ii = np.where(col == group)[0]
    return group_info['dataset'][ii]
<|endoftext|>


<commit_before>import os
import gscommon as gs
import margo
import sublime

env = {}

for k, v in gs.setting('env', {}).iteritems():
	os.environ[k] = os.path.expandvars(os.path.expanduser(v))

for i in os.environ.iteritems():
	env[i[0]] = i[1]

sublime.set_timeout(lambda: margo.hello("hello world"), 0)
<commit_msg>- check return of margo.hello<commit_after>import os
import gscommon as gs
import margo
import sublime

env = {}

for k, v in gs.setting('env', {}).iteritems():
	os.environ[k] = os.path.expandvars(os.path.expanduser(v))

for i in os.environ.iteritems():
	env[i[0]] = i[1]

def margo_dep():
	motd = "hello world"
	resp, err = margo.hello(motd)
	m = resp.get('motd')

	if not err and m != motd:
		err = "Invalid response when calling MarGo. Expected `%s` got `%s`" % (motd, m)

	if err:
		gs.notice('GsInit', err)

sublime.set_timeout(margo_dep, 3)
<|endoftext|>