Ejemplo n.º 1
0
 def split_tab(self, nblock):
     """Split the text to extract the 'nblock'-th block."""
     expr = re.escape(self.idt_deb) + '(.*?)' + re.escape(self.idt_fin)
     re_split_tab = re.compile(expr, re.MULTILINE | re.DOTALL)
     l_txttab = re_split_tab.findall(self.text)
     nbtab = len(l_txttab)
     if nblock > nbtab:
         raise error('TABLE0_10', None, (nblock, nbtab))
     self.text = l_txttab[nblock - 1]
     _printDBG("TEXT:", self.text)
Ejemplo n.º 2
0
 def read_line_i(self, i, line):
     """Read a line."""
     para = self.tab.para
     mat = re.search(self.re_line, line)
     _printDBG(line, len(para), mat)
     if mat is None or len(para) != len(mat.groups()):
         lerr = [error('TABLE0_11', vali=i + 1),
                 error('TABLE0_13', vali=len(para))]
         if mat is not None:
             lerr.append(error('TABLE0_12', vali=len(mat.groups())))
         else:
             lerr.append(error('TABLE0_15', valk=(line, self.re_line)))
         raise error(lerr)
     return mat.groups()
Ejemplo n.º 3
0
 def split_tab(self, nblock):
     """Split the text to extract the 'nblock'-th block."""
     all_lines = [
         line for line in self.text.splitlines() if line.strip() != '']
     stat = [[], ]
     nbcol = 0
     curblock = 0
     _printDBG("NB: Les lignes sont numérotées après la suppression "
               "des lignes vides.")
     _printDBG("Début de la table {} à la ligne {}".format(curblock+1, 1) )
     curblock_hasvalue = False
     for i, line in enumerate(all_lines):
         cur = len(msplit(line, self.sep))
         if self.is_comment(line) and not curblock_hasvalue:
             # only comments in this block
             cur = 0
         elif nbcol > 1 and cur < nbcol:
             # less fields = new block
             _printDBG("Début de la table {} à la ligne {}".format(curblock+2, i+1) )
             if curblock >= nblock and not self.debug:
                 break
             curblock += 1
             curblock_hasvalue = False
             stat.append([])
         nbcol = cur
         stat[curblock].append((nbcol, line))
         if not self.is_comment(line):
             curblock_hasvalue = True
     nbtab = len(stat)
     _printDBG("Nombre de blocs lus :", nbtab, pformat(stat))
     if nblock > nbtab:
         raise error('TABLE0_10', None, (nblock, nbtab))
     return stat[nblock - 1]
Ejemplo n.º 4
0
 def extract_lines(self, stat):
     """First global parsing to separate lines and title."""
     nbcol = max([i[0] for i in stat])
     self.lines = []
     ltit = []
     for i, line in stat:
         if i == nbcol and not self.is_comment(line):
             self.lines.append(line)
         else:
             ltit.append(line)
     ltit = cut_long_lines(os.linesep.join(ltit), 80)
     self.title = os.linesep.join(
         maximize_lines(ltit.splitlines(), 80, ' '))
     _printDBG("TITLE:", self.title)
     _printDBG("LINES:", '\n', self.lines)
     return nbcol
Ejemplo n.º 5
0
 def read(self, nblock=None, check_para=None):
     """Read and create the Table.
     'check_para' is a function to check the list of the parameters."""
     block_stat = self.split_tab(nblock)
     nbcol = self.extract_lines(block_stat)
     line_para = self.lines.pop(0)
     para = msplit(line_para, self.sep)
     if len(para) != nbcol:
         raise error('TABLE0_43', line_para, nbcol)
     # if sep != ' ', parameter may contain a space (not valid in Table)
     para = [p.replace(' ', '_') for p in para]
     if callable(check_para):
         para = check_para(para)
     _printDBG("PARAMS:", para)
     self.tab = Table(para=para, titr=self.title)
     self.read_all_lines()
     return self.tab
Ejemplo n.º 6
0
def lire_nb_valeurs(file_object,
                    nb,
                    extend_to,
                    conversion,
                    nb_bloc=1,
                    nb_ignore=0,
                    max_per_line=-1,
                    regexp_label=None):
    """Lit nb valeurs dans file_object et les ajoute à extend_to
    après les avoir converties en utilisant la fonction conversion.
    Ignore nb_ignore lignes pour chacun des nb_bloc lus et lit au
    maximum max_per_line valeurs par ligne.
    Retourne le nombre de lignes lues."""
    if max_per_line < 0:
        max_per_line = nb
    ln = 0
    _printDBG("LIRE_NB_VALEURS nb=", nb, ", nb_bloc=", nb_bloc, ", nb_ignore=",
              nb_ignore)
    for i in range(nb_bloc):
        val = []
        j = 0
        label = []
        while j < nb_ignore:
            ln += 1
            line = file_object.readline()
            if line.strip() == '':
                continue
            j += 1
            label.append(line)
        if nb_ignore:
            _printDBG("IGNORE", label)
            slabel = ''.join(label)
            if regexp_label:
                mat = re.search(regexp_label, slabel, re.M)
                if mat is None:
                    _printDBG("LABEL", regexp_label, "non trouve, recule de",
                              nb_ignore, "lignes.", i, "bloc(s) lu(s).")
                    curpos = file_object.tell()
                    file_object.seek(curpos - len(slabel))
                    return 0
        while len(val) < nb:
            ln += 1
            line = file_object.readline()
            if line.strip() == '':
                continue
            add = [conversion(v) for v in line.split()[:max_per_line]]
            val.extend(add)
        ASSERT(
            len(val) == nb,
            "%d valeurs attendues, %d valeurs lues" % (nb, len(val)))
        extend_to.extend(val)
        _printDBG("BLOC", i, ",", nb, "valeurs lues, debut :", repr(val[:3]))
    return ln
Ejemplo n.º 7
0
    def read(self, nblock, check_para=None):
        """Read and create the Table.
        'check_para' is a function to check the list of the parameters."""
        self.split_tab(nblock)
        self.set_title()
        self.extract_lines()

        # ligne des paramètres et des types
        para = msplit(self.lines.pop(0), self.sep)
        types = msplit(self.lines.pop(0), self.sep)
        if callable(check_para):
            para = check_para(para)
        _printDBG("PARAMS:", para)
        _printDBG("TYPES:", types)
        self.tab = Table(para=para, typ=types, titr=self.title)

        lfmt = [FMT[typ[0]] % {'len': typ[1:]} for typ in types]
        self.re_line = ('%s+' % re.escape(self.sep)).join(lfmt)
        _printDBG("REGEXP:", self.re_line)

        self.read_all_lines()
        return self.tab
Ejemplo n.º 8
0
 def extract_lines(self):
     """Extract the text of the lines"""
     self.lines = [line for line in self.text.splitlines()
                   if line.strip(self.sep) != '' and not line.startswith(self.idt_tit)]
     _printDBG("LINES:", '\n', self.lines)
Ejemplo n.º 9
0
 def set_title(self):
     """Extract the title"""
     exp = re.compile(re.escape(self.idt_tit) + '(.*)$', re.M)
     self.title = os.linesep.join(
         [s.strip(self.sep) for s in exp.findall(self.text)])
     _printDBG("TITLE:", self.title)