[nxt-python] r351 committed - Added preliminary support for file configuration of nxt location infor...

22 views
Skip to first unread message

nxt-p...@googlecode.com

unread,
Jun 29, 2011, 9:51:54 AM6/29/11
to nxt-p...@googlegroups.com
Revision: 351
Author: mar...@wanners.net
Date: Tue Jun 28 17:44:52 2011
Log: Added preliminary support for file configuration of nxt location
information.

http://code.google.com/p/nxt-python/source/detail?r=351

Modified:
/trunk/nxt/locator.py

=======================================
--- /trunk/nxt/locator.py Sun Jun 5 20:49:01 2011
+++ /trunk/nxt/locator.py Tue Jun 28 17:44:52 2011
@@ -12,7 +12,7 @@
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.

-import traceback
+import traceback, ConfigParser, os

class BrickNotFoundError(Exception):
pass
@@ -27,8 +27,7 @@
#new method options MUST default to False!
self.usb = usb
self.bluetooth = bluetooth
- if fantomusb or fantombt:
- self.fantom = True
+ self.fantom = fantomusb or fantombt
self.fantomusb = fantomusb
self.fantombt = fantombt

@@ -81,21 +80,33 @@
raise NoBackendError("No selected backends are available! Did you
install the comm modules?")


-def find_one_brick(host=None, name=None, silent=False, strict=True,
debug=False, method=Method()):
- """Use to find one brick. After it returns a usbsock object or a
bluesock
-object, it automatically connects to it. The host and name args limit
-the search to a given MAC or brick name. Set silent to True to stop
-nxt-python from printing anything during the search. This function by
default
+def find_one_brick(host=None, name=None, silent=False, strict=None,
debug=False, method=None, confpath=None):
+ """Use to find one brick. The host and name args limit the search to
+a given MAC or brick name. Set silent to True to stop nxt-python from
+printing anything during the search. This function by default
automatically checks to see if the brick found has the correct host/name
-(if either are provided) and will not return a brick which doesn't
-match. This can be disabled (so the function returns any brick which
-can be connected to and provides a valid reply to get_device_info()) by
-passing strict=False. This will, however, still tell the USB/BT backends to
-only look for devices which match the args provided."""
+(if either are provided) and will not return a brick which doesn't
+match. This can be disabled (so the function returns any brick which can
+be connected to and provides a valid reply to get_device_info()) by
+passing strict=False. This will, however, still tell the comm backends
+to only look for devices which match the args provided. The confpath arg
+specifies the location of the configuration file which brick location
+information will be read from if no brick location directives (host,
+name, strict, or method) are provided."""
if debug and silent:
silent=False
print "silent and debug can't both be set; giving debug priority"

+ conf = read_config(confpath, debug)
+ if not (host or name or strict or method):
+ host = conf.get('Brick', 'host')
+ name = conf.get('Brick', 'name')
+ strict = conf.get('Brick', 'strict')
+ method = eval('Method(%s)' % conf.get('Brick', 'method'))
+ if not strict: strict = True
+ if not method: method = Method()
+ print host, name, strict, method
+
for s in find_bricks(host, name, silent, method):
try:
if host and 'host' in dir(s) and s.host != host:
@@ -128,3 +139,13 @@
import ipsock
sock = ipsock.IpSock(host, port)
return sock.connect()
+
+
+def read_config(confpath=None, debug=False):
+ conf = ConfigParser.RawConfigParser({'host': None, 'name':
None, 'strict': True, 'method': ''})
+ if not confpath: confpath = os.path.expanduser('~/.nxt-python')
+ if conf.read([confpath]) == [] and debug:
+ print "Warning: Config file (should be at %s) was not read" %
confpath
+ if conf.has_section('Brick') == False:
+ conf.add_section('Brick')
+ return conf

rhn

unread,
Jun 29, 2011, 10:21:14 AM6/29/11
to nxt-p...@googlegroups.com
Ah, I'm too late. I just read the bug report a few minutes ago and I think using a configuration file is not a good approach for a library.
I'd let the user specify the method with find_one_brick call, or use the default method (I think that's what you did anyway).

Cheers,
rhn

> --
> You received this message because you are subscribed to the Google Groups "nxt-python" group.
> To post to this group, send email to nxt-p...@googlegroups.com.
> To unsubscribe from this group, send email to nxt-python+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/nxt-python?hl=en.
>

Marcus Wanner

unread,
Jun 29, 2011, 11:59:16 AM6/29/11
to nxt-p...@googlegroups.com
On Wed, Jun 29, 2011 at 04:21:14PM +0200, rhn wrote:
>Ah, I'm too late. I just read the bug report a few minutes ago and I
>think using a configuration file is not a good approach for a library.

I wanted a config scheme which didn't require modification of the source
and was independent of individual scripts. I think an optional config
file is the best solution, even for a library.

>I'd let the user specify the method with find_one_brick call, or use
>the default method (I think that's what you did anyway).

That's what we've done in the past, but it makes it harder for scripts
to be shared betweek people and still work. I've noticed that one rarely
finds nxt-python scripts on the internet and would like this to change.
If everyone moved brick location information into the configuration
files, scripts would work universally with no changes. As it is, the
user must look for the find_one_brick() call and change it to match his
situation (if not do more complicated things). This makes it difficult
to base end-user applications (especially GUI ones) on nxt-python.

Do you have a better approach?

--
Marcus Wanner

signature.asc

rhn

unread,
Jun 29, 2011, 12:15:34 PM6/29/11
to nxt-p...@googlegroups.com

Okay, your reasoning makes sense. If autodetection was used, then the user wouldn't be able to configure his own backend. I can't think of anything else that achieves the same.

Cheers,
rhn

Marcus Wanner

unread,
Jun 29, 2011, 12:17:28 PM6/29/11
to nxt-p...@googlegroups.com
On Wed, Jun 29, 2011 at 06:15:34PM +0200, rhn wrote:
> Okay, your reasoning makes sense. If autodetection was used, then the
> user wouldn't be able to configure his own backend. I can't think of
> anything else that achieves the same.

I'm also about to add config file generation to make it really easy for
users to get a file working. After that, it will be v2.1.0 or 2.2.0 or
whatever comes next.

--
Marcus Wanner

signature.asc

Gaurav Roy

unread,
Sep 16, 2013, 6:07:16 PM9/16/13
to nxt-p...@googlegroups.com
hi,
   im trying to connect my nxt brick to my computer and program thru snap.i have got almost all the steps correctly as mentioned on the website https://github.com/Technoboy10/snap-nxt . When i run the file or the script python.snap.py it says "nxt brick not found.please connect and nxt brick and try again" . but at the same time when i try to program through the nxt programming software it recognizes the brick and downloads the code to it without any trouble.so im guessing its a problem only the python port interfacing.can somebody please provide me with a solution to it.

thanks
regards
roy.
Reply all
Reply to author
Forward
0 new messages