def setup_debug_weblib(filename_or_data):
    """
    Helper function to setup the weblib directory
    """

    # should only call this from debug or testing mode
    from minds.safe_config import cfg as testcfg

    test_path = testcfg.getpath('weblib')/Store.DEFAULT_FILENAME
    assert 'test' in test_path

    # clean the weblib directory
    files = [test_path] + ['%s.%s' % (test_path, i+1) for i in range(Store.BACKUP_COUNT)]
    for f in files:
        try:
            path(f).remove()
        except OSError:
            pass

    getStore().reset()
    getWeblib().reset()

    if not filename_or_data:
        return                  # blank data is fine

    if isinstance(filename_or_data, path) or ('\n' not in filename_or_data):
        # treat as filename
        src = path(filename_or_data)
        src.copyfile(test_path)

    else:
        # treat as data
        fp = file(test_path,'wb')
        fp.write(filename_or_data)
        fp.close()

    getStore().load(test_path)
def updateWebPage(page):
    """
    Update the corresponding file's NTFS summary property.
    @returns - pathname if data is updated, None otherwise
    """
    scheme, netloc, url_path, _, _, _ = urlparse.urlparse(page.url)
    assert scheme == 'file'
    p = path(util.nt_url2pathname(url_path))

    category = ', '.join(map(unicode,page.tags))
    # _writeProp() only work for read file
    # check this in advance to get friendlier error than the com_error
    if p.isfile():
        try:
            _writeProp(p, page.name, page.description, category)
            return p
        except (pywintypes.com_error, AttributeError), e:
            # file locked?
            log.exception('Unable to write NTFS property: %s' % p)
            return None
def makeWebPage(file_url):
    """
    Read the corresponding file's NTFS summary property and build a
    WebPage object. Fill with blank fields if can't read the
    properties.

    @returns - WebPage, tags (string)
    """
    scheme, netloc, url_path, _, _, _ = urlparse.urlparse(file_url)
    assert scheme == 'file'
    p = path(util.nt_url2pathname(url_path))

    props = None
    # _readProp() only work for read file
    # check this in advance to get friendlier error than the com_error
    if p.isfile():
        try:
            props = _readProp(p)
        except pywintypes.com_error, e:
            # file locked?
            log.exception('Unable to read NTFS property: %s' % p)
def launch(file_url):
    scheme, netloc, url_path, _, _, _ = urlparse.urlparse(file_url)
    p = path(util.nt_url2pathname(url_path))
    log.debug('Launching file: %s' % p)
    os.startfile(p)