Skip to main content
Homepage
Explore
Search or go to…
/
Sign in
Explore
Primary navigation
Project
I
invenio-previewer
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Collapse sidebar
Snippets
Groups
Projects
Show more breadcrumbs
fdm
invenio-previewer
Commits
9915c309
Commit
9915c309
authored
May 12, 2023
by
Thomas Morrell
Committed by
Nicola
Aug 11, 2023
Browse files
Options
Downloads
Patches
Plain Diff
encoding: cleanup detection and override ASCII to default encoding
parent
a8aa2c84
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
invenio_previewer/utils.py
+17
-8
17 additions, 8 deletions
invenio_previewer/utils.py
with
17 additions
and
8 deletions
invenio_previewer/utils.py
+
17
−
8
View file @
9915c309
...
...
@@ -2,6 +2,8 @@
#
# This file is part of Invenio.
# Copyright (C) 2016-2019 CERN.
# Copyright (C) 2023 Northwestern University.
# Copyright (C) 2023 California Institute of Technology.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
...
...
@@ -23,17 +25,24 @@ def detect_encoding(fp, default=None):
"""
init_pos
=
fp
.
tell
()
try
:
sample
=
fp
.
read
(
current_app
.
config
.
get
(
'
PREVIEWER_CHARDET_BYTES
'
,
1024
))
chardet_size
=
current_app
.
config
.
get
(
"
PREVIEWER_CHARDET_BYTES
"
,
1024
)
threshold
=
current_app
.
config
.
get
(
"
PREVIEWER_CHARDET_CONFIDENCE
"
,
0.9
)
sample
=
fp
.
read
(
chardet_size
)
# Result contains 'confidence' and 'encoding'
result
=
cchardet
.
detect
(
sample
)
threshold
=
current_app
.
config
.
get
(
'
PREVIEWER_CHARDET_CONFIDENCE
'
,
0.9
)
if
result
.
get
(
'
confidence
'
,
0
)
>
threshold
:
return
result
.
get
(
'
encoding
'
,
default
)
else
:
return
default
confidence
=
result
.
get
(
"
confidence
"
,
0
)
or
0
encoding
=
result
.
get
(
"
encoding
"
,
default
)
or
default
# if low confidence or ascii, override to default (usually utf8 which is
# better in case of unicode beyond checked range)
if
confidence
<=
threshold
or
encoding
==
"
ASCII
"
:
encoding
=
default
return
encoding
except
Exception
:
current_app
.
logger
.
warning
(
'
Encoding detection failed.
'
,
exc_info
=
True
)
current_app
.
logger
.
warning
(
"
Encoding detection failed.
"
,
exc_info
=
True
)
return
default
finally
:
fp
.
seek
(
init_pos
)
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
sign in
to comment