Skip to content

Commit e81ef86

Browse files
committed
refactor: extract config to multiple file
1 parent 4610797 commit e81ef86

File tree

4 files changed

+84
-78
lines changed

4 files changed

+84
-78
lines changed

config/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .config import ServerConfig

config.py renamed to config/cmd.py

Lines changed: 2 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
from functools import cache, partial
1+
from functools import partial
22
import glob
33
import inspect
4-
import json
54
import os
65
import subprocess
76
import sys
87

8+
from .config import ServerConfig
99

1010
def usage(name, msg=None):
1111
if msg:
@@ -99,77 +99,3 @@ def get_workspace():
9999
print("updated buildServer.json")
100100

101101

102-
def _config_property(name, default=None, doc=None, delete_none=True):
103-
"""
104-
default only affect getter, not write into data
105-
"""
106-
def fget(self):
107-
return self.data.get(name, default)
108-
109-
def fset(self, value):
110-
if delete_none and value is None:
111-
self.data.pop(name, None)
112-
else:
113-
self.data[name] = value
114-
115-
def fdel(self):
116-
del self.data[name]
117-
118-
return property(fget, fset, fdel, doc)
119-
120-
121-
class ServerConfig(object):
122-
"""this class control all user config. options:
123-
124-
kind: xcode|manual # where to find flags. default: manual
125-
when kind=xcode:
126-
workspace: the bind workspace path
127-
scheme: the bind scheme
128-
build_root: the build_root find from xcworkspace and scheme
129-
when kind=manual(or no kind):
130-
indexStorePath?: the manual parsed index path. may not exists
131-
132-
user can change scheme by call `xcode-build-server config`,
133-
or change to manual by call `xcode-build-server parse` directly.
134-
135-
after config change. server should change to new flags too..
136-
137-
other config:
138-
skip_validate_bin: if true, will skip validate bin for background parser
139-
"""
140-
141-
# TODO: distinguish configuration and destination #
142-
143-
default_path = "buildServer.json"
144-
145-
kind = _config_property("kind", default="manual")
146-
workspace = _config_property("workspace")
147-
scheme = _config_property("scheme")
148-
build_root = _config_property("build_root")
149-
indexStorePath = _config_property("indexStorePath")
150-
151-
skip_validate_bin = _config_property("skip_validate_bin")
152-
153-
@cache
154-
def shared():
155-
return ServerConfig(ServerConfig.default_path)
156-
157-
def __init__(self, path):
158-
self.path = os.path.abspath(path)
159-
if os.path.exists(path):
160-
with open(path, "r") as f:
161-
self.data = json.load(f)
162-
else:
163-
self.data = {}
164-
165-
self.data.update({
166-
"name": "xcode build server",
167-
"version": "0.2",
168-
"bspVersion": "2.0",
169-
"languages": ["c", "cpp", "objective-c", "objective-cpp", "swift"],
170-
"argv": [sys.argv[0]]
171-
})
172-
173-
def save(self):
174-
with open(self.path, "w") as f:
175-
json.dump(self.data, f, indent="\t")

config/config.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
from functools import cache
2+
import json
3+
import os
4+
import sys
5+
6+
7+
def _config_property(name, default=None, doc=None, delete_none=True):
8+
"""
9+
default only affect getter, not write into data
10+
"""
11+
def fget(self):
12+
return self.data.get(name, default)
13+
14+
def fset(self, value):
15+
if delete_none and value is None:
16+
self.data.pop(name, None)
17+
else:
18+
self.data[name] = value
19+
20+
def fdel(self):
21+
del self.data[name]
22+
23+
return property(fget, fset, fdel, doc)
24+
25+
class ServerConfig(object):
26+
"""this class control all user config. options:
27+
28+
kind: xcode|manual # where to find flags. default: manual
29+
when kind=xcode:
30+
workspace: the bind workspace path
31+
scheme: the bind scheme
32+
build_root: the build_root find from xcworkspace and scheme
33+
when kind=manual(or no kind):
34+
indexStorePath?: the manual parsed index path. may not exists
35+
36+
user can change scheme by call `xcode-build-server config`,
37+
or change to manual by call `xcode-build-server parse` directly.
38+
39+
after config change. server should change to new flags too..
40+
41+
other config:
42+
skip_validate_bin: if true, will skip validate bin for background parser
43+
"""
44+
45+
# TODO: distinguish configuration and destination #
46+
47+
default_path = "buildServer.json"
48+
49+
kind = _config_property("kind", default="manual")
50+
workspace = _config_property("workspace")
51+
scheme = _config_property("scheme")
52+
build_root = _config_property("build_root")
53+
indexStorePath = _config_property("indexStorePath")
54+
55+
skip_validate_bin = _config_property("skip_validate_bin")
56+
57+
@cache
58+
def shared():
59+
return ServerConfig(ServerConfig.default_path)
60+
61+
def __init__(self, path):
62+
self.path = os.path.abspath(path)
63+
if os.path.exists(path):
64+
with open(path, "r") as f:
65+
self.data = json.load(f)
66+
else:
67+
self.data = {}
68+
69+
self.data.update({
70+
"name": "xcode build server",
71+
"version": "0.2",
72+
"bspVersion": "2.0",
73+
"languages": ["c", "cpp", "objective-c", "objective-cpp", "swift"],
74+
"argv": [sys.argv[0]]
75+
})
76+
77+
def save(self):
78+
with open(self.path, "w") as f:
79+
json.dump(self.data, f, indent="\t")

xcode-build-server

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ def main():
7070
return argv
7171

7272
if sys.argv[1] == "config":
73-
import config
73+
from config.cmd import main
7474

75-
config.main(subcommand_argv())
75+
main(subcommand_argv())
7676
elif sys.argv[1] == "parse":
7777
import xclog_parser
7878

0 commit comments

Comments
 (0)