This commit is contained in:
Veit Heller
2014-09-17 18:17:26 +02:00
parent aa06c900c0
commit 5092d371c6
2 changed files with 51 additions and 14 deletions

View File

@@ -26,19 +26,19 @@ As in mames' original version, you can create such a __script__ easily:
* __ruby_script__(YourCode) will encode it and add a "require"
* __python_script__(YourCode) will encode it and add an "import"
If you have a file containing _ code, you can import it after importing
_. Cool, huh?
The idea behind it is using Pythons import hooks to achieve something similar
to mames' original script, only that it works the opposite way.
Install
-------
Issue `python setup.py install`(You might have to sudo).
Drawbacks
---------
Why is the code so ugly?
------------------------
I still cope with Pythons' differences to Ruby. That means by now you have
to keep the script as a string and give it to `_`'s `to_be_exec()` function.
Not cool, I know. I am working on it.
In case you are wondering why functions like `to_i()`and `to_s()` exist in
the module. They are already more or less similarly implemented in Python
itself. But I wanted to model the original modules' behaviour as far as possible,
and for me that meant creating methods that behave similar to Rubys.
That is due to me wanting the encoding and decoding be kept as close as possible
to the original, even if the module has a completely different structure.

View File

@@ -1,4 +1,6 @@
import atexit
import os
import sys
import imp
def to_i(num, b):
n = 0
@@ -45,7 +47,6 @@ def __ruby_script__(src):
def __py_script__(src):
return "import _\n\n" + __script__(src)
code, fragment = [], []
def to_be_exec(name):
global code
global fragment
@@ -54,6 +55,8 @@ def to_be_exec(name):
for i in name.split():
fragment.append(str((len(i)-1)))
code = []
fragment = []
def _():
global code
global fragment
@@ -63,4 +66,38 @@ def _():
eval(''.join([chr(to_i(c, 6)) for c in chunks(''.join(code), 3)]))
code, fragment = [], []
atexit.register(_)
def _to_code(filename):
"""Code generator for an _ module."""
doc = open(filename, "rb").read()
for i in doc.split():
to_be_exec(i)
_()
def _install_importer():
sys.meta_path.insert(0, Finder())
class Finder:
"""Custom module finder for _ files."""
@classmethod
def find_module(cls, fullname, path):
for dirname in sys.path:
filename = os.path.join(dirname, fullname)
if os.path.exists(filename):
return UnderScoreLoader(filename)
class UnderScoreLoader:
def __init__(self, filename):
self.filename = filename
def load_module(self, fullname):
if fullname in sys.modules:
mod = sys.modules[fullname]
else:
mod = imp.new_module(fullname)
sys.modules[fullname] = mod
mod.__file__ = self.filename
mod.__loader__ = self
_to_code(self.filename)
return
_install_importer()