Skip to content

Commit d57679a

Browse files
fix: use always utf-8 encoding when opening files(jina-ai#5821)
1 parent 779bff2 commit d57679a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+115
-115
lines changed

docs/conf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
if 'JINA_VERSION' not in os.environ:
1919
pkg_name = 'jina'
2020
libinfo_py = path.join(repo_dir, pkg_name, '__init__.py')
21-
libinfo_content = open(libinfo_py, 'r').readlines()
21+
libinfo_content = open(libinfo_py, 'r', encoding='utf-8').readlines()
2222
version_line = [
2323
l.strip() for l in libinfo_content if l.startswith('__version__')
2424
][0]

docs/pin_requirements.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import sys
33

44
file_name = sys.argv[1]
5-
with open(file_name, 'r') as f:
5+
with open(file_name, 'r', encoding='utf-8') as f:
66
input = f.read()
77

88

@@ -16,5 +16,5 @@
1616
)
1717

1818

19-
with open(file_name, 'w') as f:
19+
with open(file_name, 'w', encoding='utf-8') as f:
2020
f.write(output)

fastentrypoints.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def main():
9898
setup_path = os.path.join(dst, 'setup.py')
9999

100100
# Insert the include statement to MANIFEST.in if not present
101-
with open(manifest_path, 'a+') as manifest:
101+
with open(manifest_path, 'a+', encoding='utf-8') as manifest:
102102
manifest.seek(0)
103103
manifest_content = manifest.read()
104104
if 'include fastentrypoints.py' not in manifest_content:
@@ -107,7 +107,7 @@ def main():
107107
)
108108

109109
# Insert the import statement to setup.py if not present
110-
with open(setup_path, 'a+') as setup:
110+
with open(setup_path, 'a+', encoding='utf-8') as setup:
111111
setup.seek(0)
112112
setup_content = setup.read()
113113
if 'import fastentrypoints' not in setup_content:

jina/exporter.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,22 @@ def export_schema(args):
6363
dump_api = api_to_dict()
6464
for yp in args.yaml_path:
6565
f_name = (yp % __version__) if '%s' in yp else yp
66-
with open(f_name, 'w', encoding='utf8') as fp:
66+
with open(f_name, 'w', encoding='utf-8') as fp:
6767
JAML.dump(dump_api, fp)
6868
default_logger.info(f'API is exported to {f_name}')
6969

7070
if args.json_path:
7171
dump_api = api_to_dict()
7272
for jp in args.json_path:
7373
f_name = (jp % __version__) if '%s' in jp else jp
74-
with open(f_name, 'w', encoding='utf8') as fp:
74+
with open(f_name, 'w', encoding='utf-8') as fp:
7575
json.dump(dump_api, fp, sort_keys=True)
7676
default_logger.info(f'API is exported to {f_name}')
7777

7878
if args.schema_path:
7979
dump_api = get_full_schema()
8080
for jp in args.schema_path:
8181
f_name = (jp % __version__) if '%s' in jp else jp
82-
with open(f_name, 'w', encoding='utf8') as fp:
82+
with open(f_name, 'w', encoding='utf-8') as fp:
8383
json.dump(dump_api, fp, sort_keys=True)
8484
default_logger.info(f'API is exported to {f_name}')

jina/helper.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1482,7 +1482,7 @@ async def root():
14821482
def get_ci_vendor() -> Optional[str]:
14831483
from jina.constants import __resources_path__
14841484

1485-
with open(os.path.join(__resources_path__, 'ci-vendors.json')) as fp:
1485+
with open(os.path.join(__resources_path__, 'ci-vendors.json'), encoding='utf-8') as fp:
14861486
all_cis = json.load(fp)
14871487
for c in all_cis:
14881488
if isinstance(c['env'], str) and c['env'] in os.environ:

jina/importer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def __enter__(self):
4343
def __exit__(self, exc_type, exc_val, traceback):
4444
if exc_type == ModuleNotFoundError:
4545
missing_module = self._pkg_name or exc_val.name
46-
with open(os.path.join(__resources_path__, 'extra-requirements.txt')) as fp:
46+
with open(os.path.join(__resources_path__, 'extra-requirements.txt'), encoding='utf-8') as fp:
4747
for v in fp:
4848
if (
4949
v.strip()

jina/jaml/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ def save_config(self, filename: Optional[str] = None):
598598
warnings.warn(
599599
f'no "filename" is given, {self!r}\'s config will be saved to: {f}'
600600
)
601-
with open(f, 'w', encoding='utf8') as fp:
601+
with open(f, 'w', encoding='utf-8') as fp:
602602
JAML.dump(self, fp)
603603

604604
@classmethod

jina/jaml/helper.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def parse_config_source(
169169
return path, None
170170
elif allow_yaml_file and is_yaml_filepath(path):
171171
comp_path = complete_path(path, extra_search_paths)
172-
return open(comp_path, encoding='utf8'), comp_path
172+
return open(comp_path, encoding='utf-8'), comp_path
173173
elif allow_url and urllib.parse.urlparse(path).scheme in {'http', 'https'}:
174174
req = urllib.request.Request(path, headers={'User-Agent': 'Mozilla/5.0'})
175175
with urllib.request.urlopen(req) as fp:

jina/logging/logger.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def add_handlers(self, config_path: Optional[str] = None, **kwargs):
199199
if not os.path.exists(config_path):
200200
config_path = old_config_path
201201

202-
with open(config_path) as fp:
202+
with open(config_path, encoding='utf-8') as fp:
203203
config = JAML.load(fp)
204204

205205
for h in config['handlers']:

jina/orchestrate/deployments/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1651,7 +1651,7 @@ def to_docker_compose_yaml(
16511651
services[service_name] = service
16521652

16531653
docker_compose_dict['services'] = services
1654-
with open(output_path, 'w+') as fp:
1654+
with open(output_path, 'w+', encoding='utf-8') as fp:
16551655
yaml.dump(docker_compose_dict, fp, sort_keys=False)
16561656

16571657
command = (
@@ -1705,7 +1705,7 @@ def _to_kubernetes_yaml(
17051705
for name, k8s_objects in configs:
17061706
filename = os.path.join(output_base_path, f'{name}.yml')
17071707
os.makedirs(output_base_path, exist_ok=True)
1708-
with open(filename, 'w+') as fp:
1708+
with open(filename, 'w+', encoding='utf-8') as fp:
17091709
for i, k8s_object in enumerate(k8s_objects):
17101710
yaml.dump(k8s_object, fp)
17111711
if i < len(k8s_objects) - 1:

jina/orchestrate/deployments/config/k8slib/kubernetes_tools.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def _get_yaml(template: str, params: Dict) -> Dict:
4242

4343
path = os.path.join(DEFAULT_RESOURCE_DIR, f'{template}.yml')
4444

45-
with open(path) as f:
45+
with open(path, encoding='utf-8') as f:
4646
content = f.read()
4747
for k, v in params.items():
4848
content = content.replace(f'{{{k}}}', str(v))
@@ -56,7 +56,7 @@ def _get_configmap_yaml(template: str, params: Dict):
5656

5757
path = os.path.join(DEFAULT_RESOURCE_DIR, f'{template}.yml')
5858

59-
with open(path) as f:
59+
with open(path, encoding='utf-8') as f:
6060
config_map = yaml.safe_load(f)
6161

6262
config_map['metadata']['name'] = params.get('name') + '-' + 'configmap'

jina/orchestrate/flow/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2824,7 +2824,7 @@ def to_docker_compose_yaml(
28242824
services[service_name] = service
28252825

28262826
docker_compose_dict['services'] = services
2827-
with open(output_path, 'w+') as fp:
2827+
with open(output_path, 'w+', encoding='utf-8') as fp:
28282828
yaml.dump(docker_compose_dict, fp, sort_keys=False)
28292829

28302830
command = (

jina/orchestrate/pods/container_helper.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def get_docker_network(client) -> Optional[str]:
2424
except docker.errors.NotFound:
2525
try:
2626
# https://stackoverflow.com/a/52988227/15683245
27-
with open('/proc/1/cpuset') as f:
27+
with open('/proc/1/cpuset', encoding='utf-8') as f:
2828
hostname = os.path.basename(f.read().rstrip())
2929
container = client.containers.get(hostname)
3030
except Exception:

jina/serve/networking/utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ def in_docker():
285285
if os.path.exists('/.dockerenv'):
286286
return True
287287
if os.path.isfile(path):
288-
with open(path) as file:
288+
with open(path, encoding='utf-8') as file:
289289
return any('docker' in line for line in file)
290290
return False
291291

jina_cli/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def _get_run_args(print_args: bool = True):
3737
if isinstance(a, (_StoreAction, _StoreTrueAction))
3838
}
3939

40-
with open(os.path.join(__resources_path__, 'jina.logo')) as fp:
40+
with open(os.path.join(__resources_path__, 'jina.logo'), encoding='utf-8') as fp:
4141
logo_str = fp.read()
4242

4343
param_str = Table(

scripts/announcement.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
def rm_announce():
1010
# remove all announcement
11-
with open(readme_md) as fp:
11+
with open(readme_md, encoding='utf-8') as fp:
1212
_old = fp.read()
1313
_new = re.sub(
1414
r'(<!--startmsg-->\s*?\n).*(\n\s*?<!--endmsg-->)',
@@ -17,18 +17,18 @@ def rm_announce():
1717
flags=re.DOTALL,
1818
)
1919

20-
with open(readme_md, 'w') as fp:
20+
with open(readme_md, 'w', encoding='utf-8') as fp:
2121
fp.write(_new)
2222

23-
with open(conf_py) as fp:
23+
with open(conf_py, encoding='utf-8') as fp:
2424
_old = fp.read()
2525
_new = re.sub(
2626
r'(# start-announce\s*?\n).*(\n\s*?# end-announce)',
2727
rf'\g<1>\g<2>',
2828
_old,
2929
flags=re.DOTALL,
3030
)
31-
with open(conf_py, 'w') as fp:
31+
with open(conf_py, 'w', encoding='utf-8') as fp:
3232
fp.write(_new)
3333

3434

@@ -49,7 +49,7 @@ def rm_announce():
4949
meetup_svg_url = f'<a href="{url}"><img src="https://github.com/jina-ai/jina/blob/master/{meetup_svg}?raw=true"></a>'
5050

5151
# update meetup_svg
52-
with open(meetup_svg) as fp:
52+
with open(meetup_svg, encoding='utf-8') as fp:
5353
_old = fp.read()
5454
_new = re.sub(r'(<a href=").*(")', rf'\g<1>{url}\g<2>', _old)
5555
_new = re.sub(
@@ -59,11 +59,11 @@ def rm_announce():
5959
flags=re.DOTALL,
6060
)
6161

62-
with open(meetup_svg, 'w') as fp:
62+
with open(meetup_svg, 'w', encoding='utf-8') as fp:
6363
fp.write(_new)
6464

6565
# update readme_md
66-
with open(readme_md) as fp:
66+
with open(readme_md, encoding='utf-8') as fp:
6767
_old = fp.read()
6868
_new = re.sub(
6969
r'(<!--startmsg-->\s*?\n).*(\n\s*?<!--endmsg-->)',
@@ -72,11 +72,11 @@ def rm_announce():
7272
flags=re.DOTALL,
7373
)
7474

75-
with open(readme_md, 'w') as fp:
75+
with open(readme_md, 'w', encoding='utf-8') as fp:
7676
fp.write(_new)
7777

7878
# update conf
79-
with open(conf_py) as fp:
79+
with open(conf_py, encoding='utf-8') as fp:
8080
_old = fp.read()
8181
_new = re.sub(
8282
r'(# start-announce\s*?\n).*(\n\s*?# end-announce)',
@@ -85,5 +85,5 @@ def rm_announce():
8585
flags=re.DOTALL,
8686
)
8787

88-
with open(conf_py, 'w') as fp:
88+
with open(conf_py, 'w', encoding='utf-8') as fp:
8989
fp.write(_new)

scripts/create-conda-recipe.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
def get_extra_requires(path):
1111
try:
12-
with open(path) as fp:
12+
with open(path, encoding='utf-8') as fp:
1313
extra_deps = defaultdict(set)
1414
for k in fp:
1515
if k.strip() and not k.startswith('#'):
@@ -211,5 +211,5 @@ def increase_indent(self, flow=False, *args, **kwargs):
211211
'''
212212

213213
recipe = recipe_header + recipe
214-
with open('conda/meta.yaml', 'w+') as fp:
214+
with open('conda/meta.yaml', 'w+', encoding='utf-8') as fp:
215215
fp.write(recipe)

scripts/generate-list-args.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
type = None if v['type'] == 'null' else v['type']
1616
table.append(f'| `{k}` | {desc} | `{type}` | `{v["default"]}` |')
1717

18-
with open(f'../docs/concepts/flow/{s}-args.md', 'w') as fp:
18+
with open(f'../docs/concepts/flow/{s}-args.md', 'w', encoding='utf-8') as fp:
1919
fp.write('\n'.join(table))

scripts/get-last-release-note.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
# python scripts/get-last-release-note.py
33
## result in root/tmp.md
44

5-
with open('CHANGELOG.md') as fp:
5+
with open('CHANGELOG.md', encoding='utf-8') as fp:
66
n = []
77
for v in fp:
88
if v.startswith('## Release Note'):
99
n.clear()
1010
n.append(v)
1111

12-
with open('tmp.md', 'w') as fp:
12+
with open('tmp.md', 'w', encoding='utf-8') as fp:
1313
fp.writelines(n)

scripts/get-openapi-schemas.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,5 @@
4747
gateway_schema['servers'].append(
4848
{'url': f'http://localhost:{args.port}', 'description': 'Local Jina gateway'}
4949
)
50-
with open(GATEWAY_SCHEMA_FILENAME, 'w') as f:
50+
with open(GATEWAY_SCHEMA_FILENAME, 'w', encoding='utf-8') as f:
5151
json.dump(gateway_schema, f)

scripts/inject-cli-as-overload.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def fill_overload(
123123
re.DOTALL,
124124
)
125125

126-
with open(filepath, 'w') as fp:
126+
with open(filepath, 'w', encoding='utf-8') as fp:
127127
fp.write(final_code)
128128
return {regex_tag or cli_entrypoint: doc_str}
129129

@@ -229,7 +229,7 @@ def fill_implementation_stub(
229229
re.DOTALL,
230230
)
231231

232-
with open(filepath, 'w') as fp:
232+
with open(filepath, 'w', encoding='utf-8') as fp:
233233
fp.write(final_code)
234234

235235

scripts/inject-document-props-as-overload.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def write_signature(
7676
0,
7777
re.DOTALL,
7878
)
79-
with open(filepath, 'w') as fp:
79+
with open(filepath, 'w', encoding='utf-8') as fp:
8080
fp.write(final_code)
8181

8282

scripts/prepend_version_json.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
)
1212
args = parser.parse_args()
1313

14-
with open("./docs/_versions.json") as f:
14+
with open("./docs/_versions.json", encoding='utf-8') as f:
1515
versions: List[dict] = json.load(f)
1616
element = {k: v for k, v in args._get_kwargs()}
1717
if element != versions[0]:
1818
versions.insert(0, element)
1919

20-
with open("./docs/_versions.json", "w") as f:
20+
with open("./docs/_versions.json", "w", encoding='utf-8') as f:
2121
json.dump(versions, f)

scripts/update-autocomplete-cli.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def _gaa(key, parser):
2424
cmd = compl.pop('')
2525
compl = {'commands': cmd, 'completions': compl}
2626

27-
with open(ac_file, 'w') as fp:
27+
with open(ac_file, 'w', encoding='utf-8') as fp:
2828
fp.write(f'ac_table = {compl}\n')
2929

3030

scripts/update-fluent-interface.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
doc_md = '../docs/fundamentals/document/fluent-interface.md'
4343
text = '\n'.join(all_s)
4444

45-
with open(doc_md) as fp:
45+
with open(doc_md, encoding='utf-8') as fp:
4646
_old = fp.read()
4747
_new = re.sub(
4848
r'(<!-- fluent-interface-start -->\s*?\n).*(\n\s*?<!-- fluent-interface-end -->)',
@@ -51,5 +51,5 @@
5151
flags=re.DOTALL,
5252
)
5353

54-
with open(doc_md, 'w') as fp:
54+
with open(doc_md, 'w', encoding='utf-8') as fp:
5555
fp.write(_new)

0 commit comments

Comments
 (0)