jython open a zip file and read its content - file-io

I'm trying to open a zip file with jython using FileInputStream and ZipInputStream. But I'm strangely getting an FileNotFoundException when FileInputStream is called.
Here is my code:
from java.lang import System
from java.io import ObjectInputStream, FileInputStream, BufferedInputStream
from java.util.zip import ZipInputStream, ZipEntry
file_input_stream = FileInputStream('C:\\Documents and Settings\\usr\\My Documents\\Downloads\\test.zip')
zip_input_stream = ZipInputStream(BufferedInputStream(file_input_stream))
entry = zip_input_stream.getNextEntry()
entry = zip_input_stream.getNextEntry()
object_input_stream = ObjectInputStream(zip_input_stream)
graph.model = object_input_stream.readObject()
object_input_stream.close()
zip_input_stream.close()
file_input_stream.close()
My error is :
file_input_stream = FileInputStream('C:\\Documents and Settings\\usr\\My Documents\\Downloads\\test.zip')
Traceback (most recent call last):
File "<input>", line 1, in <module>
FileNotFoundException: java.io.FileNotFoundException: C:\Documents and Settings\usr\My Documents\Downloads\test.zip (The system cannot find the file specified)
I know for sure that the file is in the correct directory, if I call FileInputStream with a non-zip file, it works. What am I doing wrong here?
thanks

Open any folder in windows explorer. If you're using windows 7 or Vista, press alt to view the menu bar (on XP it should already be visible). Pick Tools -> Folder Options. In the view tab, search in the box for a checkbox labeled "Hide extensions for known file types" and uncheck it.
On that note, the file is probably called "test.zip.zip", which would be why "test.zip" is not found.

You know that you could use the Python zipfile library for this, in your Jython code.

Related

How to download netCDF4 file from webpage?

I want to download a netCDF4 file from a webpage. I can download the datafile, but there seems to be some errors in the file I downloaded using following codes:
import requests
from netCDF4 import Dataset
def download_file(url):
local_filename = url.split('/')[-1]
with requests.get(url, stream=True) as r:
with open(local_filename, 'wb') as f:
shutil.copyfileobj(r.raw, f)
return local_filename
url = 'https://smos-diss.eo.esa.int/oads/data/SMOS_Open_V7/SM_REPR_MIR_SMUDP2_20191222T183243_20191222T192549_700_300_1.nc'
local_filename = download_file(url)
sm_nc = Dataset(local_filename)
But finally I got error message:
Traceback (most recent call last):
File "<ipython-input-98-809c92d8bce8>", line 1, in <module>
sm_nc = Dataset(local_filename)
File "netCDF4/_netCDF4.pyx", line 2321, in netCDF4._netCDF4.Dataset.__init__
File "netCDF4/_netCDF4.pyx", line 1885, in netCDF4._netCDF4._ensure_nc_success
OSError: [Errno -51] NetCDF: Unknown file format: b'SM_REPR_MIR_SMUDP2_20191222T183243_20191222T192549_700_300_1.nc'
I also simply tried urllib.request.urlretrieve(url, './1.nc'), then sm_nc = Dataset('./1.nc'), but just got the following error message:
Traceback (most recent call last):
File "<ipython-input-101-61d1f577421e>", line 1, in <module>
sm_nc = Dataset('./1.nc')
File "netCDF4/_netCDF4.pyx", line 2321, in netCDF4._netCDF4.Dataset.__init__
File "netCDF4/_netCDF4.pyx", line 1885, in netCDF4._netCDF4._ensure_nc_success
OSError: [Errno -51] NetCDF: Unknown file format: b'./1.nc'
But the thing is that, if I paste the url in the search box of my Safari or Chrome, then click download, the file I got is readable by netCDF4.Dataset. (You could also try that.) I tried with many other solutions but didn't work. So is there anybody who could do me a favour? Thanks!
By the way, the requests and netCDF4 I am using are of version 2.26.0 and 1.5.3, urllib.request is of 3.7.
Tiy probably want to use urlretrieve. The following call to urllib should work:
import urllib
new_x = "/tmp/temp.nc"
x = "https://smos-diss.eo.esa.int/oads/data/SMOS_Open_V7/SM_REPR_MIR_SMUDP2_20191222T183243_20191222T192549_700_300_1.nc"
urllib.request.urlretrieve(x, new_x)
When I try to wget it gives me nc file but I am not sure it size is 19 KB. You can use wget in python if this file okey for you.
wget https://smos-diss.eo.esa.int/oads/data/SMOS_Open_V7/SM_REPR_MIR_SMUDP2_20191222T183243_20191222T192549_700_300_1.nc
But it is not readable because if you try access without login to site, it gives meaningless file. Just paste this link to your browser then login it gives 6 MB file which I'm sure it is readable. Still if you want to get file with python script check selenium that provide click on the website so you can login then download your file with script.

Download word document in local folder using Rselenium

I am using R selenium to navigate through a website and eventually click a button that opens a dialog window to download the file. However, I want the file to automatically save in a different local folder.
My question is similar to this question.
Instead of using remDr <- remoteDriver(browserName = "firefox") I have tried
remDr <- remoteDriver(extraCapabilities = makeFirefoxProfile(list(
"browser.helperApps.neverAsk.saveToDisk"="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
)), but this gave an error that some local folder does not exist.
Note that I have no prior knowledge of Selenium and am quite new to programming.
All help welcome!
EDIT: because it it concerns a .docx file the correct MIME type appears to be: application/vnd.openxmlformats-officedocument.wordprocessingml.document. So I tried the following:
remDr <- remoteDriver(extraCapabilities = makeFirefoxProfile(list(
"browser.helperApps.neverAsk.saveToDisk"="application/vnd.openxmlformats-officedocument.wordprocessingml.document")
))
but this gave following error:
Error in file(tmpfile, "rb") : cannot open the connection
In addition: Warning messages:
1: running command '"zip" -r9Xjq "C:\Users\PiKr\AppData\Local\Temp\RtmpApt5uQ\file19006f62c60.zip" "C:\Users\PiKr\AppData\Local\Temp\RtmpApt5uQ/firefoxprofile/prefs.js" ' had status 127
2: In file(tmpfile, "rb") :
cannot open file 'C:\Users\PiKr\AppData\Local\Temp\RtmpApt5uQ\file19006f62c60.zip': No such file or directory
I guess you must install the Rtools from the website and install the rtools before your proceed running the above program. Zipping action needs Rtools to perform. (Referring to the error 1)
Add the path to environment variable, if it doesn't add automatically

Unable to implement the "OpenERP Arabic Report Support"

I'm running openERP 7, I was faced with the problem that Arabic words show up in pdf reports as black boxes. I tried to resolve this problem so I downloaded the code in the OpenERP Arabic Report Support and followed the instructions. When I copied the textobject.py to the path D:\OpenERP 7.0-20141026-011104\server\reportlab\pdfgen and tried to restart the openerp-server-7.0 service it does not start and gave me the following error, I got it from event viewer log for windows:
The instance's SvcRun() method failed
Traceback (most recent call last):
File "win32serviceutil.pyo", line 835, in SvcRun
File "win32_service.pyo", line 42, in SvcDoRun
SystemExit: 255
%2: %3
and unable to continue. Can anyone check and help
Download the latest python version 2.7.10 from https://www.python.org/ftp/python/2.7.10/python-2.7.10.msi
Install it and find the mimetypes.pyc file from install path like C:\Python27\Lib
Copy the file and replace the same name file from openerp install path
4.start the service again. try it out.

Plone 4.3 and transmogrifier installation

I am trying to migrate content from old 3.x Plone installation to new Plone 4.3 instance. Both are running on windows platforms.
In new 4.3 installation I am trying to install quintagroup.transmogrifier and instance does not start.
bin\instance.exe fg gives following:
clip
Presumably normal entries removed...
clip
File "c:\plone43\eggs\zope.configuration-3.7.4-py2.7.egg\zope\configuration\config.py", line 179, in resolve
mod = __import__(mname, *_import_chickens)
File "c:\plone43\eggs\quintagroup.transmogrifier-0.4-py2.7.egg\quintagroup\transmogrifier\writer.py", line 13, in <module>
import quintagroup.transmogrifier.patches
File "c:\plone43\eggs\quintagroup.transmogrifier-0.4-py2.7.egg\quintagroup\transmogrifier\patches.py", line 89, in <module>
from tarfile import nts, GNUTYPE_SPARSE, normpath
zope.configuration.xmlconfig.ZopeXMLConfigurationError: File "c:\Plone43\parts\instance\etc\site.zcml", line 15.2-15.55
ZopeXMLConfigurationError: File "c:\Plone43\parts\instance\etc\package-includes\002-quintagroup.transmogrifier-configure.zcml", line 1.0-1.70
ZopeXMLConfigurationError: File "c:\plone43\eggs\quintagroup.transmogrifier-0.4-py2.7.egg\quintagroup\transmogrifier\configure.zcml", line 67.4-70.10
ImportError: cannot import name normpath
Is this something that transmogrifier is not allowd to load python module (v.2.7) in Plone or cannot find it from windows?
How could I fix the issue or approach it further?
I tried, just to copy data.fs to new site, but it seems to import a lot of "crap" from old version as well and some of the functionality dows not work properly.
I am not thet familiar with Plone/Zope/Python, but so far I have managed to get the job done.
Thanks
It's a bug in quintagroup.transmogrifier that has been fixed but not yet released.
For now, you can use the version on Github. If you are using mr.developer, the easiest way is to amend your buildout.cfg as follows:
[sources]
quintagroup.transmogrifier = git git://github.com/collective/quintagroup.transmogrifier.git

Cannot import "logging" module using Jython in embedded Jetty -> ImportError: no os specific module found

I'm trying to use Jython (embedded) in a Jetty server (all through Maven) to invoke a simple Python script.
My script works fine as long as I don't try to use any of the standard library's such as 'logging.' Whenever I try to import one of the standard library's it fails with the exception "ImportError."
The exception I get is:
File "<string>", line 1, in <module>
File "c:\home\work\sample\content\helloworld\helloworld.py", line 10, in <module>
import logging
File "c:\home\work\sample\content\Lib\logging\__init__.py", line 29, in <module>
import sys, os, types, time, string, cStringIO, traceback
File "c:\home\work\sample\content\Lib\os.py", line 119, in <module>
raise ImportError, 'no os specific module found'
ImportError: no os specific module found
at org.python.core.PyException.fillInStackTrace(PyException.java:70)
at java.lang.Throwable.<init>(Throwable.java:181)
at java.lang.Exception.<init>(Exception.java:29)
at java.lang.RuntimeException.<init>(RuntimeException.java:32)
at org.python.core.PyException.<init>(PyException.java:46)
at org.python.core.PyException.doRaise(PyException.java:200)
at org.python.core.Py.makeException(Py.java:1159)
at org.python.core.Py.makeException(Py.java:1163)
at os$py.f$0(c:\home\work\sample\content\Lib\os.py:692)
at os$py.call_function(c:\home\work\sample\content\Lib\os.py)
at org.python.core.PyTableCode.call(PyTableCode.java:165)
at org.python.core.PyCode.call(PyCode.java:18)
at org.python.core.imp.createFromCode(imp.java:325)
at org.python.core.imp.createFromPyClass(imp.java:144)
at org.python.core.imp.loadFromSource(imp.java:504)
at org.python.core.imp.find_module(imp.java:410)
at org.python.core.imp.import_next(imp.java:620)
at org.python.core.imp.import_first(imp.java:650)
at org.python.core.imp.import_name(imp.java:741)
at org.python.core.imp.importName(imp.java:791)
at org.python.core.ImportFunction.__call__(__builtin__.java:1236)
at org.python.core.PyObject.__call__(PyObject.java:367)
at org.python.core.__builtin__.__import__(__builtin__.java:1207)
at org.python.core.__builtin__.__import__(__builtin__.java:1190)
at org.python.core.imp.importOne(imp.java:802)
at logging$py.f$0(c:\home\work\sample\content\Lib\logging\__init__.py:1372)
at logging$py.call_function(c:\home\work\sample\content\Lib\logging\__init__.py)
at org.python.core.PyTableCode.call(PyTableCode.java:165)
at org.python.core.PyCode.call(PyCode.java:18)
at org.python.core.imp.createFromCode(imp.java:325)
at org.python.core.imp.createFromPyClass(imp.java:144)
at org.python.core.imp.loadFromSource(imp.java:504)
at org.python.core.imp.find_module(imp.java:410)
at org.python.core.imp.import_next(imp.java:620)
at org.python.core.imp.import_first(imp.java:650)
at org.python.core.imp.import_name(imp.java:741)
at org.python.core.imp.importName(imp.java:791)
at org.python.core.ImportFunction.__call__(__builtin__.java:1236)
at org.python.core.PyObject.__call__(PyObject.java:367)
at org.python.core.__builtin__.__import__(__builtin__.java:1207)
at org.python.core.__builtin__.__import__(__builtin__.java:1190)
at org.python.core.imp.importOne(imp.java:802)
at helloworld.helloworld$py.f$0(c:\home\work\sample\content\helloworld\helloworld.py:19)
at helloworld.helloworld$py.call_function(c:\home\work\sample\content\helloworld\helloworld.py)
at org.python.core.PyTableCode.call(PyTableCode.java:165)
at org.python.core.PyCode.call(PyCode.java:18)
at org.python.core.imp.createFromCode(imp.java:325)
at org.python.core.imp.createFromPyClass(imp.java:144)
at org.python.core.imp.loadFromSource(imp.java:504)
at org.python.core.imp.find_module(imp.java:410)
at org.python.core.PyModule.impAttr(PyModule.java:109)
at org.python.core.imp.import_next(imp.java:622)
at org.python.core.imp.import_name(imp.java:761)
at org.python.core.imp.importName(imp.java:791)
at org.python.core.ImportFunction.__call__(__builtin__.java:1236)
at org.python.core.PyObject.__call__(PyObject.java:367)
at org.python.core.__builtin__.__import__(__builtin__.java:1207)
at org.python.core.imp.importFromAs(imp.java:869)
at org.python.core.imp.importFrom(imp.java:845)
at org.python.pycode._pyx1.f$0(<string>:1)
at org.python.pycode._pyx1.call_function(<string>)
at org.python.core.PyTableCode.call(PyTableCode.java:165)
at org.python.core.PyCode.call(PyCode.java:18)
at org.python.core.Py.runCode(Py.java:1197)
at org.python.core.Py.exec(Py.java:1241)
at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:138)
My scripts looks like:
from java.util import Random
from java.util import Date
import sys
print(sys.path)
print(sys.builtin_module_names)
import logging
logging.basicConfig(level=logging.WARNING)
logger1 = logging.getLogger('aaa')
logger1.warning('************* This message comes from one module')
def say_hello():
return 'hello world1'
I've tried the following so far but nothing has worked:
Include the zip of the 'Lib' directory in my classpath
Hard-coding the 'Lib' path when i setup the interpreter.
If I do it directly from the interactive Jython shell the script works fine (and a logging message appears).
Thanks.
KJQ
I think for now i've found an answer to my own question...
Basically, i knew it had something to do with my paths but could not figure out how to do them.
I ended up creating a "standalone" version of the Jython jar through the installer (and it includes the /Libs directory) and using that.