# Path: pyp2rpm/package_data.py
# class PackageData(object):
#     credit_line = '# Created by pyp2rpm-{0}'.format(version.version)
# 
#     """A simple object that carries data about a package."""
# 
#     def __init__(self, local_file, name, pkg_name, version,
#                  md5='', source0='', srcname=None):
#         object.__setattr__(self, 'data', {})
#         self.data['local_file'] = local_file
#         self.data['name'] = name
#         self.data['srcname'] = srcname
#         self.data['pkg_name'] = pkg_name
#         self.data['version'] = version
#         self.data['python_versions'] = []
#         self.data['md5'] = md5
#         self.data['source0'] = source0
#         self.data['sphinx_dir'] = None
# 
#     def __getattr__(self, name):
#         if name == 'underscored_name':
#             return self.data['name'].replace('-', '_')
#         elif name == 'changelog_date_packager':
#             return self.get_changelog_date_packager()
#         elif name in ['runtime_deps', 'build_deps', 'classifiers',
#                       'doc_files', 'doc_license']:
#             return self.data.get(name, [])
#         elif name in ['packages', 'py_modules', 'scripts']:
#             return self.data.get(name, [])
#         elif name in ['has_egg_info', 'has_test_suite',
#                       'has_pth', 'has_extension']:
#             return self.data.get(name, False)
#         elif name == 'sorted_python_versions':
#             return sorted([self.data.get('base_python_version')] +
#                           self.data.get('python_versions', []))
#         return self.data.get(name, 'TODO:')
# 
#     def __setattr__(self, name, value):
#         if name == 'summary' and isinstance(value, utils.str_classes):
#             value = value.rstrip('.').replace('\n', ' ')
#         if value is not None:
#             self.data[name] = value
# 
#     def update_attr(self, name, value):
#         if name in self.data and value:
#             if name in ['runtime_deps', 'build_deps']:
#                 for item in value:
#                     if not item[1].lower() in get_deps_names(self.data[name]):
#                         self.data[name].append(item)
#             elif isinstance(self.data[name], list):
#                 for item in value:
#                     if item not in self.data[name]:
#                         self.data[name].append(item)
#             elif isinstance(self.data[name], set):
#                 if not isinstance(value, set):
#                     value = set(value)
#                 self.data[name] |= value
#             elif not self.data[name] and self.data[name] is not False:
#                 self.data[name] = value
#         elif name not in self.data and value is not None:
#             self.data[name] = value
# 
#     def set_from(self, data_dict, update=False):
#         for k, v in data_dict.items():
#             if update:
#                 self.update_attr(k, v)
#             else:
#                 setattr(self, k, v)
# 
#     def get_changelog_date_packager(self):
#         """Returns part of the changelog entry, containing date and packager.
#         """
#         try:
#             packager = subprocess.Popen(
#                 'rpmdev-packager', stdout=subprocess.PIPE).communicate(
#                 )[0].strip()
#         except OSError:
#             # Hi John Doe, you should install rpmdevtools
#             packager = "John Doe <john@doe.com>"
#             logger.warn("Package rpmdevtools is missing, using default "
#                         "name: {0}.".format(packager))
#         with utils.c_time_locale():
#             date_str = time.strftime('%a %b %d %Y', time.gmtime())
#         encoding = locale.getpreferredencoding()
#         return u'{0} {1}'.format(date_str, packager.decode(encoding))


import pytest
from pyp2rpm.package_data import PackageData

class TestPackageData(object):

    @pytest.mark.parametrize(('s', 'expected'), [
        ('Spam.', 'Spam'),
        ('Spam', 'Spam'),
    ])
    def test_summary_with_dot(self, s, expected):
        pd = PackageData('spam', 'spam', 'python-spam', 'spam')
        pd.summary = s
        assert pd.summary == expected

    @pytest.mark.parametrize('name', [
        'summary', 'description', ])
    def test_set_none_value(self, name):
        pd = PackageData('spam', 'spam', 'python-spam', 'spam')
        setattr(pd, name, None)
        actual = getattr(pd, name)
        assert actual == 'TODO:'

    def test_get_nonexistent_attribute(self):
        pd = PackageData('spam', 'spam', 'python-spam', 'spam')
        assert pd.eggs == 'TODO:'

    @pytest.mark.parametrize(('n', 'expected'), [
        ('py-spam', 'py_spam'),
        ('py_spam', 'py_spam'),
        ('spam', 'spam'),
    ])
    def test_underscored_name(self, n, expected):

# Path: pyp2rpm/package_data.py
# class PackageData(object):
#     credit_line = '# Created by pyp2rpm-{0}'.format(version.version)
# 
#     """A simple object that carries data about a package."""
# 
#     def __init__(self, local_file, name, pkg_name, version,
#                  md5='', source0='', srcname=None):
#         object.__setattr__(self, 'data', {})
#         self.data['local_file'] = local_file
#         self.data['name'] = name
#         self.data['srcname'] = srcname
#         self.data['pkg_name'] = pkg_name
#         self.data['version'] = version
#         self.data['python_versions'] = []
#         self.data['md5'] = md5
#         self.data['source0'] = source0
#         self.data['sphinx_dir'] = None
# 
#     def __getattr__(self, name):
#         if name == 'underscored_name':
#             return self.data['name'].replace('-', '_')
#         elif name == 'changelog_date_packager':
#             return self.get_changelog_date_packager()
#         elif name in ['runtime_deps', 'build_deps', 'classifiers',
#                       'doc_files', 'doc_license']:
#             return self.data.get(name, [])
#         elif name in ['packages', 'py_modules', 'scripts']:
#             return self.data.get(name, [])
#         elif name in ['has_egg_info', 'has_test_suite',
#                       'has_pth', 'has_extension']:
#             return self.data.get(name, False)
#         elif name == 'sorted_python_versions':
#             return sorted([self.data.get('base_python_version')] +
#                           self.data.get('python_versions', []))
#         return self.data.get(name, 'TODO:')
# 
#     def __setattr__(self, name, value):
#         if name == 'summary' and isinstance(value, utils.str_classes):
#             value = value.rstrip('.').replace('\n', ' ')
#         if value is not None:
#             self.data[name] = value
# 
#     def update_attr(self, name, value):
#         if name in self.data and value:
#             if name in ['runtime_deps', 'build_deps']:
#                 for item in value:
#                     if not item[1].lower() in get_deps_names(self.data[name]):
#                         self.data[name].append(item)
#             elif isinstance(self.data[name], list):
#                 for item in value:
#                     if item not in self.data[name]:
#                         self.data[name].append(item)
#             elif isinstance(self.data[name], set):
#                 if not isinstance(value, set):
#                     value = set(value)
#                 self.data[name] |= value
#             elif not self.data[name] and self.data[name] is not False:
#                 self.data[name] = value
#         elif name not in self.data and value is not None:
#             self.data[name] = value
# 
#     def set_from(self, data_dict, update=False):
#         for k, v in data_dict.items():
#             if update:
#                 self.update_attr(k, v)
#             else:
#                 setattr(self, k, v)
# 
#     def get_changelog_date_packager(self):
#         """Returns part of the changelog entry, containing date and packager.
#         """
#         try:
#             packager = subprocess.Popen(
#                 'rpmdev-packager', stdout=subprocess.PIPE).communicate(
#                 )[0].strip()
#         except OSError:
#             # Hi John Doe, you should install rpmdevtools
#             packager = "John Doe <john@doe.com>"
#             logger.warn("Package rpmdevtools is missing, using default "
#                         "name: {0}.".format(packager))
#         with utils.c_time_locale():
#             date_str = time.strftime('%a %b %d %Y', time.gmtime())
#         encoding = locale.getpreferredencoding()
#         return u'{0} {1}'.format(date_str, packager.decode(encoding))


# Path: tests/test_package_data.py
import pytest
from pyp2rpm.package_data import PackageData


class TestPackageData(object):

    @pytest.mark.parametrize(('s', 'expected'), [
        ('Spam.', 'Spam'),
        ('Spam', 'Spam'),
    ])
    def test_summary_with_dot(self, s, expected):
        pd = PackageData('spam', 'spam', 'python-spam', 'spam')
        pd.summary = s
        assert pd.summary == expected

    @pytest.mark.parametrize('name', [
        'summary', 'description', ])
    def test_set_none_value(self, name):
        pd = PackageData('spam', 'spam', 'python-spam', 'spam')
        setattr(pd, name, None)
        actual = getattr(pd, name)
        assert actual == 'TODO:'

    def test_get_nonexistent_attribute(self):
        pd = PackageData('spam', 'spam', 'python-spam', 'spam')
        assert pd.eggs == 'TODO:'

    @pytest.mark.parametrize(('n', 'expected'), [
        ('py-spam', 'py_spam'),
        ('py_spam', 'py_spam'),
        ('spam', 'spam'),
    ])
    def test_underscored_name(self, n, expected):

        pd = PackageData('spam', n, 'python-spam', 'spam')