<commit_before>from appcomposer import db
from appcomposer.appstorage.api import get_app_by_name, set_var
from appcomposer.composers.translate import CFG_SAME_NAME_LIMIT
from appcomposer.models import AppVar, App

"""
REMARKS ABOUT APPVARS FOR THE TRANSLATOR:

A lownership AppVar identifies that the owner of a Spec/Language combination. It is attached
to the App that owns it, and has "lownership" as its AppVar name, and the language code (ex:
ca_ES) as its value.

As of now, we use the term "lowner" rather than "owner" because the system is being redesigned and
major changes are required.
"""


def _db_get_lowner_app(spec, lang_code):
    """
    Gets from the database the App that is considered the Owner for a given spec and language.
    @param spec: String to the App's original XML.
    @param lang_code: The language code. It should actually be a partial, lang_territory code. Only the
    language without the territory is NOT enough.
    @return: The owner for the App and language. None if no owner is found.
    """
    related_apps_ids = db.session.query(AppVar.app_id).filter_by(name="spec",
                                                                 value=spec).subquery()

    # Among those AppVars for our Spec, we try to locate a lownership AppVar for our
    # lang code.
    owner_app_id = db.session.query(AppVar.app_id).filter(AppVar.name == "lownership",
                                                          AppVar.value == lang_code,
                                                          AppVar.app_id.in_(related_apps_ids)).first()

    if owner_app_id is None:
        return None

    owner_app = App.query.filter_by(id=owner_app_id[0]).first()
    return owner_app


def _db_declare_lownership(owner_app, lang_code):
    """
    Declares lownership over a given Spec and Langcode. The CALLER is responsible of ensuring
    that no other owner for that spec and lang code exists before invoking this method.

    @param owner_app: Owner App for the language.
    @param lang_code: Language code to own.
    @return: None.
    """
    set_var(owner_app, "lownership", lang_code)


def _db_get_owner_app(spec):
    """
    Gets from the database the App that is considered the Owner for a given spec.
    @param spec: String to the App's original XML.
    @return: The owner for the App. None if no owner is found.
    """
    related_apps_ids = db.session.query(AppVar.app_id).filter_by(name="spec",
                                                                 value=spec).subquery()
    owner_app_id = db.session.query(AppVar.app_id).filter(AppVar.name == "ownership",
                                                          AppVar.app_id.in_(related_apps_ids)).first()

    if owner_app_id is None:
        return None
<commit_msg>[translate]: Fixed major mini-bug in _db_declare_ownership<commit_after>from appcomposer import db
from appcomposer.appstorage.api import get_app_by_name, add_var
from appcomposer.composers.translate import CFG_SAME_NAME_LIMIT
from appcomposer.models import AppVar, App

"""
REMARKS ABOUT APPVARS FOR THE TRANSLATOR:

A lownership AppVar identifies that the owner of a Spec/Language combination. It is attached
to the App that owns it, and has "lownership" as its AppVar name, and the language code (ex:
ca_ES) as its value.

As of now, we use the term "lowner" rather than "owner" because the system is being redesigned and
major changes are required.
"""


def _db_get_lowner_app(spec, lang_code):
    """
    Gets from the database the App that is considered the Owner for a given spec and language.
    @param spec: String to the App's original XML.
    @param lang_code: The language code. It should actually be a partial, lang_territory code. Only the
    language without the territory is NOT enough.
    @return: The owner for the App and language. None if no owner is found.
    """
    related_apps_ids = db.session.query(AppVar.app_id).filter_by(name="spec",
                                                                 value=spec).subquery()

    # Among those AppVars for our Spec, we try to locate a lownership AppVar for our
    # lang code.
    owner_app_id = db.session.query(AppVar.app_id).filter(AppVar.name == "lownership",
                                                          AppVar.value == lang_code,
                                                          AppVar.app_id.in_(related_apps_ids)).first()

    if owner_app_id is None:
        return None

    owner_app = App.query.filter_by(id=owner_app_id[0]).first()
    return owner_app


def _db_declare_lownership(owner_app, lang_code):
    """
    Declares lownership over a given Spec and Langcode. The CALLER is responsible of ensuring
    that no other owner for that spec and lang code exists before invoking this method.

    @param owner_app: Owner App for the language.
    @param lang_code: Language code to own.
    @return: None.
    """
    add_var(owner_app, "lownership", lang_code)


def _db_get_owner_app(spec):
    """
    Gets from the database the App that is considered the Owner for a given spec.
    @param spec: String to the App's original XML.
    @return: The owner for the App. None if no owner is found.
    """
    related_apps_ids = db.session.query(AppVar.app_id).filter_by(name="spec",
                                                                 value=spec).subquery()
    owner_app_id = db.session.query(AppVar.app_id).filter(AppVar.name == "ownership",
                                                          AppVar.app_id.in_(related_apps_ids)).first()

    if owner_app_id is None:
        return None

{'fraction_non_alphanumeric': 0.07261078483715963, 'fraction_numerical': 0.0003559352197899982, 'mean_word_length': 2.4542102028272894, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2358061', 'n_tokens_mistral': 1588, 'n_tokens_neox': 1544, 'n_words': 569}