<commit_before>#!/usr/bin/env python
"""Module for extracting a project's UppNex id from a spreadsheet on Google Docs"""

import bcbio.google
import bcbio.google.spreadsheet
import os 


class ProjectMetaData:
    """A placeholder for metadata associated with a project.
    The data are fetched from the Google Docs spreadsheet.
    """

    def __init__(self, project_name, config):
        """Initialize the object"""

        col_mapping = self.column_mapping()
        name_column = col_mapping["project_name"]
        columns = []
        attributes = []
        for attr, col in col_mapping.items():
            columns.append(col)
            attributes.append(attr)
            setattr(self, attr, None)

        # Get the credentials
        encoded_credentials = bcbio.google.get_credentials(config)
        assert encoded_credentials is not None, \
        "The Google Docs credentials could not be found."

        # Get the name of the spreadsheet where uppnex ids can be found
        gdocs_config = config.get("gdocs_upload", {})
        ssheet_title = gdocs_config.get("projects_spreadsheet", None)
        wsheet_title = gdocs_config.get("projects_worksheet", None)

        assert ssheet_title is not None and wsheet_title is not None, \
            "The names of the projects spreadsheet and worksheet on Google \
            Docs could not be found."

        # Connect to the spread- and worksheet
        client = bcbio.google.spreadsheet.get_client(encoded_credentials)
        ssheet = bcbio.google.spreadsheet.get_spreadsheet(client, ssheet_title)

        assert ssheet is not None, \
            "Could not fetch %s from Google Docs." % ssheet_title

        # We allow multiple, comma-separated worksheets to be searched
        for wtitle in wsheet_title.split(','):
            wsheet = bcbio.google.spreadsheet.get_worksheet(client, ssheet, wtitle.strip())
            if not wsheet:
                logger2.warning("Could not locate %s in %s." % (wsheet_title, ssheet_title))
                continue

            # Get the rows for the project
            rows = bcbio.google.spreadsheet.get_rows_columns_with_constraint( \
                client, ssheet, wsheet, columns, {name_column: project_name})
            if len(rows) == 0:
                continue

            # Will only use the first result found to set each attribute
            for i, val in enumerate(rows[0]):
                setattr(self, attributes[i], val)

            # We have found the project data so stop iterating
            break

    @staticmethod
    def column_mapping():
        """Returns a dictionary with mapping between class attributes
        and the corresponding Google docs spreadsheet column name<commit_msg>HOTFIX: fixed dependency on bcbb logger object<commit_after>#!/usr/bin/env python
"""Module for extracting a project's UppNex id from a spreadsheet on Google Docs"""

import bcbio.google
import bcbio.google.spreadsheet
import os 


class ProjectMetaData:
    """A placeholder for metadata associated with a project.
    The data are fetched from the Google Docs spreadsheet.
    """

    def __init__(self, project_name, config):
        """Initialize the object"""

        col_mapping = self.column_mapping()
        name_column = col_mapping["project_name"]
        columns = []
        attributes = []
        for attr, col in col_mapping.items():
            columns.append(col)
            attributes.append(attr)
            setattr(self, attr, None)

        # Get the credentials
        encoded_credentials = bcbio.google.get_credentials(config)
        assert encoded_credentials is not None, \
        "The Google Docs credentials could not be found."

        # Get the name of the spreadsheet where uppnex ids can be found
        gdocs_config = config.get("gdocs_upload", {})
        ssheet_title = gdocs_config.get("projects_spreadsheet", None)
        wsheet_title = gdocs_config.get("projects_worksheet", None)

        assert ssheet_title is not None and wsheet_title is not None, \
            "The names of the projects spreadsheet and worksheet on Google \
            Docs could not be found."

        # Connect to the spread- and worksheet
        client = bcbio.google.spreadsheet.get_client(encoded_credentials)
        ssheet = bcbio.google.spreadsheet.get_spreadsheet(client, ssheet_title)

        assert ssheet is not None, \
            "Could not fetch %s from Google Docs." % ssheet_title

        # We allow multiple, comma-separated worksheets to be searched
        for wtitle in wsheet_title.split(','):
            wsheet = bcbio.google.spreadsheet.get_worksheet(client, ssheet, wtitle.strip())
            if not wsheet:
                print("WARNING: Could not locate {} in {}".format(wsheet_title, ssheet_title))
                continue

            # Get the rows for the project
            rows = bcbio.google.spreadsheet.get_rows_columns_with_constraint( \
                client, ssheet, wsheet, columns, {name_column: project_name})
            if len(rows) == 0:
                continue

            # Will only use the first result found to set each attribute
            for i, val in enumerate(rows[0]):
                setattr(self, attributes[i], val)

            # We have found the project data so stop iterating
            break

    @staticmethod
    def column_mapping():
        """Returns a dictionary with mapping between class attributes
        and the corresponding Google docs spreadsheet column name
{'fraction_non_alphanumeric': 0.07517704739422554, 'fraction_numerical': 0.0009079353550027238, 'mean_word_length': 2.4275046670815184, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7134247', 'n_tokens_mistral': 1490, 'n_tokens_neox': 1413, 'n_words': 492}