Skip to content

Commit d0ecef0

Browse files
committed
Fix bugs in cat and dumb
1 parent f020b91 commit d0ecef0

File tree

5 files changed

+102
-40
lines changed

5 files changed

+102
-40
lines changed

python/ccdb/cmd/commands/cat.py

+32-35
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
from ccdb import TypeTable, Assignment
66
from ccdb import AlchemyProvider
77
from ccdb.cmd import CliCommandBase, UtilityArgumentParser
8-
from ccdb.path_utils import ParseRequestResult, parse_request
9-
from ccdb import BraceMessage as Lfm # lfm is aka log format message. See BraceMessage desc about
10-
from sqlalchemy.orm.exc import NoResultFound
8+
from ccdb.path_utils import ParseRequestResult, parse_request, parse_time
9+
from ccdb import BraceMessage as Lfm # lfm is aka log format message. See BraceMessage desc about
1110

1211
log = logging.getLogger("ccdb.cmd.commands.cat")
1312

@@ -33,13 +32,6 @@ class Cat(CliCommandBase):
3332

3433
def __init__(self, context):
3534
CliCommandBase.__init__(self, context)
36-
self.raw_entry = "/" # object path with possible pattern, like /mole/*
37-
self.path = "/" # parent path
38-
self.raw_table_path = ""
39-
self.ass_id = 0
40-
self.print_horizontal = True
41-
42-
self.request = ParseRequestResult()
4335

4436
# ----------------------------------------
4537
# process
@@ -59,7 +51,7 @@ def execute(self, args):
5951

6052
parsed_args = self.process_arguments(args)
6153

62-
if self.ass_id:
54+
if parsed_args.ass_id:
6355
assignment = self.get_assignment_by_id(parsed_args.ass_id)
6456
else:
6557
assignment = self.get_assignment_by_request(parsed_args.request)
@@ -123,7 +115,7 @@ def get_assignment_by_request(self, request):
123115
# ----------------------------------------
124116
def process_arguments(self, args):
125117
parser = UtilityArgumentParser(add_help=False)
126-
parser.add_argument("obj_name", default="")
118+
parser.add_argument("obj_name", default="", nargs='?')
127119

128120
# border
129121
group = parser.add_mutually_exclusive_group()
@@ -140,44 +132,49 @@ def process_arguments(self, args):
140132
group.add_argument("-c", "--comments", action="store_true", dest='show_comments', default=False)
141133
group.add_argument("-nc", "--no-comments", action="store_false", dest='show_comments')
142134

143-
# time
144-
group = parser.add_mutually_exclusive_group()
145-
group.add_argument("-t", "--time", action="store_true", dest='show_date', default=False)
146-
group.add_argument("-nt", "--no-time", action="store_true", dest='show_date')
135+
# horizontal or vertical
136+
parser.add_argument("-ph", "--horizontal", action="store_true", dest='user_request_print_horizontal')
137+
parser.add_argument("-pv", "--vertical", action="store_true", dest='user_request_print_vertical')
147138

148-
parser.add_argument("-d", "--directory")
139+
# Assignment parameters
149140
parser.add_argument("-v", "--variation")
150-
parser.add_argument("-a", "--id")
141+
parser.add_argument("-a", "--id", dest='ass_id')
151142
parser.add_argument("-r", "--run")
152-
parser.add_argument("-f", "--file")
153-
parser.add_argument("-ph", "--horizontal", action="store_true", dest='user_request_print_horizontal')
154-
parser.add_argument("-pv", "--vertical", action="store_true", dest='user_request_print_vertical')
143+
parser.add_argument("-t", "--time", required=False)
155144

145+
# Parse args
156146
result = parser.parse_args(args)
157-
# parse loop
158-
if result.obj_name:
159-
# it probably must be a request or just a table name
160-
result.request = parse_request(result.obj_name)
147+
148+
# Parse ccdb request
149+
result.request = parse_request(result.obj_name) if result.obj_name else ParseRequestResult()
150+
151+
# Check if user set the variation
152+
if not result.request.variation_is_parsed and result.variation:
153+
result.request.variation = result.variation
154+
result.request.variation_is_parsed = True
161155

162156
# Check if user set the default variation
163157
if not result.request.variation_is_parsed and self.context.current_variation:
164158
result.request.variation = self.context.current_variation
165159
result.request.variation_is_parsed = True
166160

161+
# Check if user set the run
162+
if not result.request.run_is_parsed and result.run:
163+
result.request.run = int(result.run)
164+
result.request.run_is_parsed = True
165+
167166
# Check if user set the default run
168167
if not result.request.run_is_parsed and self.context.current_run:
169168
result.request.run = self.context.current_run
170169
result.request.run_is_parsed = True
171170

172-
return result
171+
# Check if user set time
172+
if not result.request.time_is_parsed and result.time:
173+
result.request.time_str = result.time
174+
result.request.time = parse_time(result.time)
175+
result.request.time_is_parsed = True
173176

174-
# ----------------------------------------
175-
# validate
176-
# ----------------------------------------
177-
def validate(self):
178-
if not self.raw_table_path:
179-
return False
180-
return True
177+
return result
181178

182179
# --------------------------------------------------------------------------------
183180
# print_assignment_vertical
@@ -209,14 +206,14 @@ def print_assignment_horizontal(self, assignment, print_header=True, display_bor
209206

210207
# PRINT COMMENTS
211208
if comments:
212-
# this lsep hack is for Windows. Where os.linesep is \r\n, but file might have \n only line seps
209+
# this line sep hack is for Windows. Where os.linesep is \r\n, but file might have \n only line seps
213210
comment_str = assignment.comment
214211
if os.name == 'nt':
215212
# we make sure that it is always os.linesep on windows
216213
comment_str = comment_str.replace('\r\n', '\n').replace('\n', os.linesep)
217214

218215
sharped_lines = "#" + str(comment_str).replace(os.linesep, os.linesep + "#")
219-
print (sharped_lines)
216+
print(sharped_lines)
220217

221218
column_names = [column.name for column in table.columns]
222219
column_types = [column.type for column in table.columns]

python/ccdb/cmd/commands/dump.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def execute(self, args):
2929

3030
self.theme = NoColorTheme()
3131
# self.context.utils["cat"].theme = NoColorTheme()
32-
return "cat --no-borders --no-header --comments --time --horizontal " + " ".join(args)
32+
return "cat --no-borders --no-header --comments --horizontal " + " ".join(args)
3333

3434
def print_help(self):
3535
"""Prints help of the command"""

python/ccdb/path_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def parse_time(time_str="-1", max_time_by_default=True):
137137
month = int(tmp_str)
138138

139139
# check for 30 day month
140-
if month in [9, 4, 6, 10]:
140+
if month in [4, 6, 9, 11]:
141141
day = 30
142142
if month == 2:
143143
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:

python/tests/integ_test_cli_manager.py

+63-3
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,64 @@ def tearDown(self):
6868
# restore stdout
6969
sys.stdout = self.saved_stdout
7070

71+
def clear_output(self):
72+
# Reset output
73+
self.output.seek(0)
74+
self.output.truncate()
75+
7176
def test_context(self):
7277
"""Test utils are loaded"""
7378
self.assertTrue(len(self.cli.utils) > 0)
7479

7580
def test_cat(self):
76-
"""cat. General help"""
81+
"""cat. Return constants"""
7782
self.cli.process_command_line("cat /test/test_vars/test_table")
7883
self.assertIn("2.3", self.output.getvalue())
7984

80-
def test_cat_user_set_variation(self):
81-
"""In non-interactive mode, cat should handle path without leading / as absolute anyway"""
85+
def test_cat_by_id(self):
86+
"""cat. Return """
87+
self.cli.process_command_line("cat -a 2")
88+
self.assertIn("6.0", self.output.getvalue())
89+
90+
self.clear_output()
91+
92+
# Same but full flag
93+
self.cli.process_command_line("cat --id=2")
94+
self.assertIn("6.0", self.output.getvalue())
95+
96+
def test_cat_time(self):
97+
"""cat. Test specifying time to get particular constants"""
98+
99+
# Test data has next records for test_table:
100+
# /test/test_vars/test_table
101+
# (ID) (Created) (Modified) (variation) (run range) (comments)
102+
# 5 2012-10-30 23-48-43 2012-10-30 23-48-43 subtest 0-inf Test assignment for
103+
# 4 2012-10-30 23-48-42 2012-10-30 23-48-42 default 0-inf Test assignment for
104+
# 2 2012-08-30 23-48-42 2012-08-30 23-48-42 test 500-3000 Test assignment for
105+
# 1 2012-07-30 23-48-42 2012-07-30 23-48-42 default 0-inf Test assignment for
106+
107+
# It should return assignment with --id=1
108+
self.cli.process_command_line('cat -t 2012-08 /test/test_vars/test_table')
109+
self.assertIn("1.11", self.output.getvalue())
110+
111+
def test_cat_run_variation(self):
112+
"""cat. If user sets ccdb -r <run> -v <variation> <command> ... it goes to context.current_run and context.current_variation.
113+
They should be used as a fallback by command if no run or variation is given to command
114+
"""
115+
116+
# Test DB has a test data assignment for:
117+
# variation: test
118+
# runs: 500-2000
119+
# it has data: 1.0|2.0|3.0|4.0|5.0|6.0
120+
self.cli.context.current_variation = "default" # <= should not be used as cat overwrites variation
121+
self.cli.context.current_run = 0 # <= should not be used as cat overwrites run
122+
self.cli.process_command_line("cat -v test -r 600 /test/test_vars/test_table")
123+
self.assertIn("6.0", self.output.getvalue())
124+
125+
def test_cat_default_run_variation(self):
126+
"""If user sets ccdb -r <run> -v <variation> <command> ... it goes to context.current_run and context.current_variation.
127+
They should be used as a fallback by command if no run or variation is given to command
128+
"""
82129

83130
# Test DB has a test data assignment for:
84131
# variation: test
@@ -89,6 +136,19 @@ def test_cat_user_set_variation(self):
89136
self.cli.process_command_line("cat /test/test_vars/test_table")
90137
self.assertIn("6.0", self.output.getvalue())
91138

139+
def test_cat_request_overwrite(self):
140+
"""cat. If user sets ccdb -r <run> -v <variation> cat <request> ... But the requests sets different run and
141+
and variation, then request has the top priority
142+
"""
143+
144+
# Test DB has a test data assignment for:
145+
# variation: test
146+
# runs: 500-2000
147+
# it has data: 1.0|2.0|3.0|4.0|5.0|6.0
148+
self.cli.context.current_variation = "default" # <= should not be used as cat overwrites variation
149+
self.cli.context.current_run = 0 # <= should not be used as cat overwrites run
150+
self.cli.process_command_line("cat -v default -r 0 /test/test_vars/test_table:600:test")
151+
self.assertIn("6.0", self.output.getvalue())
92152

93153
def test_cat_not_abs_path(self):
94154
"""In non-interactive mode, cat should handle path without leading / as absolute anyway"""

python/tests/unit_test_path_utils.py

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ def test_parse_time(self):
1414
dt = ccdb.path_utils.parse_time("2012-11x13") # set different symbols as separators
1515
self.assertEqual(dt.strftime("%Y-%m-%d_%H-%M-%S"), "2012-11-13_23-59-59")
1616

17+
def test_parse_year_month(self):
18+
# test of simple parse
19+
dt = ccdb.path_utils.parse_time("2012-11") # set different symbols as separators
20+
self.assertEqual(dt.strftime("%Y-%m-%d_%H-%M-%S"), "2012-11-30_23-59-59")
21+
1722
def test_full_parse_request(self):
1823
# test full request
1924
result = ccdb.path_utils.parse_request("/test/test_vars/test_table:100:mc:2012-11-13_12:37:55")

0 commit comments

Comments
 (0)