def validateNode(node):
     method = getattr(self, node.attr.get('tag'), None)
     if not method:
         return
     valid_children = getattr(method, '_valid_children', None)
     if not valid_children:
         return
     mandatoryTags = self._parseValidChildrenValues(valid_children)
     for n in node.value:
         tag = n.attr.get('tag')
         if not tag:
             return
         validpars = valid_children.get(tag)
         if not validpars:
             raise GnrException(self.exceptions['invalid_child_tag'] %
                                dict(tag=tag))
         mandatory_val = mandatoryTags.get(tag)
         if mandatory_val:
             mandatory_val -= 1
             if mandatory_val:
                 mandatoryTags[tag] = mandatory_val
             else:
                 mandatoryTags.pop(tag)
         minval, maxval = validpars
         if maxval and len(
                 n.value.filter(lambda n: n.attr['tag'] == tag)) >= int(
                     maxval):
             raise GnrException(
                 self.exceptions['already_inserted_child_tag'] %
                 dict(tag=tag, maxval=maxval))
     if mandatoryTags:
         raise GnrException(
             self.exceptions['missing_mandatory_children'] %
             dict(mandatory_children=','.join(mandatoryTags.keys())))
Beispiel #2
0
 def __init__(self, docname):
     import xlrd
     import os.path
     self.XL_CELL_DATE = xlrd.XL_CELL_DATE
     self.xldate_as_tuple = xlrd.xldate_as_tuple
     self.docname = docname
     self.dirname = os.path.dirname(docname)
     self.basename, self.ext = os.path.splitext(os.path.basename(docname))
     self.ext = self.ext.replace('.', '')
     self.book = xlrd.open_workbook(filename=self.docname)
     self.sheet = self.book.sheet_by_index(0)
     self.linegen = self.sheetlines()
     firstline = self.linegen.next()
     headers = [
         slugify(firstline[c], sep='_') for c in range(self.sheet.ncols)
     ]
     self.colindex = dict([(i, True) for i, h in enumerate(headers) if h])
     self.headers = [h for h in headers if h]
     self.index = dict()
     for i, k in enumerate(self.headers):
         if k in self.index:
             raise GnrException('Duplicated columns in source xls')
         self.index[k] = i
     self.ncols = len(headers)
     self.nrows = self.sheet.nrows - 1
Beispiel #3
0
    def translate(self,
                  what=None,
                  to_language=None,
                  from_language=None,
                  format=None):
        if not what:
            return
        direction = [to_language
                     ] if not from_language else [from_language, to_language]
        safedict = dict()

        def cb(m):
            safekey = '[NO_TR_%i]' % len(safedict)
            safedict[safekey] = m.group(1)
            return safekey

        base_to_translate = SAFETRANSLATE.sub(cb, what)
        print 'safedict', safedict
        print 'base_to_translate', base_to_translate
        result = self.translator.translate(base_to_translate,
                                           '-'.join(direction),
                                           format=format)
        if result['code'] == 200:
            txt = result['text'][0]
            for k, v in safedict.items():
                txt = txt.replace(k, '[tr-off]%s[tr-on]' % v)
            return txt
        else:
            raise GnrException('Error in translation')
Beispiel #4
0
 def __init__(self, parent=None, api_key=None):
     self.parent = parent
     self.api_key = api_key
     if not YandexTranslate:
         raise GnrException(
             'Missing YandexTranslate. hint: pip install yandex.translate')
     self.translator = YandexTranslate(self.api_key)
Beispiel #5
0
 def checkNotAllowed(self,src):
     node = True
     while node:
         node = src.getNodeByAttr('_notallowed',True)
         if node:
             if self.page.isGuest:
                 raise GnrException('!!User connection lost.')
             node.parentbag.popNode(node.label)
Beispiel #6
0
def parselocal_date(txt, locale):
    """TODO

    :param txt: TODO
    :param locale: the current locale (e.g: en, en_us, it)
    """
    if txt.isdigit() and len(txt) in (6, 8):  # is a date without separators: 101207
        result = {}
        format = dates.get_date_format(locale=locale).pattern.lower()
        year_idx = format.index('y')
        month_idx = format.index('m')
        if month_idx < 0:
            month_idx = format.index('l')
        day_idx = format.index('d')
        indexes = [(year_idx, 'Y'), (month_idx, 'M'), (day_idx, 'D')]
        indexes.sort()
        is8 = (len(txt) == 8)
        for i, k in indexes:
            w = 2
            if k == 'Y':
                if is8:
                    w = 4
                    result[k] = int(txt[:w])
                else:
                    year = int(txt[:w])
                    if year < 70:
                        result[k] = 2000 + year
                    else:
                        result[k] = 1900 + year
            else:
                result[k] = int(txt[:w])
            txt = txt[w:]
        return datetime.date(result['Y'], result['M'], result['D'])
    else:
        try:
            date = dates.parse_date(txt, locale)
        except:
            raise GnrException('Invalid date')
        return date