# Path: error.py
# def is_valid_HTTP_code(code):
# def get_HTTP_err(code):
#   def __init__(self,
#                msg,
#                http_code=400,
#                app_code=DEF_ME_CODE,
#                inner_exc=None,
#                **kwds):
#   def gen_msgs(self, **kwds):
#   def get_err_dict(self):
#   def __str__(self):
#   def __init__(self,
#                msg,
#                http_code=500,
#                app_code=ParameterizedError.DEF_ME_CODE,
#                inner_exc=None,
#                **kwds):
#   def __init__(self, msg=None, *args, **kwds):
# class ParameterizedError(Exception):
# class NetworkAddressError(ParameterizedError):
# class ContentLoadError(ParameterizedError):
# class TypeVerifyError(ParameterizedError):
# class EmailError(ParameterizedError):
# class SubscriptionError(ParameterizedError):
# class MSSError(ParameterizedError):
# class UserLookupError(ParameterizedError):
# class UserAuthError(ParameterizedError):
# class BlobError(ParameterizedError):
# class BLOBClientError(ParameterizedError):
# class RelevanceError(ParameterizedError):
# class TextSearchError(ParameterizedError):
# class AutocompleteError(ParameterizedError):
# class EmptyResult(ParameterizedError):
# class GraphConnectionError(ParameterizedError):
# class FormattingError(ParameterizedError):
# class SessionError(ParameterizedError):
# class ConfigError(ParameterizedError):
# class SanitizationError(ParameterizedError):
# class BlurbError(ParameterizedError):
# class DomainOperationError(ParameterizedError):
# class GenericRuntimeError(ParameterizedError):
# class OAuthDisabledError(ParameterizedError):
# class RecaptchaError(ParameterizedError):
# class ReadOnlyDatabaseError(ParameterizedError):
#   DEF_PFX = '/api/status/error'
#   DEF_ME_CODE = '/unknown/unknown'


import string, re
import bisect
import json
import zlib
from collections import OrderedDict
from absl import logging as glogging
from pymql.log import LOG, log_util
from error import MQLInternalParseError, MQLParseError
from base64 import urlsafe_b64encode, urlsafe_b64decode
from difflib import unified_diff
# now instanitate so that we get __eq__ and such
ReadMode = ReadMode()
WriteMode = WriteMode()
PrepareMode = PrepareMode()
CheckMode = CheckMode()


def encode_cursor(cursor):
  """
    dumbed down for now.. eventually we'd like to be smarter since
    cursors should always be ascii. Also, we need to sign cursors
    """
  if isinstance(cursor, basestring):
    return urlsafe_b64encode(zlib.compress(cursor, 9))
  return cursor


def decode_cursor(encoded_cursor):
  if isinstance(encoded_cursor, basestring):
    try:
      return zlib.decompress(urlsafe_b64decode(str(encoded_cursor)))
    except TypeError, e:
      # not base64
      raise MQLParseError(None, "Invalid cursor", cursor=encoded_cursor)
    except zlib.error:
      # base64, not zlib compressed.
      raise MQLParseError(None, "Invalid cursor", cursor=encoded_cursor)
  elif isinstance(encoded_cursor, bool):
    return encoded_cursor
  else:

# Path: error.py
# def is_valid_HTTP_code(code):
# def get_HTTP_err(code):
#   def __init__(self,
#                msg,
#                http_code=400,
#                app_code=DEF_ME_CODE,
#                inner_exc=None,
#                **kwds):
#   def gen_msgs(self, **kwds):
#   def get_err_dict(self):
#   def __str__(self):
#   def __init__(self,
#                msg,
#                http_code=500,
#                app_code=ParameterizedError.DEF_ME_CODE,
#                inner_exc=None,
#                **kwds):
#   def __init__(self, msg=None, *args, **kwds):
# class ParameterizedError(Exception):
# class NetworkAddressError(ParameterizedError):
# class ContentLoadError(ParameterizedError):
# class TypeVerifyError(ParameterizedError):
# class EmailError(ParameterizedError):
# class SubscriptionError(ParameterizedError):
# class MSSError(ParameterizedError):
# class UserLookupError(ParameterizedError):
# class UserAuthError(ParameterizedError):
# class BlobError(ParameterizedError):
# class BLOBClientError(ParameterizedError):
# class RelevanceError(ParameterizedError):
# class TextSearchError(ParameterizedError):
# class AutocompleteError(ParameterizedError):
# class EmptyResult(ParameterizedError):
# class GraphConnectionError(ParameterizedError):
# class FormattingError(ParameterizedError):
# class SessionError(ParameterizedError):
# class ConfigError(ParameterizedError):
# class SanitizationError(ParameterizedError):
# class BlurbError(ParameterizedError):
# class DomainOperationError(ParameterizedError):
# class GenericRuntimeError(ParameterizedError):
# class OAuthDisabledError(ParameterizedError):
# class RecaptchaError(ParameterizedError):
# class ReadOnlyDatabaseError(ParameterizedError):
#   DEF_PFX = '/api/status/error'
#   DEF_ME_CODE = '/unknown/unknown'


# Path: mql/utils.py
import string, re
import bisect
import json
import zlib
from collections import OrderedDict
from absl import logging as glogging
from pymql.log import LOG, log_util
from error import MQLInternalParseError, MQLParseError
from base64 import urlsafe_b64encode, urlsafe_b64decode
from difflib import unified_diff

# now instanitate so that we get __eq__ and such
ReadMode = ReadMode()
WriteMode = WriteMode()
PrepareMode = PrepareMode()
CheckMode = CheckMode()


def encode_cursor(cursor):
  """
    dumbed down for now.. eventually we'd like to be smarter since
    cursors should always be ascii. Also, we need to sign cursors
    """
  if isinstance(cursor, basestring):
    return urlsafe_b64encode(zlib.compress(cursor, 9))
  return cursor


def decode_cursor(encoded_cursor):
  if isinstance(encoded_cursor, basestring):
    try:
      return zlib.decompress(urlsafe_b64decode(str(encoded_cursor)))
    except TypeError, e:
      # not base64
      raise MQLParseError(None, "Invalid cursor", cursor=encoded_cursor)
    except zlib.error:
      # base64, not zlib compressed.
      raise MQLParseError(None, "Invalid cursor", cursor=encoded_cursor)
  elif isinstance(encoded_cursor, bool):
    return encoded_cursor
  else:

    raise MQLParseError(