first time enhancement for the code structure
Showing
24 changed files
with
54 additions
and
152 deletions
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | |||
| 3 | __author__ = 'hubian' | ||
| 4 | |||
| 5 | from flask import Flask | ||
| 6 | from flask_restful import Api | ||
| 7 | |||
| 8 | |||
| 9 | # initialize flask and flask restful | ||
| 10 | app = Flask(__name__) | ||
| 11 | app.config['SECRET_KEY'] = "myNameIsHuBian" | ||
| 12 | app.debug = True | ||
| 13 | |||
| 14 | api = Api(app) | ||
| 15 | |||
| 16 | from utils import init_factory | ||
| 17 | init_factory() | ||
| 18 | |||
| 19 | from views import init_routes | ||
| 20 | init_routes() |
server/api/__init__.py
0 → 100644
server/api/resources/__init__.py
0 → 100644
File mode changed
server/api/resources/user.py
0 → 100644
| 1 | # -*- coding: utf-8 -*- | 1 | # -*- coding: utf-8 -*- |
| 2 | 2 | ||
| 3 | __author__ = 'hubian' | ||
| 4 | 3 | ||
| 5 | from server import api | 4 | from server.app import web_api |
| 6 | from server.database import db_adapter | 5 | from server.database import db_adapter |
| 7 | from server.database.models import Host | 6 | from server.database.models import Host |
| 8 | from flask_restful import Resource | 7 | from flask_restful import Resource |
| ... | @@ -19,6 +18,5 @@ class HostResource(Resource): | ... | @@ -19,6 +18,5 @@ class HostResource(Resource): |
| 19 | 18 | ||
| 20 | 19 | ||
| 21 | def init_routes(): | 20 | def init_routes(): |
| 22 | api.add_resource(TestResource, "/api/test") | 21 | web_api.add_resource(TestResource, "/api/test") |
| 23 | api.add_resource(HostResource, "/api/host") | 22 | web_api.add_resource(HostResource, "/api/host") |
| 24 | |||
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
server/app.py
0 → 100644
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | |||
| 3 | |||
| 4 | from flask import Flask | ||
| 5 | from flask_restful import Api | ||
| 6 | |||
| 7 | |||
| 8 | # initialize flask and flask restful | ||
| 9 | web_app = Flask(__name__) | ||
| 10 | web_app.config['SECRET_KEY'] = "myNameIsHuBian" | ||
| 11 | web_app.debug = True | ||
| 12 | |||
| 13 | |||
| 14 | web_api = Api(web_app) | ||
| 15 | |||
| 16 | |||
| 17 | from server.api.routes import init_routes | ||
| 18 | init_routes() |
| 1 | # -*- coding: utf-8 -*- | 1 | # -*- coding: utf-8 -*- |
| 2 | 2 | ||
| 3 | __author__ = 'hubian' | ||
| 4 | 3 | ||
| 5 | class SQLAlchemyAdapterMetaClass(type): | 4 | class SQLAlchemyAdapterMetaClass(type): |
| 6 | @staticmethod | 5 | @staticmethod |
| ... | @@ -45,7 +44,7 @@ class SQLAlchemyAdapter(DBAdapter): | ... | @@ -45,7 +44,7 @@ class SQLAlchemyAdapter(DBAdapter): |
| 45 | def __init__(self, db_session): | 44 | def __init__(self, db_session): |
| 46 | super(SQLAlchemyAdapter, self).__init__(db_session) | 45 | super(SQLAlchemyAdapter, self).__init__(db_session) |
| 47 | 46 | ||
| 48 | # ------------------------------ methods that no need to wrap --- start ------------------------------ | 47 | # ------------------ ORM basic functions -------------------- # |
| 49 | 48 | ||
| 50 | def commit(self): | 49 | def commit(self): |
| 51 | self.db_session.commit() | 50 | self.db_session.commit() |
| ... | @@ -62,9 +61,8 @@ class SQLAlchemyAdapter(DBAdapter): | ... | @@ -62,9 +61,8 @@ class SQLAlchemyAdapter(DBAdapter): |
| 62 | def session(self): | 61 | def session(self): |
| 63 | return self.db_session | 62 | return self.db_session |
| 64 | 63 | ||
| 65 | # ------------------------------ methods that no need to wrap --- end------------------------------ | 64 | # -------------------- public usage functions ------------------ # |
| 66 | 65 | ||
| 67 | # ------------------------------ auto wrapped 'public' methods --- start ------------------------------ | ||
| 68 | def get_object(self, ObjectClass, id): | 66 | def get_object(self, ObjectClass, id): |
| 69 | """ Retrieve one object specified by the primary key 'pk' """ | 67 | """ Retrieve one object specified by the primary key 'pk' """ |
| 70 | return ObjectClass.query.get(id) | 68 | return ObjectClass.query.get(id) |
| ... | @@ -93,7 +91,6 @@ class SQLAlchemyAdapter(DBAdapter): | ... | @@ -93,7 +91,6 @@ class SQLAlchemyAdapter(DBAdapter): |
| 93 | def find_first_object_by(self, ObjectClass, **kwargs): | 91 | def find_first_object_by(self, ObjectClass, **kwargs): |
| 94 | return ObjectClass.query.filter_by(**kwargs).first() | 92 | return ObjectClass.query.filter_by(**kwargs).first() |
| 95 | 93 | ||
| 96 | |||
| 97 | def add_object(self, inst): | 94 | def add_object(self, inst): |
| 98 | self.db_session.add(inst) | 95 | self.db_session.add(inst) |
| 99 | 96 | ... | ... |
server/log/__init__.py
0 → 100644
File mode changed
File moved
server/services/__init__.py
0 → 100644
File mode changed
server/services/user/__init__.py
0 → 100644
File mode changed
server/services/user/user_service.py
0 → 100644
File mode changed
| 1 | # -*- coding: utf-8 -*- | 1 | # -*- coding: utf-8 -*- |
| 2 | 2 | ||
| 3 | __author__ = 'hubian' | ||
| 4 | 3 | ||
| 5 | from utils import Utils | ||
| 6 | from log import Log | ||
| 7 | from factory import factory | 4 | from factory import factory |
| 5 | |||
| 8 | from server.database import db_session | 6 | from server.database import db_session |
| 9 | from server.database.db_adapters import SQLAlchemyAdapter | 7 | from server.database.db_adapters import SQLAlchemyAdapter |
| 8 | from server.log.log import Log | ||
| 9 | from utils import Utils | ||
| 10 | |||
| 10 | 11 | ||
| 11 | def init_factory(): | 12 | def init_factory(): |
| 12 | factory.provide("util", Utils) | 13 | factory.provide("util", Utils) | ... | ... |
| 1 | # -*- coding: utf-8 -*- | 1 | # -*- coding: utf-8 -*- |
| 2 | 2 | ||
| 3 | __author__ = 'hubian' | ||
| 4 | 3 | ||
| 5 | 4 | class VM(object): | |
| 6 | class VM: | ||
| 7 | OS_TYPE_LINUX = 0 | 5 | OS_TYPE_LINUX = 0 |
| 8 | OS_TYPE_WINDOWS = 1 | 6 | OS_TYPE_WINDOWS = 1 |
| 9 | 7 | ||
| 10 | 8 | ||
| 11 | class NETWORK: | 9 | class NETWORK(object): |
| 12 | IP_TYPE_PUBLIC = 0 | 10 | IP_TYPE_PUBLIC = 0 |
| 13 | IP_TYPE_PRIVATE = 1 | 11 | IP_TYPE_PRIVATE = 1 |
| 14 | 12 | ||
| 15 | 13 | ||
| 16 | class DISK: | 14 | class DISK(object): |
| 17 | TYPE_SYSTEM = 0 | 15 | TYPE_SYSTEM = 0 |
| 18 | TYPE_MOUNTED = 1 | 16 | TYPE_MOUNTED = 1 |
| 19 | 17 | ||
| ... | @@ -21,7 +19,7 @@ class DISK: | ... | @@ -21,7 +19,7 @@ class DISK: |
| 21 | FORMAT_EXT4 = 1 | 19 | FORMAT_EXT4 = 1 |
| 22 | 20 | ||
| 23 | 21 | ||
| 24 | class IMAGE: | 22 | class IMAGE(object): |
| 25 | TYPE_DEFAULT = 0 # Router or Monitor | 23 | TYPE_DEFAULT = 0 # Router or Monitor |
| 26 | TYPE_PROVIDER = 1 # vm images type | 24 | TYPE_PROVIDER = 1 # vm images type |
| 27 | TYPE_CUSTOMIZE = 2 # created by users | 25 | TYPE_CUSTOMIZE = 2 # created by users | ... | ... |
server/utils/factory.py
deleted
100644 → 0
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | |||
| 3 | __author__ = 'hubian' | ||
| 4 | |||
| 5 | |||
| 6 | class SunnycloudFactory: | ||
| 7 | def __init__(self, allow_replace=False): | ||
| 8 | """Create a new factory | ||
| 9 | |||
| 10 | :param allow_replace: whether to replace existing provider with same feature. AssertionException will be raised it's | ||
| 11 | False and more than one providers with same key(feature) are provided | ||
| 12 | """ | ||
| 13 | self.providers = {} | ||
| 14 | self.allow_replace = allow_replace | ||
| 15 | |||
| 16 | def set_allow_replace(self, allow_replace): | ||
| 17 | """Set the value of allow_replace""" | ||
| 18 | self.allow_replace = allow_replace | ||
| 19 | |||
| 20 | def provide(self, feature, provider, *args, **kwargs): | ||
| 21 | """Add a provider to factory | ||
| 22 | |||
| 23 | :type feature: str|unicode | ||
| 24 | :param feature: key to store and get the object into/from the factory | ||
| 25 | |||
| 26 | :type provider: object | callable | ||
| 27 | :param provider: the object to be added. | ||
| 28 | |||
| 29 | :Example: | ||
| 30 | from *** import VMManager | ||
| 31 | factory.provide("vm_manager", VMManager) | ||
| 32 | factory.provide("vm_manager", VMManager, *init_args, **init_kwargs) | ||
| 33 | |||
| 34 | # or: | ||
| 35 | vmm = VMManager | ||
| 36 | factory.provide("user_manager", vmm) | ||
| 37 | |||
| 38 | """ | ||
| 39 | if not self.allow_replace: | ||
| 40 | assert not self.providers.has_key(feature), "Duplicate feature: %r" % feature | ||
| 41 | if callable(provider): | ||
| 42 | def call(): | ||
| 43 | return provider(*args, **kwargs) | ||
| 44 | else: | ||
| 45 | def call(): | ||
| 46 | return provider | ||
| 47 | self.providers[feature] = call | ||
| 48 | |||
| 49 | def __getitem__(self, feature): | ||
| 50 | try: | ||
| 51 | provider = self.providers[feature] | ||
| 52 | except KeyError: | ||
| 53 | raise KeyError, "Unknown feature named %r" % feature | ||
| 54 | return provider() | ||
| 55 | |||
| 56 | |||
| 57 | factory = SunnycloudFactory() | ||
| 58 | |||
| 59 | |||
| 60 | def NoAssertion(obj): | ||
| 61 | return True | ||
| 62 | |||
| 63 | |||
| 64 | class RequiredFeature(object): | ||
| 65 | def __init__(self, feature, assertion=NoAssertion): | ||
| 66 | """Create instance of RequiredFeature. | ||
| 67 | |||
| 68 | Will get the actual target from factory upon the first call. | ||
| 69 | |||
| 70 | :type feature: str|unicode | ||
| 71 | :param feature: the key to get object from factory | ||
| 72 | |||
| 73 | :Example: | ||
| 74 | inst = RequiredFeature("user_manager") | ||
| 75 | inst.some_method() # where user_manager.some_method will be called | ||
| 76 | |||
| 77 | :raise: | ||
| 78 | KeyError if feature doesn't exist in factory. | ||
| 79 | """ | ||
| 80 | self.feature = feature | ||
| 81 | self.assertion = assertion | ||
| 82 | |||
| 83 | def __get__(self, obj, T): | ||
| 84 | return self.result # <-- will request the feature upon first call | ||
| 85 | |||
| 86 | def __getattr__(self, name): | ||
| 87 | self.result = self.request() | ||
| 88 | if name == "result": | ||
| 89 | return self.result | ||
| 90 | else: | ||
| 91 | return getattr(self.result, name) | ||
| 92 | |||
| 93 | def request(self): | ||
| 94 | obj = factory[self.feature] | ||
| 95 | assert self.assertion(obj), \ | ||
| 96 | "The value %r of %r does not match the specified criteria" \ | ||
| 97 | % (obj, self.feature) | ||
| 98 | return obj |
| 1 | # -*- coding: utf-8 -*- | 1 | # -*- coding: utf-8 -*- |
| 2 | 2 | ||
| 3 | __author__ = 'hubian' | ||
| 4 | 3 | ||
| 5 | from datetime import datetime | 4 | from datetime import datetime |
| 6 | 5 | ||
| 7 | 6 | ||
| 8 | class Utils(): | 7 | def get_now(): |
| 9 | |||
| 10 | def get_now(self): | ||
| 11 | return datetime.now() | 8 | return datetime.now() |
| 12 | 9 | ||
| 13 | def get_config(self): | 10 | |
| 11 | def get_config(): | ||
| 14 | return None | 12 | return None |
| 15 | 13 | ||
| 16 | def get_safe_config(self): | 14 | |
| 15 | def get_safe_config(): | ||
| 17 | return None | 16 | return None | ... | ... |
-
Please register or sign in to post a comment