def __init__(self, app_dir, ui): """ Constructor for Couchapp object. :params ui: ui instance """ self.app_dir = ui.realpath(app_dir) self.ui = ui self.ui.updateconfig(app_dir) self.app_dir = app_dir # load extensions self.extensions = Extensions(self) self.extensions.load()
class CouchApp(object): """ app object. used to push/clone/init/create a couchapp """ default_locations = (("template", ["app-template", "../../app-template"]), ("vendor", ["vendor", "../../vendor"])) def __init__(self, app_dir, ui): """ Constructor for Couchapp object. :params ui: ui instance """ self.app_dir = ui.realpath(app_dir) self.ui = ui self.ui.updateconfig(app_dir) self.app_dir = app_dir # load extensions self.extensions = Extensions(self) self.extensions.load() def initialize(self, db_url=None): """ Initialize an app. It basically create the .couchapprc file and add default conf. :attr default_conf: dict, default configuration :attr verbose: boolean, default False :return: boolean, dict. { 'ok': True } if ok, { 'ok': False, 'error': message } if something was wrong? """ if not self.ui.isdir(self.app_dir) and self.ui.verbose: self.ui.logger.error("%s directory doesn't exist." % self.app_dir) # set default_conf default_conf = {} if db_url: default_conf = {"env": {"default": {"db": db_url}}} rc_file = "%s/.couchapprc" % self.app_dir if not self.ui.isfile(rc_file): self.ui.write_json(rc_file, default_conf) elif self.ui.verbose: raise AppError("CouchApp already initialized in %s." % self.app_dir) def generate(self): """ Generates a CouchApp in app_dir :attr verbose: boolean, default False :return: boolean, dict. { 'ok': True } if ok, { 'ok': False, 'error': message } if something was wrong. """ locations = {} for location, paths in self.default_locations: found = False location_dir = "" for path in paths: location_dir = os.path.normpath(self.ui.rjoin(self.ui.dirname(__file__), path)) if self.ui.isdir(location_dir): found = True break if found: if location == "template": dest_dir = self.app_dir else: dest_dir = self.ui.rjoin(self.app_dir, "vendor") try: self.ui.copytree(location_dir, dest_dir) except OSError, e: errno, message = e raise AppError("Can't create a CouchApp in %s: %s" % (self.app_dir, message)) else: raise AppError("Can't create a CouchApp in %s: default template not found." % (self.app_dir)) self.initialize() self.extensions.notify("post-generate", self.ui, self)
class CouchApp(object): """ app object. used to push/clone/init/create a couchapp """ default_locations = ( ("template", ['app', '../../templates/app']), ("vendor", ['vendor', '../../vendor']) ) def __init__(self, app_dir, ui): """ Constructor for Couchapp object. :params ui: ui instance """ self.app_dir = ui.realpath(app_dir) self.ui = ui self.ui.updateconfig(app_dir) self.app_dir = app_dir # load extensions self.extensions = Extensions(self) self.extensions.load() def initialize(self, db_url=None): """ Initialize an app. It basically create the .couchapprc file and add default conf. :attr default_conf: dict, default configuration :attr verbose: boolean, default False :return: boolean, dict. { 'ok': True } if ok, { 'ok': False, 'error': message } if something was wrong? """ if not self.ui.isdir(self.app_dir) and self.ui.verbose: self.ui.logger.error("%s directory doesn't exist." % self.app_dir) # set default_conf default_conf = {} if db_url: default_conf = { "env": { "default": { "db": db_url } } } rc_file = self.ui.rjoin(self.app_dir, '.couchapprc') if not self.ui.isfile(rc_file): self.ui.write_json(rc_file, default_conf) elif self.ui.verbose: raise AppError("CouchApp already initialized in %s." % self.app_dir) def generate(self, kind='app', name=None, template=None): if kind not in ["app", "view", "list", "show", 'filter', 'function', 'vendor']: raise AppError("Can't generate %s in your couchapp" % kind) if kind == "app": self.generate_app(template=template) else: if name is None: raise AppError("Can't generate %s function, name is missing" % kind) self.generate_function(kind, name, template) def generate_app(self, template=None): """ Generates a CouchApp in app_dir :attr verbose: boolean, default False :return: boolean, dict. { 'ok': True } if ok, { 'ok': False, 'error': message } if something was wrong. """ DEFAULT_APP_TREE = [ '_attachments', 'lists', 'shows', 'updates', 'views' ] TEMPLATES = ['app', 'vendor'] prefix = '' if template is not None: prefix = self.ui.rjoin(*template.split('/')) try: os.mkdir(self.app_dir) except OSError, e: errno, message = e raise AppError("Can't create a CouchApp in %s: %s" % ( self.app_dir, message)) for n in DEFAULT_APP_TREE: path = self.ui.rjoin(self.app_dir, n) self.ui.makedirs(path) for t in TEMPLATES: app_dir = self.app_dir if prefix: # we do the job twice for now to make sure an app or vendor # template exist in user template location # fast on linux since there is only one user dir location # but could be a little slower on windows for user_location in user_path(): location = os.path.join(user_location, 'templates', prefix, t) if self.ui.exists(location): if t == "vendor": app_dir = self.ui.rjoin(app_dir, "vendor") try: os.makedirs(app_dir) except: pass t = self.ui.rjoin(prefix, t) break self.ui.copy_helper(app_dir, t) self.initialize() self.extensions.notify("post-generate", self.ui, self)