# Path: henet/util.py
# def file_age(path):
#     return os.stat(path).st_mtime
# 
# Path: henet/events.py
# def emit(event, **data):
#     for subscriber in _SUBSCRIBERS[event]:
#         subscriber(event, **data)
#     for subscriber in _SUBSCRIBERS[ALL_EVENTS]:
#         subscriber(event, **data)
# 
# EVENT_DELETED_CONTENT = 1
# 
# EVENT_CREATED_CONTENT = 2


import os
import datetime
from mimetypes import guess_type
from bottle import route, request, app, post, redirect, get, static_file
from bottle_utils.csrf import csrf_token, csrf_protect
from PIL import Image
from henet.util import file_age
from henet.events import emit, EVENT_DELETED_CONTENT, EVENT_CREATED_CONTENT
    return {'name': os.path.split(path)[-1],
            'modified': datetime.datetime.fromtimestamp(file_age(path)),
            'image-type': image_type,
            'type': type,
            'size': sizeof_fmt(os.path.getsize(path))}


def parse_files(path, page=-1, page_size=20):
    if isinstance(path, unicode):
        path = path.encode('utf8')

    media = []
    for root, dirs, files in os.walk(path):
        for file_ in files:
            fullpath = os.path.join(root, file_)
            media.append(parse_file(fullpath))

    media.sort(by_date)
    total_size = len(media)
    total_pages, rest = divmod(total_size, page_size)
    if rest > 0:
        total_pages += 1

    if page != -1:
        start = page * page_size
        end = start + page_size - 1
        media = media[start:end]

    # XXX we want to sort images by their HxW ratio, so they tile
    #

# Path: henet/util.py
# def file_age(path):
#     return os.stat(path).st_mtime
# 
# Path: henet/events.py
# def emit(event, **data):
#     for subscriber in _SUBSCRIBERS[event]:
#         subscriber(event, **data)
#     for subscriber in _SUBSCRIBERS[ALL_EVENTS]:
#         subscriber(event, **data)
# 
# EVENT_DELETED_CONTENT = 1
# 
# EVENT_CREATED_CONTENT = 2


# Path: henet/views/media.py
import os
import datetime
from mimetypes import guess_type
from bottle import route, request, app, post, redirect, get, static_file
from bottle_utils.csrf import csrf_token, csrf_protect
from PIL import Image
from henet.util import file_age
from henet.events import emit, EVENT_DELETED_CONTENT, EVENT_CREATED_CONTENT

    return {'name': os.path.split(path)[-1],
            'modified': datetime.datetime.fromtimestamp(file_age(path)),
            'image-type': image_type,
            'type': type,
            'size': sizeof_fmt(os.path.getsize(path))}


def parse_files(path, page=-1, page_size=20):
    if isinstance(path, unicode):
        path = path.encode('utf8')

    media = []
    for root, dirs, files in os.walk(path):
        for file_ in files:
            fullpath = os.path.join(root, file_)
            media.append(parse_file(fullpath))

    media.sort(by_date)
    total_size = len(media)
    total_pages, rest = divmod(total_size, page_size)
    if rest > 0:
        total_pages += 1

    if page != -1:
        start = page * page_size
        end = start + page_size - 1
        media = media[start:end]

    # XXX we want to sort images by their HxW ratio, so they tile
    #

    return media, total_pages