Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File system file support #6

Merged
merged 2 commits into from
Oct 3, 2013
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Support for loading file from file system - e.g. in case you have arc…
…hive dumps
  • Loading branch information
arski committed Sep 26, 2013
commit b8c6cfc7ec38316ab7e3796813d39ae5c3cf496c
25 changes: 16 additions & 9 deletions src/oaipmh/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from types import SliceType
from lxml import etree
import time
import codecs

from oaipmh import common, metadata, validation, error
from oaipmh.datestamp import datestamp_to_datetime, datetime_to_datestamp
Expand Down Expand Up @@ -299,24 +300,30 @@ def makeRequest(self, **kw):

class Client(BaseClient):
def __init__(
self, base_url, metadata_registry=None, credentials=None):
self, base_url, metadata_registry=None, credentials=None, local_file=False):
BaseClient.__init__(self, metadata_registry)
self._base_url = base_url
self._local_file = local_file
if credentials is not None:
self._credentials = base64.encodestring('%s:%s' % credentials)
else:
self._credentials = None

def makeRequest(self, **kw):
"""Actually retrieve XML from the server.
"""Either load a local XML file or actually retrieve XML from a server.
"""
# XXX include From header?
headers = {'User-Agent': 'pyoai'}
if self._credentials is not None:
headers['Authorization'] = 'Basic ' + self._credentials.strip()
request = urllib2.Request(
self._base_url, data=urlencode(kw), headers=headers)
return retrieveFromUrlWaiting(request)
if self._local_file:
with codecs.open(self._base_url, 'r', 'utf-8') as xmlfile:
text = xmlfile.read()
return text.encode('ascii', 'replace')
else:
# XXX include From header?
headers = {'User-Agent': 'pyoai'}
if self._credentials is not None:
headers['Authorization'] = 'Basic ' + self._credentials.strip()
request = urllib2.Request(
self._base_url, data=urlencode(kw), headers=headers)
return retrieveFromUrlWaiting(request)

def buildHeader(header_node, namespaces):
e = etree.XPathEvaluator(header_node,
Expand Down