Skip to content

Commit

Permalink
Merge branch 'main' of github.com:odoonix/utils
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafabarmshory committed Jan 29, 2025
2 parents ee5fe87 + 687fb5f commit b0437e9
Show file tree
Hide file tree
Showing 16 changed files with 270 additions and 84 deletions.
36 changes: 30 additions & 6 deletions src/otoolbox/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import sys
import argparse
import importlib


from otoolbox.args import administrator
from otoolbox.args import workspace
from otoolbox.args import developer
from otoolbox.args import maintainer
from otoolbox import env
from otoolbox import workspace


if sys.version_info[:2] >= (3, 8):
Expand All @@ -16,7 +18,7 @@

try:
# Change here if project is renamed and does not equal the package name
dist_name = "utils"
dist_name = "otoolbox"
__version__ = version(dist_name)
except PackageNotFoundError: # pragma: no cover
__version__ = "unknown"
Expand Down Expand Up @@ -71,14 +73,36 @@ def init_cli():
return arg_parser, arg_parser.add_subparsers()



def load_resources(*args):
def call_init(package_name):
# Import the package dynamically
package = importlib.import_module(package_name)

# Check if the package has an __init__ method and call it if it exists
if hasattr(package, 'init'):
package.init()

# Example usage
for path in args:
call_init(path)


if __name__ == '__main__':
# Init resources
load_resources(
'otoolbox.help',
'otoolbox.workspace'
)

# Init arguments
parser, parent_parser = init_cli()
administrator_parser = administrator.init_cli(parent_parser)
developer_parser = developer.init_cli(parent_parser)
maintainer_parser = maintainer.init_cli(parent_parser)
workspace.init_cli(parent_parser)
developer.init_cli(parent_parser)
maintainer.init_cli(parent_parser)

args = parser.parse_args()
env.context.update(args.__dict__)
if not env.context.get('silent', 0):
print(env.resource_string("data/banner.txt"))
# args.func()
args.func()
2 changes: 0 additions & 2 deletions src/otoolbox/args/administrator.py

This file was deleted.

2 changes: 1 addition & 1 deletion src/otoolbox/args/developer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
ensuring a seamless development experience in Odoo projects.
"""
from otoolbox.args import common
# from otoolbox.utils import develop
from otoolbox.repositories import develop


def init_cli(parent_parser):
Expand Down
2 changes: 1 addition & 1 deletion src/otoolbox/args/maintainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"""

from otoolbox.args import common
# from otoolbox.utils import admin
from otoolbox.repositories import admin


def init_cli(parent_parser):
Expand Down
40 changes: 40 additions & 0 deletions src/otoolbox/args/workspace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Supports basic operation related to workspase"""

from otoolbox import env


def _init_resources(**kargs):
resources = env.context.get('resources')
resources.build()

def _delete_resources(**kargs):
resources = env.context.get('resources')
resources.destroy()

def init_cli(parent_parser):
"""Init CLI to support maintainer tools
"""
init_parseer = parent_parser.add_parser(
'init',
description="""
Tools and Utilites to help developers and maintainers. It makes simple to
keep dev repositories up to date.
""")
init_parseer.set_defaults(func=_init_resources)

init_parseer.add_argument(
'--odoo',
dest='odoo_version',
action='store',
required=False
)

delete_parseer = parent_parser.add_parser(
'delete',
description="""
Delete resources.
""")
delete_parseer.set_defaults(func=_delete_resources)


return parent_parser
64 changes: 38 additions & 26 deletions src/otoolbox/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,61 @@ def __init__(
description = None,
constructors = None,
destructors = None,
validators = None
validators = None,
priority=10,
):
self.path = path
self.title = title
self.description = description if description else []
self.constructors = constructors if constructors else []
self.destructors = destructors if destructors else []
self.validators = validators if validators else []
self.priority = priority

# internals
self.validation_errors = []
self.is_valied = False

def build(self, **kargs):
for constructor in self.constructors:
constructor(context=self, **kargs)

class WorkspaceResourceGroup():
def __init__(self):
self.resources = []

def append(self, resource:WorkspaceResource):
self.resources.append(resource)
###################################################################
# constructors
###################################################################
def makedir(context):
pass

def destroy(self, **kargs):
for destructor in self.destructors:
destructor(context=self, **kargs)

def verify(self, **kargs):
for validator in self.validators:
validator(context=self, **kargs)

###################################################################
# validators
###################################################################
def is_readable(context):
pass
class WorkspaceResourceGroup(WorkspaceResource):
def __init__(self, **kargs):
super().__init__(**kargs)
self.resources = kargs.get('resources', [])

def is_dir(context):
pass

def is_file(context:WorkspaceResource):
pass
def append(self, resource:WorkspaceResource):
self.resources.append(resource)
self.resources = sorted(self.resources, key=lambda x: x.priority, reverse=True)

def build(self, **kargs):
for resource in self.resources:
resource.build(**kargs)
super().build(**kargs)

def destroy(self, **kargs):
for resource in self.resources:
resource.destroy(**kargs)
super().destroy(**kargs)

###################################################################
# destructors
###################################################################
def verify(self, **kargs):
for resource in self.resources:
resource.verify(**kargs)
super().verify(**kargs)

def get(self, path, default=False, **kargs):
for resource in self.resources:
if resource.path == path:
return resource
return default


28 changes: 14 additions & 14 deletions src/otoolbox/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
import pkg_resources
import os
import sys
from otoolbox import base
from otoolbox.base import (
WorkspaceResource,
WorkspaceResourceGroup
)

VERSION = "0.0.0"
context = {
'resources': {}
'resources': WorkspaceResourceGroup(
path= 'virtual://',
title="Root Resource"
)
}


Expand Down Expand Up @@ -42,19 +48,13 @@ def get_workspace_path(path):
#################################################################################
# Resource
#################################################################################
def add_resource(resource:base.WorkspaceResource):
group = context['resources'].get(resource.path, None)
def add_resource(**kargs):
resource = WorkspaceResource(**kargs)
path = kargs.get('path')
group = context['resources'].get(path)
if not group:
group = base.WorkspaceResourceGroup()
group = WorkspaceResourceGroup(path=path)
group.append(resource)
context['resources'].append(group)
return sys.modules[__name__]

def constructor_copy_resource(path):
"""Create a constructor to copy resource with path"""
def copy_resource(resource:base.WorkspaceResource):
resource_stream = resource_stream(path)
# Open the output file in write-binary mode
with open(resource.path, 'wb') as out_file:
# Read from the resource stream and write to the output file
out_file.write(resource_stream.read())
return copy_resource
31 changes: 0 additions & 31 deletions src/otoolbox/help.py

This file was deleted.

36 changes: 36 additions & 0 deletions src/otoolbox/help/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Adds helps and documents
Resources:
- README.md
"""
from otoolbox import env
from otoolbox import utils








###################################################################
# init
###################################################################
(env
.add_resource(
path="README.md",
title="Workspace README",
description="A readme that shows parts of the workspace",
constructors=[
utils.constructor_copy_resource("data/WORKSPACE_README.md")
],
destructors=[
utils.delete_file
],
validators=[
utils.is_file,
utils.is_readable
]
)
)
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import logging

from otoolbox import env
from otoolbox.utils import linux
from otoolbox.utils import repo
from otoolbox.repositories import linux
from otoolbox.repositories import repo

logger = logging.getLogger(__name__)

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json

from otoolbox import env
from otoolbox.utils import linux
from otoolbox.repositories import linux


class ModuleList(list):
Expand Down
Loading

0 comments on commit b0437e9

Please sign in to comment.