Skip to content

Commit d974826

Browse files
committed
Refactor main function
Signed-off-by: Paul Barker <paul@pbarker.dev> Ref: #26
1 parent 44523a4 commit d974826

File tree

4 files changed

+42
-45
lines changed

4 files changed

+42
-45
lines changed

mirrorshades

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ from os.path import dirname, join
88
src_path = join(dirname(__file__), "src")
99
sys.path.insert(0, src_path)
1010

11-
import mirrorshades # noqa: E402
11+
from mirrorshades.__main__ import main # noqa: E402
1212

13-
mirrorshades.main()
13+
main()

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ install_requires =
3333

3434
[options.entry_points]
3535
console_scripts =
36-
mirrorshades = mirrorshades:main
36+
mirrorshades = mirrorshades.__main__:main
3737

3838
[options.packages.find]
3939
where=src

src/mirrorshades/__init__.py

-40
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,4 @@
11
# Copyright (c) 2020-2021 Paul Barker <paul@pbarker.dev>
22
# SPDX-License-Identifier: Apache-2.0
33

4-
import argparse
5-
import logging
6-
import sys
7-
84
__version__ = "0.2.1-dev"
9-
10-
11-
def parse_args():
12-
parser = argparse.ArgumentParser(
13-
prog="mirrorshades", description="Data mirroring tool"
14-
)
15-
parser.add_argument(
16-
"config_path",
17-
default="mirrorshades.yml",
18-
nargs="?",
19-
help="path to the configuration file (defaults to 'mirrorshades.yml' "
20-
"in the current directory)",
21-
)
22-
parser.add_argument("--source", "-s", help="select a single source to synchronize")
23-
parser.add_argument(
24-
"--version", action="version", version=f"mirrorshades {__version__}"
25-
)
26-
return parser.parse_args()
27-
28-
29-
def main():
30-
from . import config
31-
32-
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.INFO)
33-
args = parse_args()
34-
config.load(args.config_path)
35-
36-
if args.source:
37-
try:
38-
config.sources[args.source].mirror()
39-
except KeyError:
40-
logging.error(f"Source '{args.source}' does not exist in config file")
41-
sys.exit(1)
42-
else:
43-
for source in config.sources.values():
44-
source.mirror()

src/mirrorshades/__main__.py

+39-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,43 @@
11
# Copyright (c) 2020-2021 Paul Barker <paul@pbarker.dev>
22
# SPDX-License-Identifier: Apache-2.0
33

4-
from . import main
4+
import argparse
5+
import logging
6+
import sys
7+
from . import __version__, config
58

6-
main()
9+
10+
def parse_args():
11+
parser = argparse.ArgumentParser(
12+
prog="mirrorshades",
13+
description="An easily extensible tool for mirroring data from "
14+
"git repositories, cloud storage, mail servers and other remote sources.",
15+
)
16+
parser.add_argument(
17+
"config_path",
18+
default="mirrorshades.yml",
19+
nargs="?",
20+
help="path to the configuration file "
21+
"(defaults to 'mirrorshades.yml' in the current directory)",
22+
)
23+
parser.add_argument("--source", "-s", help="select a single source to synchronize")
24+
parser.add_argument(
25+
"--version", action="version", version=f"mirrorshades {__version__}"
26+
)
27+
return parser.parse_args()
28+
29+
30+
def main():
31+
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.INFO)
32+
args = parse_args()
33+
config.load(args.config_path)
34+
35+
if args.source:
36+
try:
37+
config.sources[args.source].mirror()
38+
except KeyError:
39+
logging.error(f"Source '{args.source}' does not exist in config file")
40+
sys.exit(1)
41+
else:
42+
for source in config.sources.values():
43+
source.mirror()

0 commit comments

Comments
 (0)