Created
January 18, 2012 21:35
-
-
Save honzajavorek/1635851 to your computer and use it in GitHub Desktop.
Texy! in Python by subprocess (draft)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
ini_set('display_errors', TRUE); | |
ini_set('html_errors', FALSE); | |
error_reporting(E_ALL | E_STRICT); | |
register_shutdown_function(function() { | |
$error = error_get_last(); | |
if ($error !== NULL) { | |
if (empty($error['message'])) { | |
file_put_contents('php://stderr', $error['message']); | |
} else { | |
file_put_contents('php://stderr', 'Unknown error.'); | |
} | |
exit(1); | |
} | |
}); | |
set_error_handler(function($errno, $errstr, $errfile, $errline, $errcontext) { | |
file_put_contents('php://stderr', $errstr); | |
exit(1); | |
}, E_ALL); | |
set_exception_handler(function(Exception $e) { | |
file_put_contents('php://stderr', $e->getMessage()); | |
exit(1); | |
}); | |
$input = ''; | |
if ($argc > 1) { | |
include $argv[1]; | |
} | |
require_once __DIR__ . '/Texy/Texy.php'; | |
$texy = new Texy(); | |
echo $texy->process($input); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import subprocess | |
import tempfile | |
def texy(text): | |
script = u'<?php $input = "' . text . '";' | |
with tempfile.NamedTemporaryFile() as f: | |
f.write(script.encode('utf-8')) | |
f.flush() | |
name = f.name if f.name else '' | |
# could be php -r, http://www.php.net/manual/en/features.commandline.options.php | |
process = subprocess.Popen(['php', 'texy.php', name], stdin=subprocess.PIPE, stdout=subprocess.PIPE) | |
stdout, stderr = process.communicate() | |
if process.returncode or stderr: | |
raise TexyError(stderr if stderr else 'Unknown error.') | |
return stdout |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment