file_query_text 0.1.8__tar.gz → 0.1.10__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.8
3
+ Version: 0.1.10
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
@@ -128,3 +128,15 @@ Find all markdown files in a specific year's folder:
128
128
  ```
129
129
  fq "path LIKE '%/2023/%' AND extension == 'md'"
130
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%'"
142
+ ```
@@ -112,3 +112,15 @@ Find all markdown files in a specific year's folder:
112
112
  ```
113
113
  fq "path LIKE '%/2023/%' AND extension == 'md'"
114
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%'"
126
+ ```
@@ -2,4 +2,4 @@
2
2
  SQL-like interface for querying files in your filesystem.
3
3
  """
4
4
 
5
- __version__ = "0.1.8"
5
+ __version__ = "0.1.10"
@@ -46,8 +46,15 @@ basic_condition = Group(IDENTIFIER + COMPARISON_OP + VALUE)
46
46
 
47
47
  # Define logical expressions using infixNotation for better handling of AND and OR
48
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
+
49
56
  condition_expr <<= infixNotation(
50
- basic_condition,
57
+ basic_expr,
51
58
  [
52
59
  (NOT, 1, opAssoc.RIGHT),
53
60
  (AND, 2, opAssoc.LEFT),
@@ -97,6 +97,14 @@ def evaluate_conditions(file_path, condition):
97
97
  if not condition:
98
98
  return True
99
99
 
100
+ # Get the relative path for comparison
101
+ cwd = os.getcwd()
102
+ try:
103
+ relative_path = os.path.relpath(file_path, cwd)
104
+ except ValueError:
105
+ # Handle case when on different drives (Windows)
106
+ relative_path = file_path
107
+
100
108
  def get_file_attr(attr_name):
101
109
  if attr_name == "extension":
102
110
  return os.path.splitext(file_path)[1][1:]
@@ -105,7 +113,7 @@ def evaluate_conditions(file_path, condition):
105
113
  if attr_name == "size":
106
114
  return os.path.getsize(file_path)
107
115
  if attr_name == "path":
108
- return file_path
116
+ return relative_path # Use relative path instead of absolute
109
117
  # Add more attributes as needed
110
118
  return None
111
119
 
@@ -119,7 +127,8 @@ def evaluate_conditions(file_path, condition):
119
127
 
120
128
  # 1. Basic condition: [attr, op, value]
121
129
  if isinstance(expr[0], str) and isinstance(expr[1], str):
122
- attr_val = get_file_attr(expr[0])
130
+ attr_name = expr[0]
131
+ attr_val = get_file_attr(attr_name)
123
132
  op = expr[1]
124
133
  val = expr[2].strip("'") if isinstance(expr[2], str) else expr[2] # Remove quotes if string
125
134
 
@@ -130,14 +139,7 @@ def evaluate_conditions(file_path, condition):
130
139
  if op == ">": return attr_val is not None and int(attr_val) > int(val)
131
140
  if op == ">=": return attr_val is not None and int(attr_val) >= int(val)
132
141
  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))
142
+ return check_like_condition(attr_val, val)
141
143
 
142
144
  # 2. Logical operations from infixNotation: [left, op, right]
143
145
  elif expr[1] == "AND":
@@ -149,8 +151,30 @@ def evaluate_conditions(file_path, condition):
149
151
  elif len(expr) == 2 and expr[0] == "NOT":
150
152
  return not eval_expr(expr[1])
151
153
 
154
+ # 4. Special case for NOT LIKE: [attr, 'NOT', 'LIKE', value]
155
+ elif len(expr) == 4 and expr[1] == "NOT" and expr[2] == "LIKE":
156
+ attr_name = expr[0]
157
+ attr_val = get_file_attr(attr_name)
158
+ val = expr[3].strip("'") if isinstance(expr[3], str) else expr[3]
159
+
160
+ if attr_val is None:
161
+ return True # If attribute doesn't exist, NOT LIKE is True
162
+
163
+ return not check_like_condition(attr_val, val)
164
+
152
165
  return False
153
166
 
167
+ # Helper function to check LIKE conditions with proper pattern matching
168
+ def check_like_condition(attr_val, val):
169
+ if attr_val is None:
170
+ return False
171
+ # Convert SQL LIKE pattern (with % wildcards) to regex pattern
172
+ # Escape any regex special characters in the pattern except %
173
+ pattern = re.escape(val).replace('\\%', '%') # Unescape % after escaping everything else
174
+ pattern = pattern.replace("%", ".*")
175
+ pattern = f"^{pattern}$" # Anchor pattern to match whole string
176
+ return bool(re.search(pattern, str(attr_val), re.IGNORECASE))
177
+
154
178
  return eval_expr(condition.asList())
155
179
 
156
180
  # Function to get all attributes for a file
@@ -168,8 +192,8 @@ def get_file_attributes(file_path):
168
192
  "extension": os.path.splitext(file_path)[1][1:],
169
193
  "name": os.path.basename(file_path),
170
194
  "size": os.path.getsize(file_path),
171
- "path": file_path,
172
- "relative_path": rel_path,
195
+ "path": rel_path, # Use relative path for consistency
196
+ "absolute_path": file_path, # Keep the absolute path as well
173
197
  }
174
198
  return attributes
175
199
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: file_query_text
3
- Version: 0.1.8
3
+ Version: 0.1.10
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
@@ -128,3 +128,15 @@ Find all markdown files in a specific year's folder:
128
128
  ```
129
129
  fq "path LIKE '%/2023/%' AND extension == 'md'"
130
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%'"
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.8"
7
+ version = "0.1.10"
8
8
  description = "SQL-like interface for querying files in your filesystem"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.12"
@@ -527,3 +527,44 @@ def test_like_operator_with_wildcards(temp_dir):
527
527
  # Normalize paths for comparison
528
528
  actual = [str(p) for p in results]
529
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)