rkhamis / reparo

Apache License 2.0
0 stars 0 forks source link

God object #1

Open xmonader opened 5 years ago

xmonader commented 5 years ago

Problem 1: Code Structure

We can use namespaces the way zope does with zope.interface for example. Now we can easily have jumpscale.sal , jumpscale.clients easily separated into there own pip packages like any normal python project. (bye bye installtools!)

declaring namespaces

One of two ways

1- remove __init__.py 2- add namespace declaration in __init__.py __import__('pkg_resources').declare_namespace(__name__)

Example structure

.
├── projectclients
│   └── jumpscale
│       ├── clients
│       │   ├── github.py
│       │   ├── gogs.py
│       │   ├── __init__.py
│       ├── __init__.py

├── projectmain
│   └── jumpscale
│       ├── god.py
│       ├── __init__.py
├── projectsals
│   └── jumpscale
│       ├── __init__.py
│       └── sal
│           ├── fs.py
│           ├── __init__.py
└── projecttools
    └── jumpscale
        ├── __init__.py
        └── tools
            ├── __init__.py
            └── sync.py

Problem 2: implicit imports (autoloading subpackages)

jumpscale by design wants the least amount of imports thats why all are registered under j

e.g for sal/__init__.py

import pkgutil

__all__ = []
for loader, module_name, is_pkg in pkgutil.walk_packages(__path__):
    __all__.append(module_name)
    _module = loader.find_module(module_name).load_module(module_name)
    globals()[module_name] = _module

CHECK: lazyloader/import hooks in stdpython https://docs.python.org/3/library/importlib.html#importlib.util.LazyLoader

Problem 3: Singletons

Solved by design using python modules

Problem 4: jslocation

If we open god.py

import jumpscale.sal
import jumpscale.tools
import jumpscale.clients

j = jumpscale

we have handcrafted imports for sal, tools, clients so their subpackages can be autoloaded, but how should it work with packages like digitalme

How to register digitalme in the god object

Do we generate import jumpscale.digitalme? is there a standard python way to do it? a reliable plugin system?

where would its module be registered?

for instance there might be digitalme.tools should it be under j.tools directly or j.digitalme.tools? I prefer the latter for clarity and conflict resolution too

Example usage with jumpscale

~> export PYTHONPATH="projecttools:projectsals:projectclients:projectmain"
In [1]: import jumpscale.god                                                                                     

In [2]: jumpscale.sal.fs.copyfile('a', 'b')                                                                      
copying file

In [3]: jumpscale.tools.sync.sync()                                                                              
sync tool

In [4]: jumpscale.clients.github.get_githubclient?                                                               
Signature: jumpscale.clients.github.get_githubclient(username, password)
Docstring: <no docstring>
File:      ~/wspace/jumpscale-skeleton/projectclients/jumpscale/clients/github.py
Type:      function

In [5]: jumpscale.clients.github.get_githubclient('a', 'bb')                                                     
getting client with a bb

In [6]: jumpscale.clients.gogs.get_gogs('a', 'bbb') # uses jumpscale sal.fs in its code                          
sync tool
getting gogs client with a bbb

Example usage with j


In [1]: from jumpscale.god import j                                                                              

In [2]: j.sal.fs.removefile('a')                                                                                 
removing file

In [3]: j.clients.gogs.get_gogs('a', 'b')                                                                        
sync tool
getting gogs client with a b
abom commented 5 years ago

Great, i'm still looking into current use cases, but we should consider lazy loading too anyway.

ahussein commented 5 years ago

@xmonader the issue description has some really good ideas. My main concern is that we do not have a clear idea of what is j. IMO, the other ticket https://github.com/rkhamis/reparo/issues/2 should addressed first.

Once we have a definition of what is j and what are the main components that it should have out of the box, then we can move forward to how to implement that using all/some of the ideas presented here.

xmonader commented 5 years ago

https://gitlab.com/xmonader/jumpscale-skeleton

ahussein commented 5 years ago

why is this on gitlab ?

xmonader commented 5 years ago

why is this on gitlab ?

because it's more example on how to use python namespaces anyhow I added it to https://github.com/rkhamis/reparo/tree/master/docs/ideas/namespacesgodobject/jumpscale-skeleton