"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "xhtml2pdf/files.py" between
xhtml2pdf-0.2.6.tar.gz and xhtml2pdf-0.2.7.tar.gz

About: xhtml2pdf is a HTML/CSS to PDF converter written in Python.

files.py  (xhtml2pdf-0.2.6):files.py  (xhtml2pdf-0.2.7)
skipping to change at line 50 skipping to change at line 50
STRATEGIES = STRATEGIES STRATEGIES = STRATEGIES
CAPACITY = 10 * 1024 CAPACITY = 10 * 1024
def __init__(self, buffer="", capacity=CAPACITY): def __init__(self, buffer="", capacity=CAPACITY):
"""Creates a TempFile object containing the specified buffer. """Creates a TempFile object containing the specified buffer.
If capacity is specified, we use a real temporary file once the If capacity is specified, we use a real temporary file once the
file gets larger than that size. Otherwise, the data is stored file gets larger than that size. Otherwise, the data is stored
in memory. in memory.
""" """
self.name = None
self.capacity = capacity self.capacity = capacity
self.strategy = int(len(buffer) > self.capacity) self.strategy = int(len(buffer) > self.capacity)
try: try:
self._delegate = self.STRATEGIES[self.strategy]() self._delegate = self.STRATEGIES[self.strategy]()
except IndexError: except IndexError:
# Fallback for Google AppEnginge etc. # Fallback for Google AppEnginge etc.
self._delegate = self.STRATEGIES[0]() self._delegate = self.STRATEGIES[0]()
self.write(buffer) self.write(buffer)
# we must set the file's position for preparing to read # we must set the file's position for preparing to read
self.seek(0) self.seek(0)
skipping to change at line 161 skipping to change at line 161
return self.mimetype return self.mimetype
def get_named_tmp_file(self): def get_named_tmp_file(self):
data = self.get_data() data = self.get_data()
tmp_file = tempfile.NamedTemporaryFile(suffix=self.suffix) tmp_file = tempfile.NamedTemporaryFile(suffix=self.suffix)
# print(tmp_file.name, len(data)) # print(tmp_file.name, len(data))
if data: if data:
tmp_file.write(data) tmp_file.write(data)
tmp_file.flush() tmp_file.flush()
files_tmp.append(tmp_file) files_tmp.append(tmp_file)
return tmp_file if self.path is None:
self.path = tmp_file.name
return tmp_file
def get_BytesIO(self): def get_BytesIO(self):
data = self.get_data() data = self.get_data()
if data: if data:
return BytesIO(data) return BytesIO(data)
class B64InlineURI(BaseFile): class B64InlineURI(BaseFile):
_rx_datauri = re.compile( _rx_datauri = re.compile(
"^data:(?P<mime>[a-z]+/[a-z]+);base64,(?P<data>.*)$", re.M | re.DOTALL) "^data:(?P<mime>[a-z]+/[a-z]+);base64,(?P<data>.*)$", re.M | re.DOTALL)
skipping to change at line 283 skipping to change at line 285
" Guess the mime type " " Guess the mime type "
mimetype = mimetypes.guess_type(name)[0] mimetype = mimetypes.guess_type(name)[0]
if mimetype is not None: if mimetype is not None:
mimetype = mimetypes.guess_type(name)[0].split(";")[0] mimetype = mimetypes.guess_type(name)[0].split(";")[0]
return mimetype return mimetype
def extract_data(self): def extract_data(self):
data = None data = None
log.debug("Unrecognized scheme, assuming local file path") log.debug("Unrecognized scheme, assuming local file path")
path = Path(self.path) path = Path(self.path)
uri = Path(self.basepath) / path uri = None
if not uri.exists() and path.exists(): if self.basepath is not None:
uri = Path(self.basepath) / path
else:
uri = Path('.') / path
if path.exists() and not uri.exists():
uri = path uri = path
if uri.is_file(): if uri.is_file():
self.uri = uri self.uri = uri
self.suffix = uri.suffix self.suffix = uri.suffix
self.mimetype = self.guess_mimetype(uri) self.mimetype = self.guess_mimetype(uri)
if self.mimetype and self.mimetype.startswith('text'): if self.mimetype and self.mimetype.startswith('text'):
with open(uri, "r") as file_handler: with open(uri, "r") as file_handler:
# removed bytes... lets hope it goes ok :/ # removed bytes... lets hope it goes ok :/
data = file_handler.read() data = file_handler.read()
else: else:
with open(uri, "rb") as file_handler: with open(uri, "rb") as file_handler:
# removed bytes... lets hope it goes ok :/ # removed bytes... lets hope it goes ok :/
data = file_handler.read() data = file_handler.read()
return data return data
class BytesFileUri(BaseFile): class BytesFileUri(BaseFile):
def get_data(self): def get_data(self):
return self.path return self.path
class LocalTmpFile(BaseFile):
def __init__(self, path, basepath):
self.path = path
self.basepath = None
self.mimetype = basepath
self.suffix = None
self.uri = None
def get_named_tmp_file(self):
tmp_file = super().get_named_tmp_file()
if self.path is None:
self.path = tmp_file.name
return tmp_file
def get_data(self):
if self.path is None:
return
with open(self.path, 'rb') as arch:
return arch.read()
class FileNetworkManager: class FileNetworkManager:
@staticmethod @staticmethod
def get_manager(uri, basepath=None): def get_manager(uri, basepath=None):
if uri is None:
instance = LocalTmpFile(uri, basepath)
return instance
if isinstance(uri, bytes): if isinstance(uri, bytes):
instance = BytesFileUri(uri, basepath) instance = BytesFileUri(uri, basepath)
elif uri.startswith("data:"): elif uri.startswith("data:"):
instance = B64InlineURI(uri, basepath) instance = B64InlineURI(uri, basepath)
else: else:
if basepath and not urlparse.urlparse(uri).scheme: if basepath and not urlparse.urlparse(uri).scheme:
urlParts = urlparse.urlparse(basepath) urlParts = urlparse.urlparse(basepath)
else: else:
urlParts = urlparse.urlparse(uri) urlParts = urlparse.urlparse(uri)
skipping to change at line 333 skipping to change at line 363
instance = LocalFileURI(uri, basepath) instance = LocalFileURI(uri, basepath)
return instance return instance
class pisaFileObject: class pisaFileObject:
def __init__(self, uri, basepath=None, callback=None): def __init__(self, uri, basepath=None, callback=None):
self.uri = uri self.uri = uri
basepathret = None basepathret = None
if callback is not None: if callback is not None:
basepathret = callback(uri, basepath) basepathret = callback(uri, basepath)
if basepathret is not None: if basepathret is not None:
self.basepath = basepathret self.basepath = None
uri=basepathret
else: else:
self.basepath = basepath self.basepath = basepath
uri = uri or str() #uri = uri or str()
# if not isinstance(uri, str): # if not isinstance(uri, str):
# uri = uri.decode("utf-8") # uri = uri.decode("utf-8")
log.debug("FileObject %r, Basepath: %r", uri, basepath) log.debug("FileObject %r, Basepath: %r", uri, basepath)
self.instance = FileNetworkManager.get_manager(uri, basepath=self.basepa th) self.instance = FileNetworkManager.get_manager(uri, basepath=self.basepa th)
def getFileContent(self): def getFileContent(self):
return self.instance.get_data() return self.instance.get_data()
def getNamedFile(self): def getNamedFile(self):
skipping to change at line 368 skipping to change at line 399
def getMimeType(self): def getMimeType(self):
return self.instance.get_mimetype() return self.instance.get_mimetype()
def notFound(self): def notFound(self):
return self.getData() is None return self.getData() is None
def getAbsPath(self): def getAbsPath(self):
return self.instance.get_uri() return self.instance.get_uri()
def getBytesIO(self):
return self.instance.get_BytesIO()
def getFile(*a, **kw): def getFile(*a, **kw):
return pisaFileObject(*a, **kw) return pisaFileObject(*a, **kw)
def cleanFiles(): def cleanFiles():
files_tmp = [] files_tmp = []
 End of changes. 8 change blocks. 
6 lines changed or deleted 40 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)