예제 #1
0
    def __parse_xliff(self, contents):
        ns = {"xliff": "urn:oasis:names:tc:xliff:document:1.2"}

        tree = ElementTree.parse(self.xliff_path)
        root = tree.getroot()

        for file in root:
            localized_contents = LocalizedContents()
            localized_contents.file = file.get("original")
            localized_contents.source = file.get("source-language")
            localized_contents.target = file.get("target-language")

            header = file.find("xliff:header", ns)
            header_tool = header.find("xliff:tool", ns)
            tool = Tool()
            tool.id = header_tool.get("tool-id")
            tool.name = header_tool.get("tool-name")
            tool.version = header_tool.get("tool-version")
            tool.build = header_tool.get("build-num")
            localized_contents.tool = tool

            for trans_unit in (body := file.find("xliff:body", ns)):
                translation = Translation()
                translation.id = trans_unit.get("id")

                if (source_tag := trans_unit.find("xliff:source",
                                                  ns)) is not None:
                    translation.source = source_tag.text

                if (target_tag := trans_unit.find("xliff:target",
                                                  ns)) is not None:
                    translation.target = target_tag.text
예제 #2
0
    def __parse_translation_sheets(self, contents, wb):
        # Get metadata
        index = wb["_metadata_"]
        temp_localized_contents = []
        for i in range(4, index.max_row + 1):
            localized_contents = LocalizedContents()

            localized_contents.file = index["C" + str(i)].value
            localized_contents.source = index["D" + str(i)].value
            localized_contents.target = index["E" + str(i)].value

            tool = Tool()
            tool.id = index["F" + str(i)].value
            tool.name = index["G" + str(i)].value
            tool.version = index["H" + str(i)].value
            tool.build = index["I" + str(i)].value
            localized_contents.tool = tool

            temp_localized_contents.append(localized_contents)

        # Parse all sheets
        for sheetname in wb.sheetnames:
            if sheetname == "_metadata_":
                continue

            ws = wb[sheetname]
            row = 1

            if ws["A" + str(row)].value != "No":
                row += 1

            if ws["A" + str(row)].value != "No":
                print("Sheet format is invalidated : {}".format(sheetname))
                continue

            row += 1

            # Get localized content with metadata
            filtered_localized_contents = list(
                filter(lambda c: c.basename == sheetname,
                       temp_localized_contents))
            if filtered_localized_contents == False:
                print("Sheet metadata not found: {}".format(sheetname))
                continue

            localized_contents = filtered_localized_contents[0]

            # Add translations
            for i in range(row, ws.max_row + 1):
                translation = Translation()
                translation.id = ws["B" + str(i)].value
                translation.source = ws["C" + str(i)].value
                translation.target = ws["D" + str(i)].value
                translation.note = ws["E" + str(i)].value

                localized_contents.translations.append(translation)

            contents.localized_contents.append(localized_contents)
예제 #3
0
    def __parse_metadata_sheet(self, contents, wb):
        index = wb["_metadata_"]

        contents.file = pathlib.Path(index["C2"].value)
        contents.version = index["B2"].value
        contents.source = index["D2"].value
        contents.target = index["E2"].value

        tool = Tool()
        tool.id = index["F2"].value
        tool.name = index["G2"].value
        tool.version = index["H2"].value
        tool.build = index["I2"].value
        contents.tool = tool
예제 #4
0
    def __parse_contents_json(self, contents):
        with open(self.contents_json_path, "r") as contents_json:
            root = json.load(contents_json)
            contents.source = root["developmentRegion"]
            contents.target = root["targetLocale"]
            contents.version = root["version"]

            tool_info = root["toolInfo"]
            tool = Tool()
            tool.id = tool_info["toolID"]
            tool.name = tool_info["toolName"]
            tool.version = tool_info["toolVersion"]
            tool.build = tool_info["toolBuildNumber"]
            contents.tool = tool
예제 #5
0
def save_changes(bin, form, new=False):
    """
    Save the changes to the database
    """
    # Get data from form and assign it to the correct attributes
    # of the SQLAlchemy table object
    tool = Tool()
    tool.name = form.partnumber.data

    bin.partnumber = tool
    bin.location = form.location.data

    if new:
        # Add the new album to the database
        db_session.add(bin)

    # commit the data to the database
    db_session.commit()
예제 #6
0
def view_tool():
    session['url'] = request.url[len(request.url_root):]
    if request.method == 'GET':
        if 'name' in request.args:
            name = request.args.get('name')
            result = db_session.query(Tool).filter_by(name=name).first()
            return render_template('tool.html',
                                   result=result,
                                   title='cutrenet',
                                   subtitle=name)
        elif not current_user.has_role('admin'):
            flash(u'No tienes permisos para editar esta herramienta', 'error')
            return redirect('/tools', code=302)
        elif 'add' in request.args:
            form = ToolForm()
            return render_template('tool.html',
                                   form=form,
                                   title='cutrenet',
                                   subtitle="new tool")
        elif 'edit' in request.args:
            ename = request.args.get('edit')
            form = ToolForm(self_edit=ename)
            result = db_session.query(Tool).filter_by(name=ename).first()
            form.description.data = result.description  # Prepopulate textarea with past information, can´t do it at render time
            if result.maintainer is not None:
                form.maintainer.data = result.maintainer.dni
            return render_template('tool.html',
                                   form=form,
                                   result=result,
                                   title='cutrenet',
                                   subtitle=ename)
        elif 'delete_img' in request.args:
            del_img = request.args.get('delete_img')
            tool = db_session.query(Tool).filter_by(name=del_img).first()
            if tool.image:
                os.remove(tool.image)  # Delete old image
                tool.image = None
                db_session.commit()
                flash(u'Imagen eliminada', 'success')
            else:
                flash(u'No hay imagen que eliminar', 'alert')
            return render_template('tool.html',
                                   result=tool,
                                   title='cutrenet',
                                   subtitle=tool.name)
        elif 'delete' in request.args:
            delete = request.args.get('delete')
            tool = db_session.query(Tool).filter_by(name=delete).first()
            db_session.delete(tool)
            db_session.commit()
            flash(u'Herramienta eliminada', 'success')
            return redirect('/tools', code=302)
        else:
            flash(u'Tienes que seleccionar una herramienta', 'error')
            return redirect('/tools', code=302)

    if request.method == 'POST' and current_user.has_role('admin'):
        if 'edit' in request.args:
            ename = request.args.get('edit')
            tool = db_session.query(Tool).filter_by(name=ename).first()
            form = ToolForm(self_edit=ename)
            if form.validate_on_submit():
                tool.name = request.form['name']
                tool.description = request.form['description']
                tool.location = request.form['location']
                tool.manual = request.form['manual']
                tool.documentation = request.form['documentation']

                maintainer = db_session.query(User).filter_by(
                    dni=request.form['maintainer']).first()
                if maintainer is not None:
                    maintainer.tool_maintainer.append(tool)

                if form.image.data:
                    if tool.image is not None:
                        os.remove(tool.image)  # Delete old image
                    f = form.image.data
                    filename = secure_filename(f.filename)
                    directory = app.config['UPLOAD_FOLDER'] + '/tools'
                    if not os.path.exists(directory):
                        os.makedirs(directory)
                    file_path = os.path.join(directory, filename)
                    f.save(file_path)
                    tool.image = file_path  # Save the file path of the Tool image in the database

                db_session.commit()
                flash(u'Herramienta editada', 'success')
            return render_template('tool.html',
                                   form=form,
                                   result=tool,
                                   title='cutrenet',
                                   subtitle=tool.name)
        elif 'add' in request.args:
            tool = Tool()
            form = ToolForm()
            if form.validate_on_submit():
                tool.name = request.form['name']
                tool.description = request.form['description']
                tool.location = request.form['location']
                tool.manual = request.form['manual']
                tool.documentation = request.form['documentation']

                maintainer = db_session.query(User).filter_by(
                    dni=request.form['maintainer']).first()
                if maintainer is not None:
                    maintainer.tool_maintainer.append(tool)

                if form.image.data:
                    if tool.image is not None:
                        os.remove(tool.image)  # Delete old image
                    f = form.image.data
                    filename = secure_filename(f.filename)
                    directory = app.config['UPLOAD_FOLDER'] + '/tools'
                    if not os.path.exists(directory):
                        os.makedirs(directory)
                    file_path = os.path.join(directory, filename)
                    f.save(file_path)
                    tool.image = file_path  # Save the file path of the Tool image in the database

                db_session.add(tool)
                db_session.commit()
                flash(u'Herramienta añadida', 'success')
                return redirect('tools', code=302)
            return render_template('tool.html',
                                   form=form,
                                   title='cutrenet',
                                   subtitle="new tool")