question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using custom parser as python module

See original GitHub issue

I have created custom parser crontab_h.py and placed it in the $HOME/.local/share/jc/jcparsers folder.

I can successfully use this parser when executing crontab -l | jc --crontab_h, but when I try to use it as python module, I get error that module is not found: ModuleNotFoundError: No module named 'jc.parsers.crontab_h'.

test.py

#!/usr/bin/env python3

import subprocess
import jc.parsers.crontab_h

command_output = subprocess.check_output(['crontab', '-l'], text=True)
result = jc.parsers.crontab_h.parse(command_output)
print(result)

output:

> python3 test.py
Traceback (most recent call last):
  File "//test.py", line 4, in <module>
    import jc.parsers.crontab_h
ModuleNotFoundError: No module named 'jc.parsers.crontab_h'

Does this mean that custom parsers cant be used as python modules?

Even if I override default parser crontab.py (by renaming the custom crontab_h.py to crontab.py), the default parser is still imported as python module in stead of the custom one when using import jc.parsers.crontab.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
kellyjonbrazilcommented, Jan 21, 2022

The new API to call builtin and plugin parsers is now available with v1.18.1

1reaction
kellyjonbrazilcommented, Jan 17, 2022

Here is a not-so-elegant solution for now. I’ll look into creating true interface for this:

import os
import sys
import jc.appdirs
data_dir = jc.appdirs.user_data_dir('jc', 'jc')
local_parsers_dir = os.path.join(data_dir, 'jcparsers')
sys.path.append(local_parsers_dir)

# now you can import your parser in the plugin directory
import my_custom_parser
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Build a Custom Parser - Packet Coders
The first thing is to create a template that can be used for multiple parsers, and for that, my preference would be to...
Read more >
Parsing in Python: all the tools and libraries you can use
We present and compare all possible alternatives you can use to parse languages in Python. From libraries to parser generators, we present all...
Read more >
argparse — Parser for command-line options, arguments and ...
The argparse module makes it easy to write user-friendly command-line interfaces. The program defines what arguments it requires, and argparse will figure out ......
Read more >
How to create custom argparse types in Python? - Medium
Import the argparse module; Create the parser; Added arguments (positional, optional); Parse the arguments and validate. Let's start off by ...
Read more >
Add own realtime custom parser to Python to generate and ...
Parse a .py file, read the AST, modify it, then write back the modified source code · Python's tokenize module · Python's ast...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found