Skip to content

Commit 522200f

Browse files
committed
Add flags to set default site and workdir
1 parent 3128ffa commit 522200f

File tree

5 files changed

+108
-51
lines changed

5 files changed

+108
-51
lines changed

ACedIt/main.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ def validate_args(args):
1010
print 'Sorry. ACedIt only supports %s till now.' % (', '.join(supported_sites))
1111
sys.exit(0)
1212

13+
if args['default_site'] is not None or args['workdir'] is not None:
14+
return
15+
1316
if args['source'] and (args['contest'] or args['problem'] or args['force']):
1417
print 'ACedIt --run <source_file> doesn\'t support other flags'
1518
sys.exit(0)
@@ -26,19 +29,27 @@ def validate_args(args):
2629

2730

2831
def main():
29-
args = util.Utilities.parse_flags()
32+
args = util.Utilities.parse_flags(supported_sites)
3033
validate_args(args)
3134

3235
try:
33-
if args['source']:
36+
if args['default_site']:
37+
# set default site
38+
util.Utilities.set_constants('default_site', args['default_site'])
39+
40+
if args['workdir']:
41+
# set working directory
42+
util.Utilities.set_constants('workdir', args['workdir'], supported_sites)
43+
44+
elif args['source']:
3445
# run code
3546
util.Utilities.run_solution(args['source'])
3647

3748
elif args['problem'] is not None:
3849
# fetch single problem
3950
util.Utilities.download_problem_testcases(args)
4051

41-
else:
52+
elif args['contest']:
4253
# fetch all problems for the contest
4354
util.Utilities.download_contest_testcases(args)
4455

ACedIt/util.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Utilities:
2828
}
2929

3030
@staticmethod
31-
def parse_flags():
31+
def parse_flags(supported_sites):
3232
"""
3333
Utility function to parse command line flags
3434
"""
@@ -56,6 +56,15 @@ def parse_flags():
5656
dest='source_file',
5757
help='Name of source file to be run')
5858

59+
parser.add_argument('--set-default-site',
60+
dest='default_site',
61+
choices=supported_sites,
62+
help='Name of default site to be used when -s flag is not specified')
63+
64+
parser.add_argument('--set-workdir',
65+
dest='workdir',
66+
help='ABSOLUTE PATH to working directory')
67+
5968
parser.set_defaults(force=False)
6069

6170
args = parser.parse_args()
@@ -82,9 +91,39 @@ def parse_flags():
8291
flags['force'] = args.force
8392
flags['site'] = flags['site'].lower()
8493
flags['source'] = args.source_file
94+
flags['default_site'] = args.default_site
95+
flags['workdir'] = args.workdir
8596

8697
return flags
8798

99+
@staticmethod
100+
def set_constants(key, value, supported_sites=None):
101+
"""
102+
Utility method to set default site and working directory
103+
"""
104+
with open(os.path.join(Utilities.cache_dir, 'constants.json'), 'r+') as f:
105+
data = f.read()
106+
data = json.loads(data)
107+
previous_value = data[key]
108+
data[key] = value
109+
f.seek(0)
110+
f.write(json.dumps(data, indent=2))
111+
f.truncate()
112+
113+
print 'Set %s to %s' % (key, value)
114+
115+
if key == 'workdir':
116+
workdir = os.path.join(value, 'ACedIt')
117+
previous_path = os.path.join(previous_value, 'ACedIt')
118+
for site in supported_sites:
119+
if not os.path.isdir(os.path.join(workdir, site)):
120+
os.makedirs(os.path.join(workdir, site))
121+
choice = raw_input('Remove all files from previous working directory %s? (y/N) : ' % (previous_path))
122+
if choice == 'y':
123+
from shutil import rmtree
124+
rmtree(previous_path)
125+
126+
88127
@staticmethod
89128
def create_workdir_structure(site, contest):
90129
"""

README.md

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ pip install ACedIt
2121
```
2222
usage: ACedIt [-h] [-s SITE] [-c CONTEST] [-p PROBLEM] [-f]
2323
[--run SOURCE_FILE]
24+
[--set-default-site {codeforces,codechef,hackerrank,spoj}]
25+
[--set-workdir WORKDIR]
2426
2527
optional arguments:
2628
-h, --help show this help message and exit
@@ -33,23 +35,31 @@ optional arguments:
3335
The problem code, e.g. OAK, PRMQ etc
3436
-f, --force Force download the test cases, even if they are cached
3537
--run SOURCE_FILE Name of source file to be run
38+
--set-default-site {codeforces,codechef,hackerrank,spoj}
39+
Name of default site to be used when -s flag is not
40+
specified
41+
--set-workdir WORKDIR
42+
ABSOLUTE PATH to working directory
3643
3744
```
45+
During installation, the default site is set to `codeforces` and the default working directory is set to `/home/your-username/ACedIt`. You can change them anytime using the above mentioned flags.
3846

3947
ACedIt requires the following working directory structure.
4048
```
41-
root-dir
49+
workdir
4250
|
43-
|- Site1
44-
| |- Contest1
45-
| | |- Problem1
46-
| | |- Problem2
47-
| |- Contest2
48-
| | |- Problem1
49-
| | |- Problem2
50-
|- Site2
51-
| |- Contest1
52-
| | |- Problem1
51+
|-ACedIt
52+
| |
53+
| |- Site1
54+
| | |- Contest1
55+
| | | |- Problem1
56+
| | | |- Problem2
57+
| | |- Contest2
58+
| | | |- Problem1
59+
| | | |- Problem2
60+
| |- Site2
61+
| | |- Contest1
62+
| | | |- Problem1
5363
5464
```
5565
During installation, ACedIt will set up the basic working directory structure.

README.rst

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ Usage
3838
::
3939

4040
usage: ACedIt [-h] [-s SITE] [-c CONTEST] [-p PROBLEM] [-f]
41-
[--run SOURCE_FILE]
41+
[--run SOURCE_FILE]
42+
[--set-default-site {codeforces,codechef,hackerrank,spoj}]
43+
[--set-workdir WORKDIR]
4244

4345
optional arguments:
4446
-h, --help show this help message and exit
@@ -51,23 +53,33 @@ Usage
5153
The problem code, e.g. OAK, PRMQ etc
5254
-f, --force Force download the test cases, even if they are cached
5355
--run SOURCE_FILE Name of source file to be run
56+
--set-default-site {codeforces,codechef,hackerrank,spoj}
57+
Name of default site to be used when -s flag is not
58+
specified
59+
--set-workdir WORKDIR
60+
ABSOLUTE PATH to working directory
61+
62+
During installation, the default site is set to ``codeforces`` and the default working directory is set to ``/home/your-username/ACedIt``. You can change them anytime using the above mentioned flags.
5463

5564
ACedIt requires the following working directory structure.
5665

5766
::
5867

59-
root-dir
68+
workdir
6069
|
61-
|- Site1
62-
| |- Contest1
63-
| | |- Problem1
64-
| | |- Problem2
65-
| |- Contest2
66-
| | |- Problem1
67-
| | |- Problem2
68-
|- Site2
69-
| |- Contest1
70-
| | |- Problem1
70+
|-ACedIt
71+
| |
72+
| |- Site1
73+
| | |- Contest1
74+
| | | |- Problem1
75+
| | | |- Problem2
76+
| | |- Contest2
77+
| | | |- Problem1
78+
| | | |- Problem2
79+
| |- Site2
80+
| | |- Contest1
81+
| | | |- Problem1
82+
7183

7284
| During installation, ACedIt will set up the basic working directory
7385
structure.

setup.py

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,20 @@
1414
if sys.version_info >= (3,):
1515
extra['use_2to3'] = True
1616

17-
print 'Please specify a default site (codeforces) :',
18-
default_site = raw_input()
19-
if default_site == '':
20-
print 'Setting codeforces as default site'
21-
default_site = 'codeforces'
22-
23-
print 'Please specify working directory (relative to home directory) :',
24-
workdir = raw_input()
25-
if workdir == '':
26-
workdir = 'ACedIt'
27-
workdir = os.path.join(os.path.expanduser('~'), workdir)
17+
default_site = 'codeforces'
18+
workdir = os.path.join(os.path.expanduser('~'), 'ACedIt')
19+
cache_dir = os.path.join(os.path.expanduser('~'), '.cache', 'ACedIt')
2820

2921
from ACedIt.main import supported_sites
3022

31-
if not os.path.isdir(workdir):
32-
os.makedirs(workdir)
33-
for site in supported_sites:
34-
os.makedirs(os.path.join(workdir, site))
35-
36-
cache_dir = os.path.join(os.path.expanduser('~'), '.cache', 'ACedIt')
37-
if not os.path.isdir(cache_dir):
38-
os.makedirs(cache_dir)
39-
for site in supported_sites:
23+
for site in supported_sites:
24+
# create cache directory and working directory structure
25+
if not os.path.isdir(os.path.join(cache_dir, site)):
4026
os.makedirs(os.path.join(cache_dir, site))
27+
if not os.path.isdir(os.path.join(workdir, site)):
28+
os.makedirs(os.path.join(workdir, site))
4129

42-
print 'Setting ' + workdir + ' as working directory'
43-
44-
45-
data = {'default_site': default_site.strip(), 'workdir': workdir,
30+
data = {'default_site': default_site.strip(), 'workdir': os.path.expanduser('~'),
4631
'cachedir': cache_dir}
4732
with open(os.path.join(cache_dir, 'constants.json'), 'w') as f:
4833
f.write(json.dumps(data, indent=2))

0 commit comments

Comments
 (0)