<commit_before>/*
 * Copyright (C) 2012-2014 Red Hat, Inc.
 *
 * Licensed under the GNU Lesser General Public License Version 2.1
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "Python.h"
#include <vector>

#include "dnf-advisory.h"
#include "dnf-advisorypkg.h"
#include "dnf-advisoryref.h"
#include "hy-packageset.h"
#include "dnf-reldep.h"
#include "dnf-reldep-list.h"
#include "hy-iutil.h"
#include "hy-util-private.hpp"
#include "advisory-py.hpp"
#include "advisorypkg-py.hpp"
#include "advisoryref-py.hpp"
#include "iutil-py.hpp"
#include "package-py.hpp"
#include "query-py.hpp"
#include "reldep-py.hpp"
#include "sack-py.hpp"
#include "pycomp.hpp"
#include "sack/advisorypkg.hpp"
#include "sack/changelog.hpp"
#include "sack/packageset.hpp"
#include "sack/query.hpp"

#include "../../libdnf/repo/solvable/Dependency.hpp"
#include "libdnf/repo/solvable/DependencyContainer.hpp"

UniquePtrPyObject & UniquePtrPyObject::operator =(UniquePtrPyObject && src) noexcept
{
    if (this == &src)
        return *this;
    Py_XDECREF(pyObj);
    pyObj = src.pyObj;
    src.pyObj = NULL;
    return *this;
}

void UniquePtrPyObject::reset(PyObject * pyObj) noexcept
{
    Py_XDECREF(this->pyObj);
    this->pyObj = pyObj;
}

UniquePtrPyObject::~UniquePtrPyObject()
{
    Py_XDECREF(pyObj);
}

PyObject *
advisorylist_to_pylist(const GPtrArray *advisorylist, PyObject *sack)
{
    UniquePtrPyObject list(PyList_New(0));
    if (!list)
        return NULL;

    for (unsigned int i = 0; i < advisorylist->len; ++i) {
        auto cadvisory =
            static_cast<DnfAdvisory *>(g_ptr_array_index(advisorylist, i));
        UniquePtrPyObject advisory(advisoryToPyObject(cadvisory, sack));

        if (!advisory)
            return NULL;

        int rc = PyList_Append(list.get(), advisory.get());
        if (rc == -1)
            return NULL;
    }

    return list.release();
}

PyObject *
advisoryPkgVectorToPylist(const std::vector<libdnf::AdvisoryPkg> & advisorypkgs)
{
    UniquePtrPyObject list(PyList_New(0));
    if (!list)
        return NULL;

    for (auto& advisorypkg : advisorypkgs) {
        UniquePtrPyObject pyAdvisoryPkg(advisorypkgToPyObject(new libdnf::AdvisoryPkg(advisorypkg)));
        if (!pyAdvisoryPkg)
            return NULL;
        int rc = PyList_Append(list.get(), pyAdvisoryPkg.get());
        if (rc == -1)
            return NULL;
    }

    return list.release();
}

PyObject *
advisoryRefVectorToPylist(const std::vector<libdnf::AdvisoryRef> & advisoryRefs, PyObject *sack)
{
    UniquePtrPyObject list(PyList_New(0));
    if (!list)
        return NULL;

    for (auto& advisoryRef : advisoryRefs) {
        UniquePtrPyObject pyAdvisoryRef(advisoryrefToPyObject(new libdnf::AdvisoryRef(advisoryRef), sack));
        if (!pyAdvisoryRef)
            return NULL;
        int rc = PyList_Append(list.get(), pyAdvisoryRef.get());
        if (rc == -1)
            return NULL;
    }

    return list.release();
}

PyObject *
changelogslist_to_pylist(const GPtrArray *changelogslist)
{
    UniquePtrPyObject list(PyList_New(0));
    if (!list)
        return NULL;

    for (unsigned int i = 0; i < changelogslist->len; ++i) {
        auto citem = static_cast<libdnf::Changelog *>(g_ptr_array_index(changelogslist, i));
        PyObject *d = PyDict_New();
        if (!d)
            return NULL;
        if (PyDict_SetItemString(d, "author", PyUnicode_FromString(citem->author)) == -1)
            return NULL;
        if (PyDict_SetItemString(d, "text", PyUnicode_FromString(citem->text)) == -1)
            return NULL;
        if (PyDict_SetItemString(d, "timestamp", PyLong_FromUnsignedLongLong(citem->timestamp)) == -1)
            return NULL;
        if (PyList_Append(list.get(), d) == -1)
            return NULL;
    }

    return list.release();
}

PyObject *
packagelist_to_pylist(GPtrArray *plist, PyObject *sack)
{
    UniquePtrPyObject list(PyList_New(0));
    if (!list)
        return NULL;

    for (unsigned int i = 0; i < plist->len; i++) {
        auto cpkg = static_cast<DnfPackage *>(g_ptr_array_index(plist, i));
        UniquePtrPyObject package(new_package(sack, dnf_package_get_id(cpkg)));
        if (!package)
            return NULL;

        int rc = PyList_Append(list.get(), package.get());
        if (rc == -1)
            return NULL;
    }
    return list.release();
}<commit_msg>In changelog return directly datetime.date object as timestamp<commit_after>/*
 * Copyright (C) 2012-2014 Red Hat, Inc.
 *
 * Licensed under the GNU Lesser General Public License Version 2.1
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "Python.h"
#include <vector>
#include <ctime>
#include "datetime.h"

#include "dnf-advisory.h"
#include "dnf-advisorypkg.h"
#include "dnf-advisoryref.h"
#include "hy-packageset.h"
#include "dnf-reldep.h"
#include "dnf-reldep-list.h"
#include "hy-iutil.h"
#include "hy-util-private.hpp"
#include "advisory-py.hpp"
#include "advisorypkg-py.hpp"
#include "advisoryref-py.hpp"
#include "iutil-py.hpp"
#include "package-py.hpp"
#include "query-py.hpp"
#include "reldep-py.hpp"
#include "sack-py.hpp"
#include "pycomp.hpp"
#include "sack/advisorypkg.hpp"
#include "sack/changelog.hpp"
#include "sack/packageset.hpp"
#include "sack/query.hpp"

#include "../../libdnf/repo/solvable/Dependency.hpp"
#include "libdnf/repo/solvable/DependencyContainer.hpp"

UniquePtrPyObject & UniquePtrPyObject::operator =(UniquePtrPyObject && src) noexcept
{
    if (this == &src)
        return *this;
    Py_XDECREF(pyObj);
    pyObj = src.pyObj;
    src.pyObj = NULL;
    return *this;
}

void UniquePtrPyObject::reset(PyObject * pyObj) noexcept
{
    Py_XDECREF(this->pyObj);
    this->pyObj = pyObj;
}

UniquePtrPyObject::~UniquePtrPyObject()
{
    Py_XDECREF(pyObj);
}

PyObject *
advisorylist_to_pylist(const GPtrArray *advisorylist, PyObject *sack)
{
    UniquePtrPyObject list(PyList_New(0));
    if (!list)
        return NULL;

    for (unsigned int i = 0; i < advisorylist->len; ++i) {
        auto cadvisory =
            static_cast<DnfAdvisory *>(g_ptr_array_index(advisorylist, i));
        UniquePtrPyObject advisory(advisoryToPyObject(cadvisory, sack));

        if (!advisory)
            return NULL;

        int rc = PyList_Append(list.get(), advisory.get());
        if (rc == -1)
            return NULL;
    }

    return list.release();
}

PyObject *
advisoryPkgVectorToPylist(const std::vector<libdnf::AdvisoryPkg> & advisorypkgs)
{
    UniquePtrPyObject list(PyList_New(0));
    if (!list)
        return NULL;

    for (auto& advisorypkg : advisorypkgs) {
        UniquePtrPyObject pyAdvisoryPkg(advisorypkgToPyObject(new libdnf::AdvisoryPkg(advisorypkg)));
        if (!pyAdvisoryPkg)
            return NULL;
        int rc = PyList_Append(list.get(), pyAdvisoryPkg.get());
        if (rc == -1)
            return NULL;
    }

    return list.release();
}

PyObject *
advisoryRefVectorToPylist(const std::vector<libdnf::AdvisoryRef> & advisoryRefs, PyObject *sack)
{
    UniquePtrPyObject list(PyList_New(0));
    if (!list)
        return NULL;

    for (auto& advisoryRef : advisoryRefs) {
        UniquePtrPyObject pyAdvisoryRef(advisoryrefToPyObject(new libdnf::AdvisoryRef(advisoryRef), sack));
        if (!pyAdvisoryRef)
            return NULL;
        int rc = PyList_Append(list.get(), pyAdvisoryRef.get());
        if (rc == -1)
            return NULL;
    }

    return list.release();
}

PyObject *
changelogslist_to_pylist(const GPtrArray *changelogslist)
{
    UniquePtrPyObject list(PyList_New(0));
    if (!list)
        return NULL;
    PyDateTime_IMPORT;

    for (unsigned int i = 0; i < changelogslist->len; ++i) {
        auto citem = static_cast<libdnf::Changelog *>(g_ptr_array_index(changelogslist, i));
        PyObject *d = PyDict_New();
        if (!d)
            return NULL;
        if (PyDict_SetItemString(d, "author", PyUnicode_FromString(citem->author)) == -1)
            return NULL;
        if (PyDict_SetItemString(d, "text", PyUnicode_FromString(citem->text)) == -1)
            return NULL;
        struct tm ts;
        ts = *localtime(&citem->timestamp);
        if (PyDict_SetItemString(d, "timestamp", PyDate_FromDate(ts.tm_year+1900, ts.tm_mon+1, ts.tm_mday)) == -1)
            return NULL;
        if (PyList_Append(list.get(), d) == -1)
            return NULL;
    }

    return list.release();
}

PyObject *
packagelist_to_pylist(GPtrArray *plist, PyObject *sack)
{
    UniquePtrPyObject list(PyList_New(0));
    if (!list)
        return NULL;

    for (unsigned int i = 0; i < plist->len; i++) {
        auto cpkg = static_cast<DnfPackage *>(g_ptr_array_index(plist, i));
        UniquePtrPyObject package(new_package(sack, dnf_package_get_id(cpkg)));
        if (!package)
            return NULL;

        int rc = PyList_Append(list.get(), package.get());
        if (rc == -1)
            return NULL;
    }
    return list.release();
}
{'fraction_non_alphanumeric': 0.12197962154294031, 'fraction_numerical': 0.008054342552159146, 'mean_word_length': 3.241152263374486, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2730329', 'n_tokens_mistral': 3513, 'n_tokens_neox': 3408, 'n_words': 859}