コード例 #1
0
 def wrapped(*args, **kwargs):
     try:
         start = time.time()
         r = f(*args, **kwargs)
     finally:
         logger().debug('Latency (%s): %.4fs' \
                            % (f.__name__, time.time() - start))
     return r
コード例 #2
0
ファイル: utils.py プロジェクト: fogcitymarathoner/rma
def create_filename(*args, **kwargs):
    """
    Generate filename based on hash of secret key and supplied params.

    Example usage:

        import datatime
        timestamp = datetime.datetime.utcnow().strftime('%Y%m%d.%H%M%S%Z')
        group_id = group_profile.id
        contact_email = group_profile.contact_email
        unique_filename = create_unique_filename(timestamp,
                          group_id, contact_email)

    * if args is empty list function will generate file name using
      timestamp and a random key (of length 'FILENAME_RANDOM_KEY_LEN')

    :param args: list of basestrings to use to construct file name
    :param kwargs: options
    :returns: sha1hashed file name
    """
    # extract options
    append_random_key = kwargs.get('append_random_key', False)
    random_key_len = kwargs.get('random_key_len', FILENAME_RANDOM_KEY_LEN)
    lowercase = kwargs.get('lowercase', False)
    fext = kwargs.get('fext', '')

    # construct parts for hashing
    parts = []

    def _add(x):
        if isinstance(x, (list, tuple)):
            for a in x:
                _add(a)
        else:
            try:
                assert x
                parts.append(str(x))
            except (AssertionError, TypeError):
                pass

    if not args:
        args = [datetime.datetime.utcnow().strftime('%Y%m%d.%H%M%S%Z'),
                random_key(random_key_len)]
    _add(_s('SECRET_KEY'))
    _add(args)
    if append_random_key:
        _add(random_key(random_key_len))
    filename = '%s%s' % (sha1hash('.'.join(parts)), fext)
    if lowercase:
        filename = filename.lower()
    logger().debug('create_filename - parts: %s' % repr(parts))
    logger().debug('create_filename - hashed filename: %s' % filename)
    return filename
コード例 #3
0
ファイル: utils.py プロジェクト: fogcitymarathoner/rma
 def _delete(url):
     e = 0
     if use_remote_storage:
         if not is_local_path(url):
             ok = delete_remote_media(url)
             if not ok:
                 e += 1
             logger().debug('deleted remote image - %s %s' % (ok, url))
     path = url_to_path(url)
     if os.path.isfile(path):
         os.remove(path)
         ok = os.path.isfile(path) == False
         if not ok:
             e += 1
         logger().debug('deleted image - %s %s' % (ok, path))
     return e
コード例 #4
0
ファイル: tests.py プロジェクト: shuiziliu/django-garage
    def _test_new_logger(self):
        """
        logger should create new logging object if DebugLogger is None.
        * verify function is calling ``create_log`` with the correct
          parameters to create a new logger.
        """
        from garage.logger import logger, DebugLogger

        self._msg("test", "logger", first=True)
        DebugLogger = None
        with patch("garage.logger.create_log") as mock_create_log:
            expected = "foo"
            mock_create_log.return_value = expected
            result = logger()
            args, kwargs = mock_create_log.call_args
            self.assertEqual(result, expected)
            self.assertTrue("logname" in kwargs)
            self.assertTrue("logfile" in kwargs)
            self.assertTrue("format" in kwargs)
            self.assertEqual(kwargs["logname"], "garage-test")
            self.assertEqual(kwargs["logfile"], "garage-test.log")
            self.assertEqual(kwargs["format"], "%(asctime)s - %(levelname)s - %(message)s")
            self._msg("result", result)
            self._msg("args", args)
            self._msg("kwargs", kwargs)
コード例 #5
0
def delete_cache(cache_key):
    """
    Delete cached object.
    """
    if not get_setting('USE_MINI_CACHE'):
        return False
    if cache.get(cache_key):
        cache.set(cache_key, None, 0)
        result = True
    else:
        result = False
    if get_setting('CACHE_DEBUG'):
        if result is True:
            logger().debug('delete_cache: Deleting cached data: %s' % cache_key)
        else:
            logger().debug('delete_cache: Unable to get cached data: %s' % cache_key)
    return result
コード例 #6
0
 def _cache_controller(*args, **kwargs):
     if not get_setting('USE_MINI_CACHE'):
         return f(*args, **kwargs)
     if isinstance(cache_key, basestring):
         k = cache_key % locals()
     elif callable(cache_key):
         k = cache_key(*args, **kwargs)
     result = cache.get(k)
     if result is None:
         result = f(*args, **kwargs)
         cache.set(k, result, timeout)
         if get_setting('CACHE_DEBUG'):
             logger().debug('Cached data: %s' % k)
     else:
         if get_setting('CACHE_DEBUG'):
             logger().debug('Return cached data: %s' % k)
     return result
コード例 #7
0
def save_to_remote_storage(path, storage=None):
    """
    Push a local file to remote media storage.

    :param path: local path of file
    :param storage: storage instance to use (default: default_storage)
    :returns: url of file on remote storage server
    """
    if not path or not os.path.isfile(path):
        logger().debug('save to remote storage - cannot find file: %s' % path)
        remote_path = path
    else:
        if not storage:
            storage = default_storage

        if storage == resources_storage:
            fkey = path.replace(RESOURCES_ROOT, '')
        else:
            fkey = path.replace(MEDIA_ROOT, '')

        # determine mime type
        fext = get_img_ext(path)
        content_type = MIME_TYPES.get(fext, UNKNOWN_MIME_TYPE)

        # write file to remote server
        file = storage.open(fkey, 'w')
        storage.headers.update({"Content-Type": content_type})
        f = open(path, 'rb')
        media = File(f)
        for chunk in media.chunks(chunk_size=FILE_IO_CHUNK_SIZE):
            file.write(chunk)
        file.close()
        media.close()
        f.close()

        # construct remote url
        if storage == resources_storage:
            remote_path = '%s%s' % (RESOURCES_URL, fkey)
        else:
            remote_path = '%s%s' % (MEDIA_URL, fkey)
        logger().debug(
            'save_to_remote_storage - local: %s / remote: %s / mime-type: %s' %
            (path, remote_path, content_type))

    return remote_path
コード例 #8
0
ファイル: tests.py プロジェクト: shuiziliu/django-garage
    def test_existing_logger(self):
        """
        logger should return existing loggng object if DebugLogger is
        not None.
        """
        from garage.logger import logger
        from garage.logger import logger, DebugLogger

        self._msg("test", "logger", first=True)
        with patch("garage.logger.DebugLogger") as mock_logger:
            expected = "foobar"
            mock_logger.debug.return_value = expected
            result = logger().debug("this is a test message")
            self.assertEqual(result, expected)
            self._msg("result", result)
コード例 #9
0
ファイル: utils.py プロジェクト: fogcitymarathoner/rma
def copy_media_to_remote(path, verify=True):
    """
    Verify local file path has remote version and copy to remote
    storage if remote does not exist.
    * if verify is True, function will skip copy if remote version
      exists.
    * NOTE: this is for files in the ``media`` directory.

    :param path: local file path
    :param verify: if True, verify and skip copy if remote exists
    :returns: remote server media url or None if error
    """
    from helpers.files.utils import (
        local_path_to_remote_media_url,
        remote_media_url_exists,
        save_to_remote_media_storage,
    )
    try:
        assert local_file_exists(path)
    except AssertionError:
        url = None
        logger().debug(
            'copy_media_to_remote - local file not found -'
            'local: %s' % path)
    else:
        copy_to_remote = True
        if verify:
            url = local_path_to_remote_media_url(path)
            remote_exists = remote_media_url_exists(url)
            if remote_exists:
                copy_to_remote = False
                logger().debug(
                    'copy_media_to_remote - remote exists - '
                    'local: %s / remote: %s' %
                    (path, url))
        if copy_to_remote:
            url = save_to_remote_media_storage(path)
            logger().debug(
                'copy_media_to_remote - copied to remote - '
                'local: %s / remote: %s' %
                (path, url))
    return url
コード例 #10
0
ファイル: utils.py プロジェクト: fogcitymarathoner/rma
def delete_images(entry, image_fields=None):
    """
    Delete image associated with entry and all its thumbnails.
    * if image_fields is None, check entry for get_image_fields
      method.

    Example usage:

    ENTRY_IMAGES = (
        'image',
        'thumbnail',
        'thumbnail_small'
    )
    delete_images(entry, ENTRY_IMAGES)

    * FIXME: add get_images method to model to fetch image paths
      directly instead of using field to fetch paths

    :param entry: object instance
    :param image_fields: list of field names for thumbnails
    :returns: number of errors encountered (0 is successful)
    """
    from helpers.files.utils import delete_remote_media

    use_remote_storage = _s('USE_REMOTE_STORAGE', False)
    errors = 0

    def _delete(url):
        e = 0
        if use_remote_storage:
            if not is_local_path(url):
                ok = delete_remote_media(url)
                if not ok:
                    e += 1
                logger().debug('deleted remote image - %s %s' % (ok, url))
        path = url_to_path(url)
        if os.path.isfile(path):
            os.remove(path)
            ok = os.path.isfile(path) == False
            if not ok:
                e += 1
            logger().debug('deleted image - %s %s' % (ok, path))
        return e

    if entry:
        if image_fields is None:
            if hasattr(entry, 'get_image_fields'):
                image_fields = entry.get_image_fields()
        if isinstance(image_fields, (list, tuple)):
            for img in image_fields:
                try:
                    assert img
                    url = getattr(entry, img)
                    assert url
                except AssertionError:
                    pass
                else:
                    result = _delete(url)
                    errors += result
                    setattr(entry, img, None)

    logger().debug('delete_images - errors: %d' % errors)
    return errors
コード例 #11
0
 def _msg(self, msg):
     """
     Dump debug message to log file.
     """
     if self.debug:
         logger().debug(msg)
コード例 #12
0
def handle_new_job(sender, **kwargs):
    site = kwargs.get('instance')
    logger().debug("POST_SAVE : site : %s" % site)