Implement snapcraft search #608

Merged
merged 11 commits into from Jun 27, 2016

Conversation

Projects
None yet
3 participants
Collaborator

sergiusens commented Jun 25, 2016

LP: #1596222

Signed-off-by: Sergio Schvezov sergio.schvezov@ubuntu.com

sergiusens added some commits Jun 25, 2016

Implement snapcraft search
LP: #1596222

Signed-off-by: Sergio Schvezov <sergio.schvezov@ubuntu.com>
snapcraft/internal/common.py
+ width = max_width
+ if max_width:
+ width = min(max_width, width)
+ return width
@elopio

elopio Jun 26, 2016

Member

This seems wrong.
You are calling: terminal_width = get_terminal_width(max_width=None). For not a tty, that will return None
Then you do: description_space = terminal_width - part_length - 5
So None - something - 5, will raise:
TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'

@sergiusens

sergiusens Jun 26, 2016

Collaborator

Ah, nice catch

snapcraft/internal/parts.py
@@ -117,6 +124,25 @@ def get_part(self, part_name, full=False):
remote_part.pop(key)
return remote_part
+ def matches_for(self, part_match, max_len=0):
+ sm = difflib.SequenceMatcher(isjunk=None, autojunk=False)
+ sm.set_seq2(part_match)
@elopio

elopio Jun 26, 2016

Member

using sm as a variable is a gopher thing. Something like "matcher" would be more pythonic.

snapcraft/internal/parts.py
+
+ if add_part_name or add_description or part_match in part_name:
+ matching_parts[part_name] = self._parts[part_name]
+ matching_parts[part_name] = self._parts[part_name]
@elopio

elopio Jun 26, 2016

Member

why twice?

@sergiusens

sergiusens Jun 26, 2016

Collaborator

this is a vim problem

@@ -117,6 +124,25 @@ def get_part(self, part_name, full=False):
remote_part.pop(key)
return remote_part
+ def matches_for(self, part_match, max_len=0):
@elopio

elopio Jun 26, 2016

Member

_matches_for

@sergiusens

sergiusens Jun 26, 2016

Collaborator

The class is private, the method public, I don't see the reason for this one.

@kyrofa

kyrofa Jun 27, 2016

Member

This method is only ever used within this class though, no? Do you envision it being used by others? i.e. just because the class is private doesn't mean encapsulation is no longer important.

@elopio

elopio Jun 27, 2016

Member

This is how I see it: an internal class starts with _ and is used only in the module. An internal method starts with _ and is used only in the class. So internal classes can have internal methods. Of course, this is how I use internals in python, not a good practice I have seen everywhere. So we can have a discussion to define our rules for internals.

@sergiusens

sergiusens Jun 27, 2016

Collaborator

@elopio I am in agreement. An instance of _RemoteParts is created under the implementation of search and calls this method so it is not internal to the class https://github.com/ubuntu-core/snapcraft/pull/608/files#diff-ac0a8a66808d29ef0e343cb310093978R179

@elopio

elopio Jun 27, 2016

Member

hum, that is more complex. It's like a sandwich of internal. I'll say you win. :D

+ if not matches:
+ # apt search does not return error, we probably shouldn't either.
+ logger.info('No matches found, try to run `snapcraft update` to '
+ 'refresh the remote parts cache.')
@elopio

elopio Jun 26, 2016

Member

or maybe the part just doesn't exist. Should that be part of the message?
What about: to contribute new parts, read ?

@sergiusens

sergiusens Jun 26, 2016

Collaborator

well it really is a search string, not really a part

snapcraft/tests/test_commands_search.py
+ self.addCleanup(patcher.stop)
+
+ @mock.patch('sys.stdout', new_callable=io.StringIO)
+ def test_searching_for_a_part_that_exists(self, mock_stdout):
@elopio

elopio Jun 26, 2016

Member

you are missing a test for a match in description, not in name.
And for completenes, maybe a match in name, not in description.

@sergiusens

sergiusens Jun 26, 2016

Collaborator

I removed matching description for now

sergiusens added some commits Jun 26, 2016

Collaborator

sergiusens commented Jun 26, 2016

@elopio ok, one more round!

snapcraft/internal/parts.py
+ matcher.set_seq1(part_name)
+ add_part_name = matcher.ratio() >= _MATCH_RATIO
+
+ if add_part_name or part_match in part_name:
@elopio

elopio Jun 27, 2016

Member

Use parentheses here to make the rules of precedence clear:
if add_part_name or (part_match in part_name):

@sergiusens

sergiusens Jun 27, 2016

Collaborator

Sure, that's doable

+ for part_key in matches.keys():
+ description = matches[part_key]['description']
+ if len(description) > description_space:
+ description = '{}...'.format(description[0:description_space])
@kyrofa

kyrofa Jun 27, 2016

Member

I haven't thought through the difficulty of this, but would it be better to wrap instead? Looking something like this:

$ snapcraft search qt
PART NAME          DESCRIPTION
qt5conf            This sets up qt5.conf for projects using qml
                   and other qt5 components that need an
                   Ubuntu standard internal path setup by
                   default like the Ubuntu Core Apps.
mqtt-paho/python2  mqtt-paho for python.
mqtt-paho          mqtt-paho for python.
[...]

as opposed to:

$ snapcraft search qt
PART NAME          DESCRIPTION
qt5conf            This sets up qt5.conf for projects using...
mqtt-paho/python2  mqtt-paho for python.
mqtt-paho          mqtt-paho for python.
[...]
@sergiusens

sergiusens Jun 27, 2016

Collaborator

this is bad for filtering @kyrofa

@kyrofa

kyrofa Jun 27, 2016

Member

Filtering? You mean e.g. piping to grep? So then the way to see the entire description is just to snapcraft define it?

@sergiusens

sergiusens Jun 27, 2016

Collaborator

@kyrofa your idea is not bad, but a bug report would be better so if not grepping we present a different layout.

@kyrofa

kyrofa Jun 27, 2016

Member

Yeah no problem, I don't have strong feelings here. I can make a bug if you'd like one.

snapcraft/tests/test_commands_update.py
+ 'source': 'http://source.tar.gz',
+ 'description': 'this is a repetetive description ' * 3,
+ 'maintainer': 'none',
+ },
@elopio

elopio Jun 27, 2016

Member

where is this one used?

@sergiusens

sergiusens Jun 27, 2016

Collaborator

when searching for everything

@elopio

elopio Jun 27, 2016

Member

ah, I see it now. It comes from the fake server.

sergiusens added some commits Jun 27, 2016

Member

elopio commented Jun 27, 2016

There's a typo: repetetive instead of repetitive. Looks good to me.

sergiusens added some commits Jun 27, 2016

Member

kyrofa commented Jun 27, 2016

👍 from me.

@sergiusens sergiusens merged commit ea6595e into snapcore:master Jun 27, 2016

4 checks passed

Examples tests Success
Details
autopkgtest Success
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
coverage/coveralls Coverage increased (+0.03%) to 96.24%
Details

@sergiusens sergiusens deleted the sergiusens:bugfix/1596222/snapcraft-search branch Jun 27, 2016

kalikiana pushed a commit to kalikiana/snapcraft that referenced this pull request Apr 6, 2017

Implement snapcraft search (#608)
LP: #1596222

Signed-off-by: Sergio Schvezov <sergio.schvezov@ubuntu.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment