def wrap(line, lineend='', indent=0, width=75): if type(line) is tuple or type(line) is list: if len(line) >= 2: s1 = line[0] else: s1 = '' s2 = line[1:] elif type(line) is str: s1 = '' s2 = [line] else: raise error.internal('line is not a tuple, list or string') s1len = len(s1) s = '' first = True for ss in s2: if type(ss) is not str and type(ss) is not unicode: raise error.internal('text needs to be a string') for l in textwrap.wrap(ss, width=width - s1len - indent - 1): s += '%s%s%s%s%s' % (' ' * indent, s1, l, lineend, os.linesep) if first and s1len > 0: s1 = ' ' * s1len if lineend != '': s = s[:0 - len(os.linesep) - 1] + os.linesep return s
def find_arg(self, arg): if self.optargs is None or arg not in self.optargs: raise error.internal('bad arg: %s' % (arg)) for a in self.args: sa = a.split('=') if sa[0].startswith(arg): return sa return None
def row(cols, data, indent=0, marker='|', linesep=os.linesep): if len(cols) != len(data): raise error.internal('data size (%d) does not' \ ' match columns (%d)' % (len(data), len(cols))) s = ' ' * indent + '|' for c in range(0, len(cols)): if c < len(cols) - 1: m = marker else: m = '|' s += '%-*s%s' % (int(cols[c] - 1), str(data[c]), m) return s + linesep
def jobs(self, cpus): try: cpus = int(cpus) except: raise error.general('invalid host cpu value') opt_jobs = self.opts['jobs'] if opt_jobs == 'default': _jobs = self.defaults.get_value('jobs') if _jobs is not None: if _jobs == 'none': cpus = 0 elif _jobs == 'max': pass elif _jobs == 'half': cpus = cpus / 2 else: try: cpus = int(_jobs) except: raise error.general('invalid %%{jobs} value: %s' % (_jobs)) else: opt_jobs = 'max' if opt_jobs != 'default': if opt_jobs == 'none': cpus = 0 elif opt_jobs == 'max': pass elif opt_jobs == 'half': cpus = cpus / 2 else: ok = False try: i = int(opt_jobs) cpus = i ok = True except: pass if not ok: try: f = float(opt_jobs) cpus = f * cpus ok = True except: pass if not ok: raise error.internal('bad jobs option: %s' % (opt_jobs)) if cpus <= 0: cpu = 1 return cpus
def parse(self, lines): def _clean(l): if '#' in l: l = l[:l.index('#')] if '\r' in l: l = l[:l.index('r')] if '\n' in l: l = l[:l.index('\n')] return l.strip() trace_me = False if trace_me: print('[[[[]]]] parsing macros') orig_macros = copy.copy(self.macros) map = 'global' lc = 0 state = 'key' token = '' macro = [] for l in lines: lc += 1 #print 'l:%s' % (l[:-1]) if len(l) == 0: continue l_remaining = l for c in l: if trace_me: print(']]]]]]]] c:%s(%d) s:%s t:"%s" m:%r M:%s' % \ (c, ord(c), state, token, macro, map)) l_remaining = l_remaining[1:] if c is '#' and not state.startswith('value'): break if c == '\n' or c == '\r': if not (state is 'key' and len(token) == 0) and \ not state.startswith('value-multiline'): self.macros = orig_macros raise error.general('malformed macro line:%d: %s' % (lc, l)) if state is 'key': if c not in string.whitespace: if c is '[': state = 'map' elif c is '%': state = 'directive' elif c is ':': macro += [token] token = '' state = 'attribs' elif c is '#': break else: token += c elif state is 'map': if c is ']': if token not in self.macros: self.macros[token] = {} map = token token = '' state = 'key' elif c in string.printable and c not in string.whitespace: token += c else: self.macros = orig_macros raise error.general('invalid macro map:%d: %s' % (lc, l)) elif state is 'directive': if c in string.whitespace: if token == 'include': self.load(_clean(l_remaining)) token = '' state = 'key' break elif c in string.printable and c not in string.whitespace: token += c else: self.macros = orig_macros raise error.general('invalid macro directive:%d: %s' % (lc, l)) elif state is 'include': if c is string.whitespace: if token == 'include': state = 'include' elif c in string.printable and c not in string.whitespace: token += c else: self.macros = orig_macros raise error.general('invalid macro directive:%d: %s' % (lc, l)) elif state is 'attribs': if c not in string.whitespace: if c is ',': macro += [token] token = '' if len(macro) == 3: state = 'value-start' else: token += c elif state is 'value-start': if c is "'": state = 'value-line-start' elif state is 'value-line-start': if c is "'": state = 'value-multiline-start' else: state = 'value-line' token += c elif state is 'value-multiline-start': if c is "'": state = 'value-multiline' else: macro += [token] state = 'macro' elif state is 'value-line': if c is "'": macro += [token] state = 'macro' else: token += c elif state is 'value-multiline': if c is "'": state = 'value-multiline-end' else: token += c elif state is 'value-multiline-end': if c is "'": state = 'value-multiline-end-end' else: state = 'value-multiline' token += "'" + c elif state is 'value-multiline-end-end': if c is "'": macro += [token] state = 'macro' else: state = 'value-multiline' token += "''" + c else: self.macros = orig_macros raise error.internal('bad state: %s' % (state)) if state is 'macro': self.macros[map][macro[0].lower()] = (macro[1], macro[2], macro[3]) macro = [] token = '' state = 'key'
def _process_block(self, results, directive, info, data): raise error.internal('known block type: %s' % (results[0]))