Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
geomag-algorithms
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ghsc
National Geomagnetism Program
geomag-algorithms
Commits
d399af25
Commit
d399af25
authored
8 years ago
by
Jeremy M Fee
Browse files
Options
Downloads
Patches
Plain Diff
Add utility to read files, short circuit file url reads with direct io
parent
ddbf4bd7
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
geomagio/Util.py
+33
-4
33 additions, 4 deletions
geomagio/Util.py
with
33 additions
and
4 deletions
geomagio/Util.py
+
33
−
4
View file @
d399af25
...
...
@@ -105,6 +105,30 @@ def get_intervals(starttime, endtime, size=86400, align=True, trim=False):
return
intervals
def
read_file
(
filepath
):
"""
Open and read file contents.
Parameters
----------
filepath : str
path to a file
Returns
-------
str
contents of file
Raises
------
IOError
if file does not exist
"""
file_data
=
None
with
open
(
filepath
,
'
r
'
)
as
f
:
file_data
=
f
.
read
()
return
file_data
def
read_url
(
url
,
connect_timeout
=
15
,
max_redirects
=
5
,
timeout
=
300
):
"""
Open and read url contents.
...
...
@@ -123,9 +147,17 @@ def read_url(url, connect_timeout=15, max_redirects=5, timeout=300):
urllib2.URLError
if any occurs
"""
try
:
# short circuit file urls
filepath
=
get_file_from_url
(
url
)
return
read_file
(
filepath
)
except
IOError
as
e
:
raise
e
except
Exception
:
pass
content
=
None
curl
=
pycurl
.
Curl
()
out
=
StringIO
()
curl
=
pycurl
.
Curl
()
try
:
curl
.
setopt
(
pycurl
.
FOLLOWLOCATION
,
1
)
curl
.
setopt
(
pycurl
.
MAXREDIRS
,
max_redirects
)
...
...
@@ -136,9 +168,6 @@ def read_url(url, connect_timeout=15, max_redirects=5, timeout=300):
curl
.
setopt
(
pycurl
.
WRITEFUNCTION
,
out
.
write
)
curl
.
perform
()
content
=
out
.
getvalue
()
except
Exception
as
e
:
print
>>
sys
.
stderr
,
"
Error reading url:
"
+
str
(
e
)
print
>>
sys
.
stderr
,
url
finally
:
curl
.
close
()
return
content
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment