rust-cpython icon indicating copy to clipboard operation
rust-cpython copied to clipboard

Add in subclasses. (Python 3.x)

Open AraHaan opened this issue 9 years ago • 0 comments

I know that subclasses used to be in the bindings but I would like it readded for those who want to rewrite their python code that subclasses another class in the bindings and document it with a subclass example. It would be absolutely cool to be able to take this and turn it to rust code for python 3:

# coding=utf-8
"""
The MIT License (MIT)

Copyright (c) 2016 AraHaan

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
from importlib.machinery import FileFinder
from . import api
import sys


class ExtensionImporter(object):
    """Base Importer Class."""
    def __init__(self, extension_list):
        self.extensions = extension_list
        self.path = None

    def find_loader(self, name, path):
        """Filds some loaders for importing the module."""
        str(name)  # use name even though we are not using it for anything.
        self.path = path
        return self

    def load_module(self, fullname):
        """Loads a module into sys.modules."""
        if fullname in sys.modules:
            return sys.modules[fullname]
        return None


class JsonImporter(ExtensionImporter):
    """JSON File Importer Class."""
    def __init__(self):
        super(JsonImporter, self).__init__(['.json'])

    def load_module(self, fullname):
        """Loads modules found with the extension"""
        premodule = super(JsonImporter, self).load_module(fullname)
        api.import_json(premodule, self.path, fullname)


class PycxImporter(ExtensionImporter):
    """PYCX File Importer Class."""
    def __init__(self):
        super(PycxImporter, self).__init__(['.pycx'])

    def load_module(self, fullname):
        """
        Loads modules found with the pycx excention.

        Info: pycx files are compressed python source code files.

        Order of processing (to import):
            zlib decompression
            base64 decode.
        """
        premodule = super(PycxImporter, self).load_module(fullname)
        api.import_pycx(premodule, self.path, fullname)


extension_importers = [JsonImporter(), PycxImporter()]
hook_list = []
for importer in extension_importers:
    hook_list.append((importer.find_loader, importer.extensions))

sys.path_hooks.insert(0, FileFinder.path_hook(*hook_list))
sys.path_importer_cache.clear()

(And yes I add in doc strings even to the top of the module and the file uses Unix line encodings).

This also gives me the perfect opportunity to also learn rust at the same time.

For now I have to make a backend file with the actual Classes and then figure out how to do this in rust:

# coding=utf-8
"""
The MIT License (MIT)

Copyright (c) 2016 AraHaan

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
from importlib.machinery import FileFinder
from ._pycx_backend import *
import sys

extension_importers = [JsonImporter(), PycxImporter()]
hook_list = []
for importer in extension_importers:
    hook_list.append((importer.find_loader, importer.extensions))

sys.path_hooks.insert(0, FileFinder.path_hook(*hook_list))
sys.path_importer_cache.clear()

It would be nice if I could actually have the Classes implemented in rust with the subclasses and stuff instead of making a backend for it.

AraHaan avatar Dec 25 '16 22:12 AraHaan