Skip to content

Commit 7a257e5

Browse files
committed
Moved more functions to util
1 parent 2d76849 commit 7a257e5

File tree

2 files changed

+63
-57
lines changed

2 files changed

+63
-57
lines changed

pythonforandroid/toolchain.py

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
from functools import wraps
2929
from datetime import datetime
3030
from distutils.spawn import find_executable
31-
from tempfile import mkdtemp
3231
from math import log10
3332

3433
import argparse
@@ -43,7 +42,7 @@
4342
from pythonforandroid.logger import (logger, info, debug, warning, error,
4443
Out_Style, Out_Fore, Err_Style, Err_Fore,
4544
info_notify, info_main, shprint)
46-
from pythonforandroid.util import ensure_dir, current_directory, temp_directory
45+
from pythonforandroid.util import (ensure_dir, current_directory, temp_directory)
4746

4847
# monkey patch to show full output
4948
sh.ErrorReturnCode.truncate_cap = 999999
@@ -56,7 +55,6 @@
5655

5756
DEFAULT_ANDROID_API = 15
5857

59-
IS_PY3 = sys.version_info[0] >= 3
6058

6159
info(''.join(
6260
[Err_Style.BRIGHT, Err_Fore.RED,
@@ -201,57 +199,6 @@ def _cache_execution(self, *args, **kwargs):
201199

202200

203201

204-
class JsonStore(object):
205-
"""Replacement of shelve using json, needed for support python 2 and 3.
206-
"""
207-
208-
def __init__(self, filename):
209-
super(JsonStore, self).__init__()
210-
self.filename = filename
211-
self.data = {}
212-
if exists(filename):
213-
try:
214-
with io.open(filename, encoding='utf-8') as fd:
215-
self.data = json.load(fd)
216-
except ValueError:
217-
print("Unable to read the state.db, content will be replaced.")
218-
219-
def __getitem__(self, key):
220-
return self.data[key]
221-
222-
def __setitem__(self, key, value):
223-
self.data[key] = value
224-
self.sync()
225-
226-
def __delitem__(self, key):
227-
del self.data[key]
228-
self.sync()
229-
230-
def __contains__(self, item):
231-
return item in self.data
232-
233-
def get(self, item, default=None):
234-
return self.data.get(item, default)
235-
236-
def keys(self):
237-
return self.data.keys()
238-
239-
def remove_all(self, prefix):
240-
for key in self.data.keys()[:]:
241-
if not key.startswith(prefix):
242-
continue
243-
del self.data[key]
244-
self.sync()
245-
246-
def sync(self):
247-
# http://stackoverflow.com/questions/12309269/write-json-data-to-file-in-python/14870531#14870531
248-
if IS_PY3:
249-
with open(self.filename, 'w') as fd:
250-
json.dump(self.data, fd, ensure_ascii=False)
251-
else:
252-
with io.open(self.filename, 'w', encoding='utf-8') as fd:
253-
fd.write(unicode(json.dumps(self.data, ensure_ascii=False)))
254-
255202

256203
class Graph(object):
257204
# Taken from the old python-for-android/depsort
@@ -771,9 +718,6 @@ def __init__(self):
771718
self.env.pop("ARCHFLAGS", None)
772719
self.env.pop("CFLAGS", None)
773720

774-
# set the state
775-
self.state = JsonStore(join(self.dist_dir, "state.db"))
776-
777721
def set_archs(self, arch_names):
778722
all_archs = self.archs
779723
new_archs = set()

pythonforandroid/util.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
import contextlib
22
from os.path import exists
3+
from os import getcwd, chdir, makedirs
4+
import io
5+
import json
6+
import shutil
7+
import sys
8+
from tempfile import mkdtemp
39
try:
410
from urllib.request import FancyURLopener
511
except ImportError:
612
from urllib import FancyURLopener
713

14+
from pythonforandroid.logger import (logger, Err_Fore)
15+
16+
IS_PY3 = sys.version_info[0] >= 3
17+
818

919
class ChromeDownloader(FancyURLopener):
1020
version = (
@@ -42,3 +52,55 @@ def temp_directory():
4252
def ensure_dir(filename):
4353
if not exists(filename):
4454
makedirs(filename)
55+
56+
57+
class JsonStore(object):
58+
"""Replacement of shelve using json, needed for support python 2 and 3.
59+
"""
60+
61+
def __init__(self, filename):
62+
super(JsonStore, self).__init__()
63+
self.filename = filename
64+
self.data = {}
65+
if exists(filename):
66+
try:
67+
with io.open(filename, encoding='utf-8') as fd:
68+
self.data = json.load(fd)
69+
except ValueError:
70+
print("Unable to read the state.db, content will be replaced.")
71+
72+
def __getitem__(self, key):
73+
return self.data[key]
74+
75+
def __setitem__(self, key, value):
76+
self.data[key] = value
77+
self.sync()
78+
79+
def __delitem__(self, key):
80+
del self.data[key]
81+
self.sync()
82+
83+
def __contains__(self, item):
84+
return item in self.data
85+
86+
def get(self, item, default=None):
87+
return self.data.get(item, default)
88+
89+
def keys(self):
90+
return self.data.keys()
91+
92+
def remove_all(self, prefix):
93+
for key in self.data.keys()[:]:
94+
if not key.startswith(prefix):
95+
continue
96+
del self.data[key]
97+
self.sync()
98+
99+
def sync(self):
100+
# http://stackoverflow.com/questions/12309269/write-json-data-to-file-in-python/14870531#14870531
101+
if IS_PY3:
102+
with open(self.filename, 'w') as fd:
103+
json.dump(self.data, fd, ensure_ascii=False)
104+
else:
105+
with io.open(self.filename, 'w', encoding='utf-8') as fd:
106+
fd.write(unicode(json.dumps(self.data, ensure_ascii=False)))

0 commit comments

Comments
 (0)