import datetime
from JsonStats.FetchStats import Fetcher


class Facter(Fetcher):
    import yaml

    def __init__(self):
        self.context = 'facter'
        self._cmd = 'facter --yaml'

        try:
            import rpm
            ts = rpm.TransactionSet()
            mi = ts.dbMatch('name', 'puppet')
            if mi.count() > 0:
                self._cmd = 'facter -p --yaml'
        except:
            pass
        self._load_data()

    def _load_data(self):
        self._refresh_time = datetime.datetime.utcnow()

        try:
            output = self._exec(self._cmd)
            self.facts = self.yaml.load(output)
            self._loaded(True)
        except OSError, e:
            # Couldn't find facter command, most likely
            self._loaded(False, msg=str(e))
        except Exception, e:
            # Something else did indeed go wrong
            self._loaded(False, msg=str(e))

    def dump(self):
        # poor mans cache, refresh cache in an hour
        if (datetime.datetime.utcnow() -
                datetime.timedelta(minutes=5)) > self._refresh_time:
            self._load_data()
        return self.facts

    def dump_json(self):
        return self.json.dumps(self.dump())

@@ -50,16 +50,31 @@
 Fetcher%0A
+import os.path%0A
 %0A%0Aclass 
@@ -98,273 +98,622 @@
     
-import yaml%0A%0A    def __init__(self):%0A        self.context = 'facter'%0A        self._cmd = 'facter --yaml'%0A%0A        try:%0A            import rpm%0A            ts = rpm.TransactionSet()%0A            mi = ts.dbMatch('name', '
+%22%22%22%0A    Facter plugin for %60jsonstats%60. Returns key-value pairs of general%0A    system information provided by the %60facter%60 command.%0A%0A    Load conditions:%0A    * Plugin will load if the %60facter%60 command is found%0A%0A    Operating behavior:%0A    * Plugin will call %60facter%60 with the %60-p%60 (return %60puppet%60 facts)%0A      option if the %60puppet%60 command is on the system.%0A%0A    Dependencies:%0A    * Facter - http://puppetlabs.com/blog/facter-part-1-facter-101%0A%0A    Optional dependencies:%0A    * Puppet - http://puppetlabs.com/puppet/what-is-
 puppet
-')
 %0A    
-        if mi.count() %3E 0:%0A        
+%22%22%22%0A%0A    import yaml%0A%0A    def __init__(self):%0A        self.context = 'facter'%0A
     
@@ -740,19 +740,16 @@
 ter 
--p 
 --yaml'%0A
     
@@ -744,24 +744,25 @@
 --yaml'%0A
+%0A
         
 except:%0A
@@ -757,32 +757,88 @@
     
-except:%0A            pass
+if os.path.exists('/usr/bin/puppet'):%0A            self._cmd = 'facter -p --yaml'
 %0A   

Make Facter plugin puppet-output portable.