(service, username, labels, labelname)
Get label ID if it already exists, otherwise create it.
Get label ID if it already exists, otherwise create it.
Get label ID if it already exists otherwise create it .
def get_label_id_from_name(service, username, labels, labelname):
  """Get label ID if it already exists, otherwise create it."""
  if labelname.endswith('.mbox'):
    # Strip .mbox suffix from folder names
    labelname = labelname[:-5]
  for label in labels:
    if label['name'].upper() == labelname.upper():
      return label['id']

  logging.info("Label '%s' doesn't exist, creating it", labelname)
  try:
    label_object = {
        'messageListVisibility': 'show',
        'name': labelname,
        'labelListVisibility': 'labelShow'
    }
    label = service.users().labels().create(
        userId=username,
        body=label_object).execute(num_retries=args.num_retries)
    logging.info("Label '%s' created", labelname)
    labels.append(label)
    return label['id']
  except Exception:
    logging.exception("Can't create label '%s' for user %s", labelname, username)
    raise
def get_label_id_from_name ( service , username , labels , labelname ) : if labelname . endswith ( '.mbox' ) : # Strip .mbox suffix from folder names labelname = labelname [ : - 5 ] for label in labels : if label [ 'name' ] . upper ( ) == labelname . upper ( ) : return label [ 'id' ] logging . info ( "Label '%s' doesn't exist, creating it" , labelname ) try : label_object = { 'messageListVisibility' : 'show' , 'name' : labelname , 'labelListVisibility' : 'labelShow' } label = service . users ( ) . labels ( ) . create ( userId = username , body = label_object ) . execute ( num_retries = args . num_retries ) logging . info ( "Label '%s' created" , labelname ) labels . append ( label ) return label [ 'id' ] except Exception : logging . exception ( "Can't create label '%s' for user %s" , labelname , username ) raise