Skip to content

Commit 35e1d7e

Browse files
author
Fabrice Benhamouda
committedJul 23, 2024
Linting & formatting & python3 in shebang
1 parent 3032fc6 commit 35e1d7e

File tree

4 files changed

+76
-62
lines changed

4 files changed

+76
-62
lines changed
 

‎bibyml.py

+30-22
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,33 @@
1212
2015: bb
1313
1414
corresponds to the dictionnary:
15+
1516
{
1617
"eurocrypt": {
1718
"": test,
1819
"2013": aa,
1920
"2015": bb
2021
}
2122
}
22-
23-
2423
"""
2524

2625
import re
2726
import io
2827
from collections import OrderedDict
28+
import typing
2929

3030
_parser_re = re.compile(r'^(\s*)([^:]+):(.*)$')
3131
_spaces_re = re.compile(r'^ *$')
3232

33+
3334
class ParserError(Exception):
3435
def __init__(self, line, msg=""):
35-
message = """BibYml parsing error:\n line: "{}"\n message: {}""".format(line[:-1], msg)
36-
super(ParserError,self).__init__(message)
36+
message = """BibYml parsing error:\n line: "{}"\n message: {}""".format(
37+
line[:-1], msg)
38+
super(ParserError, self).__init__(message)
39+
3740

38-
def dict_get_path(d, p, make=False):
41+
def dict_get_path(d: dict, p: list, make=False):
3942
""" get the element of path p in dictionnary d,
4043
make the path if it does not exists and make=True """
4144
cur = d
@@ -46,22 +49,26 @@ def dict_get_path(d, p, make=False):
4649

4750
return cur
4851

49-
def parse(f):
52+
53+
def parse(f: typing.TextIO) -> dict:
5054
""" Parse a bibyml file f intro a dictionnary """
5155
res = OrderedDict()
52-
path = [] # path of the current element
53-
path_indent = [0] # indentation of all elements along the path + indentation of the children of the path
54-
# can end by -1 if the indentation of the children is not yet known (i.e., before the first children)
56+
# `path` is the path of the current element
57+
path = []
58+
# `path_indent` stores the indentation of all the elements along the path
59+
# + the indentation of the children in the path
60+
# it can end by -1 if the indentation of the children is not yet known (i.e., before the first children)
61+
path_indent = [0]
5562

5663
for line in f:
5764
if line.strip() == "":
5865
continue
5966
r = _parser_re.match(line)
60-
if r == None:
67+
if r is None:
6168
raise ParserError(line)
6269

6370
(spaces_indent, key, value) = r.groups()
64-
if _spaces_re.match(spaces_indent) == None:
71+
if _spaces_re.match(spaces_indent) is None:
6572
raise ParserError(line, "only spaces are accepted")
6673
value = value.strip()
6774
key = key.strip()
@@ -80,38 +87,39 @@ def parse(f):
8087
while len(path_indent) > 1 and path_indent[-1] > indent:
8188
path_indent.pop()
8289
path.pop()
83-
90+
8491
if indent != path_indent[-1]:
8592
raise ParserError(line, "indentation problem")
8693

8794
d = dict_get_path(res, path)
88-
95+
8996
d[key] = OrderedDict([("", value)]) if value != "" else OrderedDict()
90-
path_indent.append(-1) # we do not know the next indentation level
97+
path_indent.append(-1) # we do not know the next indentation level
9198
path.append(key)
9299

93100
return res
94101

95-
def write(out, d, indent_key=4, indent_value=24, cur_indent=0):
96-
for (k,v) in d.items():
97-
if k=="":
102+
103+
def write(out: typing.TextIO, d: dict, indent_key=4, indent_value=24, cur_indent=0) -> None:
104+
for (k, v) in d.items():
105+
if k == "":
98106
continue
99107
if "" in v and v[""] != "":
100108
out.write("{}{}: {}{}\n".format(
101-
" "*indent_key*cur_indent,
102-
k,
109+
" "*indent_key*cur_indent,
110+
k,
103111
" "*(max(0, indent_value-(len(k)+2+indent_key*cur_indent))),
104112
v[""]
105113
))
106114
else:
107115
out.write("{}{}: \n".format(
108-
" "*indent_key*cur_indent,
116+
" "*indent_key*cur_indent,
109117
k
110118
))
111119
write(out, v, cur_indent=cur_indent+1)
112120

113-
def write_str(d, *args, **kwargs):
121+
122+
def write_str(d: dict, *args, **kwargs) -> str:
114123
out = io.StringIO()
115124
write(out, d, *args, **kwargs)
116125
return out.getvalue()
117-

‎confs_years.py

+22-17
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,24 @@
66

77
import logging
88

9-
def get_confs_years(db):
9+
10+
def get_confs_years(db) -> dict:
1011
""" Return a dict associating a conference key to the set of years present in db """
1112
confs = {}
1213
for (key, entry) in db.entries.items():
13-
if key.auth == None:
14-
continue # we are only interested in papers !
14+
if key.auth is None:
15+
continue # we are only interested in papers !
1516
conf = key.confkey
1617
if conf not in confs:
1718
confs[conf] = set()
1819
confs[conf].add(tools.short_to_full_year(key.year))
19-
20+
2021
return confs
2122

22-
def get_confs_years_inter_from_set(confs, missing_years):
23-
""" Convert a dictionnary of conferences (keys) with set of years to a dictionnary of conferences with intervals, i.e., tuples (start year, end year)
23+
24+
def get_confs_years_inter_from_set(confs: dict, missing_years) -> dict:
25+
""" Convert a dictionnary of conferences (keys) with set of years
26+
to a dictionnary of conferences with intervals, i.e., tuples (start year, end year)
2427
If some years are missing, log it except if missing_years[conf] contain this year """
2528

2629
def get_missing_years(conf):
@@ -33,22 +36,24 @@ def set_to_tuple(conf, years):
3336
min_year = min(years)
3437
max_year = max(years)
3538
years = years | get_missing_years(conf)
36-
if not (set(range(min_year, max_year+1)) <= years):
37-
mis_years = set(range(min_year, max_year+1)) - years
38-
min_year = max(mis_years)+1
39-
logging.warning("For conference \"{}\", years {} are missing - so min year={} - to add an exception please modify db/config.py - variable missing_years".format(
40-
conf,
41-
", ".join([str(y) for y in sorted(list(mis_years))]),
42-
min_year
43-
))
44-
return (min_year, max_year)
39+
if not (set(range(min_year, max_year + 1)) <= years):
40+
mis_years = set(range(min_year, max_year + 1)) - years
41+
min_year = max(mis_years) + 1
42+
logging.warning(
43+
"For conference \"{}\", years {} are missing - so min year={} "
44+
"- to add an exception please modify db/config.py - variable missing_years".format(
45+
conf,
46+
", ".join([str(y) for y in sorted(list(mis_years))]),
47+
min_year
48+
))
49+
return min_year, max_year
4550

4651
confs_inter = {
47-
conf: set_to_tuple(conf, confs[conf])
52+
conf: set_to_tuple(conf, confs[conf])
4853
for conf in sorted(confs.keys())
4954
}
5055
return confs_inter
5156

57+
5258
def get_confs_years_inter(db, confs_missing_years):
5359
return get_confs_years_inter_from_set(get_confs_years(db), confs_missing_years)
54-

‎header.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import datetime
22
from string import Template
33

4-
header_template="""% File generated by "$script"
4+
header_template = """% File generated by "$script"
55
% DO NOT MODIFY MANUALLY
66
77
%------------------------------------------------------------------
@@ -90,15 +90,15 @@
9090
def get_header(config, script, conf_years=None):
9191
timestamp = datetime.date.today().isoformat()
9292

93-
if conf_years!=None:
93+
if conf_years is not None:
9494
conf_years_complete = """%------------------------------------------------------------------
9595
% List of complete conferences:
9696
%""".split("\n")
97-
97+
9898
for conf in sorted(iter(conf_years.keys()), key=lambda x: config.get_conf_name(x)):
9999
(start, end) = conf_years[conf]
100100
name = config.get_conf_name(conf)
101-
conf_years_complete.append("% {}:{}{} - {}".format(name, " "*(16-len(conf)-1), start, end))
101+
conf_years_complete.append("% {}:{}{} - {}".format(name, " " * (16 - len(conf) - 1), start, end))
102102

103103
conf_years_complete = "\n".join(conf_years_complete)
104104
conf_years_complete += """\n%
@@ -109,16 +109,16 @@ def get_header(config, script, conf_years=None):
109109

110110
conf_labels = []
111111
for conf_key in sorted(
112-
iter(config.confs.keys()),
113-
key = lambda x:config.get_conf_name(x)
112+
iter(config.confs.keys()),
113+
key=lambda x: config.get_conf_name(x)
114114
):
115115
conf_name = config.get_conf_name(conf_key)
116-
conf_labels.append("% {}:{}{}\n".format(conf_name, " "*(16-len(conf_name)-1), conf_key))
116+
conf_labels.append("% {}:{}{}\n".format(conf_name, " " * (16 - len(conf_name) - 1), conf_key))
117117
conf_labels = ''.join(conf_labels)
118118

119119
return Template(header_template).substitute(
120-
script = script,
121-
timestamp = timestamp,
122-
conf_years_complete = conf_years_complete,
123-
conf_labels = conf_labels
124-
)
120+
script=script,
121+
timestamp=timestamp,
122+
conf_years_complete=conf_years_complete,
123+
conf_labels=conf_labels
124+
)

‎web2py_ctrl_default.py

+12-11
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,29 @@
44

55
re_years = re.compile(r"^\s*(\d*)\s*(-\s*(\d*)\s*)?$")
66

7-
def get_years(confs, vars):
7+
8+
def get_years(confs: iter, vars: dict) -> tuple[dict, dict, int]:
89
errors = {}
910
years = {}
1011
nb = 0
1112

1213
for conf in confs:
13-
years_conf = vars["years"+conf["key"]]
14+
years_conf = vars["years" + conf["key"]]
1415
r = re_years.match(years_conf)
15-
if r != None:
16+
if r is not None:
1617
a = r.group(1)
1718
b = r.group(2)
1819
c = r.group(3)
1920

20-
a = None if a=="" else a
21-
b = None if b=="" else b
22-
c = None if c=="" else c
21+
a = None if a == "" else a
22+
b = None if b == "" else b
23+
c = None if c == "" else c
2324

24-
start_year = int(a) if a!=None else a
25-
end_year = int(c) if c!=None else c
26-
end_year = start_year if b == None else end_year
25+
start_year = int(a) if a is not None else a
26+
end_year = int(c) if c is not None else c
27+
end_year = start_year if b is not None else end_year
2728

28-
if a != None or b != None:
29+
if a is not None or b is not None:
2930
start_year = max(start_year, conf["start_year"]) if start_year != None else conf["start_year"]
3031
end_year = min(end_year, conf["end_year"]) if end_year != None else conf["end_year"]
3132

@@ -38,4 +39,4 @@ def get_years(confs, vars):
3839
else:
3940
errors[conf["key"]] = True
4041

41-
return (errors, years, nb)
42+
return errors, years, nb

0 commit comments

Comments
 (0)
Please sign in to comment.