<commit_before># Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

""" server configuration for the assetMG tool"""
import json
from flask import Flask, request, jsonify, render_template
from googleads import adwords
from google.ads.google_ads.client import GoogleAdsClient
import app.backend.setup as setup
from app.backend.mutate import mutate_ad
from app.backend import structure
from app.backend.upload_asset import upload
from app.backend.service import Service_Class
from app.backend.yt_upload import initialize_upload
from app.backend.error_handling import error_mapping
from app.backend.get_yt import get_all_yt_videos, test_yt_credentials
from app.backend.helpers import populate_adgroup_details
from googleapiclient.discovery import build
from pathlib import Path
import copy
import logging
import yaml
from google_auth_oauthlib.flow import InstalledAppFlow, Flow
import webbrowser
import threading
import os
import shutil
from werkzeug.utils import secure_filename
import string
from google.cloud import storage
from app.backend.setup import CONFIG_PATH, CONFIG_PATH_GS, PREFIX, CLOUD_VERSION, BASE_URL, BUCKET_NAME
from app.backend.setup import YT_CONFIG_FILE_PATH, YT_CONFIG_FILE_PATH_GS, CONFIG_FILE_PATH,CONFIG_FILE_PATH_GS

# from flask_cors import CORS
# app = Flask(__name__)
# CORS(app)

app = Flask(__name__, static_url_path='',
            static_folder='app/asset_browser/frontend/dist/frontend',
            template_folder='app/asset_browser/frontend/dist/frontend')



UPLOAD_FOLDER = Path(PREFIX + 'uploads')
UPLOAD_FOLDER.mkdir(parents=True, exist_ok=True)


ALLOWED_EXTENSIONS = {'txt','png', 'jpg', 'jpeg', 'zip','gif'}
ALLOWED_DIMENSIONS = [(200,200), (240,400), (250,250), (250,360),
                      (300,250), (336,280), (580,400), (120,600),
                      (160,600), (300,600), (300,1050), (468,60),
                      (728,90), (930,180), (970,90), (970,250),
                      (980,120), (300,50), (320,50), (320,100)]

app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

#Only used in local version
LOGS_PATH = Path('app/logs/server.log')

YT_CLIENT_SCOPES = ['https://www.googleapis.com/auth/youtube.upload']

asset_to_ag_json_path = Path(PREFIX + 'cache/asset_to_ag.json')
account_struct_json_path = Path(PREFIX + 'cache/account_struct.json')
Path(PREFIX + 'cache').mkdir(parents=True, exist_ok=True)

if CLOUD_VERSION:
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s:%(levelname)s:%(message)s')
else:
    logging.basicConfig(filename=LOGS_PATH,
                        level=logging.INFO,
                        format='%(asctime)s:%(levelname)s:%(message)s')


flow = None
ADWORDS_CLIENT = ''
GOOGLEADS_CLIENT = ''

def get_global_adwords_client():
    global ADWORDS_CLIENT
    if ADWORDS_CLIENT:
        return ADWORDS_CLIENT
    else:
        setup.set_api_configs()
        ADWORDS_CLIENT = adwords.AdWordsClient.LoadFromStorage(
            CONFIG_PATH / 'googleads.yaml')
        return ADWORDS_CLIENT

def get_global_googleads_client():
    global GOOGLEADS_CLIENT
    if GOOGLEADS_CLIENT:
        return GOOGLEADS_CLIENT
    else:
        setup.set_api_configs()
        GOOGLEADS_CLIENT = GoogleAdsClient.load_from_storage(
            CONFIG_PATH / 'google-ads.yaml')
        return GOOGLEADS_CLIENT

# check if config is valid. if yes, init clients and create struct
try:
    # Copy Google Storage file to tmp file
    setup.download_file_from_gcs(CONFIG_FILE_PATH_GS, CONFIG_FILE_PATH)
    with open(CONFIG_FILE_PATH, 'r') as f:
        config_file = yaml.load(f, Loader=yaml.FullLoader)
except FileNotFoundError:
    config_file = {'config_valid': 0}


if config_file['config_valid']:
    try:
        setup.download_file_from_gcs('account_struct.json', account_struct_json_path)
        setup.download_file_from_gcs('asset_to_ag.json', asset_to_ag_json_path)
        if CLOUD_VERSION and Path(account_struct_json_path).exists():
            print('Not creating the structure, since this is cloud')
        else:
            structure.create_mcc_struct(get_global_googleads_client(), account_struct_json_path, asset_to_ag_json_path)
    except Exception as e:
        logging.exception('Error when trying to create struct')
        Service_Class.reset_cid(get_global_adwords_client())


@app.route('/')
def upload_frontend():
    return render_template('index.html')


@app.route('/config/', methods=['GET'])
def get_configs():
    """return all config parameters"""
    try:
        setup.download_file_from_gcs(CONFIG_FILE_PATH_GS, CONFIG_FILE_PATH)<commit_msg>Fixing bug with initializing googleads.yaml in cloud version<commit_after># Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

""" server configuration for the assetMG tool"""
import json
from flask import Flask, request, jsonify, render_template
from googleads import adwords
from google.ads.google_ads.client import GoogleAdsClient
import app.backend.setup as setup
from app.backend.mutate import mutate_ad
from app.backend import structure
from app.backend.upload_asset import upload
from app.backend.service import Service_Class
from app.backend.yt_upload import initialize_upload
from app.backend.error_handling import error_mapping
from app.backend.get_yt import get_all_yt_videos, test_yt_credentials
from app.backend.helpers import populate_adgroup_details
from googleapiclient.discovery import build
from pathlib import Path
import copy
import logging
import yaml
from google_auth_oauthlib.flow import InstalledAppFlow, Flow
import webbrowser
import threading
import os
import shutil
from werkzeug.utils import secure_filename
import string
from google.cloud import storage
from app.backend.setup import CONFIG_PATH, CONFIG_PATH_GS, PREFIX, CLOUD_VERSION, BASE_URL, BUCKET_NAME
from app.backend.setup import YT_CONFIG_FILE_PATH, YT_CONFIG_FILE_PATH_GS, CONFIG_FILE_PATH,CONFIG_FILE_PATH_GS

# from flask_cors import CORS
# app = Flask(__name__)
# CORS(app)

app = Flask(__name__, static_url_path='',
            static_folder='app/asset_browser/frontend/dist/frontend',
            template_folder='app/asset_browser/frontend/dist/frontend')



UPLOAD_FOLDER = Path(PREFIX + 'uploads')
UPLOAD_FOLDER.mkdir(parents=True, exist_ok=True)


ALLOWED_EXTENSIONS = {'txt','png', 'jpg', 'jpeg', 'zip','gif'}
ALLOWED_DIMENSIONS = [(200,200), (240,400), (250,250), (250,360),
                      (300,250), (336,280), (580,400), (120,600),
                      (160,600), (300,600), (300,1050), (468,60),
                      (728,90), (930,180), (970,90), (970,250),
                      (980,120), (300,50), (320,50), (320,100)]

app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

#Only used in local version
LOGS_PATH = Path('app/logs/server.log')

YT_CLIENT_SCOPES = ['https://www.googleapis.com/auth/youtube.upload']

asset_to_ag_json_path = Path(PREFIX + 'cache/asset_to_ag.json')
account_struct_json_path = Path(PREFIX + 'cache/account_struct.json')
Path(PREFIX + 'cache').mkdir(parents=True, exist_ok=True)

if CLOUD_VERSION:
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s:%(levelname)s:%(message)s')
else:
    logging.basicConfig(filename=LOGS_PATH,
                        level=logging.INFO,
                        format='%(asctime)s:%(levelname)s:%(message)s')


flow = None
ADWORDS_CLIENT = ''
GOOGLEADS_CLIENT = ''

def get_global_adwords_client():
    global ADWORDS_CLIENT
    if ADWORDS_CLIENT:
        return ADWORDS_CLIENT
    else:
        setup.set_api_configs()
        ADWORDS_CLIENT = adwords.AdWordsClient.LoadFromStorage(
            CONFIG_PATH / 'googleads.yaml')
        return ADWORDS_CLIENT

def get_global_googleads_client():
    global GOOGLEADS_CLIENT
    if GOOGLEADS_CLIENT:
        return GOOGLEADS_CLIENT
    else:
        setup.set_api_configs()
        GOOGLEADS_CLIENT = GoogleAdsClient.load_from_storage(
            CONFIG_PATH / 'google-ads.yaml')
        return GOOGLEADS_CLIENT

# check if config is valid. if yes, init clients and create struct
try:
    # Copy Google Storage file to tmp file
    setup.download_file_from_gcs(CONFIG_FILE_PATH_GS, CONFIG_FILE_PATH)
    with open(CONFIG_FILE_PATH, 'r') as f:
        config_file = yaml.load(f, Loader=yaml.FullLoader)
except FileNotFoundError:
    config_file = {'config_valid': 0}


if config_file['config_valid']:
    try:
        setup.download_file_from_gcs('account_struct.json', account_struct_json_path)
        setup.download_file_from_gcs('asset_to_ag.json', asset_to_ag_json_path)
        if CLOUD_VERSION and Path(account_struct_json_path).exists():
            get_global_googleads_client() # to initialize googleads.yaml file
            logging.info('Skipping structure creation on startup, since this is the cloud version')
        else:
            structure.create_mcc_struct(get_global_googleads_client(), account_struct_json_path, asset_to_ag_json_path)
    except Exception as e:
        logging.exception('Error when trying to create struct')
        Service_Class.reset_cid(get_global_adwords_client())


@app.route('/')
def upload_frontend():
    return render_template('index.html')


@app.route('/config/', methods=['GET'])
def get_configs():
    """return all config parameters"""
    try:
        setup.download_file_from_gcs(CONFIG_FILE_PATH_GS, CONFIG_FILE_PATH)
{'fraction_non_alphanumeric': 0.09607577807848444, 'fraction_numerical': 0.02416392808815001, 'mean_word_length': 4.178678678678678, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2378304', 'n_tokens_mistral': 3408, 'n_tokens_neox': 3193, 'n_words': 788}