Skip to content

click based CLI for moss.py #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions mosscli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import click
import logging
import mosspy

from configparser import ConfigParser

@click.command()
@click.option("--base", type=click.Path(exists=True, readable=True), multiple=True)
@click.option("--report", type=click.Path(exists=False, writable=True))
@click.option("--download", type=click.Path(exists=False, writable=True))
@click.argument("language", type=click.Choice(list(mosspy.Moss.languages)))
@click.argument("files", nargs=-1)
def moss(base, report, download, language, files):
config_ini = click.get_app_dir("moss.ini")
parser = ConfigParser()
parser.read([config_ini])
if "SERVER" not in parser or "userid" not in parser["SERVER"]:
click.echo(click.style(f"missing userid SERVER section in {config_ini}", fg='red'))
exit(2)

m = mosspy.Moss(parser["SERVER"]["userid"], language)
for file in files:
m.addFilesByWildcard(file)

for b in base:
m.addBaseFile(b)

with click.progressbar(length=len(m.files), label="uploading") as bar:
url = m.send(lambda path, name: bar.update(1, name))

print(url)

if report:
m.saveWebPage(url, report)

if download:
with click.progressbar(length=10101010101010101010, label="downloading") as bar:
left_max = 10
def update_bar(left):
nonlocal left_max
if left > left_max:
left_max = left
bar.length = left_max
bar.update(1)
mosspy.download_report(url, download, connections=8, log_level=logging.INFO,
on_read2=lambda u, left: update_bar(left))

if __name__ == '__main__':
moss()
7 changes: 4 additions & 3 deletions mosspy/download_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
except ImportError:
from urllib2 import urlopen

def process_url(url, urls, base_url, path, on_read):
def process_url(url, urls, base_url, path, on_read, on_read2):
from bs4 import BeautifulSoup # Backward compability, don't break Moss when bs4 not available.

logging.debug ("Processing URL: " + url)
response = urlopen(url)
html = response.read()
on_read(url)
on_read2(url, len(urls))
soup = BeautifulSoup(html, 'lxml')
file_name = os.path.basename(url)

Expand Down Expand Up @@ -50,7 +51,7 @@ def process_url(url, urls, base_url, path, on_read):
f.write(soup.encode(soup.original_encoding))
f.close()

def download_report(url, path, connections = 4, log_level=logging.DEBUG, on_read=lambda url: None):
def download_report(url, path, connections = 4, log_level=logging.DEBUG, on_read=lambda url: None, on_read2=lambda url, left: None):
logging.basicConfig(level=log_level)

if len(url) == 0:
Expand All @@ -69,7 +70,7 @@ def download_report(url, path, connections = 4, log_level=logging.DEBUG, on_read

# Handling thread
for url in urls:
t = Thread(target=process_url, args=[url, urls, base_url, path, on_read])
t = Thread(target=process_url, args=[url, urls, base_url, path, on_read, on_read2])
t.start()
threads.append(t)

Expand Down