コード例 #1
0
import texscript

ts = texscript.session()

did_pass_test = True

def dig_for(key, obj):
	if type(obj) != type(dict()): return None

	result = []
	for k in obj:
		if k == key:
			result += obj[key]
		else:
			temp = dig_for(key, obj[k])
			if temp:
				result += temp

	return result

for test in dig_for('example', ts._):
	try:
		ts.compute(test)
		ts.debug = False
	except (AttributeError, ValueError, SyntaxError, NameError, IndexError, TypeError) as err:
		print('Test Failed: ', test, err, '\n')
		did_pass_test = False

if did_pass_test:
	print('All tests were passed successfully')
コード例 #2
0
def app(environ, start_response):
    length = int(environ.get('CONTENT_LENGTH', '0'))
    math = environ['wsgi.input'].read(length).decode('utf-8')
    path = environ['PATH_INFO']
    content_type = mimetypes.guess_type(path)[0] or 'text/html'

    session = environ['beaker.session']

    if environ['REQUEST_METHOD'] == 'GET':
        if path == '/' or path == '/index.html':
            status = '200 OK'
            output = open('www/index.html').read()
        else:
            try:
                status = '200 OK'
                if content_type[:5] == 'image':
                    start_response(status, [('Content-Type', 'image/jpeg')])
                    return environ['wsgi.file_wrapper'](open(
                        'www' + path, 'rb'), 32768)
                else:
                    output = open('www' + path).read()
            except FileNotFoundError:
                status = '404 Not Found'
                output = status

    elif environ['REQUEST_METHOD'] == 'POST':
        if path == '/compute':
            session[
                'variables'] = {} if 'variables' not in session else session[
                    'variables']
            session[
                'functions'] = {} if 'functions' not in session else session[
                    'functions']
            session['history'] = [] if 'history' not in session else session[
                'history']

            tex = texscript.session(session['variables'], session['functions'],
                                    session['history'])

            if 'HTTP_X_NO_HISTORY' not in environ:
                tex.history.append(math)

            try:
                status = '200 OK'
                val = tex.compute(math)

                if type(val) == type(np.array([])):
                    val = val.tolist()

                try:
                    output = json.dumps(val)
                except TypeError:
                    if type(val) == type(np.array([])):
                        output = str(val.tolist())
                    else:
                        output = str(val)

                session['variables'] = tex._['variables']
                session['functions'] = [
                    tex._['functions'][fn]['math'] for fn in tex._['functions']
                ]
                session['history'] = tex.history
            except (ValueError, SyntaxError, IndexError) as err:
                status = '400 Bad Request'
                output = 'Invalid Tex'
    else:
        status = '500 Internal Server Error'
        output = status

    output = output.encode('utf-8')

    headers = [('Content-type', content_type),
               ('Access-control-allow-origin', '*'),
               ('Content-Length', str(len(output)))]

    session.save()

    start_response(status, headers)
    return [output]
コード例 #3
0
ファイル: test.py プロジェクト: calhoun137/TexCalculator
import texscript

ts = texscript.session()

did_pass_test = True


def dig_for(key, obj):
    if type(obj) != type(dict()):
        return None

    result = []
    for k in obj:
        if k == key:
            result += obj[key]
        else:
            temp = dig_for(key, obj[k])
            if temp:
                result += temp

    return result


for test in dig_for("example", ts._):
    try:
        ts.compute(test)
        ts.debug = False
    except (AttributeError, ValueError, SyntaxError, NameError, IndexError, TypeError) as err:
        print("Test Failed: ", test, err, "\n")
        did_pass_test = False
コード例 #4
0
ファイル: server.py プロジェクト: calhoun137/TexCalculator
def app(environ, start_response):
	length = int(environ.get('CONTENT_LENGTH', '0'))
	math = environ['wsgi.input'].read(length).decode('utf-8')
	path = environ['PATH_INFO']
	content_type = mimetypes.guess_type(path)[0] or 'text/html'

	session = environ['beaker.session']

	if environ['REQUEST_METHOD'] == 'GET':
		if path == '/' or path == '/index.html':
			status = '200 OK'
			output = open('www/index.html').read()
		else:
			try:
				status = '200 OK'
				if content_type[:5] == 'image':
					start_response(status, [('Content-Type', 'image/jpeg')])
					return environ['wsgi.file_wrapper'](open('www' + path, 'rb'), 32768)
				else:
					output = open('www' + path).read()
			except FileNotFoundError:
				status = '404 Not Found'
				output = status

	elif environ['REQUEST_METHOD'] == 'POST':			
		if path == '/compute':
			session['variables'] = {} if 'variables' not in session else session['variables']
			session['functions'] = {} if 'functions' not in session else session['functions']
			session['history'] = [] if 'history' not in session else session['history']
			
			tex = texscript.session(session['variables'], session['functions'], session['history'])

			if 'HTTP_X_NO_HISTORY' not in environ:
				tex.history.append(math)
			
			try:
				status = '200 OK'
				val = tex.compute(math)
				
				if type(val) == type(np.array([])):
					val = val.tolist()
				
				try:
					output = json.dumps(val)
				except TypeError:
					if type(val) == type(np.array([])):
						output = str(val.tolist())
					else:
						output = str(val)

				session['variables'] = tex._['variables']
				session['functions'] = [tex._['functions'][fn]['math'] for fn in tex._['functions']]
				session['history'] = tex.history
			except (ValueError, SyntaxError, IndexError) as err:
				status = '400 Bad Request'
				output = 'Invalid Tex'	
	else:
		status = '500 Internal Server Error'
		output = status

	output = output.encode('utf-8')
	
	headers = [
		('Content-type', content_type), 
		('Access-control-allow-origin', '*'), 
		('Content-Length', str(len(output)))
	]
	
	session.save()
	
	start_response(status, headers)
	return [output]