def dispatch_specs(self): """Return current dispatch specs""" dispatch_spec = mdict.mdict(self.cfg).get("rec.import.dispatch", mdict.mdict({})).dct dispatch_spec = ";".join("%s:%s" % (m, fs) for m, fs in dispatch_spec.items()) spec = parse_dispatch_specs(dispatch_spec).copy() spec.update(self.dispatch_cli_specs or {}) return spec
def field_specs(self): """Return current field specs""" spec = copy.deepcopy(self.field_cli_specs or {}) cfg_spec = DEFAULT_FIELD_SPEC.copy() cfg_spec.update( mdict.mdict(self.cfg).get("rec.import.fields", mdict.mdict({})).dct) cfg_spec = ";".join("%s:%s" % (m, fs) for m, fs in cfg_spec.items()) spec.update(parse_field_specs(cfg_spec)) return spec
def get(self, args, key=None): """Display configuration values Usage: %(std_usage)s %(surcmd)s [--local|--global] [KEY] Options: %(std_options)s --local Read only from local addon config file ``.oem.rc``, If no ``--local`` nor ``--global`` is provided, the result comes from all files. You can't use this option if OEM_CONFIG_FILENAME is defined. --global Read only from global config file ``~/.oem.rc`` If no ``--local`` nor ``--global`` is provided, the result comes from all files. You can't use this option if OEM_CONFIG_FILENAME is defined. """ Null = object() cfg = self._get_target_cfg(args, write=False) mcfg = mdict(cfg) mcfg = mcfg if key is None else mcfg.get(key, Null) p = "%s." % key if key else "" if isinstance(mcfg, mdict): for k, v in sorted(mcfg.flat.items()): print("%s%s=%s" % (p, k, v)) elif mcfg is Null: exit(1) else: print(mcfg)
def _get_target_cfg(self, args, write=True, first_has_key=None): cfg_file_var = "%s_CONFIG_FILENAME" % args["__env__"]["name"].upper() if os.environ.get(cfg_file_var, False) is not False: if args["--global"] or args["--local"]: raise ValueError( "``--local`` or ``--global`` are not allowed with '$%s' " "set." % cfg_file_var) return self.cfg.__cfg_head__ if first_has_key is None: has_local = "local" in self.cfg.__cfg_labels__ if not has_local and args["--local"]: raise ValueError( "Can't access local config as you are not in a package.") if args["--global"] or not has_local: return self.cfg.__cfg_global__ if args["--local"] or write: return self.cfg.__cfg_local__ return self.cfg Null = object() ## Get first having given key cfg = None for label in ["local", "global"]: cfg = getattr(self.cfg, "__cfg_%s__" % label, Null) if cfg is Null: continue try: _ = mdict(cfg)[first_has_key] return cfg except KeyError: pass raise KeyError("Can't find key %r" % first_has_key)
def rm(self, key, args): """Remove configuration values Usage: %(std_usage)s %(surcmd)s [--local|--global] KEY Options: %(std_options)sx --local Remove only in local addon config file ``.oem.rc`` If no ``--local`` nor ``--global`` is provided, the first matching place where this value is set will be removed. You can't use this option if %(name)s_CONFIG_FILENAME is defined. --global Remove only in global config file ``~/.oem.rc`` If no ``--local`` nor ``--global`` is provided, the first matching place where this value is set will be removed. You can't use this option if OEM_CONFIG_FILENAME is defined. """ cfg = self._get_target_cfg(args, first_has_key=key) del mdict(cfg)[key]
def get_fields_for_model(self, model): """Return list of fields to import""" fields = [ f for f, fdef in self.o.get_fields(model).items() if is_field_selected(model, f, self.field_specs) and not fdef.get("readonly", False) ] ## order mcfg = mdict.mdict(self.cfg) default_rank_cfg = mcfg.get("rec.import.order.*", 'name,sequence') rank = mcfg.get("rec.import.order.%s" % model, default_rank_cfg) ## force 'name', then 'sequence' to be first field displayed... order_rank = dict( (label, i) for i, label in enumerate(rank.split(','))) fields.sort(key=lambda x: order_rank.get(x[0], x[0])) return fields
def set(self, key, value, args): """Display configuration values Usage: %(std_usage)s %(surcmd)s [--local|--global] KEY VALUE Options: %(std_options)s --local Write in the local addon config file ``.oem.rc`` This is the default if a local repository is detected. You can't use this option if %(name)s_CONFIG_FILENAME is defined. --global Write in the global config file ``~/.oem.rc`` This is the default if no local repository is detected. You can't use this option if OEM_CONFIG_FILENAME is defined. """ cfg = self._get_target_cfg(args) mdict(cfg)[key] = value
def Command(path=None, template=None, flavor=None, prompt=False, author=None, license_years=None, module_version=None, website=None): """Initialise a module by populating a directory Will populate specified path (or current path if not specified) with OpenERP/Odoo modules files and directory structure. Usage: %(std_usage)s %(surcmd)s [PATH] [--template=TEMPLATE] [--flavor=FLAVOR] [--prompt|-p] [--author=AUTHOREMAIL] [--license-years=YEARS] [--module-version VERSION] [--website WEBSITE] Options: %(std_options)s PATH If not specified, equivalent to "." --template TEMPLATE Advanced cookiecutter template. --flavor FLAVOR Target template version (ie: 8.0, 7.0) (default: master) --prompt, -p Prompt for all values, except those provided on the command line. And values from config files. --author AUTHOREMAIL Author name and email. Default taken in config. (ie: 'Robert Dubois <*****@*****.**>') --license-years YEARS License applicable years. (ie: 2010-2012, 2013) (defaults to current year) --module-version VERSION Starting version number --website WEBSITE Website of the module. """ cfg = kids.cfg.load() path = os.getcwd() if path is None else os.path.abspath(path) root = common.find_root(path) if root: msg.die("Module %r already initialized." % root) name = os.path.basename(path) output_dir = os.path.dirname(path) if not os.path.isdir(output_dir): msg.die("Destination directory %r doesn't exists." % (output_dir, )) if template is None: template = mdict(cfg).get("init.template", DEFAULT_TEMPLATE) ## Command line provided values cli_values = {"name": name} default_values = {} dct = default_values if author is None else cli_values if author is None: author = mdict(cfg).get("author") if author is None and not os.environ.get("NO_GIT_CONFIG", ""): author = get_git_author(path) if author is None: msg.die( "No author found on command line nor in config files.\n" "Please provide an author with '--author=', for instance:\n\n" " oem init --author='Robert Dubois <*****@*****.**>'" "\n\nor set a default author in your config file before " "running this command:\n\n" " oem config set author 'Robert Dubois <*****@*****.**>'" "\n\nor set a default author in your git config like this:" "\n\n" " git config --global user.name 'Robert Dubois'\n" " git config --global user.email '*****@*****.**'\n") match = AUTHOR_REGEX.search(author) if not match: msg.die("Your value %r for 'author' doesn't match specs.\n" "You should try to match this example:\n\n" " Robert Dubois <*****@*****.**>" % (author, )) match = match.groupdict() dct["author"] = match["name"].strip() dct["email"] = match["email"] if license_years is None: license_years = sact.epoch.Time.now().strftime('%Y') default_values["license_years"] = license_years else: cli_values["license_years"] = license_years if module_version is None: module_version = '0.1' default_values["version"] = module_version else: cli_values["version"] = module_version if website is not None: cli_values["website"] = website ## A big part of the following code comes directly from ## cookiecutter, and I would be glad that some of the ## functionality I implemented would come in a way or another ## in cookiecutter itslef. # Get user config from ~/.cookiecutterrc or equivalent # If no config file, sensible defaults from config.DEFAULT_CONFIG are used config_dict = cc.get_user_config() template = cc.expand_abbreviations(template, config_dict) # TODO: find a better way to tell if it's a repo URL if 'git@' in template or 'https://' in template: repo_dir = cc.clone(repo_url=template, checkout=flavor, clone_to_dir=config_dict['cookiecutters_dir'], no_input=True) else: # If it's a local repo, no need to clone or copy to your # cookiecutters_dir repo_dir = template context_file = cc.find_cfg_file(repo_dir) default_context = config_dict.get('default_context', {}) if default_values: default_context.update(default_values) context = cc.generate_context( context_file=context_file, default_context=default_context, extra_context=config_dict, ) context = cc.prompt_for_config(context, no_input=False, values=cli_values, only_missing=not prompt, with_optional=prompt) ## XXXvlab: missing no-overwrite mode # Create project from local context and project template. if not os.environ.get("OEM_DRY_RUN", ""): cc.generate_files(repo_dir=repo_dir, context=context, output_dir=output_dir)
def __repr__(self, ): return ("<%s %r (%s values%s)>" % (self.__class__.__name__, self._cfg_manager._filename, len(mdict.mdict(self).flat), (", prefix=%r" % self._prefix) if self._prefix else ""))