当前位置: 首页>>代码示例>>Python>>正文


Python Cache.load_index方法代码示例

本文整理汇总了Python中cache.Cache.load_index方法的典型用法代码示例。如果您正苦于以下问题:Python Cache.load_index方法的具体用法?Python Cache.load_index怎么用?Python Cache.load_index使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在cache.Cache的用法示例。


在下文中一共展示了Cache.load_index方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Junkie

# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import load_index [as 别名]
class Junkie(object):
  agent = 'Junkie https://github.com/bgnori/Junkie'

  def __init__(self):
    self.posts = {}
    self.cache = Cache('depot', 128)
    self.cache.load_index()
    self.renderer = render.XSLTRenderer('basic.xslt')
    with open('config') as f:
      self.auth = yaml.load(f.read())

  def save(self):
    self.cache.save_entries()
    self.cache.save_index()

  def get(self, url):
    ce = self._cache(url)
    if not ce:
      ce = self._web(url)
    return ce

  def _cache(self, url):
    '''
      retrieve content from cache.
      returns CacheEntry object
    '''
    return self.cache.get(url) 

  def _web(self, url):
    '''
      retrieve content from web.
      returns CacheEntry object
    '''
    entry = self.cache.make_entry(url)

    d = client.getPage(url)
    def onPageArrival(data):
      f = DataFile(data, 'OK') #FIXME Don't guess, use header
      entry.write(f)
      return f
    d.addCallback(onPageArrival)
    def onFail(f):#FIXME
      print 'onFail', f
      entry.abort()
      return 'fail' #FIXME
    d.addErrback(onFail) 
    return entry

  def prefetch(self):
    data_dict = {'start': 0, 'num': 50}
    data_dict.update(self.auth)
    postdata = urllib.urlencode(data_dict)
    d = client.getPage('http://www.tumblr.com/api/dashboard', 
                            method='POST',
                            agent=self.agent,
                            headers = {'Content-Type': 'application/x-www-form-urlencoded'},
                            postdata=postdata)
    def onDataArrival(data):
      if data.startswith('''<!DOCTYPE html PUBLIC "-'''):
        ''' login '''
        print 'ugh! login failed.'
        return None
      with open('dashboard/%s.xml'%(time.time(),), 'w') as f:
        f.write(data)
      self.update_posts(data)
    d.addCallback(onDataArrival).addErrback(printError)


  def update_posts(self, xmldata):
    '''
      parse v1 XML data and store posts.
    '''
    t = etree.XML(xmldata)
    find = etree.XPath('/tumblr/posts/post')
    for post in find(t):
      p = PostFactory(post)
      for url in p.assets_urls():
        self.get(url)
      self.posts[p.id] = p

  def make_dashboard(self):
    '''
      returns html
    '''

    uniques = {}
    #unique by root post
    for p in self.posts.itervalues():
      q = uniques.get(p.reblogged_root_url, None)
      if q is None or q.unix_timestamp > p.unix_timestamp:
        uniques[p.reblogged_root_url] = p

    posts = sorted(uniques.itervalues(), key=lambda x:x.unix_timestamp)
    
    post_div = self.renderer.render(posts)

    html = E.HTML(
      E.HEAD(
        E.META(charset="UTF-8"),
        E.META({"http-equiv":"Content-Type", "content":"text/html;charset=utf-8"}),
#.........这里部分代码省略.........
开发者ID:bgnori,项目名称:Junkie,代码行数:103,代码来源:junkie.py


注:本文中的cache.Cache.load_index方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。