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
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.
>
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
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
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