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
2590a992
Commit
2590a992
authored
11 months ago
by
Geels, Brendan Ryan
Browse files
Options
Downloads
Patches
Plain Diff
Add optional encoder/decoder params to json util functions
parent
47e8e7f4
No related branches found
No related tags found
1 merge request
!318
Add UTCDateTime encoder/decoder for state file utils
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
geomagio/Util.py
+14
-4
14 additions, 4 deletions
geomagio/Util.py
with
14 additions
and
4 deletions
geomagio/Util.py
+
14
−
4
View file @
2590a992
...
...
@@ -225,7 +225,7 @@ def decode_utcdatetime(dct):
return
dct
def
write_state_file
(
filename
,
data
,
directory
=
None
):
def
write_state_file
(
filename
,
data
,
directory
=
None
,
encoder
=
None
):
"""
Writes data to a state file in a thread-safe manner.
...
...
@@ -237,6 +237,8 @@ def write_state_file(filename, data, directory=None):
The data to write to the file. This should be a Python object that can be serialized with json.
directory: String
The directory to write the file to. If not provided, the file will be written to the .cache directory in the current user
'
s home directory.
encoder: function
Function to be given to json.dump
'
s
'
default
'
parameter. If not provided it will use a simple encoder that handles UTCDateTime objects.
Returns:
--------
...
...
@@ -250,6 +252,9 @@ def write_state_file(filename, data, directory=None):
if
directory
is
None
:
directory
=
os
.
path
.
join
(
os
.
path
.
expanduser
(
"
~
"
),
"
.cache
"
,
"
geomag-algorithms
"
)
if
encoder
is
None
:
encoder
=
encode_utcdatetime
# Create the directory if it doesn't exist
try
:
os
.
makedirs
(
directory
,
exist_ok
=
True
)
...
...
@@ -263,7 +268,7 @@ def write_state_file(filename, data, directory=None):
with
open
(
filepath
,
"
w
"
)
as
f
:
try
:
fcntl
.
flock
(
f
,
fcntl
.
LOCK_EX
)
json
.
dump
(
data
,
f
,
default
=
encode
_utcdatetime
)
json
.
dump
(
data
,
f
,
default
=
encode
r
)
fcntl
.
flock
(
f
,
fcntl
.
LOCK_UN
)
except
IOError
as
e
:
print
(
f
"
Error locking or writing to file:
{
e
}
"
)
...
...
@@ -276,7 +281,7 @@ def write_state_file(filename, data, directory=None):
raise
def
read_state_file
(
filename
,
directory
=
None
):
def
read_state_file
(
filename
,
directory
=
None
,
decoder
=
None
):
"""
Reads data from a state file in a thread-safe manner.
...
...
@@ -285,6 +290,8 @@ def read_state_file(filename, directory=None):
The name of the file to read from.
directory: String
The directory to read the file from. If not provided, the file will be read from the .cache directory in the current user
'
s home directory.
encoder: function
Object hook function to be given to json.load. If not provided it will use a simple decoder that handles common start/end time fields.
Returns:
--------
...
...
@@ -299,13 +306,16 @@ def read_state_file(filename, directory=None):
if
directory
is
None
:
directory
=
os
.
path
.
join
(
os
.
path
.
expanduser
(
"
~
"
),
"
.cache
"
,
"
geomag-algorithms
"
)
if
decoder
is
None
:
decoder
=
decode_utcdatetime
filepath
=
os
.
path
.
join
(
directory
,
filename
)
try
:
with
open
(
filepath
,
"
r
"
)
as
f
:
try
:
fcntl
.
flock
(
f
,
fcntl
.
LOCK_SH
)
data
=
json
.
load
(
f
,
object_hook
=
decode
_utcdatetime
)
data
=
json
.
load
(
f
,
object_hook
=
decode
r
)
fcntl
.
flock
(
f
,
fcntl
.
LOCK_UN
)
return
data
except
IOError
as
e
:
...
...
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