예제 #1
0
def import_texts(database, directory):
    connection = pytis.data.DBConnection(database=database)
    data = data_object('texts', ('text_id', 'label', 'lang', 'content'),
                       connection)
    label_data = data_object('text_labels', ('label', ), connection)
    label_column_type = label_data.columns()[0].type()
    for filename in os.listdir(directory):
        parts = filename.split('.')
        print("  - %s ..." % filename, end=' ')
        if filename.endswith('.txt') and len(parts) == 4:
            label = '.'.join(parts[:2])
            lang = parts[2]
            label_key = label_column_type.validate(label)[0]
            if not label_data.row(label_key):
                print("new label `%s' ..." % label, end=' ')
                label_data.insert(pytis.data.Row([('label', label_key)]))
            else:
                print("updating label `%s' ..." % label, end=' ')
            content = codecs.open(os.path.join(directory, filename), 'r',
                                  'utf-8').read()
            text_id = label + '@' + lang
            row_data = [(
                c.id(),
                c.type().validate(v)[0],
            ) for c, v in zip(data.columns(), [text_id, label, lang, content])]
            row = pytis.data.Row(row_data)
            error, success = data.update(row[0], row)
            if success:
                print("done")
            else:
                print("FAILED:", error)
        else:
            print("ignored (file name doesn't match the required pattern)")
예제 #2
0
def import_texts(database, directory):
    connection = pytis.data.DBConnection(database=database)
    data = data_object("texts", ("text_id", "label", "lang", "content"), connection)
    label_data = data_object("text_labels", ("label",), connection)
    label_column_type = label_data.columns()[0].type()
    for filename in os.listdir(directory):
        parts = filename.split(".")
        print "  - %s ..." % filename,
        if filename.endswith(".txt") and len(parts) == 4:
            label = ".".join(parts[:2])
            lang = parts[2]
            label_key = label_column_type.validate(label)[0]
            if not label_data.row(label_key):
                print "new label `%s' ..." % label,
                label_data.insert(pytis.data.Row([("label", label_key)]))
            else:
                print "updating label `%s' ..." % label,
            content = codecs.open(os.path.join(directory, filename), "r", "utf-8").read()
            text_id = label + "@" + lang
            row_data = [
                (c.id(), c.type().validate(v)[0]) for c, v in zip(data.columns(), [text_id, label, lang, content])
            ]
            row = pytis.data.Row(row_data)
            error, success = data.update(row[0], row)
            if success:
                print "done"
            else:
                print "FAILED:", error
        else:
            print "ignored (file name doesn't match the required pattern)"
예제 #3
0
def check_form():
    """Zeptá se na název specifikace a zobrazí její report."""
    resolver = pytis.util.resolver()
    spec = pytis.form.run_dialog(pytis.form.InputDialog,
                                 message="Kontrola defsu",
                                 prompt="Specifikace",
                                 input_width=30)
    if spec:
        try:
            data_spec = resolver.get(spec, 'data_spec')
            view_spec = resolver.get(spec, 'view_spec')
        except pytis.util.ResolverError:
            msg = 'Specifikace nenalezena.'
            pytis.form.run_dialog(pytis.form.Warning, msg)
            return
        data = data_spec.create(dbconnection_spec=config.dbconnection)
        # Title
        obsah = "Title: %s\n" % (view_spec.title())
        # Název tabulky
        obsah += "Tabulka: %s\n\n" % (data.table(data.columns()[0].id()))
        # Políčka v bindings
        obsah += "Políčka v data_spec:\n%s\n\n" % \
                 "\n".join(["  - %s" % c.id()
                            for c in data.columns() if c.id() != 'oid'])
        # Políčka ve fields
        obsah += "Políčka ve fields:\n%s\n\n" % \
                 "\n".join(["  - %s" % f.id() for f in view_spec.fields()])
        # Actions - TODO: Bude třeba zohlednit vnořené seznamy a ActionGroup.
        # obsah += "\n\nAkce kontextového menu:\n\n"
        # actions = view_spec.actions()
        # if actions:
        #     obsah += "\n".join([a.title() for a in actions])
        # else:
        #     obsah += "Nejsou definovány"
        # Default select
        _get_default_select(spec)
        pytis.form.run_dialog(pytis.form.Message,
                              "DEFS: %s" % spec,
                              report=obsah)
예제 #4
0
파일: defs.py 프로젝트: brailcom/pytis
def check_form():
    """Zeptá se na název specifikace a zobrazí její report."""
    resolver = pytis.util.resolver()
    spec = pytis.form.run_dialog(pytis.form.InputDialog,
                                 message="Kontrola defsu",
                                 prompt="Specifikace",
                                 input_width=30)
    if spec:
        try:
            data_spec = resolver.get(spec, 'data_spec')
            view_spec = resolver.get(spec, 'view_spec')
        except pytis.util.ResolverError:
            msg = 'Specifikace nenalezena.'
            pytis.form.run_dialog(pytis.form.Warning, msg)
            return
        data = data_spec.create(dbconnection_spec=config.dbconnection)
        # Title
        obsah = "Title: %s\n" % (view_spec.title())
        # Název tabulky
        obsah += "Tabulka: %s\n\n" % (data.table(data.columns()[0].id()))
        # Políčka v bindings
        obsah += "Políčka v data_spec:\n%s\n\n" % \
                 "\n".join(["  - %s" % c.id()
                            for c in data.columns() if c.id() != 'oid'])
        # Políčka ve fields
        obsah += "Políčka ve fields:\n%s\n\n" % \
                 "\n".join(["  - %s" % f.id() for f in view_spec.fields()])
        # Actions - TODO: Bude třeba zohlednit vnořené seznamy a ActionGroup.
        # obsah += "\n\nAkce kontextového menu:\n\n"
        # actions = view_spec.actions()
        # if actions:
        #     obsah += "\n".join([a.title() for a in actions])
        # else:
        #     obsah += "Nejsou definovány"
        # Default select
        _get_default_select(spec)
        pytis.form.run_dialog(pytis.form.Message, "DEFS: %s" % spec, report=obsah)
예제 #5
0
파일: www.py 프로젝트: cerha/pytis
def form_validate(spec, prefill):
    # Sestavíme datový objekt
    data = pytis.util.data_object(spec)
    if not data:
        return None, None
    failed = []
    row = []
    for c in data.columns():
        if c.id() in prefill:
            value, error = c.type().validate(prefill[c.id()])
            if error:
                failed.append(c.id())
                continue
            else:
                row.append((c.id(), value))
    if len(failed) > 0:
        return None, failed
    else:
        return pytis.data.Row(row), None
예제 #6
0
파일: www.py 프로젝트: nicLucian/pytis
def form_validate(spec, prefill):
    # Sestavíme datový objekt
    data = data_object(spec)
    if not data:
        return None, None
    failed = []
    row = []
    for c in data.columns():
        if c.id() in prefill:
            value, error = c.type().validate(prefill[c.id()])
            if error:
                failed.append(c.id())
                continue
            else:
                row.append((c.id(), value))
    if len(failed) > 0:
        return None, failed
    else:
        return pytis.data.Row(row), None