Example farmOS.py CSV import script

Thanks, @paul121, for the quick & helpful response. Skipping first to the bottom line:

-i accept the challenge, have begun process of scripting my own CSV Importer (pseudocode only at this point), and will initiate a new thread on that topic directly.
PS: Done -see this thread in farmOS Users. /w

As a first step tho, i want to make sure i understand this little import script example you have provided, and can use it for its intended purpose. Do i understand correctly that this script- modified only for access to my v2 instance at farmier host- should enable upload of this little plantings.csv table you have set up as an example?

NB: considering the content of this table:

name,crop,variety,date
Big Corn planing,corn,big,5/5/2021
Pumpkins,squash,pumpkin,8/5/2021
Green beans #1,beans,green beans,6/5/2021
Green beans #2 (future),beans,green beans,10/5/2021

-i still don’t understand how the import can work, since schema of that little table is not the same as schema in my farmOS instance (referenced in my previous msg above), but perhaps farmOS/API is somehow able to interpret those column names & map to schema as appropriate (?)

Notwithstanding that little detail: If i am correct in the more general assumption above, then there is still some problem(s) here, although i did pass the line 37 hitch mentioned above (something in the client authorization script, not sure what, but i fixed it by copying in the auth codes from my farmos.py login to API, which works fine). The two sections of code that are failing in my case are:

  1. File command line args in lines 17-19: thinking that maybe this block is superfluous (since i am not running script at the command line), i skipped over this block, ran the function def in lines 37-91 just fine, but then…
  2. Something in the CSV processing is not working right; traceback appended below the fold.

Is there anything you can suggest to get this working as it should?

/walt

8<------(snip)------>8

%tb
Traceback (most recent call last):

  File "/var/folders/hc/46157fbs5t1cscm8c38y3fd80000gn/T/ipykernel_98970/2364323154.py", line 1, in <module>
    runfile('/Users/walterludwick/Library/Mobile Documents/com~apple~CloudDocs/WLcode/py_farmOS/import_plantings.py', wdir='/Users/walterludwick/Library/Mobile Documents/com~apple~CloudDocs/WLcode/py_farmOS')

  File "/Users/walterludwick/opt/anaconda3/lib/python3.8/site-packages/debugpy/_vendored/pydevd/_pydev_bundle/pydev_umd.py", line 167, in runfile
    execfile(filename, namespace)

  File "/Users/walterludwick/opt/anaconda3/lib/python3.8/site-packages/debugpy/_vendored/pydevd/_pydev_imps/_pydev_execfile.py", line 25, in execfile
    exec(compile(contents + "\n", file, 'exec'), glob, loc)

  File "/Users/walterludwick/Library/Mobile Documents/com~apple~CloudDocs/WLcode/py_farmOS/import_plantings.py", line 25, in <module>
    args = parser.parse_args()

  File "/Users/walterludwick/opt/anaconda3/lib/python3.8/argparse.py", line 1768, in parse_args
    args, argv = self.parse_known_args(args, namespace)

  File "/Users/walterludwick/opt/anaconda3/lib/python3.8/argparse.py", line 1800, in parse_known_args
    namespace, args = self._parse_known_args(args, namespace)

  File "/Users/walterludwick/opt/anaconda3/lib/python3.8/argparse.py", line 2034, in _parse_known_args
    self.error(_('the following arguments are required: %s') %

  File "/Users/walterludwick/opt/anaconda3/lib/python3.8/argparse.py", line 2521, in error
    self.exit(2, _('%(prog)s: error: %(message)s\n') % args)

  File "/Users/walterludwick/opt/anaconda3/lib/python3.8/argparse.py", line 2508, in exit
    _sys.exit(status)

SystemExit: 2
1 Like