Skip to content

Commit

Permalink
[feat] allow search by reference product
Browse files Browse the repository at this point in the history
Fix #30
  • Loading branch information
tngTUDOR committed Mar 16, 2024
1 parent fac6515 commit fa1c4bb
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 14 deletions.
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# ui Changelog

## [0.34.0]

### Added

+ feature to search by reference product. search now allows to search location & categories
without providing search criterion

## [0.33.0]

### Fixed
Expand Down
66 changes: 52 additions & 14 deletions bw2ui/bin/bw2_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,14 @@
Working with databases:
ldb: List available databases.
db name: Go to database name. No quotes needed.
s string: Search activity names in current database with string.
s -loc {LOCATION} string: Search activity names in current database with \
s [string]: Search activity names in current database with string. Without string \
the search provides no results.
s -loc {LOCATION} [string]: Search activity names in current database with \
string and location LOCATION.
s -cat {CAT::SUBCAT::SUBSUBCAT} string: Search activity names in current database \
with string and category cat, subcat, subcat [useful for biosphere].
s -cat {CAT::SUBCAT::SUBSUBCAT} [string]: Search activity names in current \
database with string and category CAT, SUBCAT, SUBCAT [useful for biosphere].
s -rp {REFERENCE PRODUCT} [string]: Search activities in current database that \
have reference product and optionnaly match string in search.
Working with activities:
a id: Go to activity id in current database. Complex ids in quotes.
Expand Down Expand Up @@ -1071,8 +1074,13 @@ def do_s(self, arg):
elif "-cat" in arg:
re1a = r"(-cat\s)" # Any Single Whitespace Character 1
search_criterion = "category"
elif "-rp" in arg:
re1a = r"(-rp\s)" # Any Single Whitespace Character 1
search_criterion = "reference product"
re1b = r"(\{.*\})" # Curly Braces 1
re2 = r"\s(.+)"
re2 = (
r"(?:\s(.+))?" # at least a space, and then 1 to n chars, but optional
)
rg = re.compile(re1a + re1b + re2, re.IGNORECASE | re.DOTALL)
m = rg.search(arg)
needle = arg # Find the needle in the haystack
Expand All @@ -1082,6 +1090,12 @@ def do_s(self, arg):
elif m is None and "-cat " in arg:
print("Missing category in curly braces in command: -cat {water} ...")
return
elif m is None and "-rp" in arg:
print(
"Missing reference product in curly braces in command: -rp "
"{electricity high voltage} ..."
)
return
elif m:
c2 = m.group(2)
criterion_value = c2.strip("{}")
Expand All @@ -1094,25 +1108,49 @@ def do_s(self, arg):

specials = [" ", "/", "-", "&"]
if m and search_criterion == "location":
if not any(
special in criterion_value for special in specials
): # complex criterion_value
if not any(special in criterion_value for special in specials):
criteria = {"location": criterion_value}
results = Database(self.database).search(
needle, filter=criteria, limit=self.search_limit
)
else:
results = Database(self.database).search(needle, limit=None)
if needle:
results = Database(self.database).search(
needle, filter=criteria, limit=self.search_limit
)
else:
results = [
a
for a in Database(self.database)
if a.get("location", "").casefold()
== criterion_value.casefold()
]

else: # complex criterion_value
if needle:
results = Database(self.database).search(needle, limit=None)
else:
results = Database(self.database)
results = [
r
for r in results
if r[search_criterion].lower() == criterion_value.lower()
if r[search_criterion].casefold() == criterion_value.casefold()
]
elif m and search_criterion == "category":
criteria = {"categories": criterion_value.split("::")}
results = Database(self.database).search(
needle, filter=criteria, limit=self.search_limit
)
elif m and search_criterion == "reference product":
if needle is None:
results = [
a
for a in Database(self.database)
if a[search_criterion].casefold() == criterion_value.casefold()
]
else:
criteria = {"product": criterion_value}
results = Database(self.database).search(
needle,
filter=criteria,
limit=self.search_limit,
)
else:
results = Database(self.database).search(
needle, limit=self.search_limit
Expand Down

0 comments on commit fa1c4bb

Please sign in to comment.