import os
import logging
log = logging.getLogger(__file__)



from ott.utils.cache_base import CacheBase


class SobiCache(CacheBase):
    """ cache a SOBI .json file
        @see http://socialbicycles.com/
    """
    url = None
    name = None
    file_name = None
    file_path = None
    solr_file_name = None
    solr_file_path = None

    def __init__(self):
        super(SobiCache, self).__init__(section='sobi')
        self.url = self.config.get('download_url')
        self.name = self.config.get('name')

        self.file_name = self.name + ".json"
        self.file_path = os.path.join(self.cache_dir, self.file_name)

        self.solr_file_name = self.name + "-solr.xml"
        self.solr_file_path = os.path.join(self.cache_dir, self.solr_file_name)

    def check_feed(self, force_update=False):
        ret_val = self.simple_cache_item_update(self.file_name, self.url, force_update)
        if True or ret_val:
            ret_val = self.to_solr()
        return ret_val

    def to_solr(self):
        '''
        '''
        success = False

        from xml.etree.ElementTree import Element, SubElement, Comment
        from xml.etree import ElementTree
        #from ElementTree_pretty import prettify

        top = Element('top')

        comment = Comment('Generated for PyMOTW')
        top.append(comment)

        child = SubElement(top, 'child')
        child.text = 'This child contains text.'

        child_with_tail = SubElement(top, 'child_with_tail')
        child_with_tail.text = 'This child has regular text.'
        child_with_tail.tail = 'And "tail" text.'

        child_with_entity_ref = SubElement(top, 'child_with_entity_ref')
        child_with_entity_ref.text = 'This & that'

        #print prettify(top)
        print ElementTree.tostring(top, encoding='utf8', method='xml')

        success = True
        return success

@@ -3,16 +3,32 @@
 port os%0A
+import datetime%0A
 import l
@@ -1185,24 +1185,25 @@
 entTree%0A
+%0A
         
 #from El
@@ -1198,13 +1198,13 @@
     
-#from
+rec =
  Ele
@@ -1215,47 +1215,9 @@
 Tree
-_pretty import prettify%0A%0A        top = 
+.
 Elem
@@ -1225,15 +1225,14 @@
 nt('
-top
+add
 ')%0A
-%0A
     
@@ -1245,16 +1245,28 @@
 mment = 
+ElementTree.
 Comment(
@@ -1280,19 +1280,44 @@
 ted 
-for PyMOTW'
+on %7B%7D'.format(datetime.date.today())
 )%0A  
@@ -1322,19 +1322,19 @@
         
-top
+rec
 .append(
@@ -1342,25 +1342,24 @@
 omment)%0A
-%0A
         
 child = 
@@ -1350,21 +1350,19 @@
         
-child
+doc
  = SubEl
@@ -1371,399 +1371,131 @@
 ent(
-top, 'child')%0A        child.text = 'This child contains text.'%0A%0A        child_with_tail = SubElement(top, 'child_with_tail')%0A        child_with_tail.text = 'This child has regular text.'%0A        child_with_tail.tail = 'And %22tail%22 text.'%0A%0A        child_with_entity_ref = SubElement(top, 'child_with_entity_ref')%0A        child_with_entity_ref.text = 'This & that'%0A%0A        #print prettify(top)
+rec, 'doc', attrib=%7B'boost':'1.0'%7D)%0A        field = SubElement(doc, 'field', attrib=%7B'name':'id'%7D)%0A        field.text = '1'
 %0A   
@@ -1530,11 +1530,11 @@
 ing(
-top
+rec
 , en

make xml look like SOLR doc ... note, need to refactor this SOLR stuff somewhere else