Exemple #1
0
 def print_path_help(self, path):
     """print the help of certain path"""
     (app, type) = self.app_path[path]
     if type == "func":
         print "  %s [%s] --> %s" % (path, type, Job.get_func_help(app))
         for line in _decode(app.__doc__).split("\n"):
             print "    " + line
         print Job.get_func_config(app, prefix="    ")
     elif type in ("Job", "MJob", "PJob"):
         print "  %s [%s] --> %s" % (path, type, app.get_line_help())
         for line in _decode(app.__doc__).split("\n"):
             print "    " + line
         print app.get_config_str(prefix="    ")
     else:
         raise Exception("unknown Object type = %s of %s" % (type, app) )
     print ""        
Exemple #2
0
 def get_config_str(self, prefix=""):
     """
     get the detailed class doc and source code of config method (IO interface)
     """
     sz = _decode(inspect.getsource(self.config))
     # 以def所在行作为基准,去掉多于空格,添加prefix
     lines = sz.split("\n")
     i = 0
     while lines[0][i] == ' ':
         i += 1
     res = []
     for l in lines:
         res.append(prefix+l[i:]+"\n")
     return "".join(res)
Exemple #3
0
 def get_func_help(func, prefix=""):
     """
     return the similar single line help for a callable function.
     """
     if not callable(func):
         raise Exception("%s is not a function" % func)
     spec = inspect.getargspec(func)
     varnames = list(spec.args)
     #  添加default信息
     if spec.defaults:
         defaults = list(spec.defaults)                
         for i in range(-1, -len(defaults)-1, -1):
             varnames[i] += _decode("=%s" % repr(defaults[i]))
     # 去掉self
     if varnames and varnames[0] == 'self':
         varnames = varnames[1:]
     sz = "%s < %s >" % (func.func_name, ", ".join(varnames))
     return prefix + sz
Exemple #4
0
 def get_func_config(func, prefix="", top=10):
     """
     get the detailed func doc and source
     """
     sz = _decode(inspect.getsource(func))
     # 以def所在行作为基准,去掉多于空格,添加prefix
     lines = sz.split("\n")
     i = 0
     while lines[0][i] == ' ':
         i += 1
     res = []
     n = top
     if n > len(lines):
         n = len(lines)
     j = 0
     while j < n: 
         l = lines[j]
         res.append(prefix+l[i:]+"\n")
         j += 1
     return "".join(res)        
Exemple #5
0
    def __getattr__(self, name):
        try:
            info = self._metadata[name]
        except KeyError:
            msg = 'the "%s" field is not indexed nor stored'
            raise AttributeError, msg % name

        field_cls = _get_field_cls(name, self._fields, info)

        # Get the data
        try:
            value = info['value']
        except KeyError:
            raise AttributeError, 'the "%s" field is not stored' % name
        data = self._xdoc.get_value(value)

        # Multilingual field: language negotiation
        if not data and issubclass(field_cls, Unicode) and 'from' not in info:
            prefix = '%s_' % name
            n = len(prefix)

            languages = []
            values = {}
            for k in self._metadata:
                if k[:n] == prefix:
                    language = k[n:]
                    value = getattr(self, '%s_%s' % (name, language))
                    if not field_cls.is_empty(value):
                        languages.append(language)
                        values[language] = value

            if languages:
                language = select_language(languages)
                if language is None:
                    language = languages[0]
                return values[language]

        # Standard (monolingual)
        return _decode(field_cls, data)
Exemple #6
0
 def get_line_help(self, prefix = ""):
     """
         get the single line help string
         
         >> app = PlotJob()
         >> print app.get_line_help()
            PlotJob.config < date, hour="03" >
            
     """
     # spec = (args, varargs, varkw, defaults)
     spec = inspect.getargspec(self.config)
     varnames = list(spec.args)
     #  添加default信息
     if spec.defaults:
         defaults = list(spec.defaults)                
         for i in range(-1, -len(defaults)-1, -1):
             varnames[i] += _decode("=%s" % repr(defaults[i]))
     # 去掉self
     if varnames[0] == 'self':
         varnames = varnames[1:]
     sz = "%s.config < %s >" % (self.__class__.__name__, ", ".join(varnames))
     return prefix + sz
Exemple #7
0
    def __getattr__(self, name):
        try:
            info = self._metadata[name]
        except KeyError:
            msg = 'the "%s" field is not indexed nor stored'
            raise AttributeError, msg % name

        field_cls = _get_field_cls(name, self._fields, info)

        # Get the data
        try:
            value = info['value']
        except KeyError:
            raise AttributeError, 'the "%s" field is not stored' % name
        data = self._xdoc.get_value(value)

        # Multilingual field: language negotiation
        if not data and issubclass(field_cls, Unicode) and 'from' not in info:
            prefix = '%s_' % name
            n = len(prefix)

            languages = []
            values = {}
            for k in self._metadata:
                if k[:n] == prefix:
                    language = k[n:]
                    value = getattr(self, '%s_%s' % (name, language))
                    if not field_cls.is_empty(value):
                        languages.append(language)
                        values[language] = value

            if languages:
                language = select_language(languages)
                if language is None:
                    language = languages[0]
                return values[language]

        # Standard (monolingual)
        return _decode(field_cls, data)