-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathutils.py
More file actions
138 lines (100 loc) · 3.78 KB
/
utils.py
File metadata and controls
138 lines (100 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python
##############################################################################
#
# PDFgui by DANSE Diffraction group
# Simon J. L. Billinge
# (c) 2007 trustees of the Michigan State University.
# All rights reserved.
#
# File coded by: Pavol Juhas
#
# See AUTHORS.txt for a list of people who contributed.
# See LICENSE.txt for license information.
#
##############################################################################
"""Small shared routines:
numericStringSort -- sort list of strings according to numeric value
safeCPickleDumps -- same as pickle.dumps, but safe for NaN and Inf
"""
import six
import six.moves.cPickle as pickle
from six.moves.configparser import RawConfigParser
# protocol=2 keep project files compatible with Python 2
# PDFGUI_PICKLE_PROTOCOL = 2
def numericStringSort(lst):
"""Sort list of strings inplace according to general numeric value.
Each string gets split to string and integer segments to create keys
for comparison. Signs, decimal points and exponents are ignored.
lst -- sorted list of strings
No return value to highlight inplace sorting.
"""
import re
rx = re.compile(r"(\d+)")
keys = [rx.split(s) for s in lst]
for k in keys:
k[1::2] = [int(i) for i in k[1::2]]
newlst = sorted(zip(keys, lst))
lst[:] = [kv[1] for kv in newlst]
return
def pickle_loads(sdata, encoding="latin1"):
"""Mimic interface of Python 3 pickle.loads.
Using encoding='latin1' is required for unpickling NumPy arrays and
instances of datetime, date and time pickled by Python 2.
Return the reconstructed object hierarchy.
"""
rv = pickle.loads(sdata, encoding=encoding) if six.PY3 else pickle.loads(sdata)
return rv
def safeCPickleDumps(obj):
"""Get pickle representation of an object possibly containing NaN or Inf.
By default it uses pickle.HIGHEST_PROTOCOL, but falls back to ASCII
protocol 0 if there is SystemError frexp() exception.
obj -- object to be pickled
Return pickle string.
"""
ascii_protocol = 0
try:
s = pickle.dumps(obj, pickle.HIGHEST_PROTOCOL)
except SystemError:
s = pickle.dumps(obj, ascii_protocol)
return s
# This should be unnecessary in Python 3
# TODO - replace getquoted/setquoted with get/set after dropping Python 2
class QuotedConfigParser(RawConfigParser):
def getquoted(self, section, option):
"""Retrieve option value previously set with setquoted.
This allows to work with unicode strings.
"""
vq = self.get(section, option)
rv = vq.decode("utf-8") if six.PY2 else vq
return rv
def setquoted(self, section, option, value):
"""Set option to a value encoded with urllib.quote.
This allows to store and write out unicode strings.
Use getquoted to recover the decoded value.
"""
vq = value.encode("utf-8") if six.PY2 else value
return self.set(section, option, vq)
# class QuotedConfigParser
def quote_plain(s):
"""Return a possibly Unicode string quoted as plain ASCII.
The returned value is suitable as a path component in the
project file format.
"""
from six.moves.urllib.parse import quote_plus
rv = quote_plus(asunicode(s).encode("utf-8"))
return rv
def unquote_plain(s):
"""Unquote string previously encoded with quote_plain."""
from six.moves.urllib.parse import unquote_plus
u = unquote_plus(s)
rv = asunicode(u)
return rv
def asunicode(s):
"""Convert string or bytes object to a text type.
This is `unicode` in Python 2 and `str` in Python 3.
"""
rv = s
if not isinstance(s, six.text_type):
rv = s.decode("utf-8")
return rv
# End of file