file_query_text 0.1.7__tar.gz → 0.1.9__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: file_query_text
3
- Version: 0.1.7
3
+ Version: 0.1.9
4
4
  Summary: SQL-like interface for querying files in your filesystem
5
5
  Author-email: nik <42a11b@nikdav.is>
6
6
  License-Expression: MIT
@@ -14,7 +14,7 @@ Requires-Dist: pyparsing>=3.2.3
14
14
  Provides-Extra: dev
15
15
  Requires-Dist: pytest>=8.3.5; extra == "dev"
16
16
 
17
- # File Query
17
+ # File Query (fq)
18
18
 
19
19
  A SQL-like interface for querying files in your filesystem.
20
20
 
@@ -111,4 +111,32 @@ fq "SELECT * FROM '.' WHERE (extension == 'jpg' OR extension == 'png') AND size
111
111
 
112
112
  # Find files with 'config' in their path
113
113
  fq "path == '.*config.*'"
114
+
115
+ ### Using wildcards with the LIKE operator
116
+
117
+ Find all Python files with "test" in their name:
118
+ ```
119
+ fq "name LIKE '%test%.py'"
120
+ ```
121
+
122
+ Find all files with a specific prefix:
123
+ ```
124
+ fq "name LIKE 'config%'"
125
+ ```
126
+
127
+ Find all markdown files in a specific year's folder:
128
+ ```
129
+ fq "path LIKE '%/2023/%' AND extension == 'md'"
130
+ ```
131
+
132
+ ### Excluding files with NOT LIKE
133
+
134
+ Find all JavaScript files in src directory except those in lib folders:
135
+ ```
136
+ fq "path LIKE 'src%' AND path NOT LIKE '%lib%' AND extension == 'js'"
137
+ ```
138
+
139
+ Find all Python files that don't have "test" in their name:
140
+ ```
141
+ fq "extension == 'py' AND name NOT LIKE '%test%'"
114
142
  ```
@@ -1,4 +1,4 @@
1
- # File Query
1
+ # File Query (fq)
2
2
 
3
3
  A SQL-like interface for querying files in your filesystem.
4
4
 
@@ -95,4 +95,32 @@ fq "SELECT * FROM '.' WHERE (extension == 'jpg' OR extension == 'png') AND size
95
95
 
96
96
  # Find files with 'config' in their path
97
97
  fq "path == '.*config.*'"
98
+
99
+ ### Using wildcards with the LIKE operator
100
+
101
+ Find all Python files with "test" in their name:
102
+ ```
103
+ fq "name LIKE '%test%.py'"
104
+ ```
105
+
106
+ Find all files with a specific prefix:
107
+ ```
108
+ fq "name LIKE 'config%'"
109
+ ```
110
+
111
+ Find all markdown files in a specific year's folder:
112
+ ```
113
+ fq "path LIKE '%/2023/%' AND extension == 'md'"
114
+ ```
115
+
116
+ ### Excluding files with NOT LIKE
117
+
118
+ Find all JavaScript files in src directory except those in lib folders:
119
+ ```
120
+ fq "path LIKE 'src%' AND path NOT LIKE '%lib%' AND extension == 'js'"
121
+ ```
122
+
123
+ Find all Python files that don't have "test" in their name:
124
+ ```
125
+ fq "extension == 'py' AND name NOT LIKE '%test%'"
98
126
  ```
@@ -2,4 +2,4 @@
2
2
  SQL-like interface for querying files in your filesystem.
3
3
  """
4
4
 
5
- __version__ = "0.1.7"
5
+ __version__ = "0.1.9"
@@ -27,6 +27,7 @@ WHERE = Suppress(CaselessKeyword("WHERE"))
27
27
  AND = CaselessKeyword("AND")
28
28
  OR = CaselessKeyword("OR")
29
29
  NOT = CaselessKeyword("NOT")
30
+ LIKE = CaselessKeyword("LIKE")
30
31
 
31
32
  # Define identifiers and literals
32
33
  IDENTIFIER = Word(alphas + "_")
@@ -36,7 +37,7 @@ NUMERIC_LITERAL = pyparsing_common.integer
36
37
  DIRECTORY_LIST = Group(delimitedList(STRING_LITERAL))
37
38
 
38
39
  # Define comparison operators
39
- COMPARISON_OP = oneOf("= == != <> < <= > >=")
40
+ COMPARISON_OP = oneOf("= == != <> < <= > >=") | LIKE
40
41
  ATTRIBUTE = IDENTIFIER + Suppress("=") + STRING_LITERAL
41
42
 
42
43
  # Define basic condition with support for both string and numeric literals
@@ -45,8 +46,15 @@ basic_condition = Group(IDENTIFIER + COMPARISON_OP + VALUE)
45
46
 
46
47
  # Define logical expressions using infixNotation for better handling of AND and OR
47
48
  condition_expr = Forward()
49
+
50
+ # Define a new pattern for the NOT LIKE operator
51
+ not_like_condition = Group(IDENTIFIER + NOT + LIKE + VALUE)
52
+
53
+ # Include both basic conditions and NOT LIKE conditions
54
+ basic_expr = basic_condition | not_like_condition
55
+
48
56
  condition_expr <<= infixNotation(
49
- basic_condition,
57
+ basic_expr,
50
58
  [
51
59
  (NOT, 1, opAssoc.RIGHT),
52
60
  (AND, 2, opAssoc.LEFT),
@@ -3,6 +3,7 @@ import sys
3
3
  from file_query_text.grammar import query # Import the fixed grammar
4
4
  import file_query_text.gitignore_parser as gitignore_parser
5
5
  import os.path
6
+ import re
6
7
 
7
8
 
8
9
  def parse_query(query_str):
@@ -128,6 +129,15 @@ def evaluate_conditions(file_path, condition):
128
129
  if op == "<=": return attr_val is not None and int(attr_val) <= int(val)
129
130
  if op == ">": return attr_val is not None and int(attr_val) > int(val)
130
131
  if op == ">=": return attr_val is not None and int(attr_val) >= int(val)
132
+ if op.upper() == "LIKE":
133
+ if attr_val is None:
134
+ return False
135
+ # Convert SQL LIKE pattern (with % wildcards) to regex pattern
136
+ # Escape any regex special characters in the pattern except %
137
+ pattern = re.escape(val).replace('\\%', '%') # Unescape % after escaping everything else
138
+ pattern = pattern.replace("%", ".*")
139
+ pattern = f"^{pattern}$" # Anchor pattern to match whole string
140
+ return bool(re.search(pattern, str(attr_val), re.IGNORECASE))
131
141
 
132
142
  # 2. Logical operations from infixNotation: [left, op, right]
133
143
  elif expr[1] == "AND":
@@ -139,6 +149,20 @@ def evaluate_conditions(file_path, condition):
139
149
  elif len(expr) == 2 and expr[0] == "NOT":
140
150
  return not eval_expr(expr[1])
141
151
 
152
+ # 4. Special case for NOT LIKE: [attr, 'NOT', 'LIKE', value]
153
+ elif len(expr) == 4 and expr[1] == "NOT" and expr[2] == "LIKE":
154
+ attr_val = get_file_attr(expr[0])
155
+ val = expr[3].strip("'") if isinstance(expr[3], str) else expr[3]
156
+
157
+ if attr_val is None:
158
+ return True # If attribute doesn't exist, NOT LIKE is True
159
+
160
+ # Convert SQL LIKE pattern (with % wildcards) to regex pattern
161
+ pattern = re.escape(val).replace('\\%', '%') # Unescape % after escaping everything else
162
+ pattern = pattern.replace("%", ".*")
163
+ pattern = f"^{pattern}$" # Anchor pattern to match whole string
164
+ return not bool(re.search(pattern, str(attr_val), re.IGNORECASE))
165
+
142
166
  return False
143
167
 
144
168
  return eval_expr(condition.asList())
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: file_query_text
3
- Version: 0.1.7
3
+ Version: 0.1.9
4
4
  Summary: SQL-like interface for querying files in your filesystem
5
5
  Author-email: nik <42a11b@nikdav.is>
6
6
  License-Expression: MIT
@@ -14,7 +14,7 @@ Requires-Dist: pyparsing>=3.2.3
14
14
  Provides-Extra: dev
15
15
  Requires-Dist: pytest>=8.3.5; extra == "dev"
16
16
 
17
- # File Query
17
+ # File Query (fq)
18
18
 
19
19
  A SQL-like interface for querying files in your filesystem.
20
20
 
@@ -111,4 +111,32 @@ fq "SELECT * FROM '.' WHERE (extension == 'jpg' OR extension == 'png') AND size
111
111
 
112
112
  # Find files with 'config' in their path
113
113
  fq "path == '.*config.*'"
114
+
115
+ ### Using wildcards with the LIKE operator
116
+
117
+ Find all Python files with "test" in their name:
118
+ ```
119
+ fq "name LIKE '%test%.py'"
120
+ ```
121
+
122
+ Find all files with a specific prefix:
123
+ ```
124
+ fq "name LIKE 'config%'"
125
+ ```
126
+
127
+ Find all markdown files in a specific year's folder:
128
+ ```
129
+ fq "path LIKE '%/2023/%' AND extension == 'md'"
130
+ ```
131
+
132
+ ### Excluding files with NOT LIKE
133
+
134
+ Find all JavaScript files in src directory except those in lib folders:
135
+ ```
136
+ fq "path LIKE 'src%' AND path NOT LIKE '%lib%' AND extension == 'js'"
137
+ ```
138
+
139
+ Find all Python files that don't have "test" in their name:
140
+ ```
141
+ fq "extension == 'py' AND name NOT LIKE '%test%'"
114
142
  ```
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "file_query_text"
7
- version = "0.1.7"
7
+ version = "0.1.9"
8
8
  description = "SQL-like interface for querying files in your filesystem"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.12"
@@ -432,3 +432,139 @@ def test_hidden_files(temp_dir):
432
432
  # Also check if every visible file is in results
433
433
  for visible_file in visible_files:
434
434
  assert visible_file in with_hidden_files, f"Visible file {visible_file} missing from results"
435
+
436
+ def test_like_operator_with_wildcards(temp_dir):
437
+ """Test LIKE operator with SQL-style percentage wildcards."""
438
+ # Create specific files with different naming patterns
439
+ with open(temp_dir / "docs/report_2023.pdf", "w") as f:
440
+ f.write("Test PDF report 2023")
441
+ with open(temp_dir / "docs/report_2024.pdf", "w") as f:
442
+ f.write("Test PDF report 2024")
443
+ with open(temp_dir / "docs/summary_2023.pdf", "w") as f:
444
+ f.write("Test PDF summary 2023")
445
+ with open(temp_dir / "docs/note_2023.txt", "w") as f:
446
+ f.write("Test TXT note 2023")
447
+
448
+ # Test LIKE with wildcard at beginning
449
+ query_str = f"""
450
+ SELECT *
451
+ FROM '{temp_dir}/docs'
452
+ WHERE name LIKE '%2023.pdf'
453
+ """
454
+
455
+ parsed = parse_query(query_str)
456
+ visitor = QueryVisitor()
457
+ visitor.visit(parsed)
458
+
459
+ results = execute_query(
460
+ visitor.select,
461
+ visitor.from_dirs,
462
+ visitor.where
463
+ )
464
+
465
+ # Should match all 2023 PDF files
466
+ expected = [
467
+ str(temp_dir / "docs/report_2023.pdf"),
468
+ str(temp_dir / "docs/summary_2023.pdf")
469
+ ]
470
+
471
+ # Normalize paths for comparison
472
+ actual = [str(p) for p in results]
473
+ assert sorted(actual) == sorted(expected)
474
+
475
+ # Test LIKE with wildcard in middle
476
+ query_str = f"""
477
+ SELECT *
478
+ FROM '{temp_dir}/docs'
479
+ WHERE name LIKE 'report%pdf'
480
+ """
481
+
482
+ parsed = parse_query(query_str)
483
+ visitor = QueryVisitor()
484
+ visitor.visit(parsed)
485
+
486
+ results = execute_query(
487
+ visitor.select,
488
+ visitor.from_dirs,
489
+ visitor.where
490
+ )
491
+
492
+ # Should match all report PDF files
493
+ expected = [
494
+ str(temp_dir / "docs/report.pdf"),
495
+ str(temp_dir / "docs/report_2023.pdf"),
496
+ str(temp_dir / "docs/report_2024.pdf")
497
+ ]
498
+
499
+ # Normalize paths for comparison
500
+ actual = [str(p) for p in results]
501
+ assert sorted(actual) == sorted(expected)
502
+
503
+ # Test LIKE with wildcards at both ends
504
+ query_str = f"""
505
+ SELECT *
506
+ FROM '{temp_dir}/docs'
507
+ WHERE name LIKE '%report%'
508
+ """
509
+
510
+ parsed = parse_query(query_str)
511
+ visitor = QueryVisitor()
512
+ visitor.visit(parsed)
513
+
514
+ results = execute_query(
515
+ visitor.select,
516
+ visitor.from_dirs,
517
+ visitor.where
518
+ )
519
+
520
+ # Should match all files with 'report' in the name
521
+ expected = [
522
+ str(temp_dir / "docs/report.pdf"),
523
+ str(temp_dir / "docs/report_2023.pdf"),
524
+ str(temp_dir / "docs/report_2024.pdf")
525
+ ]
526
+
527
+ # Normalize paths for comparison
528
+ actual = [str(p) for p in results]
529
+ assert sorted(actual) == sorted(expected)
530
+
531
+ def test_like_with_not_like_operators(temp_dir):
532
+ """Test combining LIKE and NOT LIKE operators."""
533
+ # Create specific files with different paths
534
+ os.makedirs(temp_dir / "src/components", exist_ok=True)
535
+ os.makedirs(temp_dir / "src/lib/utils", exist_ok=True)
536
+ os.makedirs(temp_dir / "src/views", exist_ok=True)
537
+
538
+ with open(temp_dir / "src/components/Button.js", "w") as f:
539
+ f.write("Component file")
540
+ with open(temp_dir / "src/lib/utils/helpers.js", "w") as f:
541
+ f.write("Library utility file")
542
+ with open(temp_dir / "src/views/Home.js", "w") as f:
543
+ f.write("View file")
544
+
545
+ # Query: Find files in src path but exclude anything with lib in the path
546
+ query_str = f"""
547
+ SELECT *
548
+ FROM '{temp_dir}'
549
+ WHERE path LIKE '{temp_dir}/src%' AND path NOT LIKE '%lib%'
550
+ """
551
+
552
+ parsed = parse_query(query_str)
553
+ visitor = QueryVisitor()
554
+ visitor.visit(parsed)
555
+
556
+ results = execute_query(
557
+ visitor.select,
558
+ visitor.from_dirs,
559
+ visitor.where
560
+ )
561
+
562
+ # Expected result (src files not in lib directory)
563
+ expected = [
564
+ str(temp_dir / "src/components/Button.js"),
565
+ str(temp_dir / "src/views/Home.js")
566
+ ]
567
+
568
+ # Normalize paths for comparison
569
+ actual = [str(p) for p in results]
570
+ assert sorted(actual) == sorted(expected)