###########################################################################
#
#  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.
#
###########################################################################


from starthinker.util.bigquery import datasets_access
from starthinker.util.bigquery import datasets_create
from starthinker.util.bigquery import datasets_delete
from starthinker.util.project import project


@project.from_parameters
def dataset():
  if project.verbose:
    print('DATASET', project.id, project.task['dataset'])

  if project.task.get('clear', False):
    if project.verbose:
      print('DATASET CLEAR')
    datasets_delete(
        project.task['auth'],
        project.id,
        project.task['dataset'],
        delete_contents=True
    )

  if project.task.get('delete', False):
    if project.verbose:
      print('DATASET DELETE')
    # In order to fully delete a dataset, it needs to first have all tables
    # deleted, which is done with the delete_contents=True, and then the actual
    # dataset can be deleted, which is done with delete_contents=false.
    datasets_delete(
        project.task['auth'],
        project.id,
        project.task['dataset'],
        delete_contents=True
    )
    datasets_delete(
        project.task['auth'],
        project.id,
        project.task['dataset'],
        delete_contents=False
    )

  if project.verbose:
    print('DATASET CREATE')
  datasets_create(
    project.task['auth'],
    project.id,
    project.task['dataset']
  )

  if project.verbose:
    print('DATASET ACCESS')
  datasets_access(
    project.task['auth'],
    project.id,
    project.task['dataset'],
    emails=project.task.get('emails', []),
    groups=project.task.get('groups', [])
  )


if __name__ == '__main__':
  dataset()

@@ -1070,240 +1070,8 @@
 %5D)%0A%0A
-  if project.task.get('clear', False):%0A    if project.verbose:%0A      print('DATASET CLEAR')%0A    datasets_delete(%0A        project.task%5B'auth'%5D,%0A        project.id,%0A        project.task%5B'dataset'%5D,%0A        delete_contents=True%0A    )%0A%0A
   if
@@ -1667,39 +1667,300 @@
 e%0A    )%0A
-%0A  if project.verbose:%0A
+  else:%0A    if project.task.get('clear', False):%0A      if project.verbose:%0A        print('DATASET CLEAR')%0A      datasets_delete(%0A          project.task%5B'auth'%5D,%0A          project.id,%0A          project.task%5B'dataset'%5D,%0A          delete_contents=True%0A      )%0A%0A    if project.verbose:%0A  
     prin
@@ -1979,16 +1979,18 @@
 REATE')%0A
+  
   datase
@@ -1996,24 +1996,28 @@
 ets_create(%0A
+    
     project.
@@ -2022,32 +2022,36 @@
 t.task%5B'auth'%5D,%0A
+    
     project.id,%0A
@@ -2042,32 +2042,36 @@
     project.id,%0A
+    
     project.task
@@ -2082,21 +2082,25 @@
 taset'%5D%0A
+  
   )%0A%0A
+  
   if pro
@@ -2105,32 +2105,34 @@
 roject.verbose:%0A
+  
     print('DATAS
@@ -2145,16 +2145,18 @@
 ESS')%0A  
+ 
+ 
 datasets
@@ -2164,24 +2164,28 @@
 access(%0A    
+   
+ 
 project.task
@@ -2190,24 +2190,28 @@
 sk%5B'auth'%5D,%0A
+    
     project.
@@ -2210,24 +2210,28 @@
 project.id,%0A
+    
     project.
@@ -2251,16 +2251,20 @@
 '%5D,%0A    
+ 
+   
 emails=p
@@ -2298,16 +2298,20 @@
 %5D),%0A    
+  
+  
 groups=p
@@ -2340,16 +2340,18 @@
 s', %5B%5D)%0A
+  
   )%0A%0A%0Aif

Fix control flow so that `delete` is executed as an alternative to the creation or clearing of a dataset.