summaryrefslogtreecommitdiff
path: root/openerpcommand/model.py
diff options
context:
space:
mode:
authorVo Minh Thu <vmt@openerp.com>2013-01-11 14:46:57 +0100
committerVo Minh Thu <vmt@openerp.com>2013-01-11 14:46:57 +0100
commit3da57500c279cd19fa6d561eef149d708383138d (patch)
treee6ea46397217349ff0381b91346a1ba02394e14b /openerpcommand/model.py
parent3478333bca1a28483c1c7eb90431cb1e979c74ba (diff)
[ADD] oe: provides sane (unfucked) command-line interface.
The implementation is far from perfect. Some improvements are waiting in its previous location: lp:~openerp/openerp-command. Some docs are provided, see doc/openerp-command.rst and doc/adding-command.rst. bzr revid: vmt@openerp.com-20130111134657-im2f3uqjluyo4pm6
Diffstat (limited to 'openerpcommand/model.py')
-rw-r--r--openerpcommand/model.py61
1 files changed, 61 insertions, 0 deletions
diff --git a/openerpcommand/model.py b/openerpcommand/model.py
new file mode 100644
index 00000000000..26a499c647f
--- /dev/null
+++ b/openerpcommand/model.py
@@ -0,0 +1,61 @@
+"""
+Display information about a given model.
+"""
+import os
+import sys
+import textwrap
+
+def run(args):
+ assert args.database
+ assert args.model
+ import openerp
+ openerp.tools.config['log_level'] = 100
+ openerp.netsvc.init_logger()
+ registry = openerp.modules.registry.RegistryManager.get(
+ args.database, update_module=False)
+ model = registry.get(args.model)
+ longest_k = 1
+ longest_string = 1
+ columns = model._columns
+
+ if args.field and args.field not in columns:
+ print "No such field."
+ sys.exit(1)
+
+ if args.field:
+ columns = { args.field: columns[args.field] }
+ else:
+ print "Fields (model `%s`, database `%s`):" % (args.model, args.database)
+
+ for k, v in columns.items():
+ longest_k = len(k) if longest_k < len(k) else longest_k
+ longest_string = len(v.string) \
+ if longest_string < len(v.string) else longest_string
+ for k, v in sorted(columns.items()):
+ attr = []
+ if v.required:
+ attr.append("Required")
+ if v.readonly:
+ attr.append("Read-only")
+ attr = '/'.join(attr)
+ attr = '(' + attr + ')' if attr else attr
+ if args.verbose:
+ print v.string, '-- ' + k + ', ' + v._type, attr
+ else:
+ print k.ljust(longest_k + 2), v._type, attr
+ if args.verbose and v.help:
+ print textwrap.fill(v.help, initial_indent=' ', subsequent_indent=' ')
+
+def add_parser(subparsers):
+ parser = subparsers.add_parser('model',
+ description='Display information about a given model for an existing database.')
+ parser.add_argument('-d', '--database', metavar='DATABASE', required=True,
+ help='the database to connect to')
+ parser.add_argument('-m', '--model', metavar='MODEL', required=True,
+ help='the model for which information should be displayed')
+ parser.add_argument('-v', '--verbose', action='store_true',
+ help='display more information')
+ parser.add_argument('-f', '--field', metavar='FIELD',
+ help='display information only for this particular field')
+
+ parser.set_defaults(run=run)