def write_source_description(self,capability_lists=None,outfile=None,links=None): """Write a ResourceSync Description document to outfile or STDOUT""" rsd = SourceDescription(ln=links) rsd.pretty_xml = self.pretty_xml if (capability_lists is not None): for uri in capability_lists: rsd.add_capability_list(uri) if (outfile is None): print rsd.as_xml() else: rsd.write(basename=outfile)
def write_source_description(self, capability_lists=None, outfile=None, links=None): """Write a ResourceSync Description document to outfile or STDOUT""" rsd = SourceDescription(ln=links) rsd.pretty_xml = self.pretty_xml if (capability_lists is not None): for uri in capability_lists: rsd.add_capability_list(uri) if (outfile is None): print rsd.as_xml() else: rsd.write(basename=outfile)
def get(self): """Implement GET for Source Description.""" source_description = SourceDescription() source_description.describedby = self.source.describedby_uri source_description.add_capability_list(self.source.capability_list_uri) self.set_header("Content-Type", "application/xml") self.write(source_description.as_xml())
def get(self): """Implement GET for Source Description.""" source_description = SourceDescription() source_description.describedby = self.source.describedby_uri source_description.add_capability_list(self.source.capability_list_uri) self.set_header("Content-Type", "application/xml") self.write(source_description.as_xml())
def test02_one_caplist(self): rsd = SourceDescription() rsd.describedby = "http://example.org/about" self.assertEqual( len(rsd), 0 ) rsd.md_at = None rsd.add_capability_list("http://example.org/ds1/cl.xml") self.assertEqual( len(rsd), 1 ) self.assertEqual( rsd.as_xml(), '<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:rs="http://www.openarchives.org/rs/terms/"><rs:ln href="http://example.org/about" rel="describedby" /><rs:md capability="description" /><url><loc>http://example.org/ds1/cl.xml</loc><rs:md capability="capabilitylist" /></url></urlset>' )
def test01_empty(self): rsd = SourceDescription() rsd.describedby = "http://example.org/about" self.assertEqual(len(rsd), 0) rsd.md_at = None self.assertEqual( rsd.as_xml(), '<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:rs="http://www.openarchives.org/rs/terms/"><rs:ln href="http://example.org/about" rel="describedby" /><rs:md capability="description" /></urlset>' )
def synchronize(self): """ Publish the resources found in source_dir in accordance with the Resourcesync Framework in sink_dir. """ if not os.path.isdir(self.source_dir): os.makedirs(self.source_dir) print "Created %s" % self.source_dir if not os.path.isdir(self.sink_dir): os.makedirs(self.sink_dir) print "Created %s" % self.sink_dir self.handshake = self.verify_handshake() if self.handshake is None: return #################### # print "Synchronizing state as of %s" % self.handshake ### initial resource description wellknown = os.path.join(self.sink_dir, RS_WELL_KNOWN) if not os.path.isdir(wellknown): os.makedirs(wellknown) src_desc = SourceDescription() new_src_desc = True # Load existing resource-description, if any. if os.path.isfile(self.src_desc_path): new_src_desc = False with open(self.src_desc_path, "r") as src_desc_file: sm = Sitemap() sm.parse_xml(src_desc_file, resources=src_desc) count_lists = len(src_desc.resources) ### resources in subdirectories or main directory ### the existance of FILE_INDEX indicates whether resources reside directly in source_dir or in subdirectories. index_file = os.path.join(self.source_dir, FILE_INDEX) if os.path.isfile(index_file): for dirname in os.walk(self.source_dir).next()[1]: source = os.path.join(self.source_dir, dirname) sink = os.path.join(self.sink_dir, dirname) publish_url = self.publish_url + dirname + "/" self.__execute_sync__(source, sink, publish_url, src_desc) else: self.__execute_sync__(self.source_dir, self.sink_dir, self.publish_url, src_desc) if new_src_desc or count_lists != len(src_desc.resources): ### publish resource description with open(self.src_desc_path, "w") as src_desc_file: src_desc_file.write(src_desc.as_xml()) print "New resource description. See %s" % self.src_desc_url self.report()
def synchronize(self): """ Publish the resources found in source_dir in accordance with the Resourcesync Framework in sink_dir. """ if not os.path.isdir(self.source_dir): os.makedirs(self.source_dir) print "Created %s" % self.source_dir if not os.path.isdir(self.sink_dir): os.makedirs(self.sink_dir) print "Created %s" % self.sink_dir self.handshake = self.verify_handshake() if self.handshake is None: return #################### # print "Synchronizing state as of %s" % self.handshake ### initial resource description wellknown = os.path.join(self.sink_dir, RS_WELL_KNOWN) if not os.path.isdir(wellknown): os.makedirs(wellknown) src_desc = SourceDescription() new_src_desc = True # Load existing resource-description, if any. if os.path.isfile(self.src_desc_path): new_src_desc = False with open(self.src_desc_path, "r") as src_desc_file: sm = Sitemap() sm.parse_xml(src_desc_file, resources=src_desc) count_lists = len(src_desc.resources) ### resources in subdirectories or main directory ### the existance of FILE_INDEX indicates whether resources reside directly in source_dir or in subdirectories. index_file = os.path.join(self.source_dir, FILE_INDEX) if os.path.isfile(index_file): for dirname in os.walk(self.source_dir).next()[1]: source = os.path.join(self.source_dir, dirname) sink = os.path.join(self.sink_dir, dirname) publish_url = self.publish_url + dirname + "/" self.__execute_sync__(source, sink, publish_url, src_desc) else: self.__execute_sync__(self.source_dir, self.sink_dir, self.publish_url, src_desc) if new_src_desc or count_lists != len(src_desc.resources): ### publish resource description with open(self.src_desc_path, "w") as src_desc_file: src_desc_file.write(src_desc.as_xml()) print "New resource description. See %s" % self.src_desc_url self.report()
def test_build_ex_07(self): """A Source Description document """ sd = SourceDescription() sd.describedby = 'http://example.com/info-about-source.xml' r = Resource( uri='http://example.com/dataset1/capabilitylist.xml', capability='capabilitylist' ) r.link_set( rel='describedby', href='http://example.com/info_about_set1_of_resources.xml' ) sd.add( r ) ex_xml = self._open_ex('resourcesync_ex_7').read() self._assert_xml_equal( sd.as_xml(), ex_xml )
def test_build_ex_12(self): """Source Description document with describedby links""" sd = SourceDescription() sd.describedby = 'http://example.com/info_about_source.xml' cl1 = CapabilityList( uri='http://example.com/capabilitylist1.xml' ) cl1.describedby = 'http://example.com/info_about_set1_of_resources.xml' sd.add_capability_list( cl1 ) cl2 = CapabilityList( uri='http://example.com/capabilitylist2.xml' ) cl2.describedby = 'http://example.com/info_about_set2_of_resources.xml' sd.add_capability_list( cl2 ) cl3 = CapabilityList( uri='http://example.com/capabilitylist3.xml' ) cl3.describedby = 'http://example.com/info_about_set3_of_resources.xml' sd.add_capability_list( cl3 ) ex_xml = self._open_ex('resourcesync_ex_12').read() self._assert_xml_equal( sd.as_xml(), ex_xml )
# Print to file at args.resource_dir + "/resource-list.xml" resource_list_file = open(args.resource_dir + "/resource-list.xml", "w") resource_list_file.write(rl.as_xml()) resource_list_file.close() print "Wrote resource list to: " + args.resource_dir + "/resource-list.xml" timestamps.sort() caps = CapabilityList() caps.add_capability(rl, args.resource_url + "resource-list.xml") if len(timestamps) > 0: caps.md['from'] = timestamps[0] # Print to file at args.resource_dir + "/capability-list.xml" capability_list_file = open(args.resource_dir + "/capability-list.xml", "w") capability_list_file.write(caps.as_xml()) capability_list_file.close() print "Wrote capability list to: " + args.resource_dir + "/capability-list.xml" rsd = SourceDescription() rsd.md_at = None rsd.add_capability_list(args.resource_url + "capability-list.xml") # Print to file at args.resource_dir + "/resourcesync" source_description_file = open(args.resource_dir + "/resourcesync", "w") source_description_file.write(rsd.as_xml()) source_description_file.close() print "Wrote source description to: " + args.resource_dir + "/resourcesync"
# Print to file at args.resource_dir + "/resource-list.xml" resource_list_file = open(args.resource_dir + "/resource-list.xml", "w") resource_list_file.write(rl.as_xml()) resource_list_file.close() print "Wrote resource list to: " + args.resource_dir + "/resource-list.xml" timestamps.sort() caps = CapabilityList() caps.add_capability(rl, args.resource_url + "resource-list.xml") if len(timestamps) > 0: caps.md['from'] = timestamps[0] # Print to file at args.resource_dir + "/capability-list.xml" capability_list_file = open(args.resource_dir + "/capability-list.xml", "w") capability_list_file.write(caps.as_xml()) capability_list_file.close() print "Wrote capability list to: " + args.resource_dir + "/capability-list.xml" rsd = SourceDescription() rsd.md_at = None rsd.add_capability_list(args.resource_url + "capability-list.xml") # Print to file at args.resource_dir + "/resourcesync" source_description_file = open(args.resource_dir + "/resourcesync", "w") source_description_file.write(rsd.as_xml()) source_description_file.close() print "Wrote source description to: " + args.resource_dir + "/resourcesync"
def test01_empty(self): rsd = SourceDescription() rsd.describedby = "http://example.org/about" self.assertEqual( len(rsd), 0 ) rsd.md_at = None self.assertEqual( rsd.as_xml(), '<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:rs="http://www.openarchives.org/rs/terms/"><rs:ln href="http://example.org/about" rel="describedby" /><rs:md capability="description" /></urlset>' )