# Copyright 2018 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
#
#     http://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.

"""This script is used to synthesize generated parts of this library."""

import synthtool as s
import synthtool.gcp as gcp
import logging

logging.basicConfig(level=logging.DEBUG)

gapic = gcp.GAPICGenerator()
common = gcp.CommonTemplates()

for version in ['v2beta2', 'v2beta3']:
    library = gapic.py_library(
        'tasks', version,
        config_path=f'artman_cloudtasks_{version}.yaml')

    s.copy(library, excludes=['docs/conf.py', 'docs/index.rst'])

    # Fix unindentation of bullet list second line
    s.replace(
        f'google/cloud/tasks_{version}/gapic/cloud_tasks_client.py',
        '(        \* .*\n        )([^\s*])',
        '\g<1>  \g<2>')

    s.replace(
        f'google/cloud/tasks_{version}/gapic/cloud_tasks_client.py',
        '(Google IAM .*?_) ',
        '\g<1>_ ')

    s.replace(
        f'google/cloud/tasks_{version}/gapic/cloud_tasks_client.py',
        r'(Sample filter \\"app_engine_http_target: )\*\\".',
        '\g<1>\\*\\".')

    # Issues with Anonymous ('__') links. Change to named.
    s.replace(
        f"google/cloud/tasks_{version}/proto/*.py",
        ">`__",
        ">`_")

    # Wrapped link fails due ot space in link
    s.replace(
        f"google/cloud/tasks_{version}/proto/queue_pb2.py",
        '(uests in queue.yaml/xml) <\n\s+',
        '\g<1>\n          <')

# Set Release Status
release_status = 'Development Status :: 3 - Alpha'
s.replace('setup.py',
          '(release_status = )(.*)$',
          f"\\1'{release_status}'")

# Add Dependencies
s.replace('setup.py',
          'dependencies = \[\n*(^.*,\n)+',
          "\\g<0>    'grpc-google-iam-v1<0.12dev,>=0.11.4',\n")

# Fix the enable API link
s.replace(
    'README.rst',
    r'.. _Enable the Cloud Tasks API.:  https://cloud.google.com/tasks',
    '.. _Enable the Cloud Tasks API.:  https://console.cloud.google.com/apis/'
    'library/cloudtasks.googleapis.com')
@@ -1367,24 +1367,83 @@
 '%5Cg%3C1%3E_ ')%0A%0A
+    # Issues with Anonymous ('__') links. Change to named.%0A
     s.replac
@@ -1446,33 +1446,33 @@
 place(%0A        f
-'
+%22
 google/cloud/tas
@@ -1488,36 +1488,19 @@
 on%7D/
-gapic/cloud_tasks_client.py'
+proto/*.py%22
 ,%0A  
@@ -1509,150 +1509,51 @@
     
-r'(Sample filter %5C%5C%22app_engine_http_target: )%5C*%5C%5C%22.',%0A        '%5Cg%3C1%3E%5C%5C*%5C%5C%22.')%0A%0A    # Issues with Anonymous ('__') links. Change to named.%0A    
+%22%3E%60__%22,%0A        %22%3E%60_%22)%0A%0A# Issue in v2beta2%0A
 s.re
@@ -1555,38 +1555,34 @@
 %0As.replace(%0A    
-    f%22
+f'
 google/cloud/tas
@@ -1588,67 +1588,125 @@
 sks_
-%7Bversion%7D/proto/*.py%22
+v2beta2/gapic/cloud_tasks_client.py'
 ,%0A    
-    %22%3E%60__%22,%0A        %22%3E%60_%22)%0A%0A    
+r'(Sample filter %5C%5C%22app_engine_http_target: )%5C*%5C%5C%22.',%0A    '%5Cg%3C1%3E%5C%5C*%5C%5C%22.')%0A%0A
 # Wr
@@ -1726,18 +1726,18 @@
 ils due 
-o
 t
+o
  space i
@@ -1742,21 +1742,27 @@
  in link
-%0A    
+ (v2beta2)%0A
 s.replac
@@ -1760,36 +1760,32 @@
 %0As.replace(%0A    
-    
 f%22google/cloud/t
@@ -1785,33 +1785,31 @@
 cloud/tasks_
-%7Bversion%7D
+v2beta2
 /proto/queue
@@ -1814,28 +1814,24 @@
 ue_pb2.py%22,%0A
-    
     '(uests 
@@ -1854,28 +1854,24 @@
 l) %3C%5Cn%5Cs+',%0A
-    
     '%5Cg%3C1%3E%5Cn
@@ -2450,8 +2450,9 @@
 is.com')
+%0A

Update synth.py to remove unneeded replacements (#5882)